fix: align WCS vehicle snapshot method

This commit is contained in:
zgw
2026-07-24 10:04:25 +08:00
parent d6179297ab
commit 89a26e6d42
2 changed files with 68 additions and 2 deletions

View File

@@ -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();
}

View File

@@ -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<String> 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<VehicleSnapshot> 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();
}
}
}