| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import java.net.URI;
- import java.net.http.HttpClient;
- import java.net.http.HttpRequest;
- import java.net.http.HttpResponse;
- import java.nio.charset.StandardCharsets;
- import java.time.Duration;
- /** Login + simulate probe for cs1 tenant. */
- public class ProbeLoginAndSimulate {
- private static final String BASE = System.getenv().getOrDefault("LOBSTER_API_BASE", "http://127.0.0.1:8004");
- private static final String MSG = "\u4f60\u597d\uff0c\u60f3\u4e86\u89e3\u4e00\u4e0b\u5497\u4eec\u767d\u9152\u600e\u4e48\u5356\uff1f";
- public static void main(String[] args) throws Exception {
- String tenantCode = args.length > 0 ? args[0] : "cs1";
- String username = args.length > 1 ? args[1] : "admin";
- String password = args.length > 2 ? args[2] : "admin123";
- long templateId = args.length > 3 ? Long.parseLong(args[3]) : 70L;
- HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(15)).build();
- String loginBody = "{\"tenantCode\":\"" + tenantCode + "\",\"username\":\"" + username
- + "\",\"password\":\"" + password + "\",\"code\":\"\",\"uuid\":\"\"}";
- HttpRequest loginReq = HttpRequest.newBuilder()
- .uri(URI.create(BASE + "/login"))
- .timeout(Duration.ofSeconds(30))
- .header("Content-Type", "application/json; charset=utf-8")
- .POST(HttpRequest.BodyPublishers.ofString(loginBody, StandardCharsets.UTF_8))
- .build();
- System.out.println("POST " + BASE + "/login tenant=" + tenantCode + " user=" + username);
- HttpResponse<String> loginResp = client.send(loginReq, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
- System.out.println("login HTTP " + loginResp.statusCode());
- System.out.println(loginResp.body());
- if (loginResp.statusCode() != 200) {
- System.exit(1);
- }
- String token = extractToken(loginResp.body());
- if (token == null || token.isBlank()) {
- System.out.println("No token in login response");
- System.exit(2);
- }
- System.out.println("token prefix: " + token.substring(0, Math.min(24, token.length())) + "...");
- String simBody = "{\"templateId\":" + templateId + ",\"content\":" + jsonEscape(MSG) + "}";
- HttpRequest simReq = HttpRequest.newBuilder()
- .uri(URI.create(BASE + "/workflow/simulate"))
- .timeout(Duration.ofSeconds(180))
- .header("Authorization", "Bearer " + token)
- .header("Content-Type", "application/json; charset=utf-8")
- .POST(HttpRequest.BodyPublishers.ofString(simBody, StandardCharsets.UTF_8))
- .build();
- System.out.println("\nPOST " + BASE + "/workflow/simulate templateId=" + templateId);
- System.out.println("input: " + MSG);
- HttpResponse<String> simResp = client.send(simReq, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
- System.out.println("simulate HTTP " + simResp.statusCode());
- System.out.println(simResp.body());
- String body = simResp.body();
- if (body.contains("\"mode\":\"engine\"")) {
- System.out.println("\n[OK] mode=engine");
- } else if (body.contains("\"mode\":\"legacy\"")) {
- System.out.println("\n[FAIL] mode=legacy");
- System.exit(3);
- }
- if (body.contains("\"shouldProbe\":true")) {
- System.out.println("[OK] shouldProbe=true");
- } else {
- System.out.println("[WARN] shouldProbe not true");
- }
- }
- private static String extractToken(String json) {
- int i = json.indexOf("\"token\"");
- if (i < 0) {
- return null;
- }
- int colon = json.indexOf(':', i);
- int q1 = json.indexOf('"', colon + 1);
- int q2 = json.indexOf('"', q1 + 1);
- return json.substring(q1 + 1, q2);
- }
- private static String jsonEscape(String s) {
- return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"")
- .replace("\n", "\\n").replace("\r", "\\r") + "\"";
- }
- }
|