Skip to content

Commit

Permalink
EPMRPP-94504 improve saucelabs plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
grabsefx committed Oct 22, 2024
1 parent a75360a commit c455cff
Show file tree
Hide file tree
Showing 20 changed files with 288 additions and 117 deletions.
16 changes: 11 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ dependencies {
implementation 'com.github.reportportal:plugin-api:a9a8b73'
annotationProcessor 'com.github.reportportal:plugin-api:a9a8b73'
}
implementation 'org.hibernate:hibernate-core:6.5.2.Final'
implementation 'org.hibernate:hibernate-core:5.6.15.Final'

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.7.2'

// add lombok support
compileOnly "org.projectlombok:lombok:${lombokVersion}"
Expand All @@ -54,6 +57,11 @@ dependencies {

}

test {
useJUnitPlatform()
onlyIf { Boolean.getBoolean(testsEnabled) } // enable for debugging purposes only
}

generatePomFileForShadowPublication { pom.packaging = "jar" }

jar {
Expand All @@ -64,7 +72,7 @@ jar {
"Plugin-Version": "${project.version}",
"Plugin-Provider": "Pavel Bortnik",
"Plugin-Class": "com.epam.reportportal.saucelabs.SaucelabsPlugin",
// "Plugin-Dependencies": ""
// "Plugin-Dependencies": ""
)
}
}
Expand All @@ -73,9 +81,7 @@ shadowJar {
archiveClassifier.set(null)
zip64 true
dependencies {
// include(dependency('com.saucelabs:saucerest:2.5.1'))


include(dependency('org.hibernate:hibernate-core:5.6.15.Final'))
}
}

Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
version=5.11.0
lombokVersion=1.18.34
testsEnabled=false
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,42 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.epam.reportportal.saucelabs;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* @author <a href="mailto:[email protected]">Ivan Budayeu</a>
*/
public class MemoizingSupplier<T> implements Supplier<T> {

private final Supplier<T> delegate;

private AtomicBoolean initialized = new AtomicBoolean(false);

private T value;

public MemoizingSupplier(Supplier<T> delegate) {
this.delegate = checkNotNull(delegate);
}

@Override
public T get() {
if (!initialized.get()) {
synchronized (this) {
if (!initialized.get()) {
T t = delegate.get();
value = t;
initialized.set(true);
return t;
}
}
}
return value;
}
private final Supplier<T> delegate;

private AtomicBoolean initialized = new AtomicBoolean(false);

private T value;

public MemoizingSupplier(Supplier<T> delegate) {
this.delegate = checkNotNull(delegate);
}

@Override
public T get() {
if (!initialized.get()) {
synchronized (this) {
if (!initialized.get()) {
T t = delegate.get();
value = t;
initialized.set(true);
return t;
}
}
}
return value;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.epam.reportportal.saucelabs;

import static java.util.stream.Collectors.toList;

import com.epam.reportportal.extension.CommonPluginCommand;
import com.epam.reportportal.extension.PluginCommand;
import com.epam.reportportal.extension.ReportPortalExtensionPoint;
Expand All @@ -26,6 +28,7 @@
import com.epam.reportportal.saucelabs.command.GetRealDeviceJobCommand;
import com.epam.reportportal.saucelabs.command.GetVirtualDeviceJobCommand;
import com.epam.reportportal.saucelabs.command.TestConnectionCommand;
import com.epam.reportportal.saucelabs.model.DataCenter;
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -44,7 +47,6 @@ public class SaucelabsExtension implements ReportPortalExtensionPoint {

private static final String DOCUMENTATION_LINK_FIELD = "documentationLink";
private static final String DOCUMENTATION_LINK = "https://reportportal.io/docs/plugins/SauceLabs";
static final String JOB_ID = "jobId";

private final Supplier<Map<String, PluginCommand<?>>> pluginCommandMapping = new MemoizingSupplier<>(
this::getCommands);
Expand All @@ -64,8 +66,10 @@ public SaucelabsExtension() {
Map<String, Object> params = new HashMap<>();
params.put(ALLOWED_COMMANDS, new ArrayList<>(pluginCommandMapping.get().keySet()));
params.put(DOCUMENTATION_LINK_FIELD, DOCUMENTATION_LINK);
//params.put("dataCenters", Arrays.stream(DataCenter.values()).map(Enum::toString).collect(Collectors.toList()));
params.put("dataCenters", Arrays.asList("US", "EU"));
/*params.put("dataCenters", Arrays.stream(DataCenter.values())
.map(Enum::toString)
.collect(toList()));*/
params.put("dataCenters", Arrays.asList("US", "EU", "US_EAST"));

return params;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.epam.reportportal.saucelabs;

import org.pf4j.Plugin;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.epam.reportportal.saucelabs.client;

import com.epam.reportportal.saucelabs.model.SauceProperties;
import com.epam.reportportal.saucelabs.model.IntegrationProperties;
import org.jasypt.util.text.BasicTextEncryptor;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.web.client.RestTemplate;
Expand All @@ -13,12 +13,11 @@ public RestClientBuilder(BasicTextEncryptor textEncryptor) {
this.textEncryptor = textEncryptor;
}

public RestTemplate buildRestTemplate(SauceProperties sp) {
public RestTemplate buildRestTemplate(IntegrationProperties sp) {

return new RestTemplateBuilder()
.basicAuthentication(sp.getUsername(), textEncryptor.decrypt(sp.getToken()))
.rootUri(sp.getDatacenter().getBaseUrl())
.build();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import com.epam.reportportal.extension.PluginCommand;
import com.epam.reportportal.rules.exception.ErrorType;
import com.epam.reportportal.rules.exception.ReportPortalException;
import com.epam.reportportal.saucelabs.ValidationUtils;
import com.epam.reportportal.saucelabs.client.RestClientBuilder;
import com.epam.reportportal.saucelabs.model.SauceProperties;
import com.epam.reportportal.saucelabs.model.IntegrationProperties;
import com.epam.reportportal.saucelabs.utils.ValidationUtils;
import com.epam.ta.reportportal.entity.integration.Integration;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonObject;
Expand Down Expand Up @@ -55,7 +55,7 @@ public Object executeCommand(Integration integration, Map<String, Object> params
ValidationUtils.validateIntegrationParams(integration.getParams());
ValidationUtils.validateJobId(params);

SauceProperties sp = new SauceProperties(integration.getParams().getParams());
IntegrationProperties sp = new IntegrationProperties(integration.getParams().getParams());
sp.setJobId((String) params.get(JOB_ID));
RestTemplate restTemplate = restClient.buildRestTemplate(sp);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.epam.reportportal.saucelabs.command;

import static com.epam.reportportal.saucelabs.model.SaucelabsProperties.ACCESS_TOKEN;
import static com.epam.reportportal.saucelabs.model.SaucelabsProperties.USERNAME;
import static com.epam.reportportal.saucelabs.ValidationUtils.validateIntegrationParams;
import static com.epam.reportportal.saucelabs.ValidationUtils.validateJobId;
import static com.epam.reportportal.saucelabs.model.Constants.JOB_ID;
import static com.epam.reportportal.saucelabs.model.IntegrationParametersNames.ACCESS_TOKEN;
import static com.epam.reportportal.saucelabs.model.IntegrationParametersNames.USERNAME;
import static com.epam.reportportal.saucelabs.utils.ValidationUtils.validateIntegrationParams;
import static com.epam.reportportal.saucelabs.utils.ValidationUtils.validateJobId;

import com.epam.reportportal.extension.PluginCommand;
import com.epam.reportportal.rules.exception.ReportPortalException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@

package com.epam.reportportal.saucelabs.command;


import static com.epam.reportportal.saucelabs.model.Constants.GET_VDC_JOB_ASSETS;
import static com.epam.reportportal.saucelabs.model.Constants.GET_VDC_JOB_LOGS;
import static com.epam.reportportal.saucelabs.model.Constants.JOB_ID;
import static com.epam.reportportal.saucelabs.model.Constants.LOG_URL;

import com.epam.reportportal.extension.PluginCommand;
import com.epam.reportportal.rules.exception.ErrorType;
import com.epam.reportportal.rules.exception.ReportPortalException;
import com.epam.reportportal.saucelabs.ValidationUtils;
import com.epam.reportportal.saucelabs.client.RestClientBuilder;
import com.epam.reportportal.saucelabs.model.SauceProperties;
import com.epam.reportportal.saucelabs.model.IntegrationProperties;
import com.epam.reportportal.saucelabs.utils.ValidationUtils;
import com.epam.ta.reportportal.entity.integration.Integration;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

/**
* @author <a href="mailto:[email protected]">Pavel Bortnik</a>
Expand All @@ -49,12 +53,21 @@ public Object executeCommand(Integration integration, Map<String, Object> params
ValidationUtils.validateJobId(params);
ValidationUtils.validateIntegrationParams(integration.getParams());

SauceProperties sp = new SauceProperties(integration.getParams().getParams());
IntegrationProperties sp = new IntegrationProperties(integration.getParams().getParams());
sp.setJobId((String) params.get(JOB_ID));
RestTemplate restTemplate = restClient.buildRestTemplate(sp);

String logsUrl = String.format(GET_VDC_JOB_LOGS, sp.getUsername(), sp.getJobId());
try {
return restClient.buildRestTemplate(sp).getForObject(logsUrl, Object.class);
// check if logs exist
String assetsUrl = String.format(GET_VDC_JOB_ASSETS, sp.getUsername(), sp.getJobId());
String jobAssets = restTemplate.getForObject(assetsUrl, String.class);
JsonObject jsonElement = (JsonObject) JsonParser.parseString(jobAssets);
if (jsonElement.get(LOG_URL) == null) {
throw new ReportPortalException(ErrorType.NOT_FOUND, "Logs");
}

String logsUrl = String.format(GET_VDC_JOB_LOGS, sp.getUsername(), sp.getJobId());
return restTemplate.getForObject(logsUrl, Object.class);
} catch (HttpClientErrorException httpException) {
throw new ReportPortalException(ErrorType.UNABLE_INTERACT_WITH_INTEGRATION,
StringUtils.normalizeSpace("Failed to retrieve job assets"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 EPAM Systems
* Copyright 2024 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,10 +21,10 @@
import com.epam.reportportal.extension.PluginCommand;
import com.epam.reportportal.rules.exception.ErrorType;
import com.epam.reportportal.rules.exception.ReportPortalException;
import com.epam.reportportal.saucelabs.ValidationUtils;
import com.epam.reportportal.saucelabs.client.RestClientBuilder;
import com.epam.reportportal.saucelabs.model.Constants;
import com.epam.reportportal.saucelabs.model.SauceProperties;
import com.epam.reportportal.saucelabs.model.IntegrationProperties;
import com.epam.reportportal.saucelabs.utils.ValidationUtils;
import com.epam.ta.reportportal.entity.integration.Integration;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
Expand Down Expand Up @@ -53,15 +53,15 @@ public Object executeCommand(Integration integration, Map<String, Object> params
ValidationUtils.validateIntegrationParams(integration.getParams());
ValidationUtils.validateJobId(params);

SauceProperties sp = new SauceProperties(integration.getParams().getParams());
IntegrationProperties sp = new IntegrationProperties(integration.getParams().getParams());
sp.setJobId((String) params.get(Constants.JOB_ID));
RestTemplate restTemplate = restClient.buildRestTemplate(sp);

try {
String realDeviceJobUrl = String.format(GET_RDC_JOB, sp.getJobId());
String jobInfo = restTemplate.getForObject(realDeviceJobUrl, String.class);

return new ObjectMapper().readValue(jobInfo, Object.class); // try skip mapping
return new ObjectMapper().readValue(jobInfo, Object.class);

} catch (HttpClientErrorException httpException) {
throw new ReportPortalException(ErrorType.UNABLE_INTERACT_WITH_INTEGRATION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import com.epam.reportportal.extension.PluginCommand;
import com.epam.reportportal.rules.exception.ErrorType;
import com.epam.reportportal.rules.exception.ReportPortalException;
import com.epam.reportportal.saucelabs.ValidationUtils;
import com.epam.reportportal.saucelabs.client.RestClientBuilder;
import com.epam.reportportal.saucelabs.model.Constants;
import com.epam.reportportal.saucelabs.model.SauceProperties;
import com.epam.reportportal.saucelabs.model.IntegrationProperties;
import com.epam.reportportal.saucelabs.utils.ValidationUtils;
import com.epam.ta.reportportal.entity.integration.Integration;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
Expand Down Expand Up @@ -52,19 +52,19 @@ public Object executeCommand(Integration integration, Map params) {
ValidationUtils.validateIntegrationParams(integration.getParams());
ValidationUtils.validateJobId(params);

SauceProperties sp = new SauceProperties(integration.getParams().getParams());
IntegrationProperties sp = new IntegrationProperties(integration.getParams().getParams());
sp.setJobId((String) params.get(Constants.JOB_ID));
RestTemplate restTemplate = restClient.buildRestTemplate(sp);

try {
String realDeviceJobUrl = String.format(GET_VDC_JOB, sp.getUsername(), sp.getJobId());
String jobInfo = restTemplate.getForObject(realDeviceJobUrl, String.class);
String virtualDeviceJobUrl = String.format(GET_VDC_JOB, sp.getUsername(), sp.getJobId());
String jobInfo = restTemplate.getForObject(virtualDeviceJobUrl, String.class);

return new ObjectMapper().readValue(jobInfo, Object.class); // try skip mapping
return new ObjectMapper().readValue(jobInfo, Object.class);

} catch (HttpClientErrorException httpException) {
throw new ReportPortalException(ErrorType.UNABLE_INTERACT_WITH_INTEGRATION,
StringUtils.normalizeSpace("Failed to retrieve real device job info"));
StringUtils.normalizeSpace("Failed to retrieve virtual device job info"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import static com.epam.reportportal.saucelabs.model.Constants.GET_VDC_JOBS;

import com.epam.reportportal.saucelabs.ValidationUtils;
import com.epam.reportportal.saucelabs.client.RestClientBuilder;
import com.epam.reportportal.saucelabs.model.SauceProperties;
import com.epam.reportportal.saucelabs.model.IntegrationProperties;
import com.epam.reportportal.saucelabs.utils.ValidationUtils;
import com.epam.ta.reportportal.entity.integration.Integration;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -42,7 +42,7 @@ public TestConnectionCommand(RestClientBuilder restClient) {
@Override
public Boolean executeCommand(Integration integration, Map params) {
ValidationUtils.validateIntegrationParams(integration.getParams());
SauceProperties sp = new SauceProperties(integration.getParams().getParams());
IntegrationProperties sp = new IntegrationProperties(integration.getParams().getParams());
RestTemplate restTemplate = restClient.buildRestTemplate(sp);

try {
Expand Down
Loading

0 comments on commit c455cff

Please sign in to comment.