Skip to content

Commit 158bb25

Browse files
committed
1 parent b6ccdbd commit 158bb25

File tree

8 files changed

+74
-32
lines changed

8 files changed

+74
-32
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
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

Lines changed: 23 additions & 11 deletions
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

Lines changed: 1 addition & 0 deletions
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
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package io.visual_regression_tracker.sdk_java.request;
22

33
import lombok.Builder;
4+
import lombok.Data;
5+
import lombok.Getter;
46

7+
@Getter
58
@Builder
69
public class BuildRequest {
7-
String project;
8-
String branchName;
10+
private final String project;
11+
private final String branchName;
912
}
Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package io.visual_regression_tracker.sdk_java.request;
22

33
import lombok.Builder;
4+
import lombok.Data;
5+
import lombok.Getter;
46

7+
@Getter
58
@Builder
69
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;
10+
private final String projectId;
11+
private final String buildId;
12+
private final String name;
13+
private final String imageBase64;
14+
private final String os;
15+
private final String browser;
16+
private final String viewport;
17+
private final String device;
18+
private final Float diffTollerancePercent;
19+
private final String branchName;
1720
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package io.visual_regression_tracker.sdk_java.response;
22

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

6-
@Builder
77
@Getter
8+
@Builder
89
public class BuildResponse {
9-
String id;
10-
String projectId;
10+
private final String id;
11+
private final String projectId;
1112
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import io.visual_regression_tracker.sdk_java.TestRunStatus;
44
import lombok.Builder;
5+
import lombok.Data;
56
import lombok.Getter;
67

7-
@Builder
88
@Getter
9+
@Builder
910
public class TestRunResponse {
1011
private final String url;
1112
private final TestRunStatus status;

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
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;
21+
import org.slf4j.LoggerFactory;
1922
import org.testng.annotations.AfterMethod;
2023
import org.testng.annotations.BeforeMethod;
2124
import org.testng.annotations.DataProvider;
@@ -30,14 +33,17 @@ public class VisualRegressionTrackerTest {
3033
"http://localhost",
3134
"733c148e-ef70-4e6d-9ae5-ab22263697cc",
3235
"XHGDZDFD3GMJDNM87JKEMP0JS1G5",
33-
"develop"
36+
"develop",
37+
false
3438
);
3539
private MockWebServer server;
3640
private VisualRegressionTracker vrt;
3741

3842
@SneakyThrows
3943
@BeforeMethod
4044
public void setup() {
45+
LoggerMock.clearInvocations();
46+
4147
server = new MockWebServer();
4248
server.start();
4349

@@ -164,8 +170,8 @@ public void submitTestRunShouldThrowIfNotStarted() throws IOException {
164170
MatcherAssert.assertThat(exceptionMessage, CoreMatchers.is("Visual Regression Tracker has not been started"));
165171
}
166172

167-
@DataProvider(name = "trackShouldThrowExceptionCases")
168-
public Object[][] trackShouldThrowExceptionCases() {
173+
@DataProvider(name = "trackErrorCases")
174+
public Object[][] trackErrorCases() {
169175
return new Object[][]{
170176
{
171177
TestRunResponse.builder()
@@ -184,9 +190,10 @@ public Object[][] trackShouldThrowExceptionCases() {
184190
};
185191
}
186192

187-
@Test(dataProvider = "trackShouldThrowExceptionCases")
193+
@Test(dataProvider = "trackErrorCases")
188194
public void trackShouldThrowException(TestRunResponse testRunResponse, String expectedExceptionMessage) throws IOException {
189195
VisualRegressionTracker vrtMocked = Mockito.mock(VisualRegressionTracker.class);
196+
vrtMocked.visualRegressionTrackerConfig = new VisualRegressionTrackerConfig("", "", "", "", false);
190197
Mockito.when(vrtMocked.submitTestRun(Mockito.anyString(), Mockito.anyString(), Mockito.any())).thenReturn(testRunResponse);
191198

192199
Mockito.doCallRealMethod().when(vrtMocked).track(Mockito.anyString(), Mockito.anyString(), Mockito.any());
@@ -199,6 +206,19 @@ public void trackShouldThrowException(TestRunResponse testRunResponse, String ex
199206
MatcherAssert.assertThat(exceptionMessage, CoreMatchers.is(expectedExceptionMessage));
200207
}
201208

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

0 commit comments

Comments
 (0)