yeg
2026-08-27 a4f06a9095a2eab0bb48b406c4609be6e70b8854
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
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 java.lang.reflect.Method;
import java.net.InetAddress;
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.model.DeviceWorkStatus;
import com.ruoyi.web.config.BtsDevelopmentSimulatorWebConfig;
 
class BtsDevelopmentSimulatorControllerTest
{
    @Test
    void controllerUpdatesDeviceAndChannelState() throws Exception
    {
        try (BtsDevelopmentSimulator simulator = new BtsDevelopmentSimulator(0))
        {
            BtsDevelopmentSimulatorController controller = new BtsDevelopmentSimulatorController(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 = new BtsDevelopmentSimulatorController(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 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));
    }
 
    private MockHttpServletRequest requestFrom(String remoteAddress)
    {
        MockHttpServletRequest request = new MockHttpServletRequest("GET", "/dev/bts-simulator/state");
        request.setRemoteAddr(remoteAddress);
        return request;
    }
}