yeg
2026-08-27 210663040bf65ee00b5f86545fdc31bd0356fddf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.ruoyi.web.controller.device;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.time.Instant;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.device.bts.BtsDevelopmentSimulator;
import com.ruoyi.device.discovery.DeviceDiscoveryCandidateStore.CabinetCandidate;
import com.ruoyi.device.discovery.DeviceDiscoveryCandidateStore.ChannelCandidate;
import com.ruoyi.device.discovery.InMemoryDeviceDiscoveryCandidateStore;
import com.ruoyi.device.model.ChannelIdentity;
import com.ruoyi.device.model.DeviceWorkStatus;
import com.ruoyi.web.config.BtsDevelopmentSimulatorWebConfig;
 
class BtsDevelopmentSimulatorControllerTest
{
    @Test
    void controllerUpdatesDeviceAndChannelState() throws Exception
    {
        try (BtsDevelopmentSimulator simulator = new BtsDevelopmentSimulator(0))
        {
            BtsDevelopmentSimulatorController controller = controller(simulator);
 
            controller.updateDevice(1,
                    new BtsDevelopmentSimulatorController.DeviceStateCommand("127.0.0.2", 4, false));
            controller.updateChannel(1, 3,
                    new BtsDevelopmentSimulatorController.ChannelStateCommand(true, DeviceWorkStatus.PAUSED));
 
            assertFalse(controller.state().devices().get(0).online());
            assertEquals(DeviceWorkStatus.PAUSED,
                    controller.state().devices().get(0).channels().get(2).workStatus());
        }
    }
 
    @Test
    void controllerAddsUpdatesAndDeletesVirtualDevices() throws Exception
    {
        try (BtsDevelopmentSimulator simulator = new BtsDevelopmentSimulator(0))
        {
            BtsDevelopmentSimulatorController controller = controller(simulator);
 
            var created = controller.createDevice(
                    new BtsDevelopmentSimulatorController.CreateDeviceCommand("127.0.0.8", 6));
            int deviceId = created.devices().stream()
                    .filter(device -> device.ip().equals("127.0.0.8"))
                    .findFirst().orElseThrow().deviceId();
            controller.updateDevice(deviceId,
                    new BtsDevelopmentSimulatorController.DeviceStateCommand("127.0.0.9", 3, false));
 
            assertEquals(2, controller.state().devices().size());
            assertEquals(3, controller.state().devices().stream()
                    .filter(device -> device.deviceId() == deviceId)
                    .findFirst().orElseThrow().channels().size());
 
            controller.deleteDevice(deviceId);
            assertEquals(1, controller.state().devices().size());
        }
    }
 
    @Test
    void controllerConfiguresCollectorsSeparatelyFromCabinets() throws Exception
    {
        try (BtsDevelopmentSimulator simulator = new BtsDevelopmentSimulator(0))
        {
            BtsDevelopmentSimulatorController controller = controller(simulator);
            var created = controller.createCollector(
                    new BtsDevelopmentSimulatorController.CreateCollectorCommand("127.0.1.8", 6));
            int collectorId = created.collectors().stream()
                    .filter(collector -> collector.ip().equals("127.0.1.8"))
                    .findFirst().orElseThrow().collectorId();
 
            controller.updateCollector(collectorId,
                    new BtsDevelopmentSimulatorController.CollectorStateCommand("127.0.1.9", 3, false));
 
            assertEquals(1, controller.state().devices().size());
            assertEquals(2, controller.state().collectors().size());
            assertFalse(controller.state().collectors().stream()
                    .filter(collector -> collector.collectorId() == collectorId)
                    .findFirst().orElseThrow().online());
 
            controller.deleteCollector(collectorId);
            assertEquals(1, controller.state().collectors().size());
            assertEquals(1, controller.state().devices().size());
        }
    }
 
    @Test
    void virtualIpMustBeUniqueAcrossCabinetsAndCollectors() throws Exception
    {
        try (BtsDevelopmentSimulator simulator = new BtsDevelopmentSimulator(0))
        {
            BtsDevelopmentSimulatorController controller = controller(simulator);
 
            assertThrows(org.springframework.web.server.ResponseStatusException.class,
                    () -> controller.createCollector(
                            new BtsDevelopmentSimulatorController.CreateCollectorCommand("127.0.0.2", 4)));
            assertThrows(org.springframework.web.server.ResponseStatusException.class,
                    () -> controller.createDevice(
                            new BtsDevelopmentSimulatorController.CreateDeviceCommand("127.0.1.2", 4)));
        }
    }
 
    @Test
    void developmentControlBoundaryOnlyAllowsLoopbackClients() throws Exception
    {
        var interceptor = new BtsDevelopmentSimulatorWebConfig.LocalOnlyInterceptor();
        MockHttpServletResponse localResponse = new MockHttpServletResponse();
        MockHttpServletRequest localRequest = requestFrom(InetAddress.getLoopbackAddress().getHostAddress());
        MockHttpServletResponse remoteResponse = new MockHttpServletResponse();
        MockHttpServletRequest remoteRequest = requestFrom("192.168.1.20");
 
        assertTrue(interceptor.preHandle(localRequest, localResponse, new Object()));
        assertFalse(interceptor.preHandle(remoteRequest, remoteResponse, new Object()));
        assertEquals(403, remoteResponse.getStatus());
        assertTrue(BtsDevelopmentSimulatorController.class.isAnnotationPresent(Anonymous.class));
    }
 
    @Test
    void exposesStaticControlPageThroughAnonymousHandler() throws Exception
    {
        Method page = BtsDevelopmentSimulatorController.class.getMethod("page");
 
        assertArrayEquals(new String[] { "/dev/bts-simulator.html" },
                page.getAnnotation(GetMapping.class).value());
        assertTrue(BtsDevelopmentSimulatorController.class.isAnnotationPresent(Anonymous.class));
    }
 
    @Test
    void resettingSimulatorRemovesExistingDevelopmentCandidates() throws Exception
    {
        Instant now = Instant.parse("2026-08-27T00:00:00Z");
        InMemoryDeviceDiscoveryCandidateStore candidateStore = new InMemoryDeviceDiscoveryCandidateStore();
        candidateStore.recordCabinets(List.of(new CabinetCandidate("dev-sim", null, "dev-sim-22-8", 22, 8,
                "127.0.0.8", null, "旧模拟柜", null, null,
                List.of(new ChannelCandidate(new ChannelIdentity("dev-sim", 22, 8, 1, 1), true,
                        DeviceWorkStatus.STOPPED, now)), now, now.plusSeconds(30))));
        try (BtsDevelopmentSimulator simulator = new BtsDevelopmentSimulator(0))
        {
            BtsDevelopmentSimulatorController controller =
                    new BtsDevelopmentSimulatorController(simulator, candidateStore);
 
            controller.reset();
 
            assertEquals(List.of(), candidateStore.snapshot(now).cabinets());
        }
    }
 
    private BtsDevelopmentSimulatorController controller(BtsDevelopmentSimulator simulator)
    {
        return new BtsDevelopmentSimulatorController(simulator, new InMemoryDeviceDiscoveryCandidateStore());
    }
 
    private MockHttpServletRequest requestFrom(String remoteAddress)
    {
        MockHttpServletRequest request = new MockHttpServletRequest("GET", "/dev/bts-simulator/state");
        request.setRemoteAddr(remoteAddress);
        return request;
    }
}