-
Notifications
You must be signed in to change notification settings - Fork 214
/
SendingTest.java
320 lines (280 loc) · 16.6 KB
/
SendingTest.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* 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 static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.withSettings;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import org.assertj.core.api.SoftAssertions;
import org.eclipse.ditto.base.model.acks.AcknowledgementLabel;
import org.eclipse.ditto.base.model.acks.DittoAcknowledgementLabel;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.DittoHeadersSettable;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.acks.Acknowledgement;
import org.eclipse.ditto.base.model.signals.commands.CommandResponse;
import org.eclipse.ditto.connectivity.api.ExternalMessage;
import org.eclipse.ditto.connectivity.api.OutboundSignal;
import org.eclipse.ditto.connectivity.model.GenericTarget;
import org.eclipse.ditto.connectivity.model.MessageSendingFailedException;
import org.eclipse.ditto.connectivity.model.Target;
import org.eclipse.ditto.connectivity.service.messaging.monitoring.ConnectionMonitor;
import org.eclipse.ditto.internal.utils.pekko.logging.ThreadSafeDittoLoggingAdapter;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.placeholders.ExpressionResolver;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.WithThingId;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
/**
* Unit test for {@link Sending}.
*/
@RunWith(MockitoJUnitRunner.class)
public final class SendingTest {
private static final AcknowledgementLabel ACKNOWLEDGEMENT_LABEL = AcknowledgementLabel.of("twin-persisted");
@Rule
public final TestName testName = new TestName();
@Mock private OutboundSignal.Mapped mappedOutboundSignal;
@Mock private ExternalMessage externalMessage;
@Mock private GenericTarget genericTarget;
@Mock private ConnectionMonitor publishedMonitor;
@Mock private ConnectionMonitor acknowledgedMonitor;
@Mock private ConnectionMonitor droppedMonitor;
@Mock private Target autoAckTarget;
@Mock private ExpressionResolver connectionIdResolver;
@Mock private ThreadSafeDittoLoggingAdapter logger;
private DittoHeaders dittoHeaders;
private SendingContext sendingContext;
private ExceptionToAcknowledgementConverter exceptionConverter;
@Before
public void setUp() {
dittoHeaders = DittoHeaders.newBuilder().correlationId(testName.getMethodName()).build();
sendingContext = SendingContext.newBuilder()
.mappedOutboundSignal(mappedOutboundSignal)
.externalMessage(externalMessage)
.genericTarget(genericTarget)
.publishedMonitor(publishedMonitor)
.acknowledgedMonitor(acknowledgedMonitor)
.autoAckTarget(autoAckTarget)
.droppedMonitor(droppedMonitor)
.build();
Mockito.when(externalMessage.getInternalHeaders()).thenReturn(dittoHeaders);
Mockito.when(logger.withCorrelationId(Mockito.nullable(CharSequence.class))).thenReturn(logger);
Mockito.when(logger.withCorrelationId(Mockito.any(WithDittoHeaders.class))).thenReturn(logger);
exceptionConverter = DefaultExceptionToAcknowledgementConverter.getInstance();
}
@Test
public void createInstanceWithNullSendingContext() {
assertThatNullPointerException()
.isThrownBy(
() -> new Sending(null, CompletableFuture.completedStage(null), connectionIdResolver, logger))
.withMessage("The sendingContext must not be null!")
.withNoCause();
}
@Test
public void createInstanceWithNullFuture() {
assertThatNullPointerException()
.isThrownBy(() -> new Sending(sendingContext, null, connectionIdResolver, logger))
.withMessage("The futureResult must not be null!")
.withNoCause();
}
@Test
public void createInstanceWithNullLogger() {
assertThatNullPointerException()
.isThrownBy(() -> new Sending(sendingContext, CompletableFuture.completedStage(null),
connectionIdResolver, null))
.withMessage("The logger must not be null!")
.withNoCause();
}
@Test
public void acknowledgeAndMonitorNullCommandResponseWhenShouldAcknowledge() {
Mockito.when(autoAckTarget.getIssuedAcknowledgementLabel()).thenReturn(Optional.of(ACKNOWLEDGEMENT_LABEL));
final var source = Mockito.mock(WithThingId.class, withSettings()
.extraInterfaces(DittoHeadersSettable.class, Signal.class));
final var thingId = ThingId.generateRandom();
Mockito.when(source.getEntityId()).thenReturn(thingId);
Mockito.when(((DittoHeadersSettable)source).getDittoHeaders()).thenReturn(dittoHeaders);
Mockito.when(mappedOutboundSignal.getSource()).thenReturn((Signal)source);
final Acknowledgement expectedResponse =
exceptionConverter.convertException(MessageSendingFailedException.newBuilder()
.message("Message sending terminated without the expected acknowledgement.")
.description("Please contact the service team.")
.build(), ACKNOWLEDGEMENT_LABEL, thingId,
dittoHeaders);
final var expectedException = MessageSendingFailedException.newBuilder()
.message("Message sending terminated without the expected acknowledgement.")
.description("Please contact the service team.")
.build();
final Sending underTest = new Sending(sendingContext, CompletableFuture.completedStage(null),
connectionIdResolver, logger);
final Optional<CompletionStage<CommandResponse>> result = underTest.monitorAndAcknowledge(exceptionConverter);
Mockito.verify(publishedMonitor).failure(externalMessage, expectedException);
Mockito.verify(acknowledgedMonitor)
.failure(eq(externalMessage), Mockito.any(DittoRuntimeException.class));
assertThat(result).hasValueSatisfying(
resultFuture -> assertThat(resultFuture).isCompletedWithValue(expectedResponse));
}
@Test
public void monitorAcknowledgementSendSuccessKeepOriginalResponse() {
final var acknowledgement = Mockito.mock(Acknowledgement.class);
final SendResult sendResult = new SendResult(acknowledgement, DittoHeaders.empty());
final var acknowledgementStatus = HttpStatus.ACCEPTED;
Mockito.when(acknowledgement.getHttpStatus()).thenReturn(acknowledgementStatus);
final Sending underTest =
new Sending(sendingContext, CompletableFuture.completedStage(sendResult), connectionIdResolver,
logger);
final Optional<CompletionStage<CommandResponse>> result = underTest.monitorAndAcknowledge(exceptionConverter);
Mockito.verify(acknowledgedMonitor).success(eq(externalMessage));
Mockito.verify(publishedMonitor).success(eq(externalMessage));
assertThat(result)
.hasValueSatisfying(resultFuture -> assertThat(resultFuture).isCompletedWithValue(acknowledgement));
}
@Test
public void monitorAcknowledgementSendSuccessInCaseOfHandledException() {
final var acknowledgement = Mockito.mock(Acknowledgement.class);
final SendResult sendResult = new SendResult(acknowledgement, DittoHeaders.empty());
final var acknowledgementPayload = JsonObject.newBuilder().set("foo", "bar").build();
final var acknowledgementStatus = HttpStatus.INTERNAL_SERVER_ERROR;
Mockito.when(acknowledgement.getHttpStatus()).thenReturn(acknowledgementStatus);
Mockito.when(acknowledgement.getEntity()).thenReturn(Optional.of(acknowledgementPayload));
Mockito.when(acknowledgement.getLabel()).thenReturn(ACKNOWLEDGEMENT_LABEL);
final var expectedException = MessageSendingFailedException.newBuilder()
.httpStatus(acknowledgementStatus)
.message("Received negative acknowledgement for label <" + ACKNOWLEDGEMENT_LABEL + ">.")
.description("Payload: " + acknowledgementPayload)
.build();
final Sending underTest =
new Sending(sendingContext, CompletableFuture.completedStage(sendResult), connectionIdResolver,
logger);
final Optional<CompletionStage<CommandResponse>> result = underTest.monitorAndAcknowledge(exceptionConverter);
Mockito.verify(publishedMonitor).success(externalMessage);
Mockito.verify(acknowledgedMonitor).failure(externalMessage, expectedException);
assertThat(result)
.hasValueSatisfying(resultFuture -> assertThat(resultFuture).isCompletedWithValue(acknowledgement));
}
@Test
public void monitorAcknowledgementSendFailureInCaseOfUnhandledException() {
final var source = Mockito.mock(WithThingId.class, withSettings()
.extraInterfaces(Signal.class, DittoHeadersSettable.class));
final var thingId = ThingId.generateRandom();
Mockito.when(source.getEntityId()).thenReturn(thingId);
Mockito.when(((DittoHeadersSettable<?>)source).getDittoHeaders()).thenReturn(dittoHeaders);
Mockito.when(mappedOutboundSignal.getSource()).thenReturn((Signal)source);
Mockito.when(autoAckTarget.getIssuedAcknowledgementLabel()).thenReturn(Optional.of(ACKNOWLEDGEMENT_LABEL));
final var thrownException = new IllegalStateException("Test");
final var acknowledgementPayload = JsonObject.newBuilder()
.set("message", "Encountered <IllegalStateException>.")
.set("description", "Test")
.build();
final var acknowledgementStatus = HttpStatus.INTERNAL_SERVER_ERROR;
final var expectedException = MessageSendingFailedException.newBuilder()
.httpStatus(acknowledgementStatus)
.message("Received negative acknowledgement for label <" + ACKNOWLEDGEMENT_LABEL + ">.")
.description("Payload: " + acknowledgementPayload)
.build();
final CompletableFuture<SendResult> failedFuture = new CompletableFuture<>();
failedFuture.completeExceptionally(thrownException);
final Sending underTest = new Sending(sendingContext, failedFuture, connectionIdResolver, logger);
final Optional<CompletionStage<CommandResponse>> result = underTest.monitorAndAcknowledge(exceptionConverter);
Mockito.verify(publishedMonitor).exception(externalMessage, thrownException);
Mockito.verify(acknowledgedMonitor).failure(externalMessage, expectedException);
assertThat(result).hasValueSatisfying(
resultFuture -> assertThat(resultFuture).isCompletedWithValueMatching(response -> {
final SoftAssertions softly = new SoftAssertions();
softly.assertThat(response).isInstanceOf(Acknowledgement.class);
final Acknowledgement ack = (Acknowledgement) response;
softly.assertThat(ack.getLabel().toString()).isEqualTo(ACKNOWLEDGEMENT_LABEL.toString());
softly.assertThat(ack.getEntity()).contains(acknowledgementPayload);
softly.assertThat(ack.getEntityId().toString()).isEqualTo(thingId.toString());
softly.assertThat(ack.getHttpStatus()).isEqualTo(acknowledgementStatus);
softly.assertAll();
return true;
}));
}
@Test
public void monitorLiveResponseSendSuccessKeepOriginalResponse() {
final var issuedAckLabel = DittoAcknowledgementLabel.LIVE_RESPONSE;
Mockito.when(autoAckTarget.getIssuedAcknowledgementLabel()).thenReturn(Optional.of(issuedAckLabel));
final CommandResponse<?> commandResponse = Mockito.mock(CommandResponse.class);
final SendResult sendResult = new SendResult(commandResponse, DittoHeaders.empty());
final var commandResponseStatus = HttpStatus.ACCEPTED;
Mockito.when(commandResponse.getHttpStatus()).thenReturn(commandResponseStatus);
final Sending underTest =
new Sending(sendingContext, CompletableFuture.completedStage(sendResult), connectionIdResolver,
logger);
final Optional<CompletionStage<CommandResponse>> result = underTest.monitorAndAcknowledge(exceptionConverter);
Mockito.verify(acknowledgedMonitor).success(eq(externalMessage));
Mockito.verify(publishedMonitor).success(eq(externalMessage));
assertThat(result)
.hasValueSatisfying(resultFuture -> assertThat(resultFuture).isCompletedWithValue(commandResponse));
}
@Test
public void monitorLiveResponseSendFailureKeepOriginalResponse() {
final var issuedAckLabel = DittoAcknowledgementLabel.LIVE_RESPONSE;
Mockito.when(autoAckTarget.getIssuedAcknowledgementLabel()).thenReturn(Optional.of(issuedAckLabel));
final CommandResponse<?> commandResponse = Mockito.mock(CommandResponse.class);
final SendResult sendResult = new SendResult(commandResponse, DittoHeaders.empty());
final var commandResponseStatus = HttpStatus.CONFLICT;
Mockito.when(commandResponse.getHttpStatus()).thenReturn(commandResponseStatus);
final Sending underTest =
new Sending(sendingContext, CompletableFuture.completedStage(sendResult), connectionIdResolver,
logger);
final Optional<CompletionStage<CommandResponse>> result = underTest.monitorAndAcknowledge(exceptionConverter);
Mockito.verify(publishedMonitor).success(externalMessage);
Mockito.verify(acknowledgedMonitor).success(externalMessage);
assertThat(result)
.hasValueSatisfying(resultFuture -> assertThat(resultFuture).isCompletedWithValue(commandResponse));
}
@Test
public void monitorNoAckAsShouldNotAcknowledgeAndOriginalResponseIsNull() {
sendingContext = SendingContext.newBuilder()
.mappedOutboundSignal(mappedOutboundSignal)
.externalMessage(externalMessage)
.genericTarget(genericTarget)
.publishedMonitor(publishedMonitor)
.droppedMonitor(droppedMonitor)
.build();
final CommandResponse<?> commandResponse = null;
final SendResult sendResult = new SendResult(commandResponse, DittoHeaders.empty());
final Sending underTest =
new Sending(sendingContext, CompletableFuture.completedStage(sendResult), connectionIdResolver,
logger);
final Optional<CompletionStage<CommandResponse>> result = underTest.monitorAndAcknowledge(exceptionConverter);
Mockito.verifyNoInteractions(acknowledgedMonitor);
assertThat(result)
.hasValueSatisfying(resultFuture -> assertThat(resultFuture).isCompletedWithValue(commandResponse));
}
@Test
public void monitorAndAcknowledgeWhenFutureResponseTerminatedExceptionallyAndNoAckLabelIssued() {
final var rootCause = new IllegalStateException("A foo must not bar! Never ever!");
final Sending underTest =
new Sending(sendingContext, CompletableFuture.failedStage(rootCause), connectionIdResolver, logger);
final Optional<CompletionStage<CommandResponse>> result = underTest.monitorAndAcknowledge(exceptionConverter);
Mockito.verifyNoInteractions(acknowledgedMonitor);
Mockito.verify(publishedMonitor).exception(eq(externalMessage), eq(rootCause));
Mockito.verify(logger).withCorrelationId(testName.getMethodName());
assertThat(result).hasValueSatisfying(resultFuture -> assertThat(resultFuture).isCompletedWithValue(null));
}
}