Skip to content

Commit bda1084

Browse files
committed
test: add mock exception
1 parent a2e3633 commit bda1084

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

mtconnect-server/src/test/java/io/github/protocol/mtconnect/server/IoTDAServerTest.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.protocol.mtconnect.server;
22

3+
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
34
import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
45
import com.huaweicloud.sdk.iotda.v5.model.ListDevicesResponse;
56
import com.huaweicloud.sdk.iotda.v5.model.QueryDeviceSimplify;
@@ -30,7 +31,6 @@ public class IoTDAServerTest {
3031
public void setUp() {
3132
mockClient = Mockito.mock(IoTDAClient.class);
3233

33-
3434
ListDevicesResponse rsp = new ListDevicesResponse();
3535
QueryDeviceSimplify mockDevice = new QueryDeviceSimplify();
3636
mockDevice.setDeviceId("mock_device_id");
@@ -40,13 +40,13 @@ public void setUp() {
4040
}
4141

4242
// start iotda server
43-
private MTConnectServer startIoTDAServer() {
43+
private MTConnectServer startIoTDAServer(int port) {
4444

4545
MTConnectServerConfiguration configuration = new MTConnectServerConfiguration();
4646
HttpServerConfig httpServerConfig = new HttpServerConfig.Builder()
4747
.engine(HttpServerEngine.Vertx)
4848
.host("127.0.0.1")
49-
.port(36633)
49+
.port(port)
5050
.build();
5151
configuration.setHttpConfig(httpServerConfig);
5252
IoTDAMtProcessor ioTDAMtProcessor = new IoTDAMtProcessor.Builder()
@@ -65,9 +65,10 @@ private MTConnectServer startIoTDAServer() {
6565

6666
@Test
6767
public void testDevices() throws ExecutionException, InterruptedException {
68-
MTConnectServer mtConnectServer = startIoTDAServer();
68+
int inputPort = 36633;
69+
MTConnectServer mtConnectServer = startIoTDAServer(inputPort);
6970
int port = mtConnectServer.httpPort();
70-
Assertions.assertEquals(36633, port);
71+
Assertions.assertEquals(inputPort, port);
7172

7273
MTConnectClientConfiguration configuration = new MTConnectClientConfiguration();
7374
HttpClientConfig httpClientConfig = new HttpClientConfig.Builder().build();
@@ -80,4 +81,24 @@ public void testDevices() throws ExecutionException, InterruptedException {
8081
Assertions.assertEquals("mock_device_id", resp.getDevices().get(0).getId());
8182
Assertions.assertEquals("mock_device_name", resp.getDevices().get(0).getName());
8283
}
84+
85+
@Test
86+
public void testExpetion() throws ExecutionException, InterruptedException {
87+
ServiceResponseException e = new ServiceResponseException(400, "erroCode", "errorMsg", "requestId");
88+
89+
when(mockClient.listDevices(any())).thenThrow(e);
90+
91+
MTConnectServer mtConnectServer = startIoTDAServer(0);
92+
int port = mtConnectServer.httpPort();
93+
94+
MTConnectClientConfiguration configuration = new MTConnectClientConfiguration();
95+
HttpClientConfig httpClientConfig = new HttpClientConfig.Builder().build();
96+
configuration.setHttpConfig(httpClientConfig);
97+
configuration.setHost(localHost);
98+
configuration.setPort(port);
99+
MTConnectClient mtConnectClient = new MTConnectClient(configuration);
100+
101+
MTConnectDevices resp = mtConnectClient.devices();
102+
Assertions.assertEquals(resp.getDevices(), null);
103+
}
83104
}

0 commit comments

Comments
 (0)