ProbeLoginAndSimulate.java 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import java.net.URI;
  2. import java.net.http.HttpClient;
  3. import java.net.http.HttpRequest;
  4. import java.net.http.HttpResponse;
  5. import java.nio.charset.StandardCharsets;
  6. import java.time.Duration;
  7. /** Login + simulate probe for cs1 tenant. */
  8. public class ProbeLoginAndSimulate {
  9. private static final String BASE = System.getenv().getOrDefault("LOBSTER_API_BASE", "http://127.0.0.1:8004");
  10. private static final String MSG = "\u4f60\u597d\uff0c\u60f3\u4e86\u89e3\u4e00\u4e0b\u5497\u4eec\u767d\u9152\u600e\u4e48\u5356\uff1f";
  11. public static void main(String[] args) throws Exception {
  12. String tenantCode = args.length > 0 ? args[0] : "cs1";
  13. String username = args.length > 1 ? args[1] : "admin";
  14. String password = args.length > 2 ? args[2] : "admin123";
  15. long templateId = args.length > 3 ? Long.parseLong(args[3]) : 70L;
  16. HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(15)).build();
  17. String loginBody = "{\"tenantCode\":\"" + tenantCode + "\",\"username\":\"" + username
  18. + "\",\"password\":\"" + password + "\",\"code\":\"\",\"uuid\":\"\"}";
  19. HttpRequest loginReq = HttpRequest.newBuilder()
  20. .uri(URI.create(BASE + "/login"))
  21. .timeout(Duration.ofSeconds(30))
  22. .header("Content-Type", "application/json; charset=utf-8")
  23. .POST(HttpRequest.BodyPublishers.ofString(loginBody, StandardCharsets.UTF_8))
  24. .build();
  25. System.out.println("POST " + BASE + "/login tenant=" + tenantCode + " user=" + username);
  26. HttpResponse<String> loginResp = client.send(loginReq, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
  27. System.out.println("login HTTP " + loginResp.statusCode());
  28. System.out.println(loginResp.body());
  29. if (loginResp.statusCode() != 200) {
  30. System.exit(1);
  31. }
  32. String token = extractToken(loginResp.body());
  33. if (token == null || token.isBlank()) {
  34. System.out.println("No token in login response");
  35. System.exit(2);
  36. }
  37. System.out.println("token prefix: " + token.substring(0, Math.min(24, token.length())) + "...");
  38. String simBody = "{\"templateId\":" + templateId + ",\"content\":" + jsonEscape(MSG) + "}";
  39. HttpRequest simReq = HttpRequest.newBuilder()
  40. .uri(URI.create(BASE + "/workflow/simulate"))
  41. .timeout(Duration.ofSeconds(180))
  42. .header("Authorization", "Bearer " + token)
  43. .header("Content-Type", "application/json; charset=utf-8")
  44. .POST(HttpRequest.BodyPublishers.ofString(simBody, StandardCharsets.UTF_8))
  45. .build();
  46. System.out.println("\nPOST " + BASE + "/workflow/simulate templateId=" + templateId);
  47. System.out.println("input: " + MSG);
  48. HttpResponse<String> simResp = client.send(simReq, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
  49. System.out.println("simulate HTTP " + simResp.statusCode());
  50. System.out.println(simResp.body());
  51. String body = simResp.body();
  52. if (body.contains("\"mode\":\"engine\"")) {
  53. System.out.println("\n[OK] mode=engine");
  54. } else if (body.contains("\"mode\":\"legacy\"")) {
  55. System.out.println("\n[FAIL] mode=legacy");
  56. System.exit(3);
  57. }
  58. if (body.contains("\"shouldProbe\":true")) {
  59. System.out.println("[OK] shouldProbe=true");
  60. } else {
  61. System.out.println("[WARN] shouldProbe not true");
  62. }
  63. }
  64. private static String extractToken(String json) {
  65. int i = json.indexOf("\"token\"");
  66. if (i < 0) {
  67. return null;
  68. }
  69. int colon = json.indexOf(':', i);
  70. int q1 = json.indexOf('"', colon + 1);
  71. int q2 = json.indexOf('"', q1 + 1);
  72. return json.substring(q1 + 1, q2);
  73. }
  74. private static String jsonEscape(String s) {
  75. return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"")
  76. .replace("\n", "\\n").replace("\r", "\\r") + "\"";
  77. }
  78. }