-
Notifications
You must be signed in to change notification settings - Fork 214
/
RabbitMQConsumerActorTest.java
165 lines (146 loc) · 7.59 KB
/
RabbitMQConsumerActorTest.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
/*
* 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.assertj.core.api.Assertions.fail;
import static org.eclipse.ditto.connectivity.service.messaging.TestConstants.header;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.eclipse.ditto.base.model.acks.AcknowledgementRequest;
import org.eclipse.ditto.base.model.acks.FilteredAcknowledgementRequest;
import org.eclipse.ditto.base.model.common.DittoConstants;
import org.eclipse.ditto.base.model.common.ResponseType;
import org.eclipse.ditto.connectivity.model.Connection;
import org.eclipse.ditto.connectivity.model.ConnectivityModelFactory;
import org.eclipse.ditto.connectivity.model.PayloadMapping;
import org.eclipse.ditto.connectivity.model.ReplyTarget;
import org.eclipse.ditto.connectivity.service.config.ConnectivityConfig;
import org.eclipse.ditto.connectivity.service.messaging.AbstractConsumerActorWithAcknowledgementsTest;
import org.eclipse.ditto.connectivity.service.messaging.ConnectivityStatusResolver;
import org.eclipse.ditto.connectivity.service.messaging.TestConstants;
import org.junit.Test;
import org.mockito.Mockito;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Delivery;
import com.rabbitmq.client.Envelope;
import org.apache.pekko.Done;
import org.apache.pekko.NotUsed;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.Props;
import org.apache.pekko.stream.javadsl.Sink;
import org.apache.pekko.testkit.TestProbe;
import org.apache.pekko.testkit.javadsl.TestKit;
/**
* Unit test for {@link RabbitMQConsumerActor}.
*/
public final class RabbitMQConsumerActorTest extends AbstractConsumerActorWithAcknowledgementsTest<Delivery> {
private static final Connection CONNECTION = TestConstants.createConnection();
private static final Envelope ENVELOPE = new Envelope(1, false, "inbound", "ditto");
private final Channel channel = Mockito.mock(Channel.class);
@Test
public void stopConsumingOnRequest() throws Exception {
new TestKit(actorSystem) {{
final TestProbe proxyActor = TestProbe.apply(actorSystem);
final TestProbe clientActor = TestProbe.apply(actorSystem);
final Sink<Object, NotUsed> inboundMappingSink =
setupInboundMappingSink(clientActor.ref(), proxyActor.ref());
final var payloadMapping = ConnectivityModelFactory.newPayloadMapping("ditto", "ditto");
final ActorRef underTest = actorSystem.actorOf(getConsumerActorProps(inboundMappingSink, payloadMapping));
underTest.tell(RabbitMQConsumerActor.Control.STOP_CONSUMER, getRef());
expectMsg(Done.getInstance());
verify(channel).close();
}};
}
@Override
protected Props getConsumerActorProps(final Sink<Object, NotUsed> inboundMappingSink,
final Set<AcknowledgementRequest> acknowledgementRequests) {
return RabbitMQConsumerActor.props("rmq-consumer", inboundMappingSink,
ConnectivityModelFactory.newSourceBuilder()
.address("rmq-consumer")
.authorizationContext(TestConstants.Authorization.AUTHORIZATION_CONTEXT)
.enforcement(ENFORCEMENT)
.headerMapping(TestConstants.HEADER_MAPPING)
.acknowledgementRequests(FilteredAcknowledgementRequest.of(acknowledgementRequests, null))
.replyTarget(ReplyTarget.newBuilder()
.address("foo")
.expectedResponseTypes(ResponseType.ERROR, ResponseType.RESPONSE, ResponseType.NACK)
.build())
.build(),
channel,
CONNECTION,
mock(ConnectivityStatusResolver.class),
ConnectivityConfig.of(actorSystem.settings().config()));
}
@Override
protected Props getConsumerActorProps(final Sink<Object, NotUsed> inboundMappingSink,
final PayloadMapping payloadMapping) {
return RabbitMQConsumerActor.props("rmq-consumer", inboundMappingSink,
ConnectivityModelFactory.newSourceBuilder()
.address("rmq-consumer")
.authorizationContext(TestConstants.Authorization.AUTHORIZATION_CONTEXT)
.enforcement(ENFORCEMENT)
.headerMapping(TestConstants.HEADER_MAPPING)
.payloadMapping(payloadMapping)
.replyTarget(ReplyTarget.newBuilder()
.address("foo")
.expectedResponseTypes(ResponseType.ERROR, ResponseType.RESPONSE, ResponseType.NACK)
.build())
.build(),
channel,
CONNECTION,
mock(ConnectivityStatusResolver.class),
ConnectivityConfig.of(actorSystem.settings().config()));
}
@Override
protected Delivery getInboundMessage(final String payload, final Map.Entry<String, Object> header) {
final Map<String, Object> headers = new HashMap<>();
headers.put(REPLY_TO_HEADER.getKey(), REPLY_TO_HEADER.getValue());
headers.put(header.getKey(), header.getValue());
return new Delivery(ENVELOPE,
new AMQP.BasicProperties.Builder()
.contentType(DittoConstants.DITTO_PROTOCOL_CONTENT_TYPE)
.headers(headers)
.replyTo(REPLY_TO_HEADER.getValue()).build(),
payload.getBytes(StandardCharsets.UTF_8));
}
@Override
protected void verifyMessageSettlement(final TestKit testKit, final boolean isSuccessExpected,
final boolean shouldRedeliver)
throws Exception {
if (isSuccessExpected) {
Mockito.verify(channel, Mockito.timeout(3000L)).basicAck(anyLong(), eq(false));
} else {
// expect no redelivery due to DittoRuntimeException
Mockito.verify(channel, Mockito.timeout(3000L)).basicNack(anyLong(), eq(false), eq(shouldRedeliver));
}
}
@Override
protected void testHeaderMapping() {
testInboundMessage(header("device_id", TestConstants.Things.THING_ID), true, msg -> {
assertThat(msg.getDittoHeaders()).containsEntry("eclipse", "ditto");
assertThat(msg.getDittoHeaders()).containsEntry("thing_id", TestConstants.Things.THING_ID.toString());
assertThat(msg.getDittoHeaders()).containsEntry("device_id", TestConstants.Things.THING_ID.toString());
assertThat(msg.getDittoHeaders()).containsEntry("prefixed_thing_id",
"some.prefix." + TestConstants.Things.THING_ID);
assertThat(msg.getDittoHeaders()).containsEntry("suffixed_thing_id",
TestConstants.Things.THING_ID + ".some.suffix");
}, response -> fail("not expected"));
}
}