Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public class VisualRegressionTrackerConfig {
private String project;
private String apiKey;
private String branchName;
private Boolean enableSoftAssert;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.visual_regression_tracker.sdk_java.request;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class BuildRequest {
String project;
String branchName;
private final String project;
private final String branchName;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package io.visual_regression_tracker.sdk_java.request;

import lombok.Builder;
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
@Builder
public class BuildResponse {
String id;
String projectId;
private final String id;
private final String projectId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
@Builder
public class TestRunResponse {
private final String url;
private final TestRunStatus status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.mockito.Mockito;
import org.simplify4u.sjf4jmock.LoggerMock;
import org.slf4j.Logger;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
Expand All @@ -30,14 +32,17 @@ public class VisualRegressionTrackerTest {
"http://localhost",
"733c148e-ef70-4e6d-9ae5-ab22263697cc",
"XHGDZDFD3GMJDNM87JKEMP0JS1G5",
"develop"
"develop",
false
);
private MockWebServer server;
private VisualRegressionTracker vrt;

@SneakyThrows
@BeforeMethod
public void setup() {
LoggerMock.clearInvocations();

server = new MockWebServer();
server.start();

Expand Down Expand Up @@ -164,8 +169,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()
Expand All @@ -184,9 +189,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());
Expand All @@ -199,6 +205,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[][]{
Expand Down