Skip to content

Commit ca93c0a

Browse files
WeltraumschafManuelNeuer
authored andcommitted
#36 Use System-Stubs to backup/restore system properties instead of handcrafted code
Signed-off-by: Sven Strittmatter <sven.strittmatter@iteratec.com>
1 parent 8130596 commit ca93c0a

File tree

1 file changed

+87
-97
lines changed

1 file changed

+87
-97
lines changed

src/test/java/io/securecodebox/persistence/defectdojo/service/DefaultHttpRequesterTest.java

Lines changed: 87 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import org.junit.jupiter.api.AfterEach;
44
import org.junit.jupiter.api.BeforeEach;
55
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.api.extension.ExtendWith;
7+
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
8+
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
9+
import uk.org.webcompere.systemstubs.properties.SystemProperties;
610

711
import java.util.HashMap;
812
import java.util.Map;
@@ -14,135 +18,121 @@
1418
/**
1519
* Tests for {@link DefaultHttpRequesterTest}
1620
*/
21+
@ExtendWith(SystemStubsExtension.class)
1722
final class DefaultHttpRequesterTest {
18-
/**
19-
* Since System.getProperty() is a side effect we need to back up and restore it to isolate test cases.
20-
*/
21-
private final Map<ImportScanService.ProxyConfigNames, String> backup = new HashMap<>();
22-
private final DefaultImportScanService.DefaultHttpRequester sut = new DefaultImportScanService.DefaultHttpRequester();
23-
24-
@BeforeEach
25-
void backupSystemProperties() {
26-
backup.clear();
27-
for (final var name : ImportScanService.ProxyConfigNames.values()) {
28-
backup.put(name, System.getProperty(name.getLiterat()));
29-
}
30-
}
23+
@SystemStub
3124

32-
@AfterEach
33-
void restoreSystemProperties() {
34-
for (final var entry : backup.entrySet()) {
35-
final var name = entry.getKey().getLiterat();
36-
37-
if (null == entry.getValue()) {
38-
System.clearProperty(name);
39-
} else {
40-
System.setProperty(name, entry.getValue());
41-
}
42-
}
43-
}
25+
private SystemProperties systemProperties;
26+
private final DefaultImportScanService.DefaultHttpRequester sut = new DefaultImportScanService.DefaultHttpRequester();
4427

4528
@Test
4629
void shouldConfigureProxySettings_falseIfNeitherUserNorPasswordIsSet() {
47-
System.clearProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_USER.getLiterat());
48-
System.clearProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat());
49-
5030
assertThat(sut.shouldConfigureProxySettings(), is(false));
5131
}
5232

5333
@Test
54-
void shouldConfigureProxySettings_falseIfUserSetButPasswordNot() {
55-
System.setProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
56-
System.clearProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat());
34+
void shouldConfigureProxySettings_falseIfUserSetButPasswordNot() throws Exception {
35+
systemProperties.set(ImportScanService.ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
5736

58-
assertThat(sut.shouldConfigureProxySettings(), is(false));
37+
systemProperties.execute(() -> {
38+
assertThat(sut.shouldConfigureProxySettings(), is(false));
39+
});
5940
}
6041

6142
@Test
62-
void shouldConfigureProxySettings_falseIfPasswordSetButUserNot() {
63-
System.clearProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_USER.getLiterat());
64-
System.setProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
43+
void shouldConfigureProxySettings_falseIfPasswordSetButUserNot() throws Exception {
44+
systemProperties.set(ImportScanService.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
6545

66-
assertThat(sut.shouldConfigureProxySettings(), is(false));
46+
systemProperties.execute(() -> {
47+
assertThat(sut.shouldConfigureProxySettings(), is(false));
48+
});
6749
}
6850

6951
@Test
70-
void shouldConfigureProxySettings_trueIfUserAndPasswordSet() {
71-
System.setProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
72-
System.setProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
52+
void shouldConfigureProxySettings_trueIfUserAndPasswordSet() throws Exception {
53+
systemProperties.set(ImportScanService.ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
54+
systemProperties.set(ImportScanService.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
7355

74-
assertThat(sut.shouldConfigureProxySettings(), is(true));
56+
systemProperties.execute(() -> {
57+
assertThat(sut.shouldConfigureProxySettings(), is(true));
58+
});
7559
}
7660

7761
@Test
78-
void createRequestFactoryWithProxyAuthConfig_throesExceptionIfUserNotSet() {
79-
System.clearProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_USER.getLiterat());
80-
System.setProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
81-
System.setProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host");
82-
System.setProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "4242");
83-
84-
final var thrown = assertThrows(
85-
DefaultImportScanService.MissingProxyAuthenticationConfig.class,
86-
sut::createRequestFactoryWithProxyAuthConfig);
87-
88-
assertThat(thrown.getMessage(), is("Expected System property 'http.proxyUser' not set!"));
62+
void createRequestFactoryWithProxyAuthConfig_throesExceptionIfUserNotSet() throws Exception {
63+
systemProperties.set(ImportScanService.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
64+
systemProperties.set(ImportScanService.ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host");
65+
systemProperties.set(ImportScanService.ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "4242");
66+
67+
systemProperties.execute(() -> {
68+
final var thrown = assertThrows(
69+
DefaultImportScanService.MissingProxyAuthenticationConfig.class,
70+
sut::createRequestFactoryWithProxyAuthConfig);
71+
72+
assertThat(thrown.getMessage(), is("Expected System property 'http.proxyUser' not set!"));
73+
});
8974
}
9075

9176
@Test
92-
void createRequestFactoryWithProxyAuthConfig_throesExceptionIfPasswordNotSet() {
93-
System.setProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
94-
System.clearProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat());
95-
System.setProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host");
96-
System.setProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "4242");
97-
98-
final var thrown = assertThrows(
99-
DefaultImportScanService.MissingProxyAuthenticationConfig.class,
100-
sut::createRequestFactoryWithProxyAuthConfig);
101-
102-
assertThat(thrown.getMessage(), is("Expected System property 'http.proxyPassword' not set!"));
77+
void createRequestFactoryWithProxyAuthConfig_throesExceptionIfPasswordNotSet() throws Exception {
78+
systemProperties.set(ImportScanService.ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
79+
systemProperties.set(ImportScanService.ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host");
80+
systemProperties.set(ImportScanService.ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "4242");
81+
82+
systemProperties.execute(() -> {
83+
final var thrown = assertThrows(
84+
DefaultImportScanService.MissingProxyAuthenticationConfig.class,
85+
sut::createRequestFactoryWithProxyAuthConfig);
86+
87+
assertThat(thrown.getMessage(), is("Expected System property 'http.proxyPassword' not set!"));
88+
});
10389
}
10490

10591
@Test
106-
void createRequestFactoryWithProxyAuthConfig_throesExceptionIfHostNotSet() {
107-
System.setProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
108-
System.setProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
109-
System.clearProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_HOST.getLiterat());
110-
System.setProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "4242");
111-
112-
final var thrown = assertThrows(
113-
DefaultImportScanService.MissingProxyAuthenticationConfig.class,
114-
sut::createRequestFactoryWithProxyAuthConfig);
115-
116-
assertThat(thrown.getMessage(), is("Expected System property 'http.proxyHost' not set!"));
92+
void createRequestFactoryWithProxyAuthConfig_throesExceptionIfHostNotSet() throws Exception {
93+
systemProperties.set(ImportScanService.ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
94+
systemProperties.set(ImportScanService.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
95+
systemProperties.set(ImportScanService.ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "4242");
96+
97+
systemProperties.execute(() -> {
98+
final var thrown = assertThrows(
99+
DefaultImportScanService.MissingProxyAuthenticationConfig.class,
100+
sut::createRequestFactoryWithProxyAuthConfig);
101+
102+
assertThat(thrown.getMessage(), is("Expected System property 'http.proxyHost' not set!"));
103+
});
117104
}
118105

119106
@Test
120-
void createRequestFactoryWithProxyAuthConfig_throesExceptionIfPortNotSet() {
121-
System.setProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
122-
System.setProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
123-
System.setProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host");
124-
System.clearProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_PORT.getLiterat());
125-
126-
final var thrown = assertThrows(
127-
DefaultImportScanService.MissingProxyAuthenticationConfig.class,
128-
sut::createRequestFactoryWithProxyAuthConfig);
129-
130-
assertThat(thrown.getMessage(), is("Expected System property 'http.proxyPort' not set!"));
107+
void createRequestFactoryWithProxyAuthConfig_throesExceptionIfPortNotSet() throws Exception {
108+
systemProperties.set(ImportScanService.ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
109+
systemProperties.set(ImportScanService.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
110+
systemProperties.set(ImportScanService.ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host");
111+
112+
systemProperties.execute(() -> {
113+
final var thrown = assertThrows(
114+
DefaultImportScanService.MissingProxyAuthenticationConfig.class,
115+
sut::createRequestFactoryWithProxyAuthConfig);
116+
117+
assertThat(thrown.getMessage(), is("Expected System property 'http.proxyPort' not set!"));
118+
});
131119
}
132120

133121
@Test
134-
void createRequestFactoryWithProxyAuthConfig_throesExceptionIfPortIsNotInteger() {
135-
System.setProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
136-
System.setProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
137-
System.setProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host");
138-
System.setProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "FUBAR");
139-
140-
final var thrown = assertThrows(
141-
IllegalArgumentException.class,
142-
sut::createRequestFactoryWithProxyAuthConfig);
143-
144-
assertThat(
145-
thrown.getMessage(),
146-
is("Given port for proxy authentication configuration (property 'http.proxyPort') is not a valid number! Given value wa 'FUBAR'."));
122+
void createRequestFactoryWithProxyAuthConfig_throesExceptionIfPortIsNotInteger() throws Exception {
123+
systemProperties.set(ImportScanService.ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
124+
systemProperties.set(ImportScanService.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
125+
systemProperties.set(ImportScanService.ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host");
126+
systemProperties.set(ImportScanService.ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "FUBAR");
127+
128+
systemProperties.execute(() -> {
129+
final var thrown = assertThrows(
130+
IllegalArgumentException.class,
131+
sut::createRequestFactoryWithProxyAuthConfig);
132+
133+
assertThat(
134+
thrown.getMessage(),
135+
is("Given port for proxy authentication configuration (property 'http.proxyPort') is not a valid number! Given value wa 'FUBAR'."));
136+
});
147137
}
148138
}

0 commit comments

Comments
 (0)