Skip to content

Commit fbfed3b

Browse files
authored
fix deprecation warnings and cleanup code (#17)
1 parent 7511f8e commit fbfed3b

File tree

5 files changed

+78
-138
lines changed

5 files changed

+78
-138
lines changed

src/main/java/io/github/lambdatest/gradle/AppUploader.java

+4-36
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package io.github.lambdatest.gradle;
22

3-
import com.google.gson.JsonObject;
4-
import com.google.gson.JsonParser;
53
import java.io.IOException;
64
import java.util.concurrent.CompletableFuture;
7-
import okhttp3.*;
85
import org.apache.logging.log4j.LogManager;
96
import org.apache.logging.log4j.Logger;
107

@@ -30,39 +27,10 @@ public CompletableFuture<String> uploadAppAsync() {
3027
return CompletableFuture.supplyAsync(
3128
() -> {
3229
try {
33-
OkHttpClient client = new OkHttpClient().newBuilder().build();
34-
MediaType mediaType = MediaType.parse("application/octet-stream");
35-
RequestBody body =
36-
new MultipartBody.Builder()
37-
.setType(MultipartBody.FORM)
38-
.addFormDataPart(
39-
"appFile",
40-
appFilePath,
41-
RequestBody.create(
42-
mediaType, new java.io.File(appFilePath)))
43-
.addFormDataPart("type", "espresso-android")
44-
.build();
45-
Request request =
46-
new Request.Builder()
47-
.url(Constants.API_URL)
48-
.method("POST", body)
49-
.addHeader(
50-
"Authorization",
51-
Credentials.basic(username, accessKey))
52-
.build();
53-
try (Response response = client.newCall(request).execute()) {
54-
if (!response.isSuccessful())
55-
throw new IOException("Unexpected code " + response);
56-
57-
// Parse the JSON response and extract the app_id
58-
String responseBody = response.body().string();
59-
JsonObject jsonObject =
60-
JsonParser.parseString(responseBody).getAsJsonObject();
61-
String appId = jsonObject.get("app_id").getAsString();
62-
63-
logger.info("Uploaded app ID: {}", appId);
64-
return appId;
65-
}
30+
String appId =
31+
UploaderUtil.uploadAndGetId(username, accessKey, appFilePath);
32+
logger.info("Uploaded app ID: {}", appId);
33+
return appId;
6634
} catch (IOException e) {
6735
logger.error("Error uploading app: {}", e.getMessage());
6836
throw new RuntimeException(e);

src/main/java/io/github/lambdatest/gradle/Constants.java

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package io.github.lambdatest.gradle;
22

33
public class Constants {
4+
private Constants() {
5+
throw new UnsupportedOperationException(
6+
"This is a utility class and cannot be instantiated");
7+
}
8+
49
public static final String API_URL = "https://manual-api.lambdatest.com/app/uploadFramework";
510
public static final String BUILD_URL =
611
"https://mobile-api.lambdatest.com/framework/v1/espresso/build";

src/main/java/io/github/lambdatest/gradle/TestExecutor.java

+13-66
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package io.github.lambdatest.gradle;
22

3+
import com.google.gson.Gson;
34
import java.io.IOException;
5+
import java.util.HashMap;
46
import java.util.List;
57
import java.util.Map;
6-
import java.util.stream.Collectors;
78
import okhttp3.*;
89
import org.apache.logging.log4j.LogManager;
910
import org.apache.logging.log4j.Logger;
@@ -35,85 +36,31 @@ public TestExecutor(
3536

3637
public void executeTests(Map<String, String> params) throws IOException {
3738
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();
4541

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");
5443

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);
10149

102-
jsonBodyBuilder.append("\n}");
103-
logger.info("Capabilities: {}", jsonBodyBuilder);
50+
logger.info("Capabilities: {}", capabilities);
10451

10552
String url =
10653
(isFlutter == null || !isFlutter)
10754
? Constants.BUILD_URL
10855
: Constants.FLUTTER_BUILD_URL;
109-
RequestBody body = RequestBody.create(mediaType, jsonBodyBuilder.toString());
56+
RequestBody body = RequestBody.create(gson.toJson(capabilities), mediaType);
11057

11158
Request request =
11259
new Request.Builder()
11360
.url(url)
114-
.method("POST", body)
11561
.addHeader("Authorization", Credentials.basic(username, accessKey))
11662
.addHeader("Content-Type", "application/json")
63+
.post(body)
11764
.build();
11865
Response response = client.newCall(request).execute();
11966

src/main/java/io/github/lambdatest/gradle/TestSuiteUploader.java

+4-36
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package io.github.lambdatest.gradle;
22

3-
import com.google.gson.JsonObject;
4-
import com.google.gson.JsonParser;
53
import java.io.IOException;
64
import java.util.concurrent.CompletableFuture;
7-
import okhttp3.*;
85
import org.apache.logging.log4j.LogManager;
96
import org.apache.logging.log4j.Logger;
107

@@ -26,39 +23,10 @@ public CompletableFuture<String> uploadTestSuiteAsync() {
2623
return CompletableFuture.supplyAsync(
2724
() -> {
2825
try {
29-
OkHttpClient client = new OkHttpClient().newBuilder().build();
30-
MediaType mediaType = MediaType.parse("application/octet-stream");
31-
RequestBody body =
32-
new MultipartBody.Builder()
33-
.setType(MultipartBody.FORM)
34-
.addFormDataPart(
35-
"appFile",
36-
testSuiteFilePath,
37-
RequestBody.create(
38-
mediaType,
39-
new java.io.File(testSuiteFilePath)))
40-
.addFormDataPart("type", "espresso-android")
41-
.build();
42-
Request request =
43-
new Request.Builder()
44-
.url(Constants.API_URL)
45-
.method("POST", body)
46-
.addHeader(
47-
"Authorization",
48-
Credentials.basic(username, accessKey))
49-
.build();
50-
try (Response response = client.newCall(request).execute()) {
51-
if (!response.isSuccessful())
52-
throw new IOException("Unexpected code " + response);
53-
54-
String responseBody = response.body().string();
55-
JsonObject jsonObject =
56-
JsonParser.parseString(responseBody).getAsJsonObject();
57-
String testSuiteId = jsonObject.get("app_id").getAsString();
58-
59-
logger.info("Uploaded test suite ID: {}", testSuiteId);
60-
return testSuiteId;
61-
}
26+
String testSuiteId =
27+
UploaderUtil.uploadAndGetId(username, accessKey, testSuiteFilePath);
28+
logger.info("Uploaded test suite ID: {}", testSuiteId);
29+
return testSuiteId;
6230
} catch (IOException e) {
6331
logger.error("Error uploading test suite app: {}", e.getMessage());
6432
throw new RuntimeException(e);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package io.github.lambdatest.gradle;
2+
3+
import com.google.gson.JsonObject;
4+
import com.google.gson.JsonParser;
5+
import java.io.File;
6+
import java.io.IOException;
7+
import okhttp3.Credentials;
8+
import okhttp3.MediaType;
9+
import okhttp3.MultipartBody;
10+
import okhttp3.OkHttpClient;
11+
import okhttp3.Request;
12+
import okhttp3.RequestBody;
13+
import okhttp3.Response;
14+
15+
public final class UploaderUtil {
16+
private UploaderUtil() {
17+
throw new UnsupportedOperationException(
18+
"This is a utility class and cannot be instantiated");
19+
}
20+
21+
public static String uploadAndGetId(String username, String accessKey, String filePath)
22+
throws IOException {
23+
OkHttpClient client = new OkHttpClient();
24+
25+
MediaType mediaType = MediaType.parse("application/octet-stream");
26+
RequestBody body =
27+
new MultipartBody.Builder()
28+
.setType(MultipartBody.FORM)
29+
.addFormDataPart(
30+
"appFile",
31+
filePath,
32+
RequestBody.create(new File(filePath), mediaType))
33+
.addFormDataPart("type", "espresso-android")
34+
.build();
35+
Request request =
36+
new Request.Builder()
37+
.url(Constants.API_URL)
38+
.addHeader("Authorization", Credentials.basic(username, accessKey))
39+
.post(body)
40+
.build();
41+
42+
try (Response response = client.newCall(request).execute()) {
43+
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
44+
45+
String responseBody = response.body().string();
46+
JsonObject jsonObject = JsonParser.parseString(responseBody).getAsJsonObject();
47+
String id = jsonObject.get("app_id").getAsString();
48+
49+
return id;
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)