Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix deprecation warnings and cleanup code #17

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 4 additions & 36 deletions src/main/java/io/github/lambdatest/gradle/AppUploader.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.github.lambdatest.gradle;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import okhttp3.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -30,39 +27,10 @@ public CompletableFuture<String> uploadAppAsync() {
return CompletableFuture.supplyAsync(
() -> {
try {
OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("application/octet-stream");
RequestBody body =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(
"appFile",
appFilePath,
RequestBody.create(
mediaType, new java.io.File(appFilePath)))
.addFormDataPart("type", "espresso-android")
.build();
Request request =
new Request.Builder()
.url(Constants.API_URL)
.method("POST", body)
.addHeader(
"Authorization",
Credentials.basic(username, accessKey))
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);

// Parse the JSON response and extract the app_id
String responseBody = response.body().string();
JsonObject jsonObject =
JsonParser.parseString(responseBody).getAsJsonObject();
String appId = jsonObject.get("app_id").getAsString();

logger.info("Uploaded app ID: {}", appId);
return appId;
}
String appId =
UploaderUtil.uploadAndGetId(username, accessKey, appFilePath);
logger.info("Uploaded app ID: {}", appId);
return appId;
} catch (IOException e) {
logger.error("Error uploading app: {}", e.getMessage());
throw new RuntimeException(e);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/github/lambdatest/gradle/Constants.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package io.github.lambdatest.gradle;

public class Constants {
private Constants() {
throw new UnsupportedOperationException(
"This is a utility class and cannot be instantiated");
}

public static final String API_URL = "https://manual-api.lambdatest.com/app/uploadFramework";
public static final String BUILD_URL =
"https://mobile-api.lambdatest.com/framework/v1/espresso/build";
Expand Down
79 changes: 13 additions & 66 deletions src/main/java/io/github/lambdatest/gradle/TestExecutor.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.github.lambdatest.gradle;

import com.google.gson.Gson;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import okhttp3.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -35,85 +36,31 @@ public TestExecutor(

public void executeTests(Map<String, String> params) throws IOException {
try {
OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("application/json");

String devicesJsonArray =
device.stream()
.map(device -> "\"" + device + "\"")
.collect(Collectors.joining(",", "[", "]"));
OkHttpClient client = new OkHttpClient();
Gson gson = new Gson();

StringBuilder jsonBodyBuilder =
new StringBuilder(
String.format(
"{\n"
+ " \"app\" : \"%s\",\n"
+ " \"testSuite\": \"%s\",\n"
+ " \"device\" : %s",
appId, testSuiteId, devicesJsonArray));
MediaType mediaType = MediaType.parse("application/json");

if (params.get("build") != null)
jsonBodyBuilder.append(
String.format(",\n \"build\": \"%s\"", params.get("build")));
if (params.get("deviceLog") != null)
jsonBodyBuilder.append(
String.format(",\n \"deviceLog\": %s", params.get("deviceLog")));
if (params.get("IdleTimeout") != null)
jsonBodyBuilder.append(
String.format(",\n \"IdleTimeout\": %s", params.get("IdleTimeout")));
if (params.get("queueTimeout") != null)
jsonBodyBuilder.append(
String.format(",\n \"queueTimeout\": %s", params.get("queueTimeout")));
if (params.get("video") != null)
jsonBodyBuilder.append(String.format(",\n \"video\": %s", params.get("video")));
if (params.get("network") != null)
jsonBodyBuilder.append(
String.format(",\n \"network\": %s", params.get("network")));
if (params.get("tunnel") != null)
jsonBodyBuilder.append(
String.format(",\n \"tunnel\": %s", params.get("tunnel")));
if (params.get("tunnelName") != null)
jsonBodyBuilder.append(
String.format(",\n \"tunnelName\": \"%s\"", params.get("tunnelName")));
if (params.get("geoLocation") != null)
jsonBodyBuilder.append(
String.format(",\n \"geoLocation\": \"%s\"", params.get("geoLocation")));
if (params.get("fixedIp") != null)
jsonBodyBuilder.append(
String.format(",\n \"fixedIp\": \"%s\"", params.get("fixedIp")));
if (params.get("globalHttpProxy") != null)
jsonBodyBuilder.append(
String.format(
",\n \"globalHttpProxy\": %s", params.get("globalHttpProxy")));
if (params.get("singleRunnerInvocation") != null)
jsonBodyBuilder.append(
String.format(
",\n \"singleRunnerInvocation\": %s",
params.get("singleRunnerInvocation")));
if (params.get("clearPackageData") != null)
jsonBodyBuilder.append(
String.format(
",\n \"clearPackageData\": %s", params.get("clearPackageData")));
if (params.get("disableAnimation") != null)
jsonBodyBuilder.append(
String.format(
",\n \"disableAnimation\": %s", params.get("disableAnimation")));
Map<String, Object> capabilities = new HashMap<>();
capabilities.put("app", appId);
capabilities.put("testSuite", testSuiteId);
capabilities.put("device", device);
capabilities.putAll(params);

jsonBodyBuilder.append("\n}");
logger.info("Capabilities: {}", jsonBodyBuilder);
logger.info("Capabilities: {}", capabilities);

String url =
(isFlutter == null || !isFlutter)
? Constants.BUILD_URL
: Constants.FLUTTER_BUILD_URL;
RequestBody body = RequestBody.create(mediaType, jsonBodyBuilder.toString());
RequestBody body = RequestBody.create(gson.toJson(capabilities), mediaType);

Request request =
new Request.Builder()
.url(url)
.method("POST", body)
.addHeader("Authorization", Credentials.basic(username, accessKey))
.addHeader("Content-Type", "application/json")
.post(body)
.build();
Response response = client.newCall(request).execute();

Expand Down
40 changes: 4 additions & 36 deletions src/main/java/io/github/lambdatest/gradle/TestSuiteUploader.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.github.lambdatest.gradle;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import okhttp3.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -26,39 +23,10 @@ public CompletableFuture<String> uploadTestSuiteAsync() {
return CompletableFuture.supplyAsync(
() -> {
try {
OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("application/octet-stream");
RequestBody body =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(
"appFile",
testSuiteFilePath,
RequestBody.create(
mediaType,
new java.io.File(testSuiteFilePath)))
.addFormDataPart("type", "espresso-android")
.build();
Request request =
new Request.Builder()
.url(Constants.API_URL)
.method("POST", body)
.addHeader(
"Authorization",
Credentials.basic(username, accessKey))
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);

String responseBody = response.body().string();
JsonObject jsonObject =
JsonParser.parseString(responseBody).getAsJsonObject();
String testSuiteId = jsonObject.get("app_id").getAsString();

logger.info("Uploaded test suite ID: {}", testSuiteId);
return testSuiteId;
}
String testSuiteId =
UploaderUtil.uploadAndGetId(username, accessKey, testSuiteFilePath);
logger.info("Uploaded test suite ID: {}", testSuiteId);
return testSuiteId;
} catch (IOException e) {
logger.error("Error uploading test suite app: {}", e.getMessage());
throw new RuntimeException(e);
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/io/github/lambdatest/gradle/UploaderUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.github.lambdatest.gradle;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.File;
import java.io.IOException;
import okhttp3.Credentials;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public final class UploaderUtil {
private UploaderUtil() {
throw new UnsupportedOperationException(
"This is a utility class and cannot be instantiated");
}

public static String uploadAndGetId(String username, String accessKey, String filePath)
throws IOException {
OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/octet-stream");
RequestBody body =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(
"appFile",
filePath,
RequestBody.create(new File(filePath), mediaType))
.addFormDataPart("type", "espresso-android")
.build();
Request request =
new Request.Builder()
.url(Constants.API_URL)
.addHeader("Authorization", Credentials.basic(username, accessKey))
.post(body)
.build();

try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

String responseBody = response.body().string();
JsonObject jsonObject = JsonParser.parseString(responseBody).getAsJsonObject();
String id = jsonObject.get("app_id").getAsString();

return id;
}
}
}
Loading