-
Notifications
You must be signed in to change notification settings - Fork 215
/
RabbitMQPublisherActorTest.java
232 lines (194 loc) · 10.3 KB
/
RabbitMQPublisherActorTest.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
/*
* 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.connectivity.service.messaging.rabbitmq;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.signals.acks.Acknowledgement;
import org.eclipse.ditto.base.model.signals.acks.Acknowledgements;
import org.eclipse.ditto.connectivity.api.OutboundSignal;
import org.eclipse.ditto.connectivity.api.OutboundSignalFactory;
import org.eclipse.ditto.connectivity.model.Target;
import org.eclipse.ditto.connectivity.service.config.ConnectivityConfig;
import org.eclipse.ditto.connectivity.service.messaging.AbstractPublisherActorTest;
import org.eclipse.ditto.connectivity.service.messaging.ConnectivityStatusResolver;
import org.eclipse.ditto.connectivity.service.messaging.TestConstants;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import com.newmotion.akka.rabbitmq.ChannelCreated;
import com.newmotion.akka.rabbitmq.ChannelMessage;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.japi.Pair;
import akka.testkit.TestProbe;
import akka.testkit.javadsl.TestKit;
public class RabbitMQPublisherActorTest extends AbstractPublisherActorTest {
private TestProbe probe;
@Test
public void testMultipleAcknowledgements() throws Exception {
new TestKit(actorSystem) {{
// GIVEN: there is a multi-mapped message with 6 different acknowledgements
final TestProbe probe = new TestProbe(actorSystem);
setupMocks(probe);
final int signalCount = 6;
final OutboundSignal.MultiMapped multiMapped =
OutboundSignalFactory.newMultiMappedOutboundSignal(List.of(
getMockOutboundSignalWithAutoAck("rabbit1"),
getMockOutboundSignalWithAutoAck("rabbit2"),
getMockOutboundSignalWithAutoAck("rabbit3"),
getMockOutboundSignalWithAutoAck("rabbit4"),
getMockOutboundSignalWithAutoAck("rabbit5"),
getMockOutboundSignalWithAutoAck("rabbit6")
), getRef());
final Props props = getPublisherActorProps();
final ActorRef publisherActor = childActorOf(props);
publisherCreated(this, publisherActor);
// WHEN: publisher actor is created
final Pair<Channel, ConfirmListener> pair = setUpPublishConfirmMode();
final Channel channel = pair.first();
final ConfirmListener confirmListener = pair.second();
// WHEN: publisher actor is told to publish a multi-mapped message with 6 different acks
publisherActor.tell(multiMapped, getRef());
// THEN: publisher actor should tell channel to publish 6 messages with incrementing sequence numbers
for (int i = 0; i < signalCount; ++i) {
probe.expectMsgClass(ChannelMessage.class).onChannel().apply(channel);
}
// WHEN: broker acknowledges messages 1-3 positively and 4-6 negatively
confirmListener.handleNack(6, false);
confirmListener.handleNack(4, false);
confirmListener.handleNack(5, false);
confirmListener.handleAck(3, true);
// THEN: publisher actor sends an aggregated acks message to the original sender containing each status
final Acknowledgements acks = expectMsgClass(Acknowledgements.class);
for (final Acknowledgement ack : acks.getSuccessfulAcknowledgements()) {
assertThat(ack.getLabel().toString()).isBetween("rabbit1", "rabbit3");
assertThat(ack.getHttpStatus()).isEqualTo(HttpStatus.OK);
}
for (final Acknowledgement ack : acks.getFailedAcknowledgements()) {
assertThat(ack.getLabel().toString()).isBetween("rabbit4", "rabbit6");
assertThat(ack.getHttpStatus()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE);
}
}};
}
@Override
protected void setupMocks(final TestProbe probe) {
this.probe = probe;
}
@Override
protected void publisherCreated(final TestKit kit, final ActorRef publisherActor) {
final ChannelCreated channelCreated = ChannelCreated.apply(probe.ref());
publisherActor.tell(channelCreated, ActorRef.noSender());
}
@Override
protected Props getPublisherActorProps() {
return RabbitMQPublisherActor.props(TestConstants.createConnection(),
mock(ConnectivityStatusResolver.class),
ConnectivityConfig.of(actorSystem.settings().config()));
}
@Override
protected void verifyAcknowledgements(final Supplier<Acknowledgements> ackSupplier) throws Exception {
final Pair<Channel, ConfirmListener> channelAndListener = setUpPublishConfirmMode();
final CompletableFuture<Acknowledgements> acksFuture = CompletableFuture.supplyAsync(ackSupplier);
probe.expectMsgClass(ChannelMessage.class).onChannel().apply(channelAndListener.first());
channelAndListener.second().handleAck(1L, false);
final Acknowledgements acks = acksFuture.join();
assertThat(acks.getSize()).describedAs("Expect 1 acknowledgement in: " + acks).isEqualTo(1);
final Acknowledgement ack = acks.stream().findAny().orElseThrow();
assertThat(ack.getLabel().toString()).describedAs("Ack label").isEqualTo("please-verify");
assertThat(ack.getHttpStatus()).describedAs("Ack status").isEqualTo(HttpStatus.OK);
}
@Override
protected void verifyPublishedMessage() throws Exception {
// set up publish confirm with first channel message
final Channel channel = setUpPublishConfirmMode().first();
final ChannelMessage channelMessage = probe.expectMsgClass(ChannelMessage.class);
channelMessage.onChannel().apply(channel);
final ArgumentCaptor<AMQP.BasicProperties> propertiesCaptor =
ArgumentCaptor.forClass(AMQP.BasicProperties.class);
final ArgumentCaptor<byte[]> bodyCaptor = ArgumentCaptor.forClass(byte[].class);
verify(channel, timeout(1000)).basicPublish(eq("exchange"), eq("outbound"), eq(true),
propertiesCaptor.capture(), bodyCaptor.capture());
assertThat(propertiesCaptor.getValue().getHeaders().get("thing_id"))
.isEqualTo(TestConstants.Things.THING_ID.toString());
assertThat(propertiesCaptor.getValue().getHeaders().get("suffixed_thing_id")).isEqualTo(
TestConstants.Things.THING_ID + ".some.suffix");
assertThat(propertiesCaptor.getValue().getHeaders().get("prefixed_thing_id")).isEqualTo(
"some.prefix." + TestConstants.Things.THING_ID);
assertThat(propertiesCaptor.getValue().getHeaders().get("eclipse")).isEqualTo("ditto");
assertThat(propertiesCaptor.getValue().getHeaders().get("device_id"))
.isEqualTo(TestConstants.Things.THING_ID.toString());
}
@Override
protected void verifyPublishedMessageToReplyTarget() throws Exception {
final Channel channel = mock(Channel.class);
// omit first channel message
probe.expectMsgClass(ChannelMessage.class);
final ChannelMessage channelMessage = probe.expectMsgClass(ChannelMessage.class);
channelMessage.onChannel().apply(channel);
final ArgumentCaptor<AMQP.BasicProperties> propertiesCaptor =
ArgumentCaptor.forClass(AMQP.BasicProperties.class);
final ArgumentCaptor<byte[]> bodyCaptor = ArgumentCaptor.forClass(byte[].class);
verify(channel, timeout(1000)).basicPublish(
eq("replyTarget"),
eq("thing:id"),
eq(true),
propertiesCaptor.capture(),
bodyCaptor.capture());
assertThat(propertiesCaptor.getValue().getHeaders().get("correlation-id")).isEqualTo(
TestConstants.CORRELATION_ID);
assertThat(propertiesCaptor.getValue().getHeaders().get("mappedHeader2")).isEqualTo("thing:id");
}
protected String getOutboundAddress() {
return "exchange/outbound";
}
@Override
protected Target decorateTarget(final Target target) {
return target;
}
private Pair<Channel, ConfirmListener> setUpPublishConfirmMode() {
try {
final Channel channel = mock(Channel.class);
final AtomicLong nextPublishSeqNo = new AtomicLong(0L);
final AtomicReference<ConfirmListener> confirmListenerBox = new AtomicReference<>();
when(channel.getNextPublishSeqNo()).thenAnswer(args -> nextPublishSeqNo.incrementAndGet());
doAnswer(arg -> {
confirmListenerBox.set(arg.getArgument(0));
return null;
}).when(channel).addConfirmListener(any());
final ChannelMessage channelMessage = probe.expectMsgClass(ChannelMessage.class);
channelMessage.onChannel().apply(channel);
verify(channel).confirmSelect();
assertThat(confirmListenerBox)
.describedAs("Publisher actor should set up confirm mode")
.doesNotHaveValue(null);
return Pair.create(channel, confirmListenerBox.get());
} catch (final IOException e) {
throw new AssertionError(e);
}
}
}