Skip to content

Commit 5d965c5

Browse files
committed
Custom exception added
1 parent 1acc464 commit 5d965c5

File tree

6 files changed

+134
-58
lines changed

6 files changed

+134
-58
lines changed

build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ sourceCompatibility = 1.8
2929

3030
dependencies {
3131
implementation 'io.aexp.nodes.graphql:nodes:0.5.0'
32-
testImplementation group: 'junit', name: 'junit', version: '4.12'
32+
testImplementation group: 'org.testng', name: 'testng', version: '7.1.0'
3333
testImplementation 'commons-io:commons-io:2.6'
34+
}
35+
36+
test{
37+
useTestNG()
3438
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package agent_java_core;
2+
3+
public class ImageProcessException extends RuntimeException {
4+
public ImageProcessException(String errorMessage) {
5+
super(errorMessage);
6+
}
7+
}

src/main/java/agent_java_core/UploadScreenshotMutation.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import io.aexp.nodes.graphql.annotations.GraphQLArgument;
44
import io.aexp.nodes.graphql.annotations.GraphQLProperty;
5-
import lombok.Getter;
6-
import lombok.Setter;
5+
import lombok.*;
76

87
@Getter
98
@Setter
9+
@Builder
10+
@NoArgsConstructor
11+
@AllArgsConstructor
1012
@GraphQLProperty(name = "uploadScreenshot", arguments = {
1113
@GraphQLArgument(name = "testSessionId", type = "String!"),
1214
@GraphQLArgument(name = "base64Image", type = "String!")

src/main/java/agent_java_core/VisualKnightCore.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,23 @@ public void processScreenshot(String testName, String base64Image, VisualKnightC
3131
this.processTestSessionResult(uploadResult);
3232
}
3333

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

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

4343
if (uploadResult.misMatchPercentage > visualKnightOptions.misMatchTolerance) {
44-
throw new Error("Mismatch of " + uploadResult.misMatchPercentage + " " +
44+
throw new ImageProcessException("Mismatch of " + uploadResult.misMatchPercentage + " " +
4545
"is greater than the tolerance " + visualKnightOptions.misMatchTolerance + " " +
4646
"-> " + uploadResult.link);
4747
}
4848
}
4949

50-
private String invokeTestSession(String testName, VisualKnightCapabilities capabilities) {
50+
String invokeTestSession(String testName, VisualKnightCapabilities capabilities) {
5151
@GraphQLProperty(name = "invokeTestSession", arguments = {
5252
@GraphQLArgument(name = "project", type = "String!"),
5353
@GraphQLArgument(name = "testname", type = "String!"),
@@ -79,7 +79,7 @@ class InvokeTestSession {
7979
return responseEntity.getResponse().get("invokeTestSession").toString();
8080
}
8181

82-
private UploadScreenshotMutation uploadScreenshot(String testSessionId, String base64Image) {
82+
UploadScreenshotMutation uploadScreenshot(String testSessionId, String base64Image) {
8383
GraphQLRequestEntity requestEntity = graphQLRequestBuilder
8484
.request(UploadScreenshotMutation.class)
8585
.arguments(

src/test/java/Example.java

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package agent_java_core;
2+
3+
import org.testng.Assert;
4+
import org.testng.annotations.BeforeSuite;
5+
import org.testng.annotations.DataProvider;
6+
import org.testng.annotations.Test;
7+
8+
import java.net.MalformedURLException;
9+
10+
public class Example {
11+
VisualKnightOptions visualKnightOptions;
12+
VisualKnightCore core;
13+
14+
@BeforeSuite
15+
public void setUp() throws MalformedURLException {
16+
visualKnightOptions = VisualKnightOptions.builder()
17+
.apiEndpoint("http://localhost:3333/graphql")
18+
.build();
19+
20+
core = new VisualKnightCore(visualKnightOptions);
21+
}
22+
//
23+
// @Test
24+
// public void asd() throws MalformedURLException {
25+
// VisualKnightCapabilities visualKnightCapabilities = VisualKnightCapabilities.builder().browserName("Chrome").os("Windows").build();
26+
// VisualKnightOptions visualKnightOptions = VisualKnightOptions.builder()
27+
// .apiEndpoint("http://localhost:3333/graphql")
28+
// .apiKey("asdasd")
29+
// .project("test")
30+
// .visualKnightCapabilities(visualKnightCapabilities)
31+
// .build();
32+
//
33+
// VisualKnightCore core = new VisualKnightCore(visualKnightOptions);
34+
//
35+
//// core.invokeTestSession("test", capabilities);
36+
//
37+
// System.out.println("END");
38+
// }
39+
//
40+
// @Test
41+
// public void uploadScreenshot() throws IOException {
42+
// VisualKnightCapabilities visualKnightCapabilities = VisualKnightCapabilities.builder().browserName("Chrome").os("Windows").build();
43+
// VisualKnightOptions visualKnightOptions = VisualKnightOptions.builder()
44+
// .apiEndpoint("http://localhost:3333/graphql")
45+
// .apiKey("asdasd")
46+
// .project("test")
47+
// .visualKnightCapabilities(visualKnightCapabilities)
48+
// .build();
49+
//
50+
// VisualKnightCore core = new VisualKnightCore(visualKnightOptions);
51+
//
52+
//// byte[] fileContent = FileUtils.readFileToByteArray(new File(getClass().getResource("large.png").getFile()));
53+
//// core.uploadScreenshot("ck8t908ie000111pjg8z2x58o", Base64.getEncoder().encodeToString(fileContent));
54+
// core.uploadScreenshot("ck8t908ie000111pjg8z2x58o", "asdsfgsdf");
55+
//
56+
// System.out.println("END");
57+
// }
58+
59+
@DataProvider(name = "processTestSessionResultCases")
60+
public Object[][] complexParamsDataProvider() {
61+
return new Object[][]{
62+
{
63+
UploadScreenshotMutation.builder()
64+
.misMatchPercentage(null)
65+
.misMatchTolerance(0.01f)
66+
.isSameDimensions(null)
67+
.link("http://google.com")
68+
.build(),
69+
"For this image is no baseline defined! -> http://google.com",
70+
},
71+
{
72+
UploadScreenshotMutation.builder()
73+
.misMatchPercentage(null)
74+
.misMatchTolerance(0.01f)
75+
.isSameDimensions(true)
76+
.link("http://google.com")
77+
.build(),
78+
"For this image is no baseline defined! -> http://google.com",
79+
},
80+
{
81+
UploadScreenshotMutation.builder()
82+
.misMatchPercentage(null)
83+
.misMatchTolerance(0.01f)
84+
.isSameDimensions(false)
85+
.link("http://google.com")
86+
.build(),
87+
"Compared Screenshots are not in the same dimension! -> http://google.com",
88+
},
89+
{
90+
UploadScreenshotMutation.builder()
91+
.misMatchPercentage(0.02f)
92+
.misMatchTolerance(0.01f)
93+
.isSameDimensions(true)
94+
.link("http://google.com")
95+
.build(),
96+
"Mismatch of 0.02 is greater than the tolerance 0.01 -> http://google.com",
97+
},
98+
};
99+
}
100+
101+
@Test(dataProvider = "processTestSessionResultCases")
102+
public void processTestSessionResult(UploadScreenshotMutation testSessionResult, String expectedMessage) {
103+
String actualMessage = "";
104+
try {
105+
core.processTestSessionResult(testSessionResult);
106+
} catch (ImageProcessException exception) {
107+
actualMessage = exception.getMessage();
108+
}
109+
Assert.assertEquals(actualMessage, expectedMessage);
110+
}
111+
}

0 commit comments

Comments
 (0)