diff --git a/ruoyi-admin/src/main/java/com/ruoyi/algorithm/wms/tidy/WcsVehicleSnapshotClient.java b/ruoyi-admin/src/main/java/com/ruoyi/algorithm/wms/tidy/WcsVehicleSnapshotClient.java index a3e9cf6..4685338 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/algorithm/wms/tidy/WcsVehicleSnapshotClient.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/algorithm/wms/tidy/WcsVehicleSnapshotClient.java @@ -1,6 +1,6 @@ package com.ruoyi.algorithm.wms.tidy; -import cn.hutool.http.HttpUtil; +import cn.hutool.http.HttpRequest; import cn.hutool.json.JSONUtil; import com.ruoyi.algorithm.integration.AlgorithmProperties; import org.slf4j.Logger; @@ -26,7 +26,7 @@ public class WcsVehicleSnapshotClient { return List.of(); } try { - String body = HttpUtil.get(url, 3_000); + String body = HttpRequest.post(url).timeout(3_000).execute().body(); if (body == null || body.isBlank()) { return List.of(); } diff --git a/ruoyi-admin/src/test/java/com/ruoyi/algorithm/wms/tidy/WcsVehicleSnapshotClientTest.java b/ruoyi-admin/src/test/java/com/ruoyi/algorithm/wms/tidy/WcsVehicleSnapshotClientTest.java new file mode 100644 index 0000000..0e54769 --- /dev/null +++ b/ruoyi-admin/src/test/java/com/ruoyi/algorithm/wms/tidy/WcsVehicleSnapshotClientTest.java @@ -0,0 +1,66 @@ +package com.ruoyi.algorithm.wms.tidy; + +import com.ruoyi.algorithm.integration.AlgorithmProperties; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpServer; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.concurrent.atomic.AtomicReference; + +import static org.assertj.core.api.Assertions.assertThat; + +class WcsVehicleSnapshotClientTest { + private HttpServer server; + + @AfterEach + void stopServer() { + if (server != null) { + server.stop(0); + } + } + + @Test + void fetchPostsToTheWcsVehicleSnapshotContract() throws IOException { + AtomicReference requestMethod = new AtomicReference<>(); + server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); + server.createContext("/wcs2Wms/api/tidyVehicleSnapshot", exchange -> { + requestMethod.set(exchange.getRequestMethod()); + if (!"POST".equals(exchange.getRequestMethod())) { + sendJson(exchange, 405, "{\"code\":405,\"msg\":\"method not allowed\"}"); + return; + } + sendJson(exchange, 200, """ + {"code":200,"data":[{ + "guid":"SRGV-1","online":true,"healthy":true,"idle":true + }]} + """); + }); + server.start(); + + AlgorithmProperties properties = new AlgorithmProperties(); + properties.setTidyWcsSnapshotUrl("http://127.0.0.1:" + server.getAddress().getPort() + + "/wcs2Wms/api/tidyVehicleSnapshot"); + + List snapshots = new WcsVehicleSnapshotClient(properties).fetch(); + + assertThat(requestMethod.get()).isEqualTo("POST"); + assertThat(snapshots).containsExactly(new VehicleSnapshot("SRGV-1", true, true, true, "")); + } + + private static void sendJson(HttpExchange exchange, int status, String json) throws IOException { + byte[] body = json.getBytes(StandardCharsets.UTF_8); + try { + exchange.getRequestBody().readAllBytes(); + exchange.getResponseHeaders().set("Content-Type", "application/json; charset=utf-8"); + exchange.sendResponseHeaders(status, body.length); + exchange.getResponseBody().write(body); + } finally { + exchange.close(); + } + } +}