Repository navigation
Expand file tree
/
Copy pathVisualRegressionTrackerConfigTest.java
More file actions
206 lines (173 loc) · 8.79 KB
/
Copy pathVisualRegressionTrackerConfigTest.java
File metadata and controls
206 lines (173 loc) · 8.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package io.visual_regression_tracker.sdk_java;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.Callable;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class VisualRegressionTrackerConfigTest {
private static final String REQUIRED_FIELDS = "{\"apiUrl\": \"http://localhost:4200\", \"apiKey\": \"KEY\", \"project\": \"PROJECT\"";
@Test(expectedExceptions = NullPointerException.class,
expectedExceptionsMessageRegExp = "apiKey is marked non-null but is null")
public void shouldThrowExceptionIfApiKeyMissed() {
VisualRegressionTrackerConfig
.builder()
.apiUrl("")
.project("")
.build();
}
@Test(expectedExceptions = NullPointerException.class,
expectedExceptionsMessageRegExp = "apiUrl is marked non-null but is null")
public void shouldThrowExceptionIfApiUrlMissed() {
VisualRegressionTrackerConfig
.builder()
.apiKey("")
.project("")
.build();
}
@Test(expectedExceptions = NullPointerException.class,
expectedExceptionsMessageRegExp = "project is marked non-null but is null")
public void shouldThrowExceptionIfProjectMissed() {
VisualRegressionTrackerConfig
.builder()
.apiKey("")
.apiUrl("")
.build();
}
@Test
public void shouldBeCreatedFromConfigFile() {
File configFile = new File("src/test/resources/vrt_config_example.json");
VisualRegressionTrackerConfig config = VisualRegressionTrackerConfig.builder()
.configFile(configFile)
.build();
assertThat(config.getApiKey(), is("SECRET"));
assertThat(config.getApiUrl(), is("http://162.243.161.172:4200"));
assertThat(config.getProject(), is("VRT"));
assertThat(config.getBranchName(), is("master"));
assertThat(config.getEnableSoftAssert(), is(false));
assertThat(config.getCiBuildId(), is("SOME_UNIQUE_ID"));
assertThat(config.getHttpTimeoutInSeconds(), is(15));
assertThat(config.getSourceBranch(), is("develop"));
}
@Test
public void shouldBeCreatedFromEnvironment() throws Exception {
EnvironmentVariables environmentVariables = new EnvironmentVariables("VRT_APIKEY", "SECRET")
.set("VRT_APIURL", "http://162.243.161.172:4200")
.set("VRT_PROJECT", "VRT")
.set("VRT_BRANCHNAME", "master")
.set("VRT_ENABLESOFTASSERT", "false")
.set("VRT_CIBUILDID", "SOME_UNIQUE_ID")
.set("VRT_SOURCEBRANCH", "develop");
VisualRegressionTrackerConfig config = environmentVariables.execute(() ->
VisualRegressionTrackerConfig.builder()
.build()
);
assertThat(config.getApiKey(), is("SECRET"));
assertThat(config.getApiUrl(), is("http://162.243.161.172:4200"));
assertThat(config.getProject(), is("VRT"));
assertThat(config.getBranchName(), is("master"));
assertThat(config.getEnableSoftAssert(), is(false));
assertThat(config.getCiBuildId(), is("SOME_UNIQUE_ID"));
assertThat(config.getSourceBranch(), is("develop"));
}
@Test
public void shouldResolveFinalValuesInTheRightOrder() throws Exception {
EnvironmentVariables environmentVariables = new EnvironmentVariables("VRT_APIKEY", "ENV_SECRET")
.set("VRT_APIURL", "http://162.243.161.172:4201")
.set("VRT_SOURCEBRANCH", "env");
File configFile = new File("src/test/resources/vrt_config_example.json");
VisualRegressionTrackerConfig config = environmentVariables.execute(() ->
VisualRegressionTrackerConfig.builder()
.apiUrl("http://localhost:4200")
.sourceBranch("builder")
.configFile(configFile)
.build()
);
assertThat(config.getApiKey(), is("ENV_SECRET"));
assertThat(config.getApiUrl(), is("http://localhost:4200"));
assertThat(config.getSourceBranch(), is("builder"));
assertThat(config.getProject(), is("VRT"));
assertThat(config.getBranchName(), is("master"));
assertThat(config.getEnableSoftAssert(), is(false));
assertThat(config.getCiBuildId(), is("SOME_UNIQUE_ID"));
}
@Test
public void shouldUseDefaultValuesIfNotProvided() {
VisualRegressionTrackerConfig config = VisualRegressionTrackerConfig.builder()
.apiUrl("http://localhost:4200")
.apiKey("KEY")
.project("PROJECT")
.build();
assertThat(config.getEnableSoftAssert(), is(false));
assertThat(config.getHttpTimeoutInSeconds(), is(360));
}
@Test
public void shouldUseDefaultValuesWhenCreatedWithConstructor() {
VisualRegressionTrackerConfig config = new VisualRegressionTrackerConfig("http://localhost:4200", "KEY", "PROJECT");
assertThat(config.getEnableSoftAssert(), is(false));
assertThat(config.getHttpTimeoutInSeconds(), is(360));
}
@Test
public void shouldIgnoreNonScalarValuesInConfigFile() throws Exception {
VisualRegressionTrackerConfig config = VisualRegressionTrackerConfig.builder()
.configFile(configFile(REQUIRED_FIELDS + ", \"extra\": {\"nested\": 1}}"))
.build();
assertThat(config.getProject(), is("PROJECT"));
}
@Test(expectedExceptions = NullPointerException.class,
expectedExceptionsMessageRegExp = "apiUrl is marked non-null but is null")
public void shouldTreatEmptyConfigFileAsNoValues() throws Exception {
VisualRegressionTrackerConfig.builder()
.configFile(configFile(""))
.build();
}
@Test
public void shouldReadWholeNumberWrittenAsDecimalFromConfigFile() throws Exception {
VisualRegressionTrackerConfig config = VisualRegressionTrackerConfig.builder()
.configFile(configFile(REQUIRED_FIELDS + ", \"httpTimeoutInSeconds\": 15.0}"))
.build();
assertThat(config.getHttpTimeoutInSeconds(), is(15));
}
@Test(expectedExceptions = IllegalArgumentException.class,
expectedExceptionsMessageRegExp = "Invalid value for 'httpTimeoutInSeconds': 15.5")
public void shouldNameTheKeyOfAnInvalidValue() throws Exception {
VisualRegressionTrackerConfig.builder()
.configFile(configFile(REQUIRED_FIELDS + ", \"httpTimeoutInSeconds\": 15.5}"))
.build();
}
@Test
public void shouldKeepTheConstructorWithoutSourceBranch() {
VisualRegressionTrackerConfig config = new VisualRegressionTrackerConfig(
"http://localhost:4200", "KEY", "PROJECT", "develop", "ci", true, 15);
assertThat(config.getBranchName(), is("develop"));
assertThat(config.getHttpTimeoutInSeconds(), is(15));
assertThat(config.getSourceBranch(), is((String) null));
}
@DataProvider(name = "apiUrlSources")
public Object[][] apiUrlSources() {
return new Object[][]{
{(Callable<VisualRegressionTrackerConfig>) () ->
new VisualRegressionTrackerConfig("http://localhost:4200/", "KEY", "PROJECT")},
{(Callable<VisualRegressionTrackerConfig>) () -> VisualRegressionTrackerConfig.builder()
.apiUrl("http://localhost:4200//").apiKey("KEY").project("PROJECT").build()},
{(Callable<VisualRegressionTrackerConfig>) () -> new EnvironmentVariables("VRT_APIURL", "http://localhost:4200/")
.set("VRT_APIKEY", "KEY").set("VRT_PROJECT", "PROJECT")
.execute(() -> VisualRegressionTrackerConfig.builder().build())},
{(Callable<VisualRegressionTrackerConfig>) () -> VisualRegressionTrackerConfig.builder()
.configFile(configFile("{\"apiUrl\": \"http://localhost:4200/\", \"apiKey\": \"KEY\", \"project\": \"PROJECT\"}"))
.build()},
};
}
@Test(dataProvider = "apiUrlSources")
public void shouldStripTrailingSlashesFromApiUrl(Callable<VisualRegressionTrackerConfig> source) throws Exception {
assertThat(source.call().getApiUrl(), is("http://localhost:4200"));
}
private File configFile(String content) throws Exception {
Path configFile = Files.createTempFile("vrt", ".json");
Files.writeString(configFile, content);
return configFile.toFile();
}
}