Skip to content

Commit 02e3247

Browse files
committed
test: add the testcase of devices
Signed-off-by: zhiheng123 <[email protected]>
1 parent f3b4804 commit 02e3247

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed

mtconnect-client/src/main/java/io/github/protocol/mtconnect/client/MTConnectClient.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import io.github.openfacade.http.HttpClientFactory;
77
import io.github.openfacade.http.HttpResponse;
88
import io.github.protocol.mtconnect.api.MTConnectDevices;
9+
import io.github.protocol.mtconnect.common.XmlUtil;
910

11+
import java.nio.charset.StandardCharsets;
1012
import java.util.Arrays;
1113
import java.util.concurrent.CompletableFuture;
1214
import java.util.concurrent.ExecutionException;
@@ -21,10 +23,10 @@ public MTConnectClient(MTConnectClientConfiguration configuration) {
2123
this.config = configuration;
2224
this.httpClient = HttpClientFactory.createHttpClient(configuration.httpConfig());
2325
}
24-
25-
public MTConnectDevices deivce(String Id) {
26+
27+
public MTConnectDevices device(String Id) {
2628
return null;
27-
};
29+
}
2830

2931
public static <T> T toObject(String json, Class<T> type) throws JsonProcessingException {
3032
if (json == null || json.isEmpty()) {
@@ -33,16 +35,17 @@ public static <T> T toObject(String json, Class<T> type) throws JsonProcessingEx
3335
return MAPPER.readValue(json, type);
3436
}
3537

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

4042
CompletableFuture<MTConnectDevices> resp = future.thenCompose(response -> {
4143
if (response.statusCode() >= 200 && response.statusCode() < 300) {
4244
try {
43-
MTConnectDevices body = toObject(Arrays.toString(response.body()), MTConnectDevices.class);
45+
String string = new String(response.body(), StandardCharsets.UTF_8);
46+
MTConnectDevices body = XmlUtil.fromXml(string, MTConnectDevices.class);
4447
return CompletableFuture.completedFuture(body);
45-
} catch (JsonProcessingException e) {
48+
} catch (Exception e) {
4649
return CompletableFuture.failedFuture(e);
4750
}
4851
} else {
@@ -51,5 +54,7 @@ public MTConnectDevices deivces() throws ExecutionException, InterruptedExceptio
5154
});
5255

5356
return resp.get();
54-
};
57+
}
58+
59+
;
5560
}

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

Lines changed: 19 additions & 0 deletions
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,13 +31,30 @@ 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

3538
public int getHttpPort() {
3639
return httpServer.listenPort();
3740
}
3841

42+
class MtDevicesHandler implements SyncRequestHandler {
43+
@Override
44+
public HttpResponse handle(HttpRequest request) {
45+
MTConnectDevices devices = mtProcessor.device(new DeviceRequest());
46+
// convert the response to http response
47+
String body;
48+
try {
49+
body = XmlUtil.toXml(devices);
50+
} catch (Exception e) {
51+
throw new RuntimeException(e);
52+
}
53+
54+
return new HttpResponse(HttpResponseStatus.OK.code(), body.getBytes(StandardCharsets.UTF_8));
55+
}
56+
}
57+
3958
class MtAssetsHandler implements SyncRequestHandler {
4059
@Override
4160
public HttpResponse handle(HttpRequest request) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public MTConnectAssets asset(AssetRequest assetRequest) {
2424

2525
@Override
2626
public MTConnectDevices device(DeviceRequest deviceRequest) {
27-
return null;
27+
return devices;
2828
}
2929

3030
public void updateDevice(MTConnectDevices devices) {

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

Lines changed: 5 additions & 5 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()
@@ -43,6 +43,7 @@ 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));
4849
memoryMtProcessor.updateDevice(devices);
@@ -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)