-
Notifications
You must be signed in to change notification settings - Fork 215
/
InboundMappingProcessorActorTest.java
144 lines (127 loc) · 6.9 KB
/
InboundMappingProcessorActorTest.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
/*
* Copyright (c) 2021 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;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.connectivity.api.ExternalMessage;
import org.eclipse.ditto.connectivity.api.ExternalMessageFactory;
import org.eclipse.ditto.connectivity.api.MappedInboundExternalMessage;
import org.eclipse.ditto.connectivity.model.Connection;
import org.eclipse.ditto.connectivity.model.ConnectionId;
import org.eclipse.ditto.connectivity.model.ConnectionType;
import org.eclipse.ditto.connectivity.model.PayloadMapping;
import org.eclipse.ditto.connectivity.service.mapping.DittoMessageMapper;
import org.eclipse.ditto.connectivity.service.mapping.MessageMapperRegistry;
import org.eclipse.ditto.connectivity.service.messaging.mappingoutcome.MappingOutcome;
import org.eclipse.ditto.edge.service.headers.DittoHeadersValidator;
import org.eclipse.ditto.internal.utils.pekko.ActorSystemResource;
import org.eclipse.ditto.internal.utils.pekko.logging.ThreadSafeDittoLoggingAdapter;
import org.eclipse.ditto.internal.utils.tracing.DittoTracingInitResource;
import org.eclipse.ditto.protocol.TopicPath;
import org.eclipse.ditto.protocol.adapter.ProtocolAdapter;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mockito;
import com.typesafe.config.Config;
import org.apache.pekko.NotUsed;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.dispatch.MessageDispatcher;
import org.apache.pekko.stream.OverflowStrategy;
import org.apache.pekko.stream.javadsl.Sink;
import org.apache.pekko.stream.scaladsl.Source;
import org.apache.pekko.testkit.TestProbe;
import org.apache.pekko.testkit.javadsl.TestKit;
public final class InboundMappingProcessorActorTest {
@ClassRule
public static final DittoTracingInitResource DITTO_TRACING_INIT_RESOURCE =
DittoTracingInitResource.disableDittoTracing();
@Rule
public final ActorSystemResource actorSystemResource = ActorSystemResource.newInstance(TestConstants.CONFIG);
@Test
public void onError() {
new TestKit(actorSystemResource.getActorSystem()) {{
// GIVEN: InboundMappingProcessorActor is constructed with a processor that throws an exception always.
final TestProbe inboundDispatcher = actorSystemResource.newTestProbe("inboundDispatcher");
final Sink<Object, ?> inboundSink =
Sink.foreach(x -> inboundDispatcher.ref().tell(x, ActorRef.noSender()));
final InboundMappingProcessor throwingProcessor = createThrowingProcessor();
final Sink<Object, NotUsed> inboundMappingSink = InboundMappingSink.createSink(List.of(throwingProcessor),
TestConstants.createRandomConnectionId(),
1,
inboundSink,
TestConstants.MAPPING_CONFIG,
null,
getDefaultGlobalDispatcher());
final ActorRef underTest = Source.actorRef(1, OverflowStrategy.dropNew())
.to(inboundMappingSink)
.run(actorSystemResource.getMaterializer());
// WHEN: InboundMappingProcessorActor receives a text message.
final ExternalMessage message = ExternalMessageFactory.newExternalMessageBuilder(Map.of())
.withSource(TestConstants.Sources.SOURCES_WITH_AUTH_CONTEXT.get(0))
.withText("text")
// attach non-null payload mapping to avoid using the default mapper
.withPayloadMapping(Mockito.mock(PayloadMapping.class))
.build();
underTest.tell(new ExternalMessageWithSender(message, getRef()), ActorRef.noSender());
// THEN: InboundDispatchingActor receives 1 error outcome with the exception thrown.
final InboundMappingOutcomes outcomes =
inboundDispatcher.expectMsgClass(InboundMappingOutcomes.class);
assertThat(outcomes.getOutcomes()).hasSize(1);
outcomes.getOutcomes().get(0).accept(new MappingOutcome.Visitor<>() {
@Override
public Integer onMapped(final String mapperId, final MappedInboundExternalMessage mapped) {
throw new AssertionError("Expect error, got: mapped " + mapped);
}
@Override
public Integer onDropped(final String mapperId,
@Nullable final ExternalMessage droppedMessage) {
throw new AssertionError("Expect error, got: dropped " + droppedMessage);
}
@Override
public Integer onError(final String mapperId, final Exception error,
@Nullable final TopicPath topicPath,
@Nullable final ExternalMessage externalMessage) {
assertThat(error).isEqualTo(ThrowingMapper.EXCEPTION);
return 0;
}
});
}};
}
private static InboundMappingProcessor createThrowingProcessor() {
final MessageMapperRegistry registry = Mockito.mock(MessageMapperRegistry.class);
Mockito.doAnswer(inv -> new DittoMessageMapper(Mockito.mock(ActorSystem.class), Mockito.mock(Config.class)))
.when(registry).getDefaultMapper();
Mockito.doAnswer(inv -> List.of(new ThrowingMapper())).when(registry).getMappers(Mockito.any());
final ThreadSafeDittoLoggingAdapter logger = Mockito.mock(ThreadSafeDittoLoggingAdapter.class);
Mockito.doAnswer(inv -> logger).when(logger).withCorrelationId(Mockito.<CharSequence>any());
Mockito.doAnswer(inv -> logger).when(logger).withCorrelationId(Mockito.<DittoHeaders>any());
Mockito.doAnswer(inv -> logger).when(logger).withCorrelationId(Mockito.<Map>any());
final Connection connection =
TestConstants.createConnection(ConnectionId.of("connectionId"), ConnectionType.MQTT);
return InboundMappingProcessor.of(connection,
registry,
logger,
Mockito.mock(ProtocolAdapter.class),
Mockito.mock(DittoHeadersValidator.class));
}
private MessageDispatcher getDefaultGlobalDispatcher() {
final var actorSystem = actorSystemResource.getActorSystem();
final var dispatchers = actorSystem.dispatchers();
return dispatchers.defaultGlobalDispatcher();
}
}