|
3 | 3 | import org.junit.jupiter.api.AfterEach; |
4 | 4 | import org.junit.jupiter.api.BeforeEach; |
5 | 5 | 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; |
6 | 10 |
|
7 | 11 | import java.util.HashMap; |
8 | 12 | import java.util.Map; |
|
14 | 18 | /** |
15 | 19 | * Tests for {@link DefaultHttpRequesterTest} |
16 | 20 | */ |
| 21 | +@ExtendWith(SystemStubsExtension.class) |
17 | 22 | 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 |
31 | 24 |
|
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(); |
44 | 27 |
|
45 | 28 | @Test |
46 | 29 | void shouldConfigureProxySettings_falseIfNeitherUserNorPasswordIsSet() { |
47 | | - System.clearProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_USER.getLiterat()); |
48 | | - System.clearProperty(ImportScanService.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat()); |
49 | | - |
50 | 30 | assertThat(sut.shouldConfigureProxySettings(), is(false)); |
51 | 31 | } |
52 | 32 |
|
53 | 33 | @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"); |
57 | 36 |
|
58 | | - assertThat(sut.shouldConfigureProxySettings(), is(false)); |
| 37 | + systemProperties.execute(() -> { |
| 38 | + assertThat(sut.shouldConfigureProxySettings(), is(false)); |
| 39 | + }); |
59 | 40 | } |
60 | 41 |
|
61 | 42 | @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"); |
65 | 45 |
|
66 | | - assertThat(sut.shouldConfigureProxySettings(), is(false)); |
| 46 | + systemProperties.execute(() -> { |
| 47 | + assertThat(sut.shouldConfigureProxySettings(), is(false)); |
| 48 | + }); |
67 | 49 | } |
68 | 50 |
|
69 | 51 | @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"); |
73 | 55 |
|
74 | | - assertThat(sut.shouldConfigureProxySettings(), is(true)); |
| 56 | + systemProperties.execute(() -> { |
| 57 | + assertThat(sut.shouldConfigureProxySettings(), is(true)); |
| 58 | + }); |
75 | 59 | } |
76 | 60 |
|
77 | 61 | @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 | + }); |
89 | 74 | } |
90 | 75 |
|
91 | 76 | @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 | + }); |
103 | 89 | } |
104 | 90 |
|
105 | 91 | @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 | + }); |
117 | 104 | } |
118 | 105 |
|
119 | 106 | @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 | + }); |
131 | 119 | } |
132 | 120 |
|
133 | 121 | @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 | + }); |
147 | 137 | } |
148 | 138 | } |
0 commit comments