From 158bb25770a6782787b3687c6f3eb6a0bae83c85 Mon Sep 17 00:00:00 2001 From: Pavel Strunkin Date: Tue, 1 Sep 2020 23:28:07 +0200 Subject: [PATCH 1/3] Soft assert added https://github.com/Visual-Regression-Tracker/Visual-Regression-Tracker/issues/63 --- build.gradle | 3 +- .../sdk_java/VisualRegressionTracker.java | 34 +++++++++++++------ .../VisualRegressionTrackerConfig.java | 1 + .../sdk_java/request/BuildRequest.java | 7 ++-- .../sdk_java/request/TestRunRequest.java | 23 +++++++------ .../sdk_java/response/BuildResponse.java | 7 ++-- .../sdk_java/response/TestRunResponse.java | 3 +- .../sdk_java/VisualRegressionTrackerTest.java | 28 ++++++++++++--- 8 files changed, 74 insertions(+), 32 deletions(-) diff --git a/build.gradle b/build.gradle index a60b27c..eed7c52 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'io.visual-regression-tracker.sdk-java' -version '4.0.3' +version '4.1.0' apply plugin: 'java' apply plugin: 'jacoco' @@ -27,6 +27,7 @@ buildscript { dependencies { implementation 'com.squareup.okhttp3:okhttp:3.11.0' implementation 'com.google.code.gson:gson:2.8.6' + implementation group: 'org.simplify4u', name: 'slf4j-mock', version: '1.0.2' testImplementation group: 'org.testng', name: 'testng', version: '7.1.0' testImplementation 'commons-io:commons-io:2.7' testImplementation 'org.mockito:mockito-core:3.4.6' diff --git a/src/main/java/io/visual_regression_tracker/sdk_java/VisualRegressionTracker.java b/src/main/java/io/visual_regression_tracker/sdk_java/VisualRegressionTracker.java index 3af3145..ad5c4ae 100644 --- a/src/main/java/io/visual_regression_tracker/sdk_java/VisualRegressionTracker.java +++ b/src/main/java/io/visual_regression_tracker/sdk_java/VisualRegressionTracker.java @@ -10,13 +10,17 @@ import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.Optional; + public class VisualRegressionTracker { protected static final String apiKeyHeaderName = "apiKey"; protected static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); + private static final Logger LOGGER = LoggerFactory.getLogger(VisualRegressionTracker.class); protected Gson gson; protected VisualRegressionTrackerConfig visualRegressionTrackerConfig; protected String buildId; @@ -48,10 +52,8 @@ public void start() throws IOException { BuildResponse buildDTO = handleResponse(response, BuildResponse.class); - this.buildId = Optional.ofNullable(buildDTO.getId()) - .orElseThrow(() -> new TestRunException("Build id is null")); - this.projectId = Optional.ofNullable(buildDTO.getProjectId()) - .orElseThrow(() -> new TestRunException("Project id is null")); + this.buildId = buildDTO.getId(); + this.projectId = buildDTO.getProjectId(); } } @@ -74,15 +76,25 @@ public void stop() throws IOException { public void track(String name, String imageBase64, TestRunOptions testRunOptions) throws IOException { TestRunResponse testResultDTO = this.submitTestRun(name, imageBase64, testRunOptions); - TestRunStatus status = Optional.ofNullable(testResultDTO.getStatus()) - .orElseThrow(() -> new TestRunException("Status is null")); - - if (status.equals(TestRunStatus.NEW)) { - throw new TestRunException("No baseline: ".concat(testResultDTO.getUrl())); + String errorMessage; + switch (testResultDTO.getStatus()) { + case NEW: + errorMessage = "No baseline: ".concat(testResultDTO.getUrl()); + break; + case UNRESOLVED: + errorMessage = "Difference found: ".concat(testResultDTO.getUrl()); + break; + default: + errorMessage = ""; + break; } - if (status.equals(TestRunStatus.UNRESOLVED)) { - throw new TestRunException("Difference found: ".concat(testResultDTO.getUrl())); + if (!errorMessage.isEmpty()) { + if (this.visualRegressionTrackerConfig.getEnableSoftAssert()) { + LOGGER.error(errorMessage); + } else { + throw new TestRunException(errorMessage); + } } } diff --git a/src/main/java/io/visual_regression_tracker/sdk_java/VisualRegressionTrackerConfig.java b/src/main/java/io/visual_regression_tracker/sdk_java/VisualRegressionTrackerConfig.java index 96a18e3..d97aacb 100644 --- a/src/main/java/io/visual_regression_tracker/sdk_java/VisualRegressionTrackerConfig.java +++ b/src/main/java/io/visual_regression_tracker/sdk_java/VisualRegressionTrackerConfig.java @@ -10,4 +10,5 @@ public class VisualRegressionTrackerConfig { private String project; private String apiKey; private String branchName; + private Boolean enableSoftAssert; } diff --git a/src/main/java/io/visual_regression_tracker/sdk_java/request/BuildRequest.java b/src/main/java/io/visual_regression_tracker/sdk_java/request/BuildRequest.java index ed909b7..733baa7 100644 --- a/src/main/java/io/visual_regression_tracker/sdk_java/request/BuildRequest.java +++ b/src/main/java/io/visual_regression_tracker/sdk_java/request/BuildRequest.java @@ -1,9 +1,12 @@ package io.visual_regression_tracker.sdk_java.request; import lombok.Builder; +import lombok.Data; +import lombok.Getter; +@Getter @Builder public class BuildRequest { - String project; - String branchName; + private final String project; + private final String branchName; } diff --git a/src/main/java/io/visual_regression_tracker/sdk_java/request/TestRunRequest.java b/src/main/java/io/visual_regression_tracker/sdk_java/request/TestRunRequest.java index 8e40c47..1973372 100644 --- a/src/main/java/io/visual_regression_tracker/sdk_java/request/TestRunRequest.java +++ b/src/main/java/io/visual_regression_tracker/sdk_java/request/TestRunRequest.java @@ -1,17 +1,20 @@ package io.visual_regression_tracker.sdk_java.request; import lombok.Builder; +import lombok.Data; +import lombok.Getter; +@Getter @Builder public class TestRunRequest { - String projectId; - String buildId; - String name; - String imageBase64; - String os; - String browser; - String viewport; - String device; - Float diffTollerancePercent; - String branchName; + private final String projectId; + private final String buildId; + private final String name; + private final String imageBase64; + private final String os; + private final String browser; + private final String viewport; + private final String device; + private final Float diffTollerancePercent; + private final String branchName; } diff --git a/src/main/java/io/visual_regression_tracker/sdk_java/response/BuildResponse.java b/src/main/java/io/visual_regression_tracker/sdk_java/response/BuildResponse.java index d1da56c..ade4770 100644 --- a/src/main/java/io/visual_regression_tracker/sdk_java/response/BuildResponse.java +++ b/src/main/java/io/visual_regression_tracker/sdk_java/response/BuildResponse.java @@ -1,11 +1,12 @@ package io.visual_regression_tracker.sdk_java.response; import lombok.Builder; +import lombok.Data; import lombok.Getter; -@Builder @Getter +@Builder public class BuildResponse { - String id; - String projectId; + private final String id; + private final String projectId; } diff --git a/src/main/java/io/visual_regression_tracker/sdk_java/response/TestRunResponse.java b/src/main/java/io/visual_regression_tracker/sdk_java/response/TestRunResponse.java index 2dd9f81..742c23f 100644 --- a/src/main/java/io/visual_regression_tracker/sdk_java/response/TestRunResponse.java +++ b/src/main/java/io/visual_regression_tracker/sdk_java/response/TestRunResponse.java @@ -2,10 +2,11 @@ import io.visual_regression_tracker.sdk_java.TestRunStatus; import lombok.Builder; +import lombok.Data; import lombok.Getter; -@Builder @Getter +@Builder public class TestRunResponse { private final String url; private final TestRunStatus status; diff --git a/src/test/java/io/visual_regression_tracker/sdk_java/VisualRegressionTrackerTest.java b/src/test/java/io/visual_regression_tracker/sdk_java/VisualRegressionTrackerTest.java index 0c6554d..15c9daa 100644 --- a/src/test/java/io/visual_regression_tracker/sdk_java/VisualRegressionTrackerTest.java +++ b/src/test/java/io/visual_regression_tracker/sdk_java/VisualRegressionTrackerTest.java @@ -16,6 +16,9 @@ import org.hamcrest.CoreMatchers; import org.hamcrest.MatcherAssert; import org.mockito.Mockito; +import org.simplify4u.sjf4jmock.LoggerMock; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; @@ -30,7 +33,8 @@ public class VisualRegressionTrackerTest { "http://localhost", "733c148e-ef70-4e6d-9ae5-ab22263697cc", "XHGDZDFD3GMJDNM87JKEMP0JS1G5", - "develop" + "develop", + false ); private MockWebServer server; private VisualRegressionTracker vrt; @@ -38,6 +42,8 @@ public class VisualRegressionTrackerTest { @SneakyThrows @BeforeMethod public void setup() { + LoggerMock.clearInvocations(); + server = new MockWebServer(); server.start(); @@ -164,8 +170,8 @@ public void submitTestRunShouldThrowIfNotStarted() throws IOException { MatcherAssert.assertThat(exceptionMessage, CoreMatchers.is("Visual Regression Tracker has not been started")); } - @DataProvider(name = "trackShouldThrowExceptionCases") - public Object[][] trackShouldThrowExceptionCases() { + @DataProvider(name = "trackErrorCases") + public Object[][] trackErrorCases() { return new Object[][]{ { TestRunResponse.builder() @@ -184,9 +190,10 @@ public Object[][] trackShouldThrowExceptionCases() { }; } - @Test(dataProvider = "trackShouldThrowExceptionCases") + @Test(dataProvider = "trackErrorCases") public void trackShouldThrowException(TestRunResponse testRunResponse, String expectedExceptionMessage) throws IOException { VisualRegressionTracker vrtMocked = Mockito.mock(VisualRegressionTracker.class); + vrtMocked.visualRegressionTrackerConfig = new VisualRegressionTrackerConfig("", "", "", "", false); Mockito.when(vrtMocked.submitTestRun(Mockito.anyString(), Mockito.anyString(), Mockito.any())).thenReturn(testRunResponse); Mockito.doCallRealMethod().when(vrtMocked).track(Mockito.anyString(), Mockito.anyString(), Mockito.any()); @@ -199,6 +206,19 @@ public void trackShouldThrowException(TestRunResponse testRunResponse, String ex MatcherAssert.assertThat(exceptionMessage, CoreMatchers.is(expectedExceptionMessage)); } + @Test(dataProvider = "trackErrorCases") + public void trackShouldLogSevere(TestRunResponse testRunResponse, String expectedExceptionMessage) throws IOException { + Logger loggerMock = LoggerMock.getLoggerMock(VisualRegressionTracker.class); + VisualRegressionTracker vrtMocked = Mockito.mock(VisualRegressionTracker.class); + vrtMocked.visualRegressionTrackerConfig = new VisualRegressionTrackerConfig("", "", "", "", true); + Mockito.when(vrtMocked.submitTestRun(Mockito.anyString(), Mockito.anyString(), Mockito.any())).thenReturn(testRunResponse); + + Mockito.doCallRealMethod().when(vrtMocked).track(Mockito.anyString(), Mockito.anyString(), Mockito.any()); + vrtMocked.track("name", "image", TestRunOptions.builder().build()); + + Mockito.verify(loggerMock).error(expectedExceptionMessage); + } + @DataProvider(name = "shouldTrackPassCases") public Object[][] shouldTrackPassCases() { return new Object[][]{ From 49b549f2633c7e6f65bc4cfd811f1bf3c4596f46 Mon Sep 17 00:00:00 2001 From: Pavel Strunkin Date: Tue, 1 Sep 2020 23:30:34 +0200 Subject: [PATCH 2/3] unused imports removed --- .../visual_regression_tracker/sdk_java/request/BuildRequest.java | 1 - .../sdk_java/request/TestRunRequest.java | 1 - .../sdk_java/response/BuildResponse.java | 1 - .../sdk_java/response/TestRunResponse.java | 1 - 4 files changed, 4 deletions(-) diff --git a/src/main/java/io/visual_regression_tracker/sdk_java/request/BuildRequest.java b/src/main/java/io/visual_regression_tracker/sdk_java/request/BuildRequest.java index 733baa7..a3cd317 100644 --- a/src/main/java/io/visual_regression_tracker/sdk_java/request/BuildRequest.java +++ b/src/main/java/io/visual_regression_tracker/sdk_java/request/BuildRequest.java @@ -1,7 +1,6 @@ package io.visual_regression_tracker.sdk_java.request; import lombok.Builder; -import lombok.Data; import lombok.Getter; @Getter diff --git a/src/main/java/io/visual_regression_tracker/sdk_java/request/TestRunRequest.java b/src/main/java/io/visual_regression_tracker/sdk_java/request/TestRunRequest.java index 1973372..818a4db 100644 --- a/src/main/java/io/visual_regression_tracker/sdk_java/request/TestRunRequest.java +++ b/src/main/java/io/visual_regression_tracker/sdk_java/request/TestRunRequest.java @@ -1,7 +1,6 @@ package io.visual_regression_tracker.sdk_java.request; import lombok.Builder; -import lombok.Data; import lombok.Getter; @Getter diff --git a/src/main/java/io/visual_regression_tracker/sdk_java/response/BuildResponse.java b/src/main/java/io/visual_regression_tracker/sdk_java/response/BuildResponse.java index ade4770..e1a9470 100644 --- a/src/main/java/io/visual_regression_tracker/sdk_java/response/BuildResponse.java +++ b/src/main/java/io/visual_regression_tracker/sdk_java/response/BuildResponse.java @@ -1,7 +1,6 @@ package io.visual_regression_tracker.sdk_java.response; import lombok.Builder; -import lombok.Data; import lombok.Getter; @Getter diff --git a/src/main/java/io/visual_regression_tracker/sdk_java/response/TestRunResponse.java b/src/main/java/io/visual_regression_tracker/sdk_java/response/TestRunResponse.java index 742c23f..fc32485 100644 --- a/src/main/java/io/visual_regression_tracker/sdk_java/response/TestRunResponse.java +++ b/src/main/java/io/visual_regression_tracker/sdk_java/response/TestRunResponse.java @@ -2,7 +2,6 @@ import io.visual_regression_tracker.sdk_java.TestRunStatus; import lombok.Builder; -import lombok.Data; import lombok.Getter; @Getter From 7539948c25bcdac56e98057d72532e73734b75f0 Mon Sep 17 00:00:00 2001 From: Pavel Strunkin Date: Tue, 1 Sep 2020 23:30:59 +0200 Subject: [PATCH 3/3] Update VisualRegressionTrackerTest.java --- .../sdk_java/VisualRegressionTrackerTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/io/visual_regression_tracker/sdk_java/VisualRegressionTrackerTest.java b/src/test/java/io/visual_regression_tracker/sdk_java/VisualRegressionTrackerTest.java index 15c9daa..f6fd942 100644 --- a/src/test/java/io/visual_regression_tracker/sdk_java/VisualRegressionTrackerTest.java +++ b/src/test/java/io/visual_regression_tracker/sdk_java/VisualRegressionTrackerTest.java @@ -18,7 +18,6 @@ import org.mockito.Mockito; import org.simplify4u.sjf4jmock.LoggerMock; import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider;