-
Notifications
You must be signed in to change notification settings - Fork 215
/
MessageCommandAckRequestSetterTest.java
200 lines (174 loc) · 9.65 KB
/
MessageCommandAckRequestSetterTest.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
/*
* 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.messages.model.signals.commands.acks;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf;
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import org.assertj.core.api.Assertions;
import org.eclipse.ditto.base.model.acks.AcknowledgementLabel;
import org.eclipse.ditto.base.model.acks.AcknowledgementRequest;
import org.eclipse.ditto.base.model.acks.DittoAcknowledgementLabel;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.messages.model.Message;
import org.eclipse.ditto.messages.model.MessageDirection;
import org.eclipse.ditto.messages.model.MessageHeaders;
import org.eclipse.ditto.messages.model.MessagesModelFactory;
import org.eclipse.ditto.messages.model.signals.commands.MessageCommand;
import org.eclipse.ditto.messages.model.signals.commands.SendThingMessage;
import org.eclipse.ditto.things.model.ThingId;
import org.junit.Ignore;
import org.junit.Test;
/**
* Unit test for {@link MessageCommandAckRequestSetter}.
*/
public final class MessageCommandAckRequestSetterTest {
private static final ThingId THING_ID = ThingId.of("test.ns", "theThingId");
private static final String SUBJECT = "theSubject";
private static final String CONTENT_TYPE = "application/xml";
private static final String KNOWN_RAW_PAYLOAD_STR = "<some>42</some>";
private static final byte[] KNOWN_RAW_PAYLOAD_BYTES = KNOWN_RAW_PAYLOAD_STR.getBytes(StandardCharsets.UTF_8);
static final Message<?> MESSAGE = MessagesModelFactory.newMessageBuilder(
MessageHeaders.newBuilder(MessageDirection.TO, THING_ID, SUBJECT)
.contentType(CONTENT_TYPE)
.build())
.rawPayload(ByteBuffer.wrap(KNOWN_RAW_PAYLOAD_BYTES))
.build();
@Test
@Ignore("https://github.com/MutabilityDetector/MutabilityDetector/issues/185")
public void assertImmutability() {
assertInstancesOf(MessageCommandAckRequestSetter.class, areImmutable());
}
@Test
public void tryToApplyNullCommand() {
final MessageCommandAckRequestSetter underTest = MessageCommandAckRequestSetter.getInstance();
assertThatNullPointerException()
.isThrownBy(() -> underTest.apply(null))
.withMessage("The command must not be null!")
.withNoCause();
}
@Test
public void addLiveResponseAckLabelByDefault() {
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder()
.channel("live")
.randomCorrelationId()
.build();
final MessageCommand<?, ?> command = SendThingMessage.of(THING_ID, MESSAGE, dittoHeaders);
final MessageCommand<?, ?> expected = command.setDittoHeaders(DittoHeaders.newBuilder(dittoHeaders)
.acknowledgementRequest(AcknowledgementRequest.of(DittoAcknowledgementLabel.LIVE_RESPONSE))
.responseRequired(true)
.build());
final MessageCommandAckRequestSetter underTest = MessageCommandAckRequestSetter.getInstance();
Assertions.assertThat(underTest.apply(command)).isEqualTo(expected);
}
@Test
public void removeLiveResponseAckLabelWhenResponseRequiredFalse() {
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder()
.channel("live")
.responseRequired(false)
.acknowledgementRequest(AcknowledgementRequest.of(DittoAcknowledgementLabel.LIVE_RESPONSE))
.randomCorrelationId()
.build();
final MessageCommand<?, ?> command = SendThingMessage.of(THING_ID, MESSAGE, dittoHeaders);
final DittoHeaders expectedHeaders = DittoHeaders.newBuilder(dittoHeaders)
.acknowledgementRequests(Collections.emptyList())
.responseRequired(false)
.build();
final MessageCommand<?, ?> expected = command.setDittoHeaders(expectedHeaders);
final MessageCommandAckRequestSetter underTest = MessageCommandAckRequestSetter.getInstance();
final MessageCommand<?, ?> appliedCommand = underTest.apply(command);
assertThat(appliedCommand.getDittoHeaders()).isEqualTo(expectedHeaders);
assertThat(appliedCommand).isEqualTo(expected);
}
@Test
public void removeLiveResponseAckLabelFromOthersWhenResponseRequiredFalse() {
final AcknowledgementRequest ackRequest1 = AcknowledgementRequest.of(AcknowledgementLabel.of("FOO"));
final AcknowledgementRequest ackRequest2 = AcknowledgementRequest.of(AcknowledgementLabel.of("BAR"));
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder()
.channel("live")
.responseRequired(false)
.acknowledgementRequest(ackRequest1, AcknowledgementRequest.of(DittoAcknowledgementLabel.LIVE_RESPONSE),
ackRequest2)
.randomCorrelationId()
.build();
final MessageCommand<?, ?> command = SendThingMessage.of(THING_ID, MESSAGE, dittoHeaders);
final DittoHeaders expectedHeaders = DittoHeaders.newBuilder(dittoHeaders)
.acknowledgementRequest(ackRequest1, ackRequest2)
.responseRequired(false)
.build();
final MessageCommand<?, ?> expected = command.setDittoHeaders(expectedHeaders);
final MessageCommandAckRequestSetter underTest = MessageCommandAckRequestSetter.getInstance();
final MessageCommand<?, ?> appliedCommand = underTest.apply(command);
assertThat(appliedCommand.getDittoHeaders()).isEqualTo(expectedHeaders);
assertThat(appliedCommand).isEqualTo(expected);
}
@Test
public void filterOutOtherBuiltInDittoAcknowledgementLabels() {
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder()
.channel("live")
.acknowledgementRequest(AcknowledgementRequest.of(DittoAcknowledgementLabel.TWIN_PERSISTED),
AcknowledgementRequest.of(DittoAcknowledgementLabel.LIVE_RESPONSE))
.randomCorrelationId()
.build();
final MessageCommand<?, ?> command = SendThingMessage.of(THING_ID, MESSAGE, dittoHeaders);
final DittoHeaders expectedHeaders = dittoHeaders.toBuilder()
.acknowledgementRequest(AcknowledgementRequest.of(DittoAcknowledgementLabel.LIVE_RESPONSE))
.responseRequired(true)
.build();
final MessageCommand<?, ?> expected = SendThingMessage.of(THING_ID, MESSAGE, expectedHeaders);
final MessageCommandAckRequestSetter underTest = MessageCommandAckRequestSetter.getInstance();
Assertions.assertThat(underTest.apply(command)).isEqualTo(expected);
}
@Test
public void addsLiveResponseAckLabelToAlreadyRequiredAckLabels() {
final AcknowledgementRequest ackRequest1 = AcknowledgementRequest.of(AcknowledgementLabel.of("FOO"));
final AcknowledgementRequest ackRequest2 = AcknowledgementRequest.of(AcknowledgementLabel.of("BAR"));
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder()
.channel("live")
.acknowledgementRequest(ackRequest1, ackRequest2)
.randomCorrelationId()
.responseRequired(true)
.build();
final MessageCommand<?, ?> command = SendThingMessage.of(THING_ID, MESSAGE, dittoHeaders);
final MessageCommandAckRequestSetter underTest = MessageCommandAckRequestSetter.getInstance();
final DittoHeaders expectedHeaders = dittoHeaders.toBuilder()
.acknowledgementRequest(ackRequest1, ackRequest2,
AcknowledgementRequest.of(DittoAcknowledgementLabel.LIVE_RESPONSE))
.build();
final MessageCommand<?, ?> expectedCommand = command.setDittoHeaders(expectedHeaders);
final MessageCommand<?, ?> appliedCommand = underTest.apply(command);
assertThat(appliedCommand.getDittoHeaders()).isEqualTo(expectedHeaders);
assertThat(appliedCommand).isEqualTo(expectedCommand);
}
@Test
public void notAddingLiveResponseAckLabelToExplicitlyEmptyRequiredAckLabels() {
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder()
.channel("live")
.acknowledgementRequests(Collections.emptyList())
.randomCorrelationId()
.responseRequired(true)
.build();
final MessageCommand<?, ?> command = SendThingMessage.of(THING_ID, MESSAGE, dittoHeaders);
final MessageCommandAckRequestSetter underTest = MessageCommandAckRequestSetter.getInstance();
final DittoHeaders expectedHeaders = dittoHeaders.toBuilder()
.acknowledgementRequests(Collections.emptyList())
.build();
final MessageCommand<?, ?> expectedCommand = command.setDittoHeaders(expectedHeaders);
final MessageCommand<?, ?> appliedCommand = underTest.apply(command);
assertThat(appliedCommand.getDittoHeaders()).isEqualTo(expectedHeaders);
assertThat(appliedCommand).isEqualTo(expectedCommand);
}
}