/
RabbitMQClientActorTest.java
266 lines (222 loc) · 10.8 KB
/
RabbitMQClientActorTest.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
/*
* Copyright (c) 2017 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.services.connectivity.messaging.rabbitmq;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Stream;
import org.assertj.core.api.ThrowableAssert;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.connectivity.Connection;
import org.eclipse.ditto.model.connectivity.ConnectionConfigurationInvalidException;
import org.eclipse.ditto.model.connectivity.ConnectionId;
import org.eclipse.ditto.model.connectivity.ConnectionType;
import org.eclipse.ditto.model.connectivity.ConnectivityModelFactory;
import org.eclipse.ditto.model.connectivity.ConnectivityStatus;
import org.eclipse.ditto.model.connectivity.Target;
import org.eclipse.ditto.model.connectivity.Topic;
import org.eclipse.ditto.services.connectivity.messaging.AbstractBaseClientActorTest;
import org.eclipse.ditto.services.connectivity.messaging.BaseClientState;
import org.eclipse.ditto.services.connectivity.messaging.TestConstants;
import org.eclipse.ditto.signals.commands.connectivity.modify.CloseConnection;
import org.eclipse.ditto.signals.commands.connectivity.modify.OpenConnection;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.Status;
import akka.testkit.CallingThreadDispatcher;
import akka.testkit.javadsl.TestKit;
@RunWith(MockitoJUnitRunner.class)
public final class RabbitMQClientActorTest extends AbstractBaseClientActorTest {
private static final Status.Success CONNECTED_SUCCESS = new Status.Success(BaseClientState.CONNECTED);
private static final Status.Success DISCONNECTED_SUCCESS = new Status.Success(BaseClientState.DISCONNECTED);
private static final IllegalArgumentException CUSTOM_EXCEPTION =
new IllegalArgumentException("custom error message");
private static final ConnectionId CONNECTION_ID = TestConstants.createRandomConnectionId();
private static final ConnectivityStatus CONNECTION_STATUS = ConnectivityStatus.OPEN;
@SuppressWarnings("NullableProblems") private static ActorSystem actorSystem;
private static Connection connection;
@Mock
private final ConnectionFactory mockConnectionFactory = Mockito.mock(ConnectionFactory.class);
private final RabbitConnectionFactoryFactory
rabbitConnectionFactoryFactory = (con, exHandler) -> mockConnectionFactory;
@Mock
private final com.rabbitmq.client.Connection mockConnection = Mockito.mock(com.rabbitmq.client.Connection.class);
@Mock
private final Channel mockChannel = Mockito.mock(Channel.class);
@BeforeClass
public static void setUp() {
actorSystem = ActorSystem.create("AkkaTestSystem", TestConstants.CONFIG);
connection = TestConstants.createConnection(CONNECTION_ID)
.toBuilder()
.connectionStatus(ConnectivityStatus.CLOSED)
.build();
}
@AfterClass
public static void tearDown() {
TestKit.shutdownActorSystem(actorSystem, scala.concurrent.duration.Duration.apply(5, TimeUnit.SECONDS), false);
}
@Before
public void init() throws IOException, TimeoutException {
when(mockConnectionFactory.newConnection()).thenReturn(mockConnection);
when(mockConnection.createChannel()).thenReturn(mockChannel);
}
@Test
public void invalidTargetFormatThrowsConnectionConfigurationInvalidException() {
final Connection connection =
ConnectivityModelFactory.newConnectionBuilder(CONNECTION_ID, ConnectionType.AMQP_091,
ConnectivityStatus.OPEN, TestConstants.getUriOfNewMockServer())
.targets(Collections.singletonList(ConnectivityModelFactory.newTarget("exchangeOnly",
TestConstants.Authorization.AUTHORIZATION_CONTEXT, null, null, Topic.TWIN_EVENTS)))
.build();
final ThrowableAssert.ThrowingCallable props1 =
() -> RabbitMQClientActor.propsForTests(connection,
null, null);
final ThrowableAssert.ThrowingCallable props2 =
() -> RabbitMQClientActor.propsForTests(connection,
null, rabbitConnectionFactoryFactory);
Stream.of(props1, props2)
.forEach(throwingCallable ->
assertThatExceptionOfType(ConnectionConfigurationInvalidException.class)
.isThrownBy(throwingCallable)
.withMessageContaining("exchangeOnly")
.withNoCause()
);
}
@Test
public void testExceptionDuringConnectionFactoryCreation() {
new TestKit(actorSystem) {{
final Props props =
RabbitMQClientActor.propsForTests(connection,
getRef(), (con, exHandler) -> { throw CUSTOM_EXCEPTION; })
.withDispatcher(CallingThreadDispatcher.Id());
final ActorRef connectionActor = actorSystem.actorOf(props);
connectionActor.tell(OpenConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsgClass(Status.Failure.class);
}};
}
@Test
public void testConnectionHandling() {
new TestKit(actorSystem) {{
final Props props = createClientActor(getRef());
final ActorRef rabbitClientActor = actorSystem.actorOf(props);
watch(rabbitClientActor);
rabbitClientActor.tell(OpenConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(CONNECTED_SUCCESS);
rabbitClientActor.tell(CloseConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(DISCONNECTED_SUCCESS);
}};
}
@Test
public void testConnectionWithoutPublisherHandling() {
new TestKit(actorSystem) {{
final ConnectionId randomConnectionId = TestConstants.createRandomConnectionId();
final Connection connectionWithoutTargets =
TestConstants.createConnection(randomConnectionId, new Target[0]);
final Props props =
RabbitMQClientActor.propsForTests(connectionWithoutTargets, getRef(),
(con, exHandler) -> mockConnectionFactory).withDispatcher(CallingThreadDispatcher.Id());
final ActorRef rabbitClientActor = actorSystem.actorOf(props);
watch(rabbitClientActor);
rabbitClientActor.tell(OpenConnection.of(randomConnectionId, DittoHeaders.empty()), getRef());
expectMsg(CONNECTED_SUCCESS);
rabbitClientActor.tell(CloseConnection.of(randomConnectionId, DittoHeaders.empty()), getRef());
expectMsg(DISCONNECTED_SUCCESS);
}};
}
@Test
public void testReconnection() {
new TestKit(actorSystem) {{
final Props props = createClientActor(getRef());
final ActorRef rabbitClientActor = actorSystem.actorOf(props);
// reconnect a few times
for (int i = 0; i < 3; ++i) {
rabbitClientActor.tell(OpenConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(CONNECTED_SUCCESS);
rabbitClientActor.tell(CloseConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(DISCONNECTED_SUCCESS);
}
}};
}
@Test
public void sendCommandDuringInit() {
new TestKit(actorSystem) {{
final Props props = createClientActor(getRef());
final ActorRef rabbitClientActor = actorSystem.actorOf(props);
watch(rabbitClientActor);
rabbitClientActor.tell(OpenConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(CONNECTED_SUCCESS);
}};
}
@Test
public void sendConnectCommandWhenAlreadyConnected() throws IOException {
new TestKit(actorSystem) {{
final Props props = createClientActor(getRef());
final ActorRef rabbitClientActor = actorSystem.actorOf(props);
rabbitClientActor.tell(OpenConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(CONNECTED_SUCCESS);
rabbitClientActor.tell(OpenConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(CONNECTED_SUCCESS);
// a publisher and a consumer channel should be created
verify(mockConnection, Mockito.times(2)).createChannel();
}};
}
@Test
public void sendDisconnectWhenAlreadyDisconnected() {
new TestKit(actorSystem) {{
final Props props = createClientActor(getRef());
final ActorRef rabbitClientActor = actorSystem.actorOf(props);
rabbitClientActor.tell(CloseConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(DISCONNECTED_SUCCESS);
Mockito.verifyZeroInteractions(mockConnection);
}};
}
@Test
public void testCloseConnectionFails() {
new TestKit(actorSystem) {{
final Props props = createClientActor(getRef());
final ActorRef rabbitClientActor = actorSystem.actorOf(props);
rabbitClientActor.tell(OpenConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(CONNECTED_SUCCESS);
rabbitClientActor.tell(CloseConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(DISCONNECTED_SUCCESS);
}};
}
@Override
protected Connection getConnection() {
return connection;
}
@Override
protected Props createClientActor(final ActorRef conciergeForwarder) {
return RabbitMQClientActor.propsForTests(getConnection(), conciergeForwarder,
(con, exHandler) -> mockConnectionFactory).withDispatcher(CallingThreadDispatcher.Id());
}
@Override
protected ActorSystem getActorSystem() {
return actorSystem;
}
}