-
Notifications
You must be signed in to change notification settings - Fork 215
/
SendThingMessageTest.java
executable file
·217 lines (180 loc) · 9.1 KB
/
SendThingMessageTest.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
/*
* Copyright (c) 2017 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;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Fail.fail;
import static org.eclipse.ditto.json.assertions.DittoJsonAssertions.assertThat;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.Base64;
import java.util.UUID;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.FieldType;
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.ThingIdInvalidException;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.mutabilitydetector.unittesting.AllowedReason;
import org.mutabilitydetector.unittesting.MutabilityAssert;
import org.mutabilitydetector.unittesting.MutabilityMatchers;
import nl.jqno.equalsverifier.EqualsVerifier;
/**
* Unit test for {@link SendThingMessage}.
*/
@RunWith(MockitoJUnitRunner.class)
public final class SendThingMessageTest {
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 CORRELATION_ID = UUID.randomUUID().toString();
private static final DittoHeaders DITTO_HEADERS = DittoHeaders.newBuilder().correlationId(CORRELATION_ID).build();
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);
private static final Message<?> MESSAGE = MessagesModelFactory.newMessageBuilder(
MessageHeaders.newBuilder(MessageDirection.TO, THING_ID, SUBJECT)
.contentType(CONTENT_TYPE)
.build())
.payload(ByteBuffer.wrap(KNOWN_RAW_PAYLOAD_BYTES))
.rawPayload(ByteBuffer.wrap(KNOWN_RAW_PAYLOAD_BYTES))
.build();
private static final Message<?> MESSAGE_EMPTY_PAYLOAD = MessagesModelFactory.newMessageBuilder(
MessageHeaders.newBuilder(MessageDirection.TO, THING_ID, SUBJECT)
.contentType(CONTENT_TYPE)
.build())
.build();
private static final JsonObject KNOWN_MESSAGE_AS_JSON = JsonFactory.newObjectBuilder()
.set(MessageCommand.JsonFields.JSON_MESSAGE_HEADERS, MESSAGE.getHeaders().toJson())
.set(MessageCommand.JsonFields.JSON_MESSAGE_PAYLOAD, JsonFactory.newValue(new String(Base64.getEncoder()
.encode(KNOWN_RAW_PAYLOAD_BYTES), StandardCharsets.UTF_8))
)
.build();
private static final JsonObject KNOWN_EMPTY_PAYLOAD_MESSAGE_AS_JSON = JsonFactory.newObjectBuilder()
.set(MessageCommand.JsonFields.JSON_MESSAGE_HEADERS, MESSAGE.getHeaders().toJson())
.build();
private static final JsonObject KNOWN_JSON = JsonFactory.newObjectBuilder()
.set(Command.JsonFields.TYPE, SendThingMessage.TYPE)
.set(MessageCommand.JsonFields.JSON_THING_ID, THING_ID.toString())
.set(MessageCommand.JsonFields.JSON_MESSAGE, KNOWN_MESSAGE_AS_JSON)
.build();
private static final JsonObject KNOWN_JSON_WITH_EMPTY_PAYLOAD = JsonFactory.newObjectBuilder()
.set(Command.JsonFields.TYPE, SendThingMessage.TYPE)
.set(MessageCommand.JsonFields.JSON_THING_ID, THING_ID.toString())
.set(MessageCommand.JsonFields.JSON_MESSAGE, KNOWN_EMPTY_PAYLOAD_MESSAGE_AS_JSON)
.build();
@Test
public void assertImmutability() {
MutabilityAssert.assertInstancesOf(SendThingMessage.class, MutabilityMatchers.areImmutable(),
AllowedReason.provided(Message.class).isAlsoImmutable());
}
@Test
public void testHashCodeAndEquals() {
EqualsVerifier.forClass(SendThingMessage.class)
.usingGetClass()
.withRedefinedSuperclass()
.verify();
}
@Test(expected = NullPointerException.class)
public void tryCreateWithNullThingId() {
SendThingMessage.of(null, MESSAGE, DITTO_HEADERS);
}
@Test(expected = NullPointerException.class)
public void tryCreateWithNullMessage() {
SendThingMessage.of(THING_ID, null, DITTO_HEADERS);
}
@Test(expected = NullPointerException.class)
public void tryCreateWithNullDittoHeaders() {
SendThingMessage.of(THING_ID, MESSAGE, null);
}
@Test
public void tryToCreateInstanceWithNonMatchingThingId() {
final ThingId expectedThingId = ThingId.of(THING_ID.getNamespace(), THING_ID.getName() + "-nomatch");
try {
SendThingMessage.of(expectedThingId, MESSAGE, DITTO_HEADERS);
fail("Expected a ThingIdInvalidException to be thrown");
} catch (final ThingIdInvalidException e) {
assertThat(e).hasMessageContaining(expectedThingId.toString()).hasNoCause();
final String expectedDescription =
MessageFormat.format("It does not match the 'thingId' from the Message " +
"the command transports (<{0}>). Please ensure that they are equal.",
MESSAGE.getEntityId().toString());
assertThat(e.getDescription()).hasValue(expectedDescription);
}
}
@Test
public void tryCreateValidMessage() {
SendThingMessage.of(THING_ID, MESSAGE, DITTO_HEADERS);
}
@Test
public void toJsonReturnsExpected() {
final SendThingMessage<?> underTest =
SendThingMessage.of(THING_ID, MESSAGE, TestConstants.EMPTY_DITTO_HEADERS);
final JsonObject actualJson = underTest.toJson(FieldType.regularOrSpecial());
assertThat(actualJson).isEqualTo(KNOWN_JSON);
}
@Test
public void toJsonWithEmptyPayloadReturnsExpected() {
final SendThingMessage<?> underTest =
SendThingMessage.of(THING_ID, MESSAGE_EMPTY_PAYLOAD, TestConstants.EMPTY_DITTO_HEADERS);
final JsonObject actualJson = underTest.toJson(FieldType.regularOrSpecial());
assertThat(actualJson).isEqualTo(KNOWN_JSON_WITH_EMPTY_PAYLOAD);
}
@Test
public void createInstanceFromValidJson() {
final SendThingMessage<?> underTest =
SendThingMessage.fromJson(KNOWN_JSON.toString(), TestConstants.EMPTY_DITTO_HEADERS);
assertThat(underTest).isNotNull();
assertThat((CharSequence) underTest.getEntityId()).isEqualTo(THING_ID);
assertThat(underTest.getMessageType()).isEqualTo(SendThingMessage.NAME);
assertThat(underTest.getMessage()).isEqualTo(MESSAGE);
}
@Test
public void createInstanceFromValidJsonWithEmptyPayload() {
final SendThingMessage<?> underTest =
SendThingMessage.fromJson(KNOWN_JSON_WITH_EMPTY_PAYLOAD, TestConstants.EMPTY_DITTO_HEADERS);
assertThat(underTest).isNotNull();
assertThat((CharSequence) underTest.getEntityId()).isEqualTo(THING_ID);
assertThat(underTest.getMessageType()).isEqualTo(SendThingMessage.NAME);
assertThat(underTest.getMessage()).isEqualTo(MESSAGE_EMPTY_PAYLOAD);
assertThat(underTest.getMessage().getPayload()).isEmpty();
assertThat(underTest.getMessage().getRawPayload()).isEmpty();
}
@Test
public void toJsonWithCustomContentType() {
final MessageHeaders headers = MessageHeaders.newBuilder(MessageDirection.TO, ThingId.of("the", "thingId"),
"theSubject")
.contentType("unknownBinaryContentType")
.build();
final String body = "binary message body";
final Message<?> message = Message.newBuilder(headers)
.payload(ByteBuffer.wrap(body.getBytes()))
.rawPayload(ByteBuffer.wrap(body.getBytes()))
.build();
final SendThingMessage<?> underTest =
SendThingMessage.of(ThingId.of("the", "thingId"), message, TestConstants.EMPTY_DITTO_HEADERS);
final JsonValue serialized = underTest.toJson(FieldType.regularOrSpecial());
final SendThingMessage<?> deserialized =
SendThingMessage.fromJson(serialized.toString(), TestConstants.EMPTY_DITTO_HEADERS);
assertThat(deserialized).isEqualTo(underTest);
}
}