Skip to content

Commit 45b3656

Browse files
authored
Merge pull request #25 from Visual-Regression-Tracker/63-soft-assert
Soft assert added
2 parents b6ccdbd + 7539948 commit 45b3656

File tree

8 files changed

+69
-32
lines changed

8 files changed

+69
-32
lines changed

build.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group 'io.visual-regression-tracker.sdk-java'
2-
version '4.0.3'
2+
version '4.1.0'
33

44
apply plugin: 'java'
55
apply plugin: 'jacoco'
@@ -27,6 +27,7 @@ buildscript {
2727
dependencies {
2828
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
2929
implementation 'com.google.code.gson:gson:2.8.6'
30+
implementation group: 'org.simplify4u', name: 'slf4j-mock', version: '1.0.2'
3031
testImplementation group: 'org.testng', name: 'testng', version: '7.1.0'
3132
testImplementation 'commons-io:commons-io:2.7'
3233
testImplementation 'org.mockito:mockito-core:3.4.6'

src/main/java/io/visual_regression_tracker/sdk_java/VisualRegressionTracker.java

+23-11
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@
1010
import okhttp3.Request;
1111
import okhttp3.RequestBody;
1212
import okhttp3.Response;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
1315

1416
import java.io.IOException;
1517
import java.util.Optional;
1618

19+
1720
public class VisualRegressionTracker {
1821
protected static final String apiKeyHeaderName = "apiKey";
1922
protected static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
23+
private static final Logger LOGGER = LoggerFactory.getLogger(VisualRegressionTracker.class);
2024
protected Gson gson;
2125
protected VisualRegressionTrackerConfig visualRegressionTrackerConfig;
2226
protected String buildId;
@@ -48,10 +52,8 @@ public void start() throws IOException {
4852

4953
BuildResponse buildDTO = handleResponse(response, BuildResponse.class);
5054

51-
this.buildId = Optional.ofNullable(buildDTO.getId())
52-
.orElseThrow(() -> new TestRunException("Build id is null"));
53-
this.projectId = Optional.ofNullable(buildDTO.getProjectId())
54-
.orElseThrow(() -> new TestRunException("Project id is null"));
55+
this.buildId = buildDTO.getId();
56+
this.projectId = buildDTO.getProjectId();
5557
}
5658
}
5759

@@ -74,15 +76,25 @@ public void stop() throws IOException {
7476
public void track(String name, String imageBase64, TestRunOptions testRunOptions) throws IOException {
7577
TestRunResponse testResultDTO = this.submitTestRun(name, imageBase64, testRunOptions);
7678

77-
TestRunStatus status = Optional.ofNullable(testResultDTO.getStatus())
78-
.orElseThrow(() -> new TestRunException("Status is null"));
79-
80-
if (status.equals(TestRunStatus.NEW)) {
81-
throw new TestRunException("No baseline: ".concat(testResultDTO.getUrl()));
79+
String errorMessage;
80+
switch (testResultDTO.getStatus()) {
81+
case NEW:
82+
errorMessage = "No baseline: ".concat(testResultDTO.getUrl());
83+
break;
84+
case UNRESOLVED:
85+
errorMessage = "Difference found: ".concat(testResultDTO.getUrl());
86+
break;
87+
default:
88+
errorMessage = "";
89+
break;
8290
}
8391

84-
if (status.equals(TestRunStatus.UNRESOLVED)) {
85-
throw new TestRunException("Difference found: ".concat(testResultDTO.getUrl()));
92+
if (!errorMessage.isEmpty()) {
93+
if (this.visualRegressionTrackerConfig.getEnableSoftAssert()) {
94+
LOGGER.error(errorMessage);
95+
} else {
96+
throw new TestRunException(errorMessage);
97+
}
8698
}
8799
}
88100

src/main/java/io/visual_regression_tracker/sdk_java/VisualRegressionTrackerConfig.java

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ public class VisualRegressionTrackerConfig {
1010
private String project;
1111
private String apiKey;
1212
private String branchName;
13+
private Boolean enableSoftAssert;
1314
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package io.visual_regression_tracker.sdk_java.request;
22

33
import lombok.Builder;
4+
import lombok.Getter;
45

6+
@Getter
57
@Builder
68
public class BuildRequest {
7-
String project;
8-
String branchName;
9+
private final String project;
10+
private final String branchName;
911
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package io.visual_regression_tracker.sdk_java.request;
22

33
import lombok.Builder;
4+
import lombok.Getter;
45

6+
@Getter
57
@Builder
68
public class TestRunRequest {
7-
String projectId;
8-
String buildId;
9-
String name;
10-
String imageBase64;
11-
String os;
12-
String browser;
13-
String viewport;
14-
String device;
15-
Float diffTollerancePercent;
16-
String branchName;
9+
private final String projectId;
10+
private final String buildId;
11+
private final String name;
12+
private final String imageBase64;
13+
private final String os;
14+
private final String browser;
15+
private final String viewport;
16+
private final String device;
17+
private final Float diffTollerancePercent;
18+
private final String branchName;
1719
}

src/main/java/io/visual_regression_tracker/sdk_java/response/BuildResponse.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import lombok.Builder;
44
import lombok.Getter;
55

6-
@Builder
76
@Getter
7+
@Builder
88
public class BuildResponse {
9-
String id;
10-
String projectId;
9+
private final String id;
10+
private final String projectId;
1111
}

src/main/java/io/visual_regression_tracker/sdk_java/response/TestRunResponse.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import lombok.Builder;
55
import lombok.Getter;
66

7-
@Builder
87
@Getter
8+
@Builder
99
public class TestRunResponse {
1010
private final String url;
1111
private final TestRunStatus status;

src/test/java/io/visual_regression_tracker/sdk_java/VisualRegressionTrackerTest.java

+23-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import org.hamcrest.CoreMatchers;
1717
import org.hamcrest.MatcherAssert;
1818
import org.mockito.Mockito;
19+
import org.simplify4u.sjf4jmock.LoggerMock;
20+
import org.slf4j.Logger;
1921
import org.testng.annotations.AfterMethod;
2022
import org.testng.annotations.BeforeMethod;
2123
import org.testng.annotations.DataProvider;
@@ -30,14 +32,17 @@ public class VisualRegressionTrackerTest {
3032
"http://localhost",
3133
"733c148e-ef70-4e6d-9ae5-ab22263697cc",
3234
"XHGDZDFD3GMJDNM87JKEMP0JS1G5",
33-
"develop"
35+
"develop",
36+
false
3437
);
3538
private MockWebServer server;
3639
private VisualRegressionTracker vrt;
3740

3841
@SneakyThrows
3942
@BeforeMethod
4043
public void setup() {
44+
LoggerMock.clearInvocations();
45+
4146
server = new MockWebServer();
4247
server.start();
4348

@@ -164,8 +169,8 @@ public void submitTestRunShouldThrowIfNotStarted() throws IOException {
164169
MatcherAssert.assertThat(exceptionMessage, CoreMatchers.is("Visual Regression Tracker has not been started"));
165170
}
166171

167-
@DataProvider(name = "trackShouldThrowExceptionCases")
168-
public Object[][] trackShouldThrowExceptionCases() {
172+
@DataProvider(name = "trackErrorCases")
173+
public Object[][] trackErrorCases() {
169174
return new Object[][]{
170175
{
171176
TestRunResponse.builder()
@@ -184,9 +189,10 @@ public Object[][] trackShouldThrowExceptionCases() {
184189
};
185190
}
186191

187-
@Test(dataProvider = "trackShouldThrowExceptionCases")
192+
@Test(dataProvider = "trackErrorCases")
188193
public void trackShouldThrowException(TestRunResponse testRunResponse, String expectedExceptionMessage) throws IOException {
189194
VisualRegressionTracker vrtMocked = Mockito.mock(VisualRegressionTracker.class);
195+
vrtMocked.visualRegressionTrackerConfig = new VisualRegressionTrackerConfig("", "", "", "", false);
190196
Mockito.when(vrtMocked.submitTestRun(Mockito.anyString(), Mockito.anyString(), Mockito.any())).thenReturn(testRunResponse);
191197

192198
Mockito.doCallRealMethod().when(vrtMocked).track(Mockito.anyString(), Mockito.anyString(), Mockito.any());
@@ -199,6 +205,19 @@ public void trackShouldThrowException(TestRunResponse testRunResponse, String ex
199205
MatcherAssert.assertThat(exceptionMessage, CoreMatchers.is(expectedExceptionMessage));
200206
}
201207

208+
@Test(dataProvider = "trackErrorCases")
209+
public void trackShouldLogSevere(TestRunResponse testRunResponse, String expectedExceptionMessage) throws IOException {
210+
Logger loggerMock = LoggerMock.getLoggerMock(VisualRegressionTracker.class);
211+
VisualRegressionTracker vrtMocked = Mockito.mock(VisualRegressionTracker.class);
212+
vrtMocked.visualRegressionTrackerConfig = new VisualRegressionTrackerConfig("", "", "", "", true);
213+
Mockito.when(vrtMocked.submitTestRun(Mockito.anyString(), Mockito.anyString(), Mockito.any())).thenReturn(testRunResponse);
214+
215+
Mockito.doCallRealMethod().when(vrtMocked).track(Mockito.anyString(), Mockito.anyString(), Mockito.any());
216+
vrtMocked.track("name", "image", TestRunOptions.builder().build());
217+
218+
Mockito.verify(loggerMock).error(expectedExceptionMessage);
219+
}
220+
202221
@DataProvider(name = "shouldTrackPassCases")
203222
public Object[][] shouldTrackPassCases() {
204223
return new Object[][]{

0 commit comments

Comments
 (0)