-
Notifications
You must be signed in to change notification settings - Fork 215
/
MessageMappingProcessorActorHeaderInteractionTest.java
171 lines (151 loc) · 7.92 KB
/
MessageMappingProcessorActorHeaderInteractionTest.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
/*
* Copyright (c) 2020 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.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import org.eclipse.ditto.base.model.acks.AcknowledgementRequest;
import org.eclipse.ditto.base.model.acks.DittoAcknowledgementLabel;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.exceptions.DittoHeaderInvalidException;
import org.eclipse.ditto.base.model.signals.commands.CommandResponse;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotAccessibleException;
import org.eclipse.ditto.things.model.signals.commands.modify.ModifyThing;
import org.eclipse.ditto.things.model.signals.commands.modify.ModifyThingResponse;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import akka.actor.ActorRef;
import akka.testkit.TestProbe;
import akka.testkit.javadsl.TestKit;
import scala.concurrent.duration.FiniteDuration;
/**
* Tests {@link InboundMappingProcessor}.
*/
@RunWith(Parameterized.class)
public final class MessageMappingProcessorActorHeaderInteractionTest extends AbstractMessageMappingProcessorActorTest {
private static final List<Duration> TIMEOUT = List.of(Duration.ZERO, Duration.ofMinutes(1L));
private static final List<Boolean> RESPONSE_REQUIRED = List.of(false, true);
private static final List<List<AcknowledgementRequest>> REQUESTED_ACKS =
List.of(List.of(), List.of(AcknowledgementRequest.of(DittoAcknowledgementLabel.TWIN_PERSISTED)));
private static final List<Boolean> IS_SUCCESS = List.of(false, true);
@Parameterized.Parameters(name = "timeout={0} response-required={1} requested-acks={2} is-success={3}")
public static Collection<Object[]> getParameters() {
return TIMEOUT.stream().flatMap(timeout ->
RESPONSE_REQUIRED.stream().flatMap(responseRequired ->
REQUESTED_ACKS.stream().flatMap(requestedAcks ->
IS_SUCCESS.stream().map(isSuccess ->
new Object[]{timeout, responseRequired, requestedAcks, isSuccess}
)
)
)
).toList();
}
private final Duration timeout;
private final boolean responseRequired;
private final List<AcknowledgementRequest> requestedAcks;
private final boolean isSuccess;
public MessageMappingProcessorActorHeaderInteractionTest(final Duration timeout, final Boolean responseRequired,
final List<AcknowledgementRequest> requestedAcks, final Boolean isSuccess) {
this.timeout = timeout;
this.responseRequired = responseRequired;
this.requestedAcks = requestedAcks;
this.isSuccess = isSuccess;
}
@Test
public void run() {
new TestKit(actorSystem) {{
final TestProbe collectorProbe = TestProbe.apply("collector", actorSystem);
final ActorRef outboundMappingProcessorActor = createOutboundMappingProcessorActor(this);
final ActorRef inboundMappingProcessorActor =
createInboundMappingProcessorActor(this, outboundMappingProcessorActor);
final ModifyThing modifyThing = getModifyThing();
final Optional<HttpStatus> expectedStatusCode = getExpectedOutcome();
final boolean isBadRequest = expectedStatusCode.filter(HttpStatus.BAD_REQUEST::equals).isPresent();
final boolean settleImmediately = modifyThing.getDittoHeaders().getAcknowledgementRequests().isEmpty();
inboundMappingProcessorActor.tell(
new InboundMappingSink.ExternalMessageWithSender(toExternalMessage(modifyThing),
collectorProbe.ref()),
ActorRef.noSender()
);
// transport-layer settlement based on requested-acks alone
if (settleImmediately && !isBadRequest) {
collectorProbe.expectMsg(FiniteDuration.apply(20l, TimeUnit.SECONDS), ResponseCollectorActor.setCount(0));
} else if (isBadRequest) {
// bad requests should settle immediately because no command is forwarded
collectorProbe.expectMsgClass(DittoHeaderInvalidException.class);
collectorProbe.expectMsg(ResponseCollectorActor.setCount(0));
} else {
// no immediate settlement; response collector actor is asked to wait for the response.
collectorProbe.expectMsg(ResponseCollectorActor.setCount(1));
}
if (!isBadRequest) {
// command forwarded for non-bad requests.
final ModifyThing forwardedModifyThing = expectMsgClass(ModifyThing.class);
// send a response always - MessageMappingProcessorActor should drop it if not wanted.
final Object response = getModifyThingResponse(forwardedModifyThing);
reply(response);
// if an acknowledgement is requested, expect response collector to receive it and trigger settlement.
if (!settleImmediately) {
collectorProbe.expectMsg(response);
}
}
if (expectedStatusCode.isPresent()) {
// check published response for expected status
final BaseClientActor.PublishMappedMessage publish =
expectMsgClass(BaseClientActor.PublishMappedMessage.class);
final HttpStatus publishedStatusCode =
((CommandResponse<?>) publish.getOutboundSignal().getSource()).getHttpStatus();
assertThat(publishedStatusCode).isEqualTo(expectedStatusCode.get());
} else {
// check that no response is published
expectNoMessage(Duration.ofMillis(250L));
}
}};
}
private ModifyThing getModifyThing() {
return ModifyThing.of(ThingId.of("thing:id"), Thing.newBuilder().build(), null,
HEADERS_WITH_REPLY_INFORMATION.toBuilder()
.timeout(timeout)
.responseRequired(responseRequired)
.acknowledgementRequests(requestedAcks)
.build());
}
private Object getModifyThingResponse(final ModifyThing modifyThing) {
return (isSuccess
? ModifyThingResponse.modified(modifyThing.getEntityId(), modifyThing.getDittoHeaders())
: ThingNotAccessibleException.newBuilder(modifyThing.getEntityId()).build())
.setDittoHeaders(modifyThing.getDittoHeaders()); // use setDittoHeaders to lose concrete exception type
}
private Optional<HttpStatus> getExpectedOutcome() {
final Optional<HttpStatus> status;
final HttpStatus successCode = HttpStatus.NO_CONTENT;
final HttpStatus errorCode = HttpStatus.NOT_FOUND;
final HttpStatus badRequest = HttpStatus.BAD_REQUEST;
if (timeout.isZero()) {
status = (responseRequired || !requestedAcks.isEmpty()) ? Optional.of(badRequest) : Optional.empty();
} else {
if (isSuccess) {
status = responseRequired ? Optional.of(successCode) : Optional.empty();
} else {
status = responseRequired ? Optional.of(errorCode) : Optional.empty();
}
}
return status;
}
}