-
Notifications
You must be signed in to change notification settings - Fork 214
/
HonoValidatorTest.java
282 lines (251 loc) · 13.3 KB
/
HonoValidatorTest.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* 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.messaging.hono;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.eclipse.ditto.connectivity.service.messaging.TestConstants.Authorization.AUTHORIZATION_CONTEXT;
import java.util.List;
import java.util.stream.Stream;
import org.assertj.core.api.JUnitSoftAssertions;
import org.eclipse.ditto.base.model.correlationid.TestNameCorrelationId;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.connectivity.model.Connection;
import org.eclipse.ditto.connectivity.model.ConnectionConfigurationInvalidException;
import org.eclipse.ditto.connectivity.model.ConnectionId;
import org.eclipse.ditto.connectivity.model.ConnectionType;
import org.eclipse.ditto.connectivity.model.ConnectivityModelFactory;
import org.eclipse.ditto.connectivity.model.ConnectivityStatus;
import org.eclipse.ditto.connectivity.model.Enforcement;
import org.eclipse.ditto.connectivity.model.HonoAddressAlias;
import org.eclipse.ditto.connectivity.model.Topic;
import org.eclipse.ditto.connectivity.service.config.ConnectivityConfig;
import org.eclipse.ditto.connectivity.service.messaging.TestConstants;
import org.eclipse.ditto.internal.utils.pekko.ActorSystemResource;
import org.eclipse.ditto.placeholders.UnresolvedPlaceholderException;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
/**
* Unit test for {@link org.eclipse.ditto.connectivity.service.messaging.hono.HonoValidator}.
*/
public final class HonoValidatorTest {
@ClassRule
public static final ActorSystemResource ACTOR_SYSTEM_RESOURCE =
ActorSystemResource.newInstance(TestConstants.CONFIG);
private static final ConnectionId CONNECTION_ID = TestConstants.createRandomConnectionId();
private static final ConnectivityConfig CONNECTIVITY_CONFIG = TestConstants.CONNECTIVITY_CONFIG;
@Rule
public final TestNameCorrelationId testNameCorrelationId = TestNameCorrelationId.newInstance();
@Rule
public final JUnitSoftAssertions softly = new JUnitSoftAssertions();
private HonoValidator underTest;
@Before
public void before() {
underTest = HonoValidator.getInstance();
}
@Test
public void validateWithValidEnforcementThrowsNoException() {
assertThatCode(
() -> underTest.validate(getConnectionWithSourceEnforcement(
ConnectivityModelFactory.newEnforcement("{{ header:device_id }}",
"{{ thing:id }}",
"{{ thing:name }}",
"{{ thing:namespace }}")
),
getDittoHeadersWithCorrelationId(),
ACTOR_SYSTEM_RESOURCE.getActorSystem(),
CONNECTIVITY_CONFIG)
).doesNotThrowAnyException();
}
@Test
public void validateWithInvalidMatcherThrowsException() {
final var dittoHeaders = getDittoHeadersWithCorrelationId();
assertThatExceptionOfType(ConnectionConfigurationInvalidException.class)
.isThrownBy(() -> underTest.validate(getConnectionWithSourceEnforcement(
ConnectivityModelFactory.newEnforcement(
"{{ header:device_id }}",
"{{ header:ditto }}")
),
dittoHeaders,
ACTOR_SYSTEM_RESOURCE.getActorSystem(),
CONNECTIVITY_CONFIG))
.withCauseInstanceOf(UnresolvedPlaceholderException.class)
.satisfies(exception -> assertThat(exception.getDittoHeaders()).isEqualTo(dittoHeaders));
}
@Test
public void validateWithValidSourceAddressesThrowsNoException() {
final var dittoHeaders = getDittoHeadersWithCorrelationId();
Stream.of(HonoAddressAlias.values())
.filter(honoAddressAlias -> HonoAddressAlias.COMMAND != honoAddressAlias)
.map(HonoAddressAlias::getAliasValue)
.forEach(honoAddressAliasValue -> softly.assertThatCode(() -> underTest.validate(
getConnectionWithSourceAddress(honoAddressAliasValue),
dittoHeaders,
ACTOR_SYSTEM_RESOURCE.getActorSystem(),
CONNECTIVITY_CONFIG
))
.as(honoAddressAliasValue)
.doesNotThrowAnyException());
}
@Test
public void validateWithEmptySourceAddressThrowsException() {
final var dittoHeaders = getDittoHeadersWithCorrelationId();
assertThatExceptionOfType(ConnectionConfigurationInvalidException.class)
.isThrownBy(() -> underTest.validate(getConnectionWithSourceAddress(""),
dittoHeaders,
ACTOR_SYSTEM_RESOURCE.getActorSystem(),
CONNECTIVITY_CONFIG))
.withMessage("The provided source address must not be empty.")
.withNoCause()
.satisfies(exception -> assertThat(exception.getDittoHeaders()).isEqualTo(dittoHeaders));
}
@Test
public void validateWithInvalidSourceAddressesThrowsException() {
final var dittoHeaders = getDittoHeadersWithCorrelationId();
Stream.of(
HonoAddressAlias.COMMAND.getAliasValue(),
"events/",
"hono.telemetry.c4bc9a62-8516-4232-bb81-dbbfe4d0fa8c_hub",
"ditto*a"
).forEach(
invalidSourceAddress -> softly.assertThatThrownBy(
() -> underTest.validate(getConnectionWithSourceAddress(invalidSourceAddress),
dittoHeaders,
ACTOR_SYSTEM_RESOURCE.getActorSystem(),
CONNECTIVITY_CONFIG)
)
.as(invalidSourceAddress)
.hasMessageStartingWith("The provided source address <%s> is invalid." +
" It should be one of the defined aliases: ",
invalidSourceAddress)
.hasMessageContainingAll(Stream.of(HonoAddressAlias.values())
.filter(honoAddressAlias -> HonoAddressAlias.COMMAND !=honoAddressAlias)
.map(HonoAddressAlias::getAliasValue)
.toArray(CharSequence[]::new))
.hasNoCause()
.isInstanceOfSatisfying(ConnectionConfigurationInvalidException.class,
exception -> assertThat(exception.getDittoHeaders()).isEqualTo(dittoHeaders))
);
}
@Test
public void validateWithInvalidSourceQosThrowsException() {
final var invalidQos = 3;
final var dittoHeaders = getDittoHeadersWithCorrelationId();
assertThatExceptionOfType(ConnectionConfigurationInvalidException.class)
.isThrownBy(() -> underTest.validate(ConnectivityModelFactory.newConnectionBuilder(CONNECTION_ID,
ConnectionType.HONO,
ConnectivityStatus.OPEN,
"tcp://localhost:999999")
.sources(singletonList(ConnectivityModelFactory.newSourceBuilder()
.address("event")
.authorizationContext(AUTHORIZATION_CONTEXT)
.qos(invalidQos)
.build()))
.build(),
dittoHeaders,
ACTOR_SYSTEM_RESOURCE.getActorSystem(),
CONNECTIVITY_CONFIG))
.withMessage("Invalid source 'qos' value <%d>. Supported values are <0> and <1>.", invalidQos)
.withNoCause()
.satisfies(exception -> assertThat(exception.getDittoHeaders()).isEqualTo(dittoHeaders));
}
@Test
public void validateWithValidTargetAddressThrowsNoException() {
assertThatCode(
() -> underTest.validate(getConnectionWithTargetAddress(HonoAddressAlias.COMMAND.getAliasValue()),
getDittoHeadersWithCorrelationId(),
ACTOR_SYSTEM_RESOURCE.getActorSystem(),
CONNECTIVITY_CONFIG)
).doesNotThrowAnyException();
}
@Test
public void validateWithEmptyTargetAddressThrowsException() {
final var dittoHeaders = getDittoHeadersWithCorrelationId();
assertThatExceptionOfType(ConnectionConfigurationInvalidException.class)
.isThrownBy(() -> underTest.validate(getConnectionWithTargetAddress(""),
dittoHeaders,
ACTOR_SYSTEM_RESOURCE.getActorSystem(),
CONNECTIVITY_CONFIG))
.withMessage("The provided target address must not be empty.")
.withNoCause()
.satisfies(exception -> assertThat(exception.getDittoHeaders()).isEqualTo(dittoHeaders));
}
@Test
public void validateWithInvalidTargetAddressesThrowsException() {
final var dittoHeaders = getDittoHeadersWithCorrelationId();
Stream.concat(
Stream.of(HonoAddressAlias.values())
.filter(honoAddressAlias -> HonoAddressAlias.COMMAND != honoAddressAlias)
.map(HonoAddressAlias::getAliasValue),
Stream.of("hono.command.c4bc9a62-8516-4232-bb81-dbbfe4d0fa8c_hub/{{thing:id}}")
).forEach(
invalidTargetAddress -> softly.assertThatThrownBy(
() -> underTest.validate(getConnectionWithTargetAddress(invalidTargetAddress),
dittoHeaders,
ACTOR_SYSTEM_RESOURCE.getActorSystem(),
CONNECTIVITY_CONFIG)
)
.as(invalidTargetAddress)
.hasMessage("The provided target address <%s> is invalid. It should be <%s>.",
invalidTargetAddress,
HonoAddressAlias.COMMAND.getAliasValue())
.hasNoCause()
.isInstanceOfSatisfying(ConnectionConfigurationInvalidException.class,
exception -> assertThat(exception.getDittoHeaders()).isEqualTo(dittoHeaders))
);
}
private static Connection getConnectionWithSourceEnforcement(final Enforcement sourceEnforcement) {
return ConnectivityModelFactory.newConnectionBuilder(CONNECTION_ID,
ConnectionType.HONO,
ConnectivityStatus.OPEN,
"tcp://localhost:99999")
.sources(List.of(ConnectivityModelFactory.newSourceBuilder()
.address(HonoAddressAlias.TELEMETRY.getAliasValue())
.authorizationContext(AUTHORIZATION_CONTEXT)
.enforcement(sourceEnforcement)
.qos(1)
.build()))
.build();
}
private static Connection getConnectionWithSourceAddress(final String sourceAddress) {
return ConnectivityModelFactory.newConnectionBuilder(CONNECTION_ID,
ConnectionType.HONO,
ConnectivityStatus.OPEN,
"tcp://localhost:99999")
.sources(List.of(ConnectivityModelFactory.newSourceBuilder()
.address(sourceAddress)
.authorizationContext(AUTHORIZATION_CONTEXT)
.qos(1)
.build()))
.build();
}
private static Connection getConnectionWithTargetAddress(final String targetAddress) {
return ConnectivityModelFactory.newConnectionBuilder(CONNECTION_ID,
ConnectionType.HONO,
ConnectivityStatus.OPEN,
"tcp://localhost:1883")
.targets(singletonList(ConnectivityModelFactory.newTargetBuilder()
.address(targetAddress)
.authorizationContext(AUTHORIZATION_CONTEXT)
.qos(1)
.topics(Topic.LIVE_EVENTS)
.build()))
.build();
}
private DittoHeaders getDittoHeadersWithCorrelationId() {
return DittoHeaders.newBuilder().correlationId(testNameCorrelationId.getCorrelationId()).build();
}
}