/
CommandConsumerActorTest.java
210 lines (186 loc) · 10.3 KB
/
CommandConsumerActorTest.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
/*
* Copyright (c) 2017 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* Contributors:
* Bosch Software Innovations GmbH - initial contribution
*/
package org.eclipse.ditto.services.connectivity.messaging.amqp;
import static org.eclipse.ditto.json.assertions.DittoJsonAssertions.assertThat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import javax.jms.MessageConsumer;
import org.apache.qpid.jms.exceptions.IdConversionException;
import org.apache.qpid.jms.message.JmsMessage;
import org.apache.qpid.jms.provider.amqp.message.AmqpJmsTextMessageFacade;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.model.connectivity.ConnectivityModelFactory;
import org.eclipse.ditto.model.connectivity.ExternalMessage;
import org.eclipse.ditto.model.connectivity.MappingContext;
import org.eclipse.ditto.model.base.auth.AuthorizationContext;
import org.eclipse.ditto.model.base.auth.AuthorizationSubject;
import org.eclipse.ditto.services.connectivity.mapping.MessageMappers;
import org.eclipse.ditto.services.connectivity.messaging.MessageMappingProcessorActor;
import org.eclipse.ditto.signals.commands.base.Command;
import org.eclipse.ditto.signals.commands.things.modify.ModifyAttribute;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.cluster.pubsub.DistributedPubSub;
import akka.cluster.pubsub.DistributedPubSubMediator;
import akka.routing.DefaultResizer;
import akka.routing.RoundRobinPool;
import akka.testkit.javadsl.TestKit;
/**
* Tests the AMQP {@link AmqpConsumerActor}.
*/
public class CommandConsumerActorTest {
private static final Config CONFIG = ConfigFactory.load("test");
private static ActorSystem actorSystem;
private static ActorRef pubSubMediator;
@BeforeClass
public static void setUp() {
actorSystem = ActorSystem.create("AkkaTestSystem", CONFIG);
pubSubMediator = DistributedPubSub.get(actorSystem).mediator();
}
@AfterClass
public static void tearDown() {
if (actorSystem != null) {
TestKit.shutdownActorSystem(actorSystem, scala.concurrent.duration.Duration.apply(5, TimeUnit.SECONDS),
false);
}
}
@Test
public void plainStringMappingTest() throws IdConversionException {
new TestKit(actorSystem) {{
final String targetActorPath = getTestActor().path().toStringWithoutAddress();
pubSubMediator.tell(new DistributedPubSubMediator.Put(getTestActor()), null);
final List<MappingContext> mappingContexts = new ArrayList<>();
mappingContexts.add(ConnectivityModelFactory.newMappingContext(
"text/plain",
"JavaScript",
MessageMappers.createJavaScriptMapperConfigurationBuilder()
.incomingMappingScript("function mapToDittoProtocolMsg(\n" +
" headers,\n" +
" textPayload,\n" +
" bytePayload,\n" +
" contentType\n" +
") {\n" +
"\n" +
" // ###\n" +
" // Insert your mapping logic here\n" +
" let namespace = \"org.eclipse.ditto\";\n" +
" let id = \"foo-bar\";\n" +
" let group = \"things\";\n" +
" let channel = \"twin\";\n" +
" let criterion = \"commands\";\n" +
" let action = \"modify\";\n" +
" let path = \"/attributes/foo\";\n" +
" let dittoHeaders = headers;\n" +
" let value = textPayload;\n" +
" // ###\n" +
"\n" +
" return buildDittoProtocolMsg(\n" +
" namespace,\n" +
" id,\n" +
" group,\n" +
" channel,\n" +
" criterion,\n" +
" action,\n" +
" path,\n" +
" dittoHeaders,\n" +
" value\n" +
" );\n" +
"}")
.outgoingMappingScript("function mapFromDittoProtocolMsg(\n" +
" namespace,\n" +
" id,\n" +
" group,\n" +
" channel,\n" +
" criterion,\n" +
" action,\n" +
" path,\n" +
" dittoHeaders,\n" +
" value\n" +
") {\n" +
"\n" +
" let externalMsg = {};\n" +
"\n" +
" // ###\n" +
" // Insert your mapping logic here\n" +
" let headers = {};\n" +
" headers['correlation-id'] = dittoHeaders['correlation-id'];\n" +
" let textPayload = \"Topic was: \" + namespace + \":\" + id;\n" +
" let contentType = \"text/plain\";\n" +
" // ###\n" +
"\n" +
" return buildExternalMsg(\n" +
" headers,\n" +
" textPayload,\n" +
" bytePayload,\n" +
" contentType\n" +
" );" +
"}")
.build()
.getProperties()
));
final Props messageMappingProcessorProps =
MessageMappingProcessorActor.props(pubSubMediator, targetActorPath, getRef(),
AuthorizationContext.newInstance(AuthorizationSubject.newInstance("foo:bar")),
mappingContexts);
final String messageMappingProcessorName = MessageMappingProcessorActor.ACTOR_NAME_PREFIX + "foo";
final ActorRef processor = actorSystem.actorOf(messageMappingProcessorProps, messageMappingProcessorName);
final ActorRef underTest = actorSystem.actorOf(
AmqpConsumerActor.props("foo", Mockito.mock(MessageConsumer.class), processor));
final String plainPayload = "hello world!";
final String correlationId = "cor-";
final AmqpJmsTextMessageFacade messageFacade = new AmqpJmsTextMessageFacade();
messageFacade.setText(plainPayload);
messageFacade.setContentType("text/plain");
messageFacade.setCorrelationId(correlationId);
final JmsMessage jmsMessage = messageFacade.asJmsMessage();
underTest.tell(jmsMessage, null);
final Command command = expectMsgClass(Command.class);
assertThat(command.getType()).isEqualTo(ModifyAttribute.TYPE);
assertThat(command.getDittoHeaders().getCorrelationId()).contains(correlationId);
assertThat(((ModifyAttribute) command).getAttributePointer()).isEqualTo(JsonPointer.of("/foo"));
assertThat(((ModifyAttribute) command).getAttributeValue()).isEqualTo(JsonValue.of(plainPayload));
}};
}
@Test
public void createWithDefaultMapperOnly() throws IdConversionException {
new TestKit(actorSystem) {{
ActorRef underTest = setupActor(getTestActor(), new ArrayList<>());
ExternalMessage in = ConnectivityModelFactory.newExternalMessageBuilderForCommand(Collections.emptyMap()).withText("").build();
underTest.tell(in, null);
}};
}
private ActorRef setupActor(final ActorRef testActor, final List<MappingContext> mappingContexts) {
pubSubMediator.tell(new DistributedPubSubMediator.Put(testActor), null);
final String pubSubTarget = testActor.path().toStringWithoutAddress();
final Props messageMappingProcessorProps =
MessageMappingProcessorActor.props(pubSubMediator, pubSubTarget, testActor,
AuthorizationContext.newInstance(AuthorizationSubject.newInstance("foo:bar")),
mappingContexts);
final String messageMappingProcessorName = MessageMappingProcessorActor.ACTOR_NAME_PREFIX + UUID.randomUUID().toString();
final DefaultResizer resizer = new DefaultResizer(1, 5);
return actorSystem.actorOf(new RoundRobinPool(2)
.withDispatcher("message-mapping-processor-dispatcher")
.withResizer(resizer)
.props(messageMappingProcessorProps), messageMappingProcessorName);
}
}