Skip to content

Commit 3a04537

Browse files
authored
test: add iotda server testcase (#25)
1 parent 4fd35be commit 3a04537

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

mtconnect-server/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
<version>0.0.1-SNAPSHOT</version>
2424
<scope>test</scope>
2525
</dependency>
26+
<dependency>
27+
<groupId>org.mockito</groupId>
28+
<artifactId>mockito-core</artifactId>
29+
<version>4.11.0</version>
30+
<scope>test</scope>
31+
</dependency>
2632
<dependency>
2733
<groupId>com.huaweicloud.sdk</groupId>
2834
<artifactId>huaweicloud-sdk-iotda</artifactId>

mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,16 @@ public Builder setEndpoint(String endpoint) {
8888
return this;
8989
}
9090

91+
// only for test
92+
public Builder setIoTDAClient(IoTDAClient client) {
93+
ioTDAMtProcessor.client = client;
94+
return this;
95+
}
96+
9197
public IoTDAMtProcessor build(){
98+
if (ioTDAMtProcessor.client != null) {
99+
return ioTDAMtProcessor;
100+
}
92101

93102
ICredential auth = new BasicCredentials()
94103
// 标准版/企业版需要使用衍生算法,基础版请删除配置"withDerivedPredicate";
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)