Skip to content

Commit

Permalink
Custom exception added
Browse files Browse the repository at this point in the history
  • Loading branch information
pashidlos committed Apr 10, 2020
1 parent 1acc464 commit 5d965c5
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 58 deletions.
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ sourceCompatibility = 1.8

dependencies {
implementation 'io.aexp.nodes.graphql:nodes:0.5.0'
testImplementation group: 'junit', name: 'junit', version: '4.12'
testImplementation group: 'org.testng', name: 'testng', version: '7.1.0'
testImplementation 'commons-io:commons-io:2.6'
}

test{
useTestNG()
}
7 changes: 7 additions & 0 deletions src/main/java/agent_java_core/ImageProcessException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package agent_java_core;

public class ImageProcessException extends RuntimeException {
public ImageProcessException(String errorMessage) {
super(errorMessage);
}
}
6 changes: 4 additions & 2 deletions src/main/java/agent_java_core/UploadScreenshotMutation.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import io.aexp.nodes.graphql.annotations.GraphQLArgument;
import io.aexp.nodes.graphql.annotations.GraphQLProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.*;

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@GraphQLProperty(name = "uploadScreenshot", arguments = {
@GraphQLArgument(name = "testSessionId", type = "String!"),
@GraphQLArgument(name = "base64Image", type = "String!")
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/agent_java_core/VisualKnightCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ public void processScreenshot(String testName, String base64Image, VisualKnightC
this.processTestSessionResult(uploadResult);
}

private void processTestSessionResult(UploadScreenshotMutation uploadResult) {
if (uploadResult.misMatchPercentage == null) {
throw new Error("For this image is no baseline defined! -> " + uploadResult.link);
void processTestSessionResult(UploadScreenshotMutation uploadResult) {
if (uploadResult.misMatchPercentage == null && (uploadResult.isSameDimensions == null || uploadResult.isSameDimensions)) {
throw new ImageProcessException("For this image is no baseline defined! -> " + uploadResult.link);
}

if (!uploadResult.isSameDimensions) {
throw new Error("Compared Screenshots are not in the same dimension! -> " + uploadResult.link);
throw new ImageProcessException("Compared Screenshots are not in the same dimension! -> " + uploadResult.link);
}

if (uploadResult.misMatchPercentage > visualKnightOptions.misMatchTolerance) {
throw new Error("Mismatch of " + uploadResult.misMatchPercentage + " " +
throw new ImageProcessException("Mismatch of " + uploadResult.misMatchPercentage + " " +
"is greater than the tolerance " + visualKnightOptions.misMatchTolerance + " " +
"-> " + uploadResult.link);
}
}

private String invokeTestSession(String testName, VisualKnightCapabilities capabilities) {
String invokeTestSession(String testName, VisualKnightCapabilities capabilities) {
@GraphQLProperty(name = "invokeTestSession", arguments = {
@GraphQLArgument(name = "project", type = "String!"),
@GraphQLArgument(name = "testname", type = "String!"),
Expand Down Expand Up @@ -79,7 +79,7 @@ class InvokeTestSession {
return responseEntity.getResponse().get("invokeTestSession").toString();
}

private UploadScreenshotMutation uploadScreenshot(String testSessionId, String base64Image) {
UploadScreenshotMutation uploadScreenshot(String testSessionId, String base64Image) {
GraphQLRequestEntity requestEntity = graphQLRequestBuilder
.request(UploadScreenshotMutation.class)
.arguments(
Expand Down
48 changes: 0 additions & 48 deletions src/test/java/Example.java

This file was deleted.

111 changes: 111 additions & 0 deletions src/test/java/agent_java_core/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package agent_java_core;

import org.testng.Assert;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.net.MalformedURLException;

public class Example {
VisualKnightOptions visualKnightOptions;
VisualKnightCore core;

@BeforeSuite
public void setUp() throws MalformedURLException {
visualKnightOptions = VisualKnightOptions.builder()
.apiEndpoint("http://localhost:3333/graphql")
.build();

core = new VisualKnightCore(visualKnightOptions);
}
//
// @Test
// public void asd() throws MalformedURLException {
// VisualKnightCapabilities visualKnightCapabilities = VisualKnightCapabilities.builder().browserName("Chrome").os("Windows").build();
// VisualKnightOptions visualKnightOptions = VisualKnightOptions.builder()
// .apiEndpoint("http://localhost:3333/graphql")
// .apiKey("asdasd")
// .project("test")
// .visualKnightCapabilities(visualKnightCapabilities)
// .build();
//
// VisualKnightCore core = new VisualKnightCore(visualKnightOptions);
//
//// core.invokeTestSession("test", capabilities);
//
// System.out.println("END");
// }
//
// @Test
// public void uploadScreenshot() throws IOException {
// VisualKnightCapabilities visualKnightCapabilities = VisualKnightCapabilities.builder().browserName("Chrome").os("Windows").build();
// VisualKnightOptions visualKnightOptions = VisualKnightOptions.builder()
// .apiEndpoint("http://localhost:3333/graphql")
// .apiKey("asdasd")
// .project("test")
// .visualKnightCapabilities(visualKnightCapabilities)
// .build();
//
// VisualKnightCore core = new VisualKnightCore(visualKnightOptions);
//
//// byte[] fileContent = FileUtils.readFileToByteArray(new File(getClass().getResource("large.png").getFile()));
//// core.uploadScreenshot("ck8t908ie000111pjg8z2x58o", Base64.getEncoder().encodeToString(fileContent));
// core.uploadScreenshot("ck8t908ie000111pjg8z2x58o", "asdsfgsdf");
//
// System.out.println("END");
// }

@DataProvider(name = "processTestSessionResultCases")
public Object[][] complexParamsDataProvider() {
return new Object[][]{
{
UploadScreenshotMutation.builder()
.misMatchPercentage(null)
.misMatchTolerance(0.01f)
.isSameDimensions(null)
.link("http://google.com")
.build(),
"For this image is no baseline defined! -> http://google.com",
},
{
UploadScreenshotMutation.builder()
.misMatchPercentage(null)
.misMatchTolerance(0.01f)
.isSameDimensions(true)
.link("http://google.com")
.build(),
"For this image is no baseline defined! -> http://google.com",
},
{
UploadScreenshotMutation.builder()
.misMatchPercentage(null)
.misMatchTolerance(0.01f)
.isSameDimensions(false)
.link("http://google.com")
.build(),
"Compared Screenshots are not in the same dimension! -> http://google.com",
},
{
UploadScreenshotMutation.builder()
.misMatchPercentage(0.02f)
.misMatchTolerance(0.01f)
.isSameDimensions(true)
.link("http://google.com")
.build(),
"Mismatch of 0.02 is greater than the tolerance 0.01 -> http://google.com",
},
};
}

@Test(dataProvider = "processTestSessionResultCases")
public void processTestSessionResult(UploadScreenshotMutation testSessionResult, String expectedMessage) {
String actualMessage = "";
try {
core.processTestSessionResult(testSessionResult);
} catch (ImageProcessException exception) {
actualMessage = exception.getMessage();
}
Assert.assertEquals(actualMessage, expectedMessage);
}
}

0 comments on commit 5d965c5

Please sign in to comment.