/
MessageHeadersBuilderTest.java
executable file
·245 lines (201 loc) · 9.97 KB
/
MessageHeadersBuilderTest.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
/*
* 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.model.messages;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;
import org.assertj.core.api.SoftAssertions;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.model.base.common.HttpStatus;
import org.eclipse.ditto.model.base.common.HttpStatusCodeOutOfRangeException;
import org.eclipse.ditto.model.base.exceptions.DittoHeaderInvalidException;
import org.eclipse.ditto.model.base.headers.DittoHeaderDefinition;
import org.eclipse.ditto.model.things.ThingId;
import org.junit.Before;
import org.junit.Test;
/**
* Unit test for {@link MessageHeadersBuilder}.
*/
public final class MessageHeadersBuilderTest {
private static final MessageDirection DIRECTION = MessageDirection.TO;
private final static ThingId THING_ID = ThingId.of("bla","foo-bar");
private static final String SUBJECT = KnownMessageSubjects.CLAIM_SUBJECT;
private static final String FEATURE_ID = "flux-condensator-0815";
private static final String CONTENT_TYPE = "application/json";
private static final String CORRELATION_ID = "myCorrelationId";
private MessageHeadersBuilder underTest;
@Before
public void setUp() {
underTest = MessageHeadersBuilder.newInstance(DIRECTION, THING_ID, SUBJECT);
}
@Test
public void tryToCreateInstanceWithNullDirection() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> MessageHeadersBuilder.newInstance(null, THING_ID, SUBJECT))
.withMessage("The %s must not be null!", "direction")
.withNoCause();
}
@Test
public void tryToCreateInstanceWithNullThingId() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> MessageHeadersBuilder.newInstance(DIRECTION, null, SUBJECT))
.withMessage("The %s must not be null!", "thing-id")
.withNoCause();
}
@Test
public void tryToCreateInstanceWithNullSubject() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> MessageHeadersBuilder.newInstance(DIRECTION, THING_ID, null))
.withMessage("The %s must not be null!", "subject")
.withNoCause();
}
@Test
public void tryToCreateInstanceWithEmptySubject() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> MessageHeadersBuilder.newInstance(DIRECTION, THING_ID, ""))
.withMessage("The argument '%s' must not be empty!", "subject")
.withNoCause();
}
@Test
public void tryToCreateInstanceWithInvalidSubject() {
final String invalidSubject = "§foo";
assertThatExceptionOfType(DittoHeaderInvalidException.class)
.isThrownBy(() -> MessageHeadersBuilder.newInstance(DIRECTION, THING_ID, invalidSubject))
.withMessageContaining(invalidSubject)
.withMessageEndingWith("is not a valid message subject.")
.withNoCause();
}
@Test
public void createInstanceFromMinimalMap() {
final Map<String, String> validHeaders = new HashMap<>();
validHeaders.put(MessageHeaderDefinition.DIRECTION.getKey(), MessageDirection.TO.toString());
validHeaders.put(MessageHeaderDefinition.SUBJECT.getKey(), SUBJECT);
validHeaders.put(MessageHeaderDefinition.THING_ID.getKey(), THING_ID.toString());
final MessageHeaders messageHeaders = MessageHeadersBuilder.of(validHeaders).build();
assertThat(messageHeaders).containsAllEntriesOf(validHeaders);
}
@Test
public void tryToCreateInstanceFromMapWithoutThingId() {
final Map<String, String> insufficientHeaders = new HashMap<>();
insufficientHeaders.put(MessageHeaderDefinition.DIRECTION.getKey(), MessageDirection.TO.toString());
insufficientHeaders.put(MessageHeaderDefinition.SUBJECT.getKey(), SUBJECT);
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> MessageHeadersBuilder.of(insufficientHeaders))
.withMessage("The headers did not contain a value for mandatory header with key <%s>!",
MessageHeaderDefinition.THING_ID.getKey())
.withNoCause();
}
@Test
public void tryToCreateInstanceFromEmptyJsonObject() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> MessageHeadersBuilder.of(JsonFactory.newObject()))
.withMessage("The headers did not contain a value for mandatory header with key <%s>!",
MessageHeaderDefinition.DIRECTION.getKey())
.withNoCause();
}
@Test
public void setFeatureIdWorksAsExpected() {
final MessageHeaders messageHeaders = underTest.featureId(FEATURE_ID).build();
assertThat(messageHeaders.getFeatureId()).contains(FEATURE_ID);
}
@Test
public void tryToSetEmptyFeatureId() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> underTest.featureId(""))
.withMessage("The %s must not be empty!", MessageHeaderDefinition.FEATURE_ID.getKey())
.withNoCause();
}
@Test
public void tryToSetUnknownStatusCode() {
final int unknownCode = 42;
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> underTest.statusCode(unknownCode))
.withCauseInstanceOf(HttpStatusCodeOutOfRangeException.class);
}
@Test
public void setStatusCodeAsIntWorksAsExpected() {
final HttpStatus knownStatus = HttpStatus.NOT_FOUND;
final MessageHeaders messageHeaders = underTest.statusCode(knownStatus.getCode()).build();
assertThat(messageHeaders.getHttpStatus()).contains(knownStatus);
}
@Test
public void setHttpStatusWorksAsExpected() {
final HttpStatus knownStatus = HttpStatus.NOT_FOUND;
final MessageHeaders messageHeaders = underTest.httpStatus(knownStatus).build();
assertThat(messageHeaders.getHttpStatus()).contains(knownStatus);
}
@Test
public void putValidHeadersWorksAsExpected() {
final Map<String, String> validHeaders = new HashMap<>();
validHeaders.put(DittoHeaderDefinition.CONTENT_TYPE.getKey(), CONTENT_TYPE);
validHeaders.put(MessageHeaderDefinition.STATUS_CODE.getKey(), String.valueOf(HttpStatus.CREATED.getCode()));
validHeaders.put(DittoHeaderDefinition.CORRELATION_ID.getKey(), CORRELATION_ID);
final MessageHeaders messageHeaders = underTest.putHeaders(validHeaders).build();
assertThat(messageHeaders).containsAllEntriesOf(validHeaders);
}
@Test
public void tryToPutMapWithInvalidDittoHeader() {
final String key = DittoHeaderDefinition.RESPONSE_REQUIRED.getKey();
final String invalidValue = "bar";
final Map<String, String> invalidHeaders = new HashMap<>();
invalidHeaders.put(DittoHeaderDefinition.CONTENT_TYPE.getKey(), CONTENT_TYPE);
invalidHeaders.put(MessageHeaderDefinition.STATUS_CODE.getKey(), String.valueOf(HttpStatus.CREATED.getCode()));
invalidHeaders.put(key, invalidValue);
assertThatExceptionOfType(DittoHeaderInvalidException.class)
.isThrownBy(() -> underTest.putHeaders(invalidHeaders))
.withMessage("The value '%s' of the header '%s' is not a valid boolean.", invalidValue, key)
.withNoCause();
}
@Test
public void tryToRemoveMandatoryHeader() {
final SoftAssertions softly = new SoftAssertions();
for(final MessageHeaderDefinition mandatoryHeaderDefinition : MessageHeadersBuilder.MANDATORY_HEADERS) {
final String key = mandatoryHeaderDefinition.getKey();
softly.assertThatThrownBy(() -> underTest.removeHeader(key))
.isInstanceOf(IllegalArgumentException.class)
.withFailMessage("Mandatory header with key <%s> cannot be removed!", key)
.hasNoCause();
}
softly.assertAll();
}
@Test
public void tryToRemoveMandatoryHeaderBySettingToNull() {
final SoftAssertions softly = new SoftAssertions();
for(final MessageHeaderDefinition mandatoryHeaderDefinition : MessageHeadersBuilder.MANDATORY_HEADERS) {
final String key = mandatoryHeaderDefinition.getKey();
softly.assertThatThrownBy(() -> underTest.putHeader(key, null))
.isInstanceOf(NullPointerException.class)
.withFailMessage("The value must not be null!")
.hasNoCause();
}
softly.assertAll();
}
@Test
public void tryToPutInvalidHeader() {
final String key = MessageHeaderDefinition.TIMESTAMP.getKey();
final String invalidValue = "foo";
assertThatExceptionOfType(DittoHeaderInvalidException.class)
.isThrownBy(() -> underTest.putHeader(key, invalidValue))
.withMessageContaining(invalidValue)
.withMessageEndingWith("is not a valid timestamp.");
}
@Test
public void setTimestampAsString() {
final OffsetDateTime expectedTimestamp = OffsetDateTime.of(2016, 1, 12, 17, 11, 0, 0, ZoneOffset.ofHours(1));
final MessageHeaders messageHeaders = underTest.timestamp("2016-01-12T17:11:00+01:00").build();
assertThat(messageHeaders.getTimestamp()).contains(expectedTimestamp);
}
}