Skip to content

Commit 6eda7d4

Browse files
zhiheng123ZhangJian He
andcommitted
test: add the testcase of devices
Signed-off-by: zhiheng123 <[email protected]> Co-authored-by: ZhangJian He <[email protected]>
1 parent f3b4804 commit 6eda7d4

File tree

5 files changed

+46
-33
lines changed

5 files changed

+46
-33
lines changed
Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package io.github.protocol.mtconnect.client;
22

3-
import com.fasterxml.jackson.core.JsonProcessingException;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
53
import io.github.openfacade.http.HttpClient;
64
import io.github.openfacade.http.HttpClientFactory;
75
import io.github.openfacade.http.HttpResponse;
86
import io.github.protocol.mtconnect.api.MTConnectDevices;
7+
import io.github.protocol.mtconnect.common.XmlUtil;
98

9+
import java.nio.charset.StandardCharsets;
1010
import java.util.Arrays;
1111
import java.util.concurrent.CompletableFuture;
1212
import java.util.concurrent.ExecutionException;
@@ -15,34 +15,27 @@ public class MTConnectClient {
1515
private final MTConnectClientConfiguration config;
1616

1717
private final HttpClient httpClient;
18-
private static final ObjectMapper MAPPER = new ObjectMapper();
1918

2019
public MTConnectClient(MTConnectClientConfiguration configuration) {
2120
this.config = configuration;
2221
this.httpClient = HttpClientFactory.createHttpClient(configuration.httpConfig());
2322
}
24-
25-
public MTConnectDevices deivce(String Id) {
26-
return null;
27-
};
2823

29-
public static <T> T toObject(String json, Class<T> type) throws JsonProcessingException {
30-
if (json == null || json.isEmpty()) {
31-
return null;
32-
}
33-
return MAPPER.readValue(json, type);
24+
public MTConnectDevices device(String Id) {
25+
return null;
3426
}
3527

36-
public MTConnectDevices deivces() throws ExecutionException, InterruptedException {
28+
public MTConnectDevices devices() throws ExecutionException, InterruptedException {
3729
String url = String.format("http://%s:%s/devices", config.host(), config.port());
3830
CompletableFuture<HttpResponse> future = httpClient.get(url);
3931

4032
CompletableFuture<MTConnectDevices> resp = future.thenCompose(response -> {
4133
if (response.statusCode() >= 200 && response.statusCode() < 300) {
4234
try {
43-
MTConnectDevices body = toObject(Arrays.toString(response.body()), MTConnectDevices.class);
35+
String string = new String(response.body(), StandardCharsets.UTF_8);
36+
MTConnectDevices body = XmlUtil.fromXml(string, MTConnectDevices.class);
4437
return CompletableFuture.completedFuture(body);
45-
} catch (JsonProcessingException e) {
38+
} catch (Exception e) {
4639
return CompletableFuture.failedFuture(e);
4740
}
4841
} else {
@@ -51,5 +44,5 @@ public MTConnectDevices deivces() throws ExecutionException, InterruptedExceptio
5144
});
5245

5346
return resp.get();
54-
};
47+
}
5548
}

mtconnect-server/pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
<artifactId>mtconnect-common</artifactId>
1818
<version>${project.version}</version>
1919
</dependency>
20+
<dependency>
21+
<groupId>io.github.protocol-laboratory</groupId>
22+
<artifactId>mtconnect-client</artifactId>
23+
<version>0.0.1-SNAPSHOT</version>
24+
<scope>test</scope>
25+
</dependency>
2026
<dependency>
2127
<groupId>com.huaweicloud.sdk</groupId>
2228
<artifactId>huaweicloud-sdk-iotda</artifactId>
@@ -37,12 +43,6 @@
3743
<artifactId>vertx-web</artifactId>
3844
<version>${vertx.version}</version>
3945
</dependency>
40-
<dependency>
41-
<groupId>io.github.protocol-laboratory</groupId>
42-
<artifactId>mtconnect-client</artifactId>
43-
<version>0.0.1-SNAPSHOT</version>
44-
<scope>test</scope>
45-
</dependency>
4646
</dependencies>
4747

4848
</project>

mtconnect-server/src/main/java/io/github/protocol/mtconnect/server/MTConnectServer.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import io.github.openfacade.http.HttpServerFactory;
88
import io.github.openfacade.http.SyncRequestHandler;
99
import io.github.protocol.mtconnect.api.AssetRequest;
10+
import io.github.protocol.mtconnect.api.DeviceRequest;
1011
import io.github.protocol.mtconnect.api.MTConnectAssets;
12+
import io.github.protocol.mtconnect.api.MTConnectDevices;
1113
import io.github.protocol.mtconnect.common.XmlUtil;
1214
import io.netty.handler.codec.http.HttpResponseStatus;
1315

@@ -29,10 +31,11 @@ public MTConnectServer(MTConnectServerConfiguration configuration) {
2931

3032
public CompletableFuture<Void> start() {
3133
this.httpServer.addSyncRoute("/assets", HttpMethod.GET, new MtAssetsHandler());
34+
this.httpServer.addSyncRoute("/devices", HttpMethod.GET, new MtDevicesHandler());
3235
return httpServer.start();
3336
}
3437

35-
public int getHttpPort() {
38+
public int httpPort() {
3639
return httpServer.listenPort();
3740
}
3841

@@ -51,4 +54,20 @@ public HttpResponse handle(HttpRequest request) {
5154
return new HttpResponse(HttpResponseStatus.OK.code(), body.getBytes(StandardCharsets.UTF_8));
5255
}
5356
}
57+
58+
class MtDevicesHandler implements SyncRequestHandler {
59+
@Override
60+
public HttpResponse handle(HttpRequest request) {
61+
MTConnectDevices devices = mtProcessor.device(new DeviceRequest());
62+
// convert the response to http response
63+
String body;
64+
try {
65+
body = XmlUtil.toXml(devices);
66+
} catch (Exception e) {
67+
throw new RuntimeException(e);
68+
}
69+
70+
return new HttpResponse(HttpResponseStatus.OK.code(), body.getBytes(StandardCharsets.UTF_8));
71+
}
72+
}
5473
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
*/
1515
public class MemoryMtProcessor implements MTProcessor {
1616

17-
Map<String, MTConnectAssets> mtConnectAssetsMap = new HashMap<>();
17+
private final Map<String, MTConnectAssets> mtConnectAssetsMap = new HashMap<>();
18+
1819
private MTConnectDevices devices;
1920

2021
@Override
@@ -24,10 +25,10 @@ public MTConnectAssets asset(AssetRequest assetRequest) {
2425

2526
@Override
2627
public MTConnectDevices device(DeviceRequest deviceRequest) {
27-
return null;
28+
return devices;
2829
}
2930

30-
public void updateDevice(MTConnectDevices devices) {
31+
public void updateDevices(MTConnectDevices devices) {
3132
this.devices = devices;
3233
}
3334
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import io.github.protocol.mtconnect.client.MTConnectClient;
99
import io.github.protocol.mtconnect.client.MTConnectClientConfiguration;
1010
import io.github.protocol.mtconnect.server.impl.MemoryMtProcessor;
11+
import org.junit.jupiter.api.Assertions;
1112
import org.junit.jupiter.api.Test;
1213

13-
import java.util.ArrayList;
1414
import java.util.Collections;
1515
import java.util.concurrent.ExecutionException;
1616

@@ -19,7 +19,7 @@ public class MTConnectDeviceTest {
1919
private int port;
2020
private final String localHost = "127.0.0.1";
2121

22-
// start memery server
22+
// start memory server
2323
private MemoryMtProcessor startMemoryServer() {
2424
MTConnectServerConfiguration configuration = new MTConnectServerConfiguration();
2525
HttpServerConfig httpServerConfig = new HttpServerConfig.Builder()
@@ -33,7 +33,7 @@ private MemoryMtProcessor startMemoryServer() {
3333
MTConnectServer mtConnectServer = new MTConnectServer(configuration);
3434
mtConnectServer.start().join();
3535

36-
port = mtConnectServer.getHttpPort();
36+
port = mtConnectServer.httpPort();
3737

3838
return mtProcessor;
3939
}
@@ -43,9 +43,10 @@ public void testDevices() throws ExecutionException, InterruptedException {
4343
MemoryMtProcessor memoryMtProcessor = startMemoryServer();
4444
MTConnectDevices devices = new MTConnectDevices();
4545
Device device = new Device();
46+
device.setId("test_id");
4647

4748
devices.setDevices(Collections.singletonList(device));
48-
memoryMtProcessor.updateDevice(devices);
49+
memoryMtProcessor.updateDevices(devices);
4950

5051
MTConnectClientConfiguration configuration = new MTConnectClientConfiguration();
5152
HttpClientConfig httpClientConfig = new HttpClientConfig.Builder().build();
@@ -54,8 +55,7 @@ public void testDevices() throws ExecutionException, InterruptedException {
5455
configuration.setPort(port);
5556
MTConnectClient mtConnectClient = new MTConnectClient(configuration);
5657

57-
// use client show device
58-
MTConnectDevices resp = mtConnectClient.deivces();
59-
System.out.println(resp.getDevices());
58+
MTConnectDevices resp = mtConnectClient.devices();
59+
Assertions.assertEquals(device.getId(), resp.getDevices().get(0).getId());
6060
}
6161
}

0 commit comments

Comments
 (0)