From c455cfff1cfd8d1b89afa6326a01efff557fb440 Mon Sep 17 00:00:00 2001 From: siarhei_hrabko Date: Tue, 22 Oct 2024 22:51:02 +0300 Subject: [PATCH] EPMRPP-94504 improve saucelabs plugin --- build.gradle | 16 ++++-- gradle.properties | 1 + .../saucelabs/MemoizingSupplier.java | 53 +++++++++--------- .../saucelabs/SaucelabsExtension.java | 10 +++- .../saucelabs/SaucelabsPlugin.java | 1 + .../saucelabs/client/RestClientBuilder.java | 5 +- .../saucelabs/command/AssetsCommand.java | 6 +- .../command/GenerateAuthTokenCommand.java | 8 +-- .../saucelabs/command/GetLogsCommand.java | 25 +++++++-- .../command/GetRealDeviceJobCommand.java | 10 ++-- .../command/GetVirtualDeviceJobCommand.java | 14 ++--- .../command/TestConnectionCommand.java | 6 +- .../saucelabs/model/Constants.java | 26 +++++++-- .../saucelabs/model/DataCenter.java | 24 ++++++-- ...s.java => IntegrationParametersNames.java} | 4 +- .../model/IntegrationProperties.java | 51 +++++++++++++++++ .../saucelabs/model/SauceProperties.java | 34 ----------- .../utils/DatacenterNameResolver.java | 56 +++++++++++++++++++ .../{ => utils}/ValidationUtils.java | 10 ++-- .../GetVirtualDeviceJobCommandTest.java | 45 +++++++++++++++ 20 files changed, 288 insertions(+), 117 deletions(-) rename src/main/java/com/epam/reportportal/saucelabs/model/{SaucelabsProperties.java => IntegrationParametersNames.java} (92%) create mode 100644 src/main/java/com/epam/reportportal/saucelabs/model/IntegrationProperties.java delete mode 100644 src/main/java/com/epam/reportportal/saucelabs/model/SauceProperties.java create mode 100644 src/main/java/com/epam/reportportal/saucelabs/utils/DatacenterNameResolver.java rename src/main/java/com/epam/reportportal/saucelabs/{ => utils}/ValidationUtils.java (84%) create mode 100644 src/test/java/com/epam/reportportal/saucelabs/command/GetVirtualDeviceJobCommandTest.java diff --git a/build.gradle b/build.gradle index 3fe8837..1e39ed9 100644 --- a/build.gradle +++ b/build.gradle @@ -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}" @@ -54,6 +57,11 @@ dependencies { } +test { + useJUnitPlatform() + onlyIf { Boolean.getBoolean(testsEnabled) } // enable for debugging purposes only +} + generatePomFileForShadowPublication { pom.packaging = "jar" } jar { @@ -64,7 +72,7 @@ jar { "Plugin-Version": "${project.version}", "Plugin-Provider": "Pavel Bortnik", "Plugin-Class": "com.epam.reportportal.saucelabs.SaucelabsPlugin", -// "Plugin-Dependencies": "" + // "Plugin-Dependencies": "" ) } } @@ -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')) } } diff --git a/gradle.properties b/gradle.properties index 4c492f1..a901d0b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,3 @@ version=5.11.0 lombokVersion=1.18.34 +testsEnabled=false diff --git a/src/main/java/com/epam/reportportal/saucelabs/MemoizingSupplier.java b/src/main/java/com/epam/reportportal/saucelabs/MemoizingSupplier.java index 6036c3f..1281488 100644 --- a/src/main/java/com/epam/reportportal/saucelabs/MemoizingSupplier.java +++ b/src/main/java/com/epam/reportportal/saucelabs/MemoizingSupplier.java @@ -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 Ivan Budayeu */ public class MemoizingSupplier implements Supplier { - private final Supplier delegate; - - private AtomicBoolean initialized = new AtomicBoolean(false); - - private T value; - - public MemoizingSupplier(Supplier 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 delegate; + + private AtomicBoolean initialized = new AtomicBoolean(false); + + private T value; + + public MemoizingSupplier(Supplier 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; + } } diff --git a/src/main/java/com/epam/reportportal/saucelabs/SaucelabsExtension.java b/src/main/java/com/epam/reportportal/saucelabs/SaucelabsExtension.java index 7f4d043..fad47fd 100644 --- a/src/main/java/com/epam/reportportal/saucelabs/SaucelabsExtension.java +++ b/src/main/java/com/epam/reportportal/saucelabs/SaucelabsExtension.java @@ -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; @@ -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; @@ -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>> pluginCommandMapping = new MemoizingSupplier<>( this::getCommands); @@ -64,8 +66,10 @@ public SaucelabsExtension() { Map 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; } diff --git a/src/main/java/com/epam/reportportal/saucelabs/SaucelabsPlugin.java b/src/main/java/com/epam/reportportal/saucelabs/SaucelabsPlugin.java index 4d7d65e..30c0918 100644 --- a/src/main/java/com/epam/reportportal/saucelabs/SaucelabsPlugin.java +++ b/src/main/java/com/epam/reportportal/saucelabs/SaucelabsPlugin.java @@ -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; diff --git a/src/main/java/com/epam/reportportal/saucelabs/client/RestClientBuilder.java b/src/main/java/com/epam/reportportal/saucelabs/client/RestClientBuilder.java index 6ae975b..ccb244f 100644 --- a/src/main/java/com/epam/reportportal/saucelabs/client/RestClientBuilder.java +++ b/src/main/java/com/epam/reportportal/saucelabs/client/RestClientBuilder.java @@ -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; @@ -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(); - } } diff --git a/src/main/java/com/epam/reportportal/saucelabs/command/AssetsCommand.java b/src/main/java/com/epam/reportportal/saucelabs/command/AssetsCommand.java index 33a3706..c09c797 100644 --- a/src/main/java/com/epam/reportportal/saucelabs/command/AssetsCommand.java +++ b/src/main/java/com/epam/reportportal/saucelabs/command/AssetsCommand.java @@ -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; @@ -55,7 +55,7 @@ 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(JOB_ID)); RestTemplate restTemplate = restClient.buildRestTemplate(sp); diff --git a/src/main/java/com/epam/reportportal/saucelabs/command/GenerateAuthTokenCommand.java b/src/main/java/com/epam/reportportal/saucelabs/command/GenerateAuthTokenCommand.java index d524d5e..4c87068 100644 --- a/src/main/java/com/epam/reportportal/saucelabs/command/GenerateAuthTokenCommand.java +++ b/src/main/java/com/epam/reportportal/saucelabs/command/GenerateAuthTokenCommand.java @@ -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; diff --git a/src/main/java/com/epam/reportportal/saucelabs/command/GetLogsCommand.java b/src/main/java/com/epam/reportportal/saucelabs/command/GetLogsCommand.java index 94a6c75..df29dee 100644 --- a/src/main/java/com/epam/reportportal/saucelabs/command/GetLogsCommand.java +++ b/src/main/java/com/epam/reportportal/saucelabs/command/GetLogsCommand.java @@ -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 Pavel Bortnik @@ -49,12 +53,21 @@ public Object executeCommand(Integration integration, Map 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")); diff --git a/src/main/java/com/epam/reportportal/saucelabs/command/GetRealDeviceJobCommand.java b/src/main/java/com/epam/reportportal/saucelabs/command/GetRealDeviceJobCommand.java index 8b4c5c0..40d7d16 100644 --- a/src/main/java/com/epam/reportportal/saucelabs/command/GetRealDeviceJobCommand.java +++ b/src/main/java/com/epam/reportportal/saucelabs/command/GetRealDeviceJobCommand.java @@ -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. @@ -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; @@ -53,7 +53,7 @@ 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); @@ -61,7 +61,7 @@ public Object executeCommand(Integration integration, Map params 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, diff --git a/src/main/java/com/epam/reportportal/saucelabs/command/GetVirtualDeviceJobCommand.java b/src/main/java/com/epam/reportportal/saucelabs/command/GetVirtualDeviceJobCommand.java index 75d7b41..1383bbe 100644 --- a/src/main/java/com/epam/reportportal/saucelabs/command/GetVirtualDeviceJobCommand.java +++ b/src/main/java/com/epam/reportportal/saucelabs/command/GetVirtualDeviceJobCommand.java @@ -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; @@ -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")); } } diff --git a/src/main/java/com/epam/reportportal/saucelabs/command/TestConnectionCommand.java b/src/main/java/com/epam/reportportal/saucelabs/command/TestConnectionCommand.java index 2bb4937..5de83f4 100644 --- a/src/main/java/com/epam/reportportal/saucelabs/command/TestConnectionCommand.java +++ b/src/main/java/com/epam/reportportal/saucelabs/command/TestConnectionCommand.java @@ -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; @@ -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 { diff --git a/src/main/java/com/epam/reportportal/saucelabs/model/Constants.java b/src/main/java/com/epam/reportportal/saucelabs/model/Constants.java index 64b2453..8a12439 100644 --- a/src/main/java/com/epam/reportportal/saucelabs/model/Constants.java +++ b/src/main/java/com/epam/reportportal/saucelabs/model/Constants.java @@ -1,16 +1,32 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + package com.epam.reportportal.saucelabs.model; public class Constants { public static final String JOB_ID = "jobId"; + public static final String LOG_URL = "log_url"; // endpoints public static final String GET_VDC_JOBS = "/rest/v1/%s/jobs"; public static final String GET_VDC_JOB = "/rest/v1/%s/jobs/%s"; - public static final String GET_VDC_JOB_LOGS ="/rest/v1/%s/jobs/%s/logs"; - public static final String GET_VDC_JOB_ASSETS ="/rest/v1/%s/jobs/%s/assets"; - public static final String GET_RDC_JOB ="/v1/rdc/jobs/%s"; - - + public static final String GET_VDC_JOB_LOGS = "/rest/v1/%s/jobs/%s/assets/log.json"; + public static final String GET_VDC_JOB_ASSETS = "/rest/v1/%s/jobs/%s/assets"; + public static final String GET_RDC_JOB = "/v1/rdc/jobs/%s"; } diff --git a/src/main/java/com/epam/reportportal/saucelabs/model/DataCenter.java b/src/main/java/com/epam/reportportal/saucelabs/model/DataCenter.java index 83f0cb9..7d715d6 100644 --- a/src/main/java/com/epam/reportportal/saucelabs/model/DataCenter.java +++ b/src/main/java/com/epam/reportportal/saucelabs/model/DataCenter.java @@ -1,3 +1,19 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.epam.reportportal.saucelabs.model; import java.util.Arrays; @@ -7,13 +23,9 @@ @Getter @AllArgsConstructor public enum DataCenter { - US_WEST("https://api.us-west-1.saucelabs.com"), US("https://api.us-west-1.saucelabs.com"), - - EU("https://eds.eu-central-1.saucelabs.com"), - EU_CENTRAL("https://eds.eu-central-1.saucelabs.com"), - - US_EAST("https://eds.us-east-1.saucelabs.com"); + EU("https://api.eu-central-1.saucelabs.com"), + US_EAST("https://api.us-east-1.saucelabs.com"); private final String baseUrl; diff --git a/src/main/java/com/epam/reportportal/saucelabs/model/SaucelabsProperties.java b/src/main/java/com/epam/reportportal/saucelabs/model/IntegrationParametersNames.java similarity index 92% rename from src/main/java/com/epam/reportportal/saucelabs/model/SaucelabsProperties.java rename to src/main/java/com/epam/reportportal/saucelabs/model/IntegrationParametersNames.java index da36d65..22c60f5 100644 --- a/src/main/java/com/epam/reportportal/saucelabs/model/SaucelabsProperties.java +++ b/src/main/java/com/epam/reportportal/saucelabs/model/IntegrationParametersNames.java @@ -21,7 +21,7 @@ /** * @author Pavel Bortnik */ -public enum SaucelabsProperties { +public enum IntegrationParametersNames { USERNAME("username"), ACCESS_TOKEN("accessToken"), @@ -29,7 +29,7 @@ public enum SaucelabsProperties { private final String name; - SaucelabsProperties(String name) { + IntegrationParametersNames(String name) { this.name = name; } diff --git a/src/main/java/com/epam/reportportal/saucelabs/model/IntegrationProperties.java b/src/main/java/com/epam/reportportal/saucelabs/model/IntegrationProperties.java new file mode 100644 index 0000000..164f7e7 --- /dev/null +++ b/src/main/java/com/epam/reportportal/saucelabs/model/IntegrationProperties.java @@ -0,0 +1,51 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.epam.reportportal.saucelabs.model; + +import static com.epam.reportportal.saucelabs.model.IntegrationParametersNames.ACCESS_TOKEN; +import static com.epam.reportportal.saucelabs.model.IntegrationParametersNames.DATA_CENTER; +import static com.epam.reportportal.saucelabs.model.IntegrationParametersNames.USERNAME; +import static com.epam.reportportal.saucelabs.utils.DatacenterNameResolver.resolveDatacenterName; + +import java.util.Map; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +@AllArgsConstructor +@Getter +@Setter +public class IntegrationProperties { + + private final String username; + private final String token; + private final DataCenter datacenter; + + private String jobId; + + public IntegrationProperties(String username, String token, DataCenter datacenter) { + this.username = username; + this.token = token; + this.datacenter = datacenter; + } + + public IntegrationProperties(Map params) { + this.username = (String) params.get(USERNAME.getName()); + this.token = (String) params.get(ACCESS_TOKEN.getName()); + this.datacenter = resolveDatacenterName((String) params.get(DATA_CENTER.getName())); + } +} diff --git a/src/main/java/com/epam/reportportal/saucelabs/model/SauceProperties.java b/src/main/java/com/epam/reportportal/saucelabs/model/SauceProperties.java deleted file mode 100644 index 085db31..0000000 --- a/src/main/java/com/epam/reportportal/saucelabs/model/SauceProperties.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.epam.reportportal.saucelabs.model; - -import static com.epam.reportportal.saucelabs.model.SaucelabsProperties.ACCESS_TOKEN; -import static com.epam.reportportal.saucelabs.model.SaucelabsProperties.DATA_CENTER; -import static com.epam.reportportal.saucelabs.model.SaucelabsProperties.USERNAME; - -import java.util.Map; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.Setter; - -@AllArgsConstructor -@Getter -@Setter -public class SauceProperties { - - private final String username; - private final String token; - private final DataCenter datacenter; - - private String jobId; - - public SauceProperties(String username, String token, DataCenter datacenter) { - this.username = username; - this.token = token; - this.datacenter = datacenter; - } - - public SauceProperties(Map params) { - this.username = (String) params.get(USERNAME.getName()); - this.token = (String) params.get(ACCESS_TOKEN.getName()); - this.datacenter = DataCenter.fromString((String) params.get(DATA_CENTER.getName())); - } -} diff --git a/src/main/java/com/epam/reportportal/saucelabs/utils/DatacenterNameResolver.java b/src/main/java/com/epam/reportportal/saucelabs/utils/DatacenterNameResolver.java new file mode 100644 index 0000000..94681bd --- /dev/null +++ b/src/main/java/com/epam/reportportal/saucelabs/utils/DatacenterNameResolver.java @@ -0,0 +1,56 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.epam.reportportal.saucelabs.utils; + +import com.epam.reportportal.rules.exception.ErrorType; +import com.epam.reportportal.rules.exception.ReportPortalException; +import com.epam.reportportal.saucelabs.model.DataCenter; + +/** + * Utility class for resolving deprecated datacenter names to their current equivalents. This is the + * temporary workaround in order to mitigate inconsistency between saved integrations and saucelabs + * rest client {@link DataCenter} Enum. + */ +public class DatacenterNameResolver { + + private DatacenterNameResolver() { + } + + /** + * Resolves the deprecated datacenter name to the current equivalent. + * + * @param oldDatacenter the deprecated datacenter name + * @return the current datacenter name + * @throws ReportPortalException if the datacenter name is invalid + */ + public static DataCenter resolveDatacenterName(String oldDatacenter) { + switch (oldDatacenter) { + case "EU": + case "EU_CENTRAL": + return DataCenter.EU; + case "US": + case "US_WEST": + return DataCenter.US; + case "US_EAST": + return DataCenter.US_EAST; + default: + throw new ReportPortalException(ErrorType.UNABLE_INTERACT_WITH_INTEGRATION, + "Invalid SauceLabs Datacenter value"); + } + + } +} diff --git a/src/main/java/com/epam/reportportal/saucelabs/ValidationUtils.java b/src/main/java/com/epam/reportportal/saucelabs/utils/ValidationUtils.java similarity index 84% rename from src/main/java/com/epam/reportportal/saucelabs/ValidationUtils.java rename to src/main/java/com/epam/reportportal/saucelabs/utils/ValidationUtils.java index 8d101bc..0e08d40 100644 --- a/src/main/java/com/epam/reportportal/saucelabs/ValidationUtils.java +++ b/src/main/java/com/epam/reportportal/saucelabs/utils/ValidationUtils.java @@ -14,13 +14,13 @@ * limitations under the License. */ -package com.epam.reportportal.saucelabs; +package com.epam.reportportal.saucelabs.utils; import static com.epam.reportportal.rules.commons.validation.BusinessRule.expect; -import static com.epam.reportportal.saucelabs.SaucelabsExtension.JOB_ID; -import static com.epam.reportportal.saucelabs.model.SaucelabsProperties.ACCESS_TOKEN; -import static com.epam.reportportal.saucelabs.model.SaucelabsProperties.DATA_CENTER; -import static com.epam.reportportal.saucelabs.model.SaucelabsProperties.USERNAME; +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.DATA_CENTER; +import static com.epam.reportportal.saucelabs.model.IntegrationParametersNames.USERNAME; import com.epam.reportportal.rules.exception.ErrorType; import com.epam.ta.reportportal.commons.Predicates; diff --git a/src/test/java/com/epam/reportportal/saucelabs/command/GetVirtualDeviceJobCommandTest.java b/src/test/java/com/epam/reportportal/saucelabs/command/GetVirtualDeviceJobCommandTest.java new file mode 100644 index 0000000..3a08e9a --- /dev/null +++ b/src/test/java/com/epam/reportportal/saucelabs/command/GetVirtualDeviceJobCommandTest.java @@ -0,0 +1,45 @@ +package com.epam.reportportal.saucelabs.command; + + +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.DATA_CENTER; +import static com.epam.reportportal.saucelabs.model.IntegrationParametersNames.USERNAME; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import com.epam.reportportal.saucelabs.client.RestClientBuilder; +import com.epam.ta.reportportal.entity.integration.Integration; +import com.epam.ta.reportportal.entity.integration.IntegrationParams; +import java.util.HashMap; +import java.util.Map; +import org.jasypt.util.text.BasicTextEncryptor; +import org.junit.jupiter.api.Test; + +public class GetVirtualDeviceJobCommandTest { + + private static final Map COMMAND_PARAMS = new HashMap<>(); + private static final Map INTEGRATION_PARAMS = new HashMap<>(); + + static { + INTEGRATION_PARAMS.put(USERNAME.getName(), ""); + INTEGRATION_PARAMS.put(ACCESS_TOKEN.getName(), ""); + INTEGRATION_PARAMS.put(DATA_CENTER.getName(), "EU"); + + COMMAND_PARAMS.put(JOB_ID, "03afb43944b849e1a9cf68989222037c"); + + } + + @Test + void executeCommand() { + BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor(); + Integration integration = new Integration(); + + integration.setParams(new IntegrationParams(INTEGRATION_PARAMS)); + + GetVirtualDeviceJobCommand command = new GetVirtualDeviceJobCommand( + new RestClientBuilder(basicTextEncryptor)); + Object o = command.executeCommand(integration, COMMAND_PARAMS); + + assertNotNull(o); + } +}