|
1 | 1 | package io.github.lambdatest.gradle;
|
2 | 2 |
|
| 3 | +import com.google.gson.Gson; |
3 | 4 | import java.io.IOException;
|
| 5 | +import java.util.HashMap; |
4 | 6 | import java.util.List;
|
5 | 7 | import java.util.Map;
|
6 |
| -import java.util.stream.Collectors; |
7 | 8 | import okhttp3.*;
|
8 | 9 | import org.apache.logging.log4j.LogManager;
|
9 | 10 | import org.apache.logging.log4j.Logger;
|
@@ -35,85 +36,31 @@ public TestExecutor(
|
35 | 36 |
|
36 | 37 | public void executeTests(Map<String, String> params) throws IOException {
|
37 | 38 | try {
|
38 |
| - OkHttpClient client = new OkHttpClient().newBuilder().build(); |
39 |
| - MediaType mediaType = MediaType.parse("application/json"); |
40 |
| - |
41 |
| - String devicesJsonArray = |
42 |
| - device.stream() |
43 |
| - .map(device -> "\"" + device + "\"") |
44 |
| - .collect(Collectors.joining(",", "[", "]")); |
| 39 | + OkHttpClient client = new OkHttpClient(); |
| 40 | + Gson gson = new Gson(); |
45 | 41 |
|
46 |
| - StringBuilder jsonBodyBuilder = |
47 |
| - new StringBuilder( |
48 |
| - String.format( |
49 |
| - "{\n" |
50 |
| - + " \"app\" : \"%s\",\n" |
51 |
| - + " \"testSuite\": \"%s\",\n" |
52 |
| - + " \"device\" : %s", |
53 |
| - appId, testSuiteId, devicesJsonArray)); |
| 42 | + MediaType mediaType = MediaType.parse("application/json"); |
54 | 43 |
|
55 |
| - if (params.get("build") != null) |
56 |
| - jsonBodyBuilder.append( |
57 |
| - String.format(",\n \"build\": \"%s\"", params.get("build"))); |
58 |
| - if (params.get("deviceLog") != null) |
59 |
| - jsonBodyBuilder.append( |
60 |
| - String.format(",\n \"deviceLog\": %s", params.get("deviceLog"))); |
61 |
| - if (params.get("IdleTimeout") != null) |
62 |
| - jsonBodyBuilder.append( |
63 |
| - String.format(",\n \"IdleTimeout\": %s", params.get("IdleTimeout"))); |
64 |
| - if (params.get("queueTimeout") != null) |
65 |
| - jsonBodyBuilder.append( |
66 |
| - String.format(",\n \"queueTimeout\": %s", params.get("queueTimeout"))); |
67 |
| - if (params.get("video") != null) |
68 |
| - jsonBodyBuilder.append(String.format(",\n \"video\": %s", params.get("video"))); |
69 |
| - if (params.get("network") != null) |
70 |
| - jsonBodyBuilder.append( |
71 |
| - String.format(",\n \"network\": %s", params.get("network"))); |
72 |
| - if (params.get("tunnel") != null) |
73 |
| - jsonBodyBuilder.append( |
74 |
| - String.format(",\n \"tunnel\": %s", params.get("tunnel"))); |
75 |
| - if (params.get("tunnelName") != null) |
76 |
| - jsonBodyBuilder.append( |
77 |
| - String.format(",\n \"tunnelName\": \"%s\"", params.get("tunnelName"))); |
78 |
| - if (params.get("geoLocation") != null) |
79 |
| - jsonBodyBuilder.append( |
80 |
| - String.format(",\n \"geoLocation\": \"%s\"", params.get("geoLocation"))); |
81 |
| - if (params.get("fixedIp") != null) |
82 |
| - jsonBodyBuilder.append( |
83 |
| - String.format(",\n \"fixedIp\": \"%s\"", params.get("fixedIp"))); |
84 |
| - if (params.get("globalHttpProxy") != null) |
85 |
| - jsonBodyBuilder.append( |
86 |
| - String.format( |
87 |
| - ",\n \"globalHttpProxy\": %s", params.get("globalHttpProxy"))); |
88 |
| - if (params.get("singleRunnerInvocation") != null) |
89 |
| - jsonBodyBuilder.append( |
90 |
| - String.format( |
91 |
| - ",\n \"singleRunnerInvocation\": %s", |
92 |
| - params.get("singleRunnerInvocation"))); |
93 |
| - if (params.get("clearPackageData") != null) |
94 |
| - jsonBodyBuilder.append( |
95 |
| - String.format( |
96 |
| - ",\n \"clearPackageData\": %s", params.get("clearPackageData"))); |
97 |
| - if (params.get("disableAnimation") != null) |
98 |
| - jsonBodyBuilder.append( |
99 |
| - String.format( |
100 |
| - ",\n \"disableAnimation\": %s", params.get("disableAnimation"))); |
| 44 | + Map<String, Object> capabilities = new HashMap<>(); |
| 45 | + capabilities.put("app", appId); |
| 46 | + capabilities.put("testSuite", testSuiteId); |
| 47 | + capabilities.put("device", device); |
| 48 | + capabilities.putAll(params); |
101 | 49 |
|
102 |
| - jsonBodyBuilder.append("\n}"); |
103 |
| - logger.info("Capabilities: {}", jsonBodyBuilder); |
| 50 | + logger.info("Capabilities: {}", capabilities); |
104 | 51 |
|
105 | 52 | String url =
|
106 | 53 | (isFlutter == null || !isFlutter)
|
107 | 54 | ? Constants.BUILD_URL
|
108 | 55 | : Constants.FLUTTER_BUILD_URL;
|
109 |
| - RequestBody body = RequestBody.create(mediaType, jsonBodyBuilder.toString()); |
| 56 | + RequestBody body = RequestBody.create(gson.toJson(capabilities), mediaType); |
110 | 57 |
|
111 | 58 | Request request =
|
112 | 59 | new Request.Builder()
|
113 | 60 | .url(url)
|
114 |
| - .method("POST", body) |
115 | 61 | .addHeader("Authorization", Credentials.basic(username, accessKey))
|
116 | 62 | .addHeader("Content-Type", "application/json")
|
| 63 | + .post(body) |
117 | 64 | .build();
|
118 | 65 | Response response = client.newCall(request).execute();
|
119 | 66 |
|
|
0 commit comments