94 lines
3.9 KiB
Java
94 lines
3.9 KiB
Java
package scripts.qa;
|
|||
|
|
|
||
|
|
import com.alibaba.fastjson2.JSON;
|
||
|
|
import com.alibaba.fastjson2.JSONObject;
|
||
|
|
import com.ruoyi.train.integration.dms.mock.MockDmsServer;
|
||
|
|
import com.sun.net.httpserver.HttpExchange;
|
||
|
|
import com.sun.net.httpserver.HttpServer;
|
||
|
|
|
||
|
|
import java.io.*;
|
||
|
|
import java.net.InetSocketAddress;
|
||
|
|
import java.nio.charset.StandardCharsets;
|
||
|
|
import java.util.concurrent.Executors;
|
||
|
|
|
||
|
|
public class MockDmsRunner {
|
||
|
|
private static MockDmsServer mockDmsServer;
|
||
|
|
private static HttpServer controlServer;
|
||
|
|
|
||
|
|
public static void main(String[] args) throws Exception {
|
||
|
|
int dmsPort = 18099;
|
||
|
|
int controlPort = 18098;
|
||
|
|
|
||
|
|
mockDmsServer = new MockDmsServer();
|
||
|
|
// Use reflection or custom wrapper to bind exact port
|
||
|
|
java.lang.reflect.Field serverField = MockDmsServer.class.getDeclaredField("server");
|
||
|
|
serverField.setAccessible(true);
|
||
|
|
HttpServer s = HttpServer.create(new InetSocketAddress("127.0.0.1", dmsPort), 0);
|
||
|
|
s.setExecutor(Executors.newCachedThreadPool());
|
||
|
|
serverField.set(mockDmsServer, s);
|
||
|
|
|
||
|
|
java.lang.reflect.Field portField = MockDmsServer.class.getDeclaredField("port");
|
||
|
|
portField.setAccessible(true);
|
||
|
|
portField.setInt(mockDmsServer, dmsPort);
|
||
|
|
|
||
|
|
java.lang.reflect.Method regMethod = MockDmsServer.class.getDeclaredMethod("registerEndpoints");
|
||
|
|
regMethod.setAccessible(true);
|
||
|
|
regMethod.invoke(mockDmsServer);
|
||
|
|
|
||
|
|
s.start();
|
||
|
|
System.out.println("MockDmsServer started on port: " + dmsPort);
|
||
|
|
|
||
|
|
// Control server on 18098 for test orchestration
|
||
|
|
controlServer = HttpServer.create(new InetSocketAddress("127.0.0.1", controlPort), 0);
|
||
|
|
controlServer.createContext("/mock/control/set-operation-status", exchange -> {
|
||
|
|
String body = readBody(exchange);
|
||
|
|
JSONObject req = JSON.parseObject(body);
|
||
|
|
Long opId = req.getLong("operationId");
|
||
|
|
String status = req.getString("status");
|
||
|
|
String msg = req.getString("message");
|
||
|
|
mockDmsServer.setOperationStatus(opId, status, msg);
|
||
|
|
sendResponse(exchange, 200, "{\"success\":true}");
|
||
|
|
});
|
||
|
|
|
||
|
|
controlServer.createContext("/mock/control/timeout", exchange -> {
|
||
|
|
String body = readBody(exchange);
|
||
|
|
JSONObject req = JSON.parseObject(body);
|
||
|
|
boolean sim = req.getBooleanValue("simulate");
|
||
|
|
mockDmsServer.setSimulateReadTimeout(sim);
|
||
|
|
sendResponse(exchange, 200, "{\"success\":true}");
|
||
|
|
});
|
||
|
|
|
||
|
|
controlServer.createContext("/mock/control/stats", exchange -> {
|
||
|
|
JSONObject resp = new JSONObject();
|
||
|
|
resp.put("loginCount", mockDmsServer.getLoginCounter());
|
||
|
|
resp.put("createCount", mockDmsServer.getPasswordCreatePostCounter());
|
||
|
|
resp.put("deleteCount", mockDmsServer.getPasswordDeletePostCounter());
|
||
|
|
resp.put("activeToken", mockDmsServer.getActiveToken());
|
||
|
|
sendResponse(exchange, 200, resp.toJSONString());
|
||
|
|
});
|
||
|
|
|
||
|
|
controlServer.start();
|
||
|
|
System.out.println("MockDms Control Server started on port: " + controlPort);
|
||
|
|
|
||
|
|
// Keep process alive
|
||
|
|
Thread.currentThread().join();
|
||
|
|
}
|
||
|
|
|
||
|
|
private static String readBody(HttpExchange exchange) throws IOException {
|
||
|
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(exchange.getRequestBody(), StandardCharsets.UTF_8))) {
|
||
|
|
StringBuilder sb = new StringBuilder();
|
||
|
|
String line;
|
||
|
|
while ((line = reader.readLine()) != null) sb.append(line);
|
||
|
|
return sb.toString();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void sendResponse(HttpExchange exchange, int code, String body) throws IOException {
|
||
|
|
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
|
||
|
|
exchange.getResponseHeaders().set("Content-Type", "application/json");
|
||
|
|
exchange.sendResponseHeaders(code, bytes.length);
|
||
|
|
try (OutputStream os = exchange.getResponseBody()) {
|
||
|
|
os.write(bytes);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|