-
Notifications
You must be signed in to change notification settings - Fork 214
/
DefaultHonoConfigTest.java
227 lines (189 loc) · 9.33 KB
/
DefaultHonoConfigTest.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.service.config;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.mutabilitydetector.unittesting.AllowedReason.assumingFields;
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf;
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.MessageFormat;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.eclipse.ditto.connectivity.model.UserPasswordCredentials;
import org.eclipse.ditto.internal.utils.config.DittoConfigError;
import org.eclipse.ditto.internal.utils.config.WithConfigPath;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigException;
import com.typesafe.config.ConfigFactory;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.testkit.javadsl.TestKit;
import nl.jqno.equalsverifier.EqualsVerifier;
import scala.concurrent.duration.FiniteDuration;
/**
* Unit test for {@link DefaultHonoConfig}.
*/
public final class DefaultHonoConfigTest {
@Rule
public final TestName testName = new TestName();
private ActorSystem actorSystem;
@After
public void after() {
if (null != actorSystem) {
TestKit.shutdownActorSystem(actorSystem, FiniteDuration.apply(5, TimeUnit.SECONDS), false);
}
}
@Test
public void assertImmutability() {
assertInstancesOf(DefaultHonoConfig.class,
areImmutable(),
assumingFields("bootstrapServerUris").areSafelyCopiedUnmodifiableCollectionsWithImmutableElements());
}
@Test
public void testHashCodeAndEquals() {
EqualsVerifier.forClass(DefaultHonoConfig.class)
.usingGetClass()
.verify();
}
@Test
public void newInstanceWithNullActorSystemThrowsException() {
assertThatNullPointerException()
.isThrownBy(() -> new DefaultHonoConfig(null))
.withMessage("The actorSystem must not be null!")
.withNoCause();
}
@Test
public void newInstanceThrowsDittoConfigErrorIfBaseUriSyntaxIsInvalid() {
final var configKey = getFullQualifiedConfigKey(HonoConfig.HonoConfigValue.BASE_URI);
final var invalidUri = "192.168.1.256:80";
assertThatExceptionOfType(DittoConfigError.class)
.isThrownBy(() -> new DefaultHonoConfig(getActorSystem(
ConfigFactory.parseMap(Map.of(configKey, invalidUri))
)))
.withMessageStartingWith("The string value at <%s> is not a URI:",
HonoConfig.HonoConfigValue.BASE_URI.getConfigPath())
.withMessageEndingWith(invalidUri)
.withCauseInstanceOf(URISyntaxException.class);
}
@Test
public void getBaseUriReturnsDefaultValueIfNotContainedInConfig() {
final var defaultHonoConfig = new DefaultHonoConfig(getActorSystem(ConfigFactory.empty()));
assertThat(defaultHonoConfig.getBaseUri())
.isEqualTo(URI.create(HonoConfig.HonoConfigValue.BASE_URI.getDefaultValue().toString()));
}
@Test
public void getBaseUriReturnsExplicitlyConfiguredValue() {
final var baseUri = URI.create("example.org:8080");
final var defaultHonoConfig = new DefaultHonoConfig(getActorSystem(ConfigFactory.parseMap(
Map.of(getFullQualifiedConfigKey(HonoConfig.HonoConfigValue.BASE_URI), baseUri.toString())
)));
assertThat(defaultHonoConfig.getBaseUri()).isEqualTo(baseUri);
}
@Test
public void isValidateCertificatesReturnsDefaultValueIfNotContainedInConfig() {
final var defaultHonoConfig = new DefaultHonoConfig(getActorSystem(ConfigFactory.empty()));
assertThat(defaultHonoConfig.isValidateCertificates())
.isEqualTo(HonoConfig.HonoConfigValue.VALIDATE_CERTIFICATES.getDefaultValue());
}
@Test
public void isValidateCertificatesReturnsExplicitlyConfiguredValue() {
final var validateCertificates = true;
final var defaultHonoConfig = new DefaultHonoConfig(getActorSystem(ConfigFactory.parseMap(
Map.of(getFullQualifiedConfigKey(HonoConfig.HonoConfigValue.VALIDATE_CERTIFICATES),
validateCertificates)
)));
assertThat(defaultHonoConfig.isValidateCertificates()).isEqualTo(validateCertificates);
}
@Test
public void newInstanceThrowsDittoConfigErrorIfSaslMechanismIsUnknown() {
assertThatExceptionOfType(DittoConfigError.class)
.isThrownBy(() -> new DefaultHonoConfig(getActorSystem(ConfigFactory.parseMap(
Map.of(getFullQualifiedConfigKey(HonoConfig.HonoConfigValue.SASL_MECHANISM), 42)
))))
.withCauseInstanceOf(ConfigException.BadValue.class);
}
@Test
public void getSaslMechanismReturnsDefaultValueIfNotContainedInConfig() {
final var defaultHonoConfig = new DefaultHonoConfig(getActorSystem(ConfigFactory.empty()));
assertThat(defaultHonoConfig.getSaslMechanism())
.isEqualTo(HonoConfig.SaslMechanism.valueOf(
HonoConfig.HonoConfigValue.SASL_MECHANISM.getDefaultValue().toString()
));
}
@Test
public void newInstanceThrowsDittoConfigErrorIfOneBootstrapServerUriSyntaxIsInvalid() {
final var configKey = getFullQualifiedConfigKey(HonoConfig.HonoConfigValue.BOOTSTRAP_SERVERS);
final var invalidUri = "192.168.1.256:80";
final var bootstrapServerUrisString = "example.com," + invalidUri + ",10.1.0.35:8080";
assertThatExceptionOfType(DittoConfigError.class)
.isThrownBy(() -> new DefaultHonoConfig(getActorSystem(
ConfigFactory.parseMap(Map.of(configKey, bootstrapServerUrisString))
)))
.withMessageStartingWith("The string at index <1> for key <%s> is not a valid URI:",
HonoConfig.HonoConfigValue.BOOTSTRAP_SERVERS.getConfigPath())
.withMessageEndingWith(invalidUri)
.withCauseInstanceOf(URISyntaxException.class);
}
@Test
public void getBootstrapServerUrisReturnsDefaultValueIfNotContainedInConfig() {
final var defaultHonoConfig = new DefaultHonoConfig(getActorSystem(ConfigFactory.empty()));
assertThat(defaultHonoConfig.getBootstrapServerUris())
.containsOnly(URI.create(HonoConfig.HonoConfigValue.BOOTSTRAP_SERVERS.getDefaultValue().toString()));
}
@Test
public void getBootstrapServerUrisReturnsExplicitlyConfiguredValues() {
final var bootstrapServerUris = List.of(URI.create("www.example.org"),
URI.create("tcp://192.168.10.1:8080"),
URI.create("file://bin/server"));
final var defaultHonoConfig = new DefaultHonoConfig(getActorSystem(ConfigFactory.parseMap(Map.of(
getFullQualifiedConfigKey(HonoConfig.HonoConfigValue.BOOTSTRAP_SERVERS),
bootstrapServerUris.stream()
.map(URI::toString)
.map(s -> s.concat(" ")) // blanks should get trimmed
.collect(Collectors.joining(","))
))));
assertThat(defaultHonoConfig.getBootstrapServerUris()).containsExactlyElementsOf(bootstrapServerUris);
}
@Test
public void getCredentialsReturnsDefaultValueIfNotContainedInConfig() {
final var defaultHonoConfig = new DefaultHonoConfig(getActorSystem(ConfigFactory.empty()));
assertThat(defaultHonoConfig.getUserPasswordCredentials())
.isEqualTo(UserPasswordCredentials.newInstance("", ""));
}
@Test
public void getCredentialsReturnsExplicitlyConfiguredValues() {
final var username = "Herbert W. Franke";
final var password = "PeterParsival";
final var defaultHonoConfig = new DefaultHonoConfig(getActorSystem(ConfigFactory.parseMap(Map.of(
getFullQualifiedConfigKey(HonoConfig.HonoConfigValue.USERNAME), username,
getFullQualifiedConfigKey(HonoConfig.HonoConfigValue.PASSWORD), password
))));
assertThat(defaultHonoConfig.getUserPasswordCredentials())
.isEqualTo(UserPasswordCredentials.newInstance(username, password));
}
private static String getFullQualifiedConfigKey(final WithConfigPath withConfigPath) {
return MessageFormat.format("{0}.{1}", HonoConfig.PREFIX, withConfigPath.getConfigPath());
}
private ActorSystem getActorSystem(final Config config) {
actorSystem = ActorSystem.create(testName.getMethodName(), config);
return actorSystem;
}
}