Skip to content

Commit 774728d

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

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

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

Lines changed: 5 additions & 2 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;
@@ -40,9 +42,10 @@ public MTConnectDevices deivces() throws ExecutionException, InterruptedExceptio
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 {

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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
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

1314
import java.util.ArrayList;
@@ -43,6 +44,7 @@ public void testDevices() throws ExecutionException, InterruptedException {
4344
MemoryMtProcessor memoryMtProcessor = startMemoryServer();
4445
MTConnectDevices devices = new MTConnectDevices();
4546
Device device = new Device();
47+
device.setId("test_id");
4648

4749
devices.setDevices(Collections.singletonList(device));
4850
memoryMtProcessor.updateDevice(devices);
@@ -54,8 +56,7 @@ public void testDevices() throws ExecutionException, InterruptedException {
5456
configuration.setPort(port);
5557
MTConnectClient mtConnectClient = new MTConnectClient(configuration);
5658

57-
// use client show device
5859
MTConnectDevices resp = mtConnectClient.deivces();
59-
System.out.println(resp.getDevices());
60+
Assertions.assertEquals(device.getId(), resp.getDevices().get(0).getId());
6061
}
6162
}

0 commit comments

Comments
 (0)