|
| 1 | +package io.github.protocol.mtconnect.server; |
| 2 | + |
| 3 | +import com.huaweicloud.sdk.iotda.v5.IoTDAClient; |
| 4 | +import com.huaweicloud.sdk.iotda.v5.model.ListDevicesResponse; |
| 5 | +import com.huaweicloud.sdk.iotda.v5.model.QueryDeviceSimplify; |
| 6 | +import io.github.openfacade.http.HttpClientConfig; |
| 7 | +import io.github.openfacade.http.HttpServerConfig; |
| 8 | +import io.github.openfacade.http.HttpServerEngine; |
| 9 | +import io.github.protocol.mtconnect.api.MTConnectDevices; |
| 10 | +import io.github.protocol.mtconnect.client.MTConnectClient; |
| 11 | +import io.github.protocol.mtconnect.client.MTConnectClientConfiguration; |
| 12 | +import io.github.protocol.mtconnect.server.impl.IoTDAMtProcessor; |
| 13 | +import org.junit.jupiter.api.Assertions; |
| 14 | +import org.junit.jupiter.api.BeforeEach; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.mockito.Mockito; |
| 17 | + |
| 18 | +import java.util.concurrent.ExecutionException; |
| 19 | + |
| 20 | +import static org.mockito.ArgumentMatchers.any; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | + |
| 23 | + |
| 24 | +public class IoTDAServerTest { |
| 25 | + |
| 26 | + private IoTDAClient mockClient; |
| 27 | + private final String localHost = "127.0.0.1"; |
| 28 | + |
| 29 | + @BeforeEach |
| 30 | + public void setUp() { |
| 31 | + mockClient = Mockito.mock(IoTDAClient.class); |
| 32 | + |
| 33 | + |
| 34 | + ListDevicesResponse rsp = new ListDevicesResponse(); |
| 35 | + QueryDeviceSimplify mockDevice = new QueryDeviceSimplify(); |
| 36 | + mockDevice.setDeviceId("mock_device_id"); |
| 37 | + mockDevice.setDeviceName("mock_device_name"); |
| 38 | + rsp.addDevicesItem(mockDevice); |
| 39 | + when(mockClient.listDevices(any())).thenReturn(rsp); |
| 40 | + } |
| 41 | + |
| 42 | + // start iotda server |
| 43 | + private MTConnectServer startIoTDAServer() { |
| 44 | + |
| 45 | + MTConnectServerConfiguration configuration = new MTConnectServerConfiguration(); |
| 46 | + HttpServerConfig httpServerConfig = new HttpServerConfig.Builder() |
| 47 | + .engine(HttpServerEngine.Vertx) |
| 48 | + .host("127.0.0.1") |
| 49 | + .port(36633) |
| 50 | + .build(); |
| 51 | + configuration.setHttpConfig(httpServerConfig); |
| 52 | + IoTDAMtProcessor ioTDAMtProcessor = new IoTDAMtProcessor.Builder() |
| 53 | + .setIoTDAClient(mockClient) |
| 54 | + .build(); |
| 55 | + |
| 56 | + configuration.setMtProcessor(ioTDAMtProcessor); |
| 57 | + MTConnectServer mtConnectServer = new MTConnectServer(configuration); |
| 58 | + mtConnectServer.start().join(); |
| 59 | + |
| 60 | + return mtConnectServer; |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testDevices() throws ExecutionException, InterruptedException { |
| 65 | + MTConnectServer mtConnectServer = startIoTDAServer(); |
| 66 | + int port = mtConnectServer.httpPort(); |
| 67 | + Assertions.assertEquals(36633, port); |
| 68 | + |
| 69 | + MTConnectClientConfiguration configuration = new MTConnectClientConfiguration(); |
| 70 | + HttpClientConfig httpClientConfig = new HttpClientConfig.Builder().build(); |
| 71 | + configuration.setHttpConfig(httpClientConfig); |
| 72 | + configuration.setHost(localHost); |
| 73 | + configuration.setPort(port); |
| 74 | + MTConnectClient mtConnectClient = new MTConnectClient(configuration); |
| 75 | + |
| 76 | + MTConnectDevices resp = mtConnectClient.devices(); |
| 77 | + Assertions.assertEquals("mock_device_id", resp.getDevices().get(0).getId()); |
| 78 | + Assertions.assertEquals("mock_device_name", resp.getDevices().get(0).getName()); |
| 79 | + } |
| 80 | +} |
0 commit comments