-
Notifications
You must be signed in to change notification settings - Fork 214
/
HttpPushValidatorTest.java
188 lines (164 loc) · 8.67 KB
/
HttpPushValidatorTest.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
/*
* Copyright (c) 2019 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.httppush;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.eclipse.ditto.connectivity.service.messaging.TestConstants.Authorization.AUTHORIZATION_CONTEXT;
import static org.eclipse.ditto.connectivity.service.messaging.httppush.HttpPushSpecificConfig.OMIT_REQUEST_BODY;
import static org.mutabilitydetector.unittesting.AllowedReason.provided;
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf;
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
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.Source;
import org.eclipse.ditto.connectivity.model.Topic;
import org.eclipse.ditto.connectivity.service.config.ConnectivityConfig;
import org.eclipse.ditto.connectivity.service.config.HttpPushConfig;
import org.eclipse.ditto.connectivity.service.messaging.TestConstants;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.typesafe.config.ConfigFactory;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.testkit.javadsl.TestKit;
/**
* Unit test for {@link HttpPushValidator}.
*/
public final class HttpPushValidatorTest {
private static final ConnectionId CONNECTION_ID = TestConstants.createRandomConnectionId();
private static Map<String, String> defaultSpecificConfig = new HashMap<>();
private static ActorSystem actorSystem;
private static ConnectivityConfig connectivityConfig;
private HttpPushValidator underTest;
@BeforeClass
public static void initTestFixture() {
defaultSpecificConfig = new HashMap<>();
defaultSpecificConfig.put("parallelism", "1");
actorSystem = ActorSystem.create("PekkoTestSystem", TestConstants.CONFIG);
connectivityConfig = TestConstants.CONNECTIVITY_CONFIG;
}
@AfterClass
public static void tearDown() {
if (actorSystem != null) {
TestKit.shutdownActorSystem(actorSystem, scala.concurrent.duration.Duration.apply(5, TimeUnit.SECONDS),
false);
}
}
@Before
public void setUp() {
underTest = HttpPushValidator.newInstance(HttpPushConfig.of(ConfigFactory.empty()));
}
@Test
public void testImmutability() {
assertInstancesOf(HttpPushValidator.class, areImmutable(), provided(HttpPushConfig.class).isAlsoImmutable());
}
@Test
public void testSourcesAreInvalid() {
final Source source = ConnectivityModelFactory.newSource(AUTHORIZATION_CONTEXT, "any");
assertThatExceptionOfType(ConnectionConfigurationInvalidException.class)
.isThrownBy(() -> underTest.validateSource(source, DittoHeaders.empty(), () -> ""));
}
@Test
public void testValidTargetAddress() {
final DittoHeaders emptyDittoHeaders = DittoHeaders.empty();
underTest.validate(getConnectionWithTarget("POST:events"), emptyDittoHeaders, actorSystem, connectivityConfig);
underTest.validate(getConnectionWithTarget("PUT:ditto/{{thing:id}}"), emptyDittoHeaders, actorSystem,
connectivityConfig);
underTest.validate(getConnectionWithTarget("PUT:ditto/{{entity:id}}"), emptyDittoHeaders, actorSystem,
connectivityConfig);
underTest.validate(getConnectionWithTarget("DELETE:ditto/{{entity:id}}"), emptyDittoHeaders, actorSystem,
connectivityConfig);
underTest.validate(getConnectionWithTarget("PATCH:/{{thing:namespace}}/{{thing:name}}"), emptyDittoHeaders,
actorSystem, connectivityConfig);
underTest.validate(getConnectionWithTarget("PATCH:/{{thing:namespace}}/{{thing:name}}/{{ feature:id }}"),
emptyDittoHeaders,
actorSystem, connectivityConfig);
underTest.validate(getConnectionWithTarget("PUT:events#{{topic:full}}"), emptyDittoHeaders, actorSystem,
connectivityConfig);
underTest.validate(getConnectionWithTarget("POST:ditto?{{header:x}}"), emptyDittoHeaders, actorSystem,
connectivityConfig);
underTest.validate(getConnectionWithTarget("POST:"), emptyDittoHeaders, actorSystem, connectivityConfig);
underTest.validate(getConnectionWithTarget("GET:foo"), emptyDittoHeaders, actorSystem, connectivityConfig);
underTest.validate(getConnectionWithTarget("POST:connection/{{connection:id}}"), emptyDittoHeaders, actorSystem,
connectivityConfig);
}
@Test
public void testInvalidTargetAddress() {
verifyConnectionConfigurationInvalidExceptionIsThrown(getConnectionWithTarget(""),
"Target address has invalid format.");
verifyConnectionConfigurationInvalidExceptionIsThrown(getConnectionWithTarget("events"),
"Target address has invalid format.");
verifyConnectionConfigurationInvalidExceptionIsThrown(getConnectionWithTarget("OPTIONS:/bar"),
"The method 'OPTIONS' is not supported");
}
@Test
public void testInvalidOmitBodyHttpMethod() {
final Connection connection = getConnectionWithTarget("POST:events").toBuilder()
.specificConfig(Map.of(OMIT_REQUEST_BODY, "GET,DELET,POST"))
.build();
verifyConnectionConfigurationInvalidExceptionIsThrown(connection, "It contains an invalid HTTP method");
}
@Test
public void testInvalidIdleTime() {
final var idleTimeout = "-3s";
final Connection connection = getConnectionWithTarget("POST:events").toBuilder()
.specificConfig(Map.of(HttpPushSpecificConfig.IDLE_TIMEOUT, idleTimeout))
.build();
verifyConnectionConfigurationInvalidExceptionIsThrown(connection,
"Idle timeout '" + idleTimeout.substring(0, idleTimeout.length() - 1) +
"' is not within the allowed range of [0, " + HttpPushValidator.MAX_IDLE_TIMEOUT.toSeconds() +
"] seconds.");
}
@Test
public void testNullOmitBodyHttpMethods() {
final Connection connection = getConnectionWithTarget("POST:events").toBuilder().build();
underTest.validate(connection, DittoHeaders.empty(), actorSystem, connectivityConfig);
}
@Test
public void testEmptyOmitBodyHttpMethods() {
final Connection connection = getConnectionWithTarget("POST:events").toBuilder()
.specificConfig(Map.of(OMIT_REQUEST_BODY, ""))
.build();
underTest.validate(connection, DittoHeaders.empty(), actorSystem, connectivityConfig);
}
private static Connection getConnectionWithTarget(final String target) {
return getConnectionWithHostAndTarget("8.8.4.4", target);
}
private static Connection getConnectionWithHostAndTarget(final String host, final String target) {
return ConnectivityModelFactory.newConnectionBuilder(CONNECTION_ID, ConnectionType.HTTP_PUSH,
ConnectivityStatus.OPEN, "http://" + host + ":80")
.targets(singletonList(ConnectivityModelFactory.newTargetBuilder()
.address(target)
.authorizationContext(AUTHORIZATION_CONTEXT)
.qos(1)
.topics(Topic.LIVE_EVENTS)
.build()))
.specificConfig(defaultSpecificConfig)
.build();
}
private void verifyConnectionConfigurationInvalidExceptionIsThrown(final Connection connection,
final String errorMessage) {
assertThatExceptionOfType(ConnectionConfigurationInvalidException.class)
.isThrownBy(() -> underTest.validate(connection, DittoHeaders.empty(), actorSystem, connectivityConfig))
.withMessageContaining(errorMessage);
}
}