-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathProxyConfigFactoryTest.java
106 lines (85 loc) · 3.73 KB
/
ProxyConfigFactoryTest.java
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
// SPDX-FileCopyrightText: the secureCodeBox authors
//
// SPDX-License-Identifier: Apache-2.0
package io.securecodebox.persistence.defectdojo.http;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.properties.SystemProperties;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* Tests for {@link ProxyConfigFactory}
*/
@ExtendWith(SystemStubsExtension.class)
class ProxyConfigFactoryTest {
@SystemStub
private SystemProperties restoreSystemProperties;
private final ProxyConfigFactory sut = new ProxyConfigFactory();
@Test
void create_returnsDefaultIfUserAndPasswordNotPresent() {
assertThat(sut.create(), is(ProxyConfigFactory.DEFAULT_CONFIG));
}
@Test
void create_returnsDefaultIfUserNotPresent() {
System.setProperty(ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
assertThat(sut.create(), is(ProxyConfigFactory.DEFAULT_CONFIG));
}
@Test
void create_returnsDefaultIfPasswordNotPresent() {
System.setProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
assertThat(sut.create(), is(ProxyConfigFactory.DEFAULT_CONFIG));
}
@Test
void create_returnsCompleteConfigIfAllPropertiesArePresent() {
System.setProperty(ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
System.setProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
System.setProperty(ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host");
System.setProperty(ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "4242");
final var expected = ProxyConfig.builder()
.user("user")
.password("password")
.host("host")
.port(4242)
.build();
assertThat(sut.create(), is(expected));
}
@Test
void create_throwsExceptionIfHostNotSet() {
System.setProperty(ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
System.setProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
System.clearProperty(ProxyConfigNames.HTTP_PROXY_HOST.getLiterat());
System.setProperty(ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "4242");
final var thrown = assertThrows(
MissingProxyConfigValue.class,
sut::create);
assertThat(thrown.getMessage(), containsString("'http.proxyHost'"));
}
@Test
void create_throwsExceptionIfPortNotSet() {
System.setProperty(ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
System.setProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
System.setProperty(ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host");
System.clearProperty(ProxyConfigNames.HTTP_PROXY_PORT.getLiterat());
final var thrown = assertThrows(
MissingProxyConfigValue.class,
sut::create);
assertThat(thrown.getMessage(), containsString("'http.proxyPort'"));
}
@Test
void create_throwsExceptionIfPortIsNotInteger() {
System.setProperty(ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
System.setProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
System.setProperty(ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host");
System.setProperty(ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "FUBAR");
final var thrown = assertThrows(
IllegalArgumentException.class,
sut::create);
assertThat(
thrown.getMessage(),
is("Given port for proxy authentication configuration (property 'http.proxyPort') is not a valid number! Given value wa 'FUBAR'."));
}
}