From e8782417e2a8274af36d01a86c0ae935d20b0601 Mon Sep 17 00:00:00 2001
From: zhiheng123 <903292776@qq.com>
Date: Mon, 9 Dec 2024 23:51:20 +0800
Subject: [PATCH] test: add iotda server testcase
---
mtconnect-server/pom.xml | 6 ++
.../server/impl/IoTDAMtProcessor.java | 9 +++
.../mtconnect/server/IoTDAServerTest.java | 80 +++++++++++++++++++
3 files changed, 95 insertions(+)
create mode 100644 mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/IoTDAServerTest.java
diff --git a/mtconnect-server/pom.xml b/mtconnect-server/pom.xml
index 7f2f681..b7a9615 100644
--- a/mtconnect-server/pom.xml
+++ b/mtconnect-server/pom.xml
@@ -23,6 +23,12 @@
0.0.1-SNAPSHOT
test
+
+ org.mockito
+ mockito-core
+ 4.11.0
+ test
+
com.huaweicloud.sdk
huaweicloud-sdk-iotda
diff --git a/mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java b/mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java
index bfe3e44..aa753ee 100644
--- a/mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java
+++ b/mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/impl/IoTDAMtProcessor.java
@@ -88,7 +88,16 @@ public Builder setEndpoint(String endpoint) {
return this;
}
+ // only for test
+ public Builder setIoTDAClient(IoTDAClient client) {
+ ioTDAMtProcessor.client = client;
+ return this;
+ }
+
public IoTDAMtProcessor build(){
+ if (ioTDAMtProcessor.client != null) {
+ return ioTDAMtProcessor;
+ }
ICredential auth = new BasicCredentials()
// 标准版/企业版需要使用衍生算法,基础版请删除配置"withDerivedPredicate";
diff --git a/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/IoTDAServerTest.java b/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/IoTDAServerTest.java
new file mode 100644
index 0000000..1355954
--- /dev/null
+++ b/mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/IoTDAServerTest.java
@@ -0,0 +1,80 @@
+package io.github.protocol.mtconnect.server;
+
+import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
+import com.huaweicloud.sdk.iotda.v5.model.ListDevicesResponse;
+import com.huaweicloud.sdk.iotda.v5.model.QueryDeviceSimplify;
+import io.github.openfacade.http.HttpClientConfig;
+import io.github.openfacade.http.HttpServerConfig;
+import io.github.openfacade.http.HttpServerEngine;
+import io.github.protocol.mtconnect.api.MTConnectDevices;
+import io.github.protocol.mtconnect.client.MTConnectClient;
+import io.github.protocol.mtconnect.client.MTConnectClientConfiguration;
+import io.github.protocol.mtconnect.server.impl.IoTDAMtProcessor;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import java.util.concurrent.ExecutionException;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+
+public class IoTDAServerTest {
+
+ private IoTDAClient mockClient;
+ private final String localHost = "127.0.0.1";
+
+ @BeforeEach
+ public void setUp() {
+ mockClient = Mockito.mock(IoTDAClient.class);
+
+
+ ListDevicesResponse rsp = new ListDevicesResponse();
+ QueryDeviceSimplify mockDevice = new QueryDeviceSimplify();
+ mockDevice.setDeviceId("mock_device_id");
+ mockDevice.setDeviceName("mock_device_name");
+ rsp.addDevicesItem(mockDevice);
+ when(mockClient.listDevices(any())).thenReturn(rsp);
+ }
+
+ // start iotda server
+ private MTConnectServer startIoTDAServer() {
+
+ MTConnectServerConfiguration configuration = new MTConnectServerConfiguration();
+ HttpServerConfig httpServerConfig = new HttpServerConfig.Builder()
+ .engine(HttpServerEngine.Vertx)
+ .host("127.0.0.1")
+ .port(36633)
+ .build();
+ configuration.setHttpConfig(httpServerConfig);
+ IoTDAMtProcessor ioTDAMtProcessor = new IoTDAMtProcessor.Builder()
+ .setIoTDAClient(mockClient)
+ .build();
+
+ configuration.setMtProcessor(ioTDAMtProcessor);
+ MTConnectServer mtConnectServer = new MTConnectServer(configuration);
+ mtConnectServer.start().join();
+
+ return mtConnectServer;
+ }
+
+ @Test
+ public void testDevices() throws ExecutionException, InterruptedException {
+ MTConnectServer mtConnectServer = startIoTDAServer();
+ int port = mtConnectServer.httpPort();
+ Assertions.assertEquals(36633, port);
+
+ MTConnectClientConfiguration configuration = new MTConnectClientConfiguration();
+ HttpClientConfig httpClientConfig = new HttpClientConfig.Builder().build();
+ configuration.setHttpConfig(httpClientConfig);
+ configuration.setHost(localHost);
+ configuration.setPort(port);
+ MTConnectClient mtConnectClient = new MTConnectClient(configuration);
+
+ MTConnectDevices resp = mtConnectClient.devices();
+ Assertions.assertEquals("mock_device_id", resp.getDevices().get(0).getId());
+ Assertions.assertEquals("mock_device_name", resp.getDevices().get(0).getName());
+ }
+}