-
Notifications
You must be signed in to change notification settings - Fork 215
/
ThingsConditionalHeadersValidatorTest.java
240 lines (197 loc) · 11.8 KB
/
ThingsConditionalHeadersValidatorTest.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
/*
* 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.things.service.persistence.actors.strategies.commands;
import static java.text.MessageFormat.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.eclipse.ditto.base.model.signals.commands.Command.Category.DELETE;
import static org.eclipse.ditto.base.model.signals.commands.Command.Category.MODIFY;
import static org.eclipse.ditto.base.model.signals.commands.Command.Category.QUERY;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf;
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;
import java.util.Optional;
import javax.annotation.Nullable;
import org.assertj.core.api.ThrowableAssertAlternative;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.entitytag.EntityTag;
import org.eclipse.ditto.base.model.headers.entitytag.EntityTagMatchers;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.internal.utils.headers.conditional.ConditionalHeadersValidator;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.base.model.signals.commands.Command.Category;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingPreconditionFailedException;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingPreconditionNotModifiedException;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThing;
import org.junit.Ignore;
import org.junit.Test;
/**
* Tests the {@link ConditionalHeadersValidator} provided by {@link ThingsConditionalHeadersValidatorProvider}.
*/
public class ThingsConditionalHeadersValidatorTest {
private static final String IF_MATCH_PRECONDITION_FAILED_MESSAGE_PATTERN =
"The comparison of precondition header ''if-match'' for the requested Thing resource evaluated to false. " +
"Header value: ''{0}'', actual entity-tag: ''{1}''.";
private static final String IF_NONE_MATCH_PRECONDITION_FAILED_MESSAGE_PATTERN =
"The comparison of precondition header ''if-none-match'' for the requested Thing resource evaluated to " +
"false. Header value: ''{0}'', actual entity-tag: ''{1}''.";
private static final String IF_NONE_MATCH_NOT_MODIFIED_MESSAGE_PATTERN =
"The comparison of precondition header ''if-none-match'' for the requested Thing resource evaluated to " +
"false. Expected: ''{0}'' not to match actual: ''{1}''.";
private static final ConditionalHeadersValidator SUT = ThingsConditionalHeadersValidatorProvider.getInstance();
@Test
@Ignore("https://github.com/MutabilityDetector/MutabilityDetector/issues/185")
public void assertImmutability() {
assertInstancesOf(ThingsConditionalHeadersValidatorProvider.class, areImmutable());
}
@Test
public void doesNotThrowExceptionsIfAllChecksSucceed() {
final String ifMatchHeaderValue = "\"rev:1\"";
final String ifNoneMatchHeaderValue = "\"rev:2\"";
final EntityTag actualEntityTag = EntityTag.fromString("\"rev:1\"");
final Command commandMock = createCommandMock(MODIFY, ifMatchHeaderValue, ifNoneMatchHeaderValue);
SUT.checkConditionalHeaders(commandMock, actualEntityTag);
}
@Test
public void doesNotThrowExceptionsIfCheckIsSkippedBecauseOfNullAndQuery() {
assertSkipForCommandCategory(QUERY);
}
@Test
public void doesNotThrowExceptionsIfCheckIsSkippedBecauseOfNullAndDelete() {
assertSkipForCommandCategory(DELETE);
}
@Test
public void doesNotSkipIfEntityTagIsNullAndCategoryIsModify() {
final String ifMatchHeaderValue = "\"rev:2\"";
final String ifNoneMatchHeaderValue = "\"rev:3\"";
final EntityTag actualEntityTag = null;
final String expectedMessage =
format(IF_MATCH_PRECONDITION_FAILED_MESSAGE_PATTERN, ifMatchHeaderValue,
String.valueOf(actualEntityTag));
assertPreconditionFailed(ifMatchHeaderValue, ifNoneMatchHeaderValue, actualEntityTag, MODIFY, expectedMessage);
}
@Test
public void throwsThingPreconditionFailedExceptionWhenIfMatchFails() {
final String ifMatchHeaderValue = "\"rev:2\"";
final String ifNoneMatchHeaderValue = "\"rev:3\"";
final EntityTag actualEntityTag = EntityTag.fromString("\"rev:1\"");
final String expectedMessage =
format(IF_MATCH_PRECONDITION_FAILED_MESSAGE_PATTERN, ifMatchHeaderValue, actualEntityTag);
assertPreconditionFailed(ifMatchHeaderValue, ifNoneMatchHeaderValue, actualEntityTag, DELETE, expectedMessage);
assertPreconditionFailed(ifMatchHeaderValue, ifNoneMatchHeaderValue, actualEntityTag, MODIFY, expectedMessage);
assertPreconditionFailed(ifMatchHeaderValue, ifNoneMatchHeaderValue, actualEntityTag, QUERY, expectedMessage);
}
@Test
public void throwsThingPreconditionFailedExceptionWhenIfNoneMatchFailsAndCategoryIsNotQuery() {
final String ifMatchHeaderValue = "\"rev:1\"";
final String ifNoneMatchHeaderValue = "\"rev:1\"";
final EntityTag actualEntityTag = EntityTag.fromString("\"rev:1\"");
final String expectedMessage =
format(IF_NONE_MATCH_PRECONDITION_FAILED_MESSAGE_PATTERN, ifNoneMatchHeaderValue, actualEntityTag);
assertPreconditionFailed(ifMatchHeaderValue, ifNoneMatchHeaderValue, actualEntityTag, DELETE, expectedMessage);
assertPreconditionFailed(ifMatchHeaderValue, ifNoneMatchHeaderValue, actualEntityTag, MODIFY, expectedMessage);
}
@Test
public void throwsThingPreconditionNotModifiedExceptionWhenIfNoneMatchFailsAndCategoryIsQuery() {
final String ifMatchHeaderValue = "\"rev:1\"";
final String ifNoneMatchHeaderValue = "\"rev:1\"";
final EntityTag actualEntityTag = EntityTag.fromString("\"rev:1\"");
final String expectedMessage =
format(IF_NONE_MATCH_NOT_MODIFIED_MESSAGE_PATTERN, ifNoneMatchHeaderValue, actualEntityTag);
assertNotModified(ifMatchHeaderValue, ifNoneMatchHeaderValue, actualEntityTag, expectedMessage);
}
@Test
public void assertNotThrowingNotModifiedWhenSelectedFieldsContainsPolicy() {
final String ifNoneMatchHeaderValue = "\"rev:1\"";
final EntityTag actualEntityTag = EntityTag.fromString("\"rev:1\"");
final RetrieveThing retrieveThing = createRetrieveThingCommand(ifNoneMatchHeaderValue, "_policy");
SUT.checkConditionalHeaders(retrieveThing, actualEntityTag);
}
@Test
public void assertThrowingNotModifiedWhenSelectedFieldDoesNotContainPolicy() {
final String ifNoneMatchHeaderValue = "\"rev:1\"";
final EntityTag actualEntityTag = EntityTag.fromString("\"rev:1\"");
final String expectedMessage =
format(IF_NONE_MATCH_NOT_MODIFIED_MESSAGE_PATTERN, ifNoneMatchHeaderValue, actualEntityTag);
final RetrieveThing retrieveThing = createRetrieveThingCommand(ifNoneMatchHeaderValue, "_revision");
final ThrowableAssertAlternative<ThingPreconditionNotModifiedException> assertion =
assertThatExceptionOfType(ThingPreconditionNotModifiedException.class)
.isThrownBy(() -> SUT.checkConditionalHeaders(retrieveThing, actualEntityTag))
.withMessage(expectedMessage);
assertion.satisfies(exception -> assertETagHeaderInDre(exception, actualEntityTag));
}
private RetrieveThing createRetrieveThingCommand(final String ifNoneMatchHeaderValue, final String selectedFields) {
final JsonFieldSelector fieldSelector = JsonFieldSelector.newInstance(selectedFields);
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder()
.ifNoneMatch(EntityTagMatchers.fromCommaSeparatedString(ifNoneMatchHeaderValue))
.build();
return RetrieveThing.getBuilder(ThingId.of("under:test"), dittoHeaders).withSelectedFields(fieldSelector).build();
}
private Command createCommandMock(final Category commandCategory, final String ifMatchHeaderValue,
final String ifNoneMatchHeaderValue) {
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder()
.ifMatch(EntityTagMatchers.fromCommaSeparatedString(ifMatchHeaderValue))
.ifNoneMatch(EntityTagMatchers.fromCommaSeparatedString(ifNoneMatchHeaderValue))
.build();
final Command commandMock = mock(Command.class);
when(commandMock.getDittoHeaders()).thenReturn(dittoHeaders);
when(commandMock.getCategory()).thenReturn(commandCategory);
return commandMock;
}
private void assertSkipForCommandCategory(final Category commandCategory) {
final String ifMatchHeaderValue = "\"rev:2\"";
final String ifNoneMatchHeaderValue = "\"rev:1\"";
final EntityTag actualEntityTag = null;
final Command commandMock =
createCommandMock(commandCategory, ifMatchHeaderValue, ifNoneMatchHeaderValue);
SUT.checkConditionalHeaders(commandMock, actualEntityTag);
}
private void assertETagHeaderInDre(final DittoRuntimeException dre, final EntityTag expectedEntityTag) {
final Optional<EntityTag> eTag = dre.getDittoHeaders().getETag();
assertThat(eTag).isPresent();
assertThat(eTag).contains(expectedEntityTag);
}
private void assertPreconditionFailed(final String ifMatchHeaderValue,
final String ifNoneMatchHeaderValue, @Nullable final EntityTag actualEntityTag,
final Category commandCategory, final String expectedMessage) {
final Command commandMock =
createCommandMock(commandCategory, ifMatchHeaderValue, ifNoneMatchHeaderValue);
final ThrowableAssertAlternative<ThingPreconditionFailedException> assertion =
assertThatExceptionOfType(ThingPreconditionFailedException.class)
.isThrownBy(() -> SUT.checkConditionalHeaders(commandMock, actualEntityTag))
.withMessage(expectedMessage);
if (actualEntityTag == null) {
assertion.satisfies(exception -> assertThat(exception.getDittoHeaders().getETag()).isNotPresent());
} else {
assertion.satisfies(exception -> assertETagHeaderInDre(exception, actualEntityTag));
}
}
private void assertNotModified(final String ifMatchHeaderValue,
final String ifNoneMatchHeaderValue, @Nullable final EntityTag actualEntityTag,
final String expectedMessage) {
final Command commandMock =
createCommandMock(QUERY, ifMatchHeaderValue, ifNoneMatchHeaderValue);
final ThrowableAssertAlternative<ThingPreconditionNotModifiedException> assertion =
assertThatExceptionOfType(ThingPreconditionNotModifiedException.class)
.isThrownBy(() -> SUT.checkConditionalHeaders(commandMock, actualEntityTag))
.withMessage(expectedMessage);
if (actualEntityTag == null) {
assertion.satisfies(exception -> assertThat(exception.getDittoHeaders().getETag()).isNotPresent());
} else {
assertion.satisfies(exception -> assertETagHeaderInDre(exception, actualEntityTag));
}
}
}