Skip to content

Commit 9ea3d67

Browse files
committed
#36 Add class to hold proxy configuration
will be used later to remove code duplication in the services. Signed-off-by: Sven Strittmatter <sven.strittmatter@iteratec.com>
1 parent df2dae9 commit 9ea3d67

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package io.securecodebox.persistence.defectdojo.http;
2+
3+
import lombok.Builder;
4+
import lombok.Value;
5+
6+
/**
7+
* Holds HTTP proxy configuration
8+
* <p>
9+
* This class is immutable by design and therefor thread safe. As defaults it does not use |{@code null} to prevent null
10+
* pointer exceptions. It utilizes sane defaults (empty string or 0) to indicate a not set value. Also it introduces a
11+
* null-object to indicate a not-existing configuration.
12+
* </p>
13+
*/
14+
@Value
15+
@Builder
16+
class ProxyConfig {
17+
/**
18+
* Null pattern object.
19+
*/
20+
static final ProxyConfig NULL = ProxyConfig.builder().build();
21+
private static final String DEFAULT_STRING = "";
22+
private static final int DEFAULT_INT = 0;
23+
24+
/**
25+
* Username to authenticate on a proxy.
26+
* <p>
27+
* Defaults to empty string.
28+
* </p>
29+
*/
30+
@Builder.Default
31+
String user = DEFAULT_STRING;
32+
33+
/**
34+
* Password to authenticate on a proxy.
35+
* <p>
36+
* Defaults to empty string.
37+
* </p>
38+
*/
39+
@Builder.Default
40+
String password = DEFAULT_STRING;
41+
42+
/**
43+
* Host name of the proxy.
44+
* <p>
45+
* Defaults to empty string.
46+
* </p>
47+
*/
48+
@Builder.Default
49+
String host = DEFAULT_STRING;
50+
51+
/**
52+
* Port of the proxy.
53+
* <p>
54+
* Defaults to 0 (zero).
55+
* </p>
56+
*/
57+
@Builder.Default
58+
int port = DEFAULT_INT;
59+
60+
/**
61+
* configuration is considered complete if all values are not default values
62+
*
63+
* @return {@code true} if all values are set else {@code false}
64+
*/
65+
boolean isComplete() {
66+
if (getUser().equals(DEFAULT_STRING)) {
67+
return false;
68+
}
69+
70+
if (getPassword().equals(DEFAULT_STRING)) {
71+
return false;
72+
}
73+
74+
if (getHost().equals(DEFAULT_STRING)) {
75+
return false;
76+
}
77+
78+
if (getPort() == DEFAULT_INT) {
79+
return false;
80+
}
81+
82+
return true;
83+
}
84+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package io.securecodebox.persistence.defectdojo.http;
2+
3+
import nl.jqno.equalsverifier.EqualsVerifier;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.Arguments;
7+
import org.junit.jupiter.params.provider.MethodSource;
8+
9+
import java.util.stream.Stream;
10+
11+
import static org.junit.jupiter.api.Assertions.*;
12+
import static org.hamcrest.MatcherAssert.assertThat;
13+
import static org.hamcrest.Matchers.*;
14+
15+
/**
16+
* Tests for {@link ProxyConfig}
17+
*/
18+
class ProxyConfigTest {
19+
@Test
20+
void equalsAndHashCode() {
21+
EqualsVerifier.forClass(ProxyConfig.class).verify();
22+
}
23+
24+
@Test
25+
void builderCreatesDefault() {
26+
final var sut = ProxyConfig.builder().build();
27+
28+
assertAll(
29+
() -> assertThat(sut.getUser(), is(emptyString())),
30+
() -> assertThat(sut.getPassword(), is(emptyString())),
31+
() -> assertThat(sut.getHost(), is(emptyString())),
32+
() -> assertThat(sut.getPort(), is(0))
33+
);
34+
}
35+
36+
@Test
37+
void buildersDefaultIsEqualToNullObject() {
38+
assertThat(ProxyConfig.builder().build(), is(ProxyConfig.NULL));
39+
}
40+
41+
@Test
42+
void isComplete_falseForNullObject() {
43+
assertThat(ProxyConfig.NULL.isComplete(), is(false));
44+
}
45+
46+
@Test
47+
void isComplete_falseForDefault() {
48+
assertThat(ProxyConfig.builder().build().isComplete(), is(false));
49+
}
50+
51+
@ParameterizedTest
52+
@MethodSource("incompleteConfigs")
53+
void isComplete_falseUnlessAllFieldsAreSet(final ProxyConfig sut) {
54+
55+
}
56+
57+
private static Stream<Arguments> incompleteConfigs() {
58+
return Stream.of(
59+
// Only one is set:
60+
Arguments.of(ProxyConfig.builder().user("user").build()),
61+
Arguments.of(ProxyConfig.builder().password("pw").build()),
62+
Arguments.of(ProxyConfig.builder().host("host").build()),
63+
Arguments.of(ProxyConfig.builder().port(42).build()),
64+
// All but one is set:
65+
Arguments.of(ProxyConfig.builder().password("pwd").host("host").port(42).build()),
66+
Arguments.of(ProxyConfig.builder().user("user").host("host").port(42).build()),
67+
Arguments.of(ProxyConfig.builder().user("user").password("pwd").port(42).build()),
68+
Arguments.of(ProxyConfig.builder().user("user").password("pwd").host("host").build())
69+
);
70+
}
71+
72+
@Test
73+
void isComplete_trueIfAllFieldsAreNonDefaults() {
74+
final var sut = ProxyConfig.builder()
75+
.user("user")
76+
.password("pw")
77+
.host("host")
78+
.port(42)
79+
.build();
80+
81+
assertThat(sut.isComplete(), is(true));
82+
}
83+
}

0 commit comments

Comments
 (0)