-
Notifications
You must be signed in to change notification settings - Fork 215
/
DefaultMetadataHeaderKeyTest.java
185 lines (157 loc) · 7.54 KB
/
DefaultMetadataHeaderKeyTest.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
/*
* 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.base.model.headers.metadata;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.mutabilitydetector.unittesting.AllowedReason.provided;
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf;
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonKey;
import org.eclipse.ditto.json.JsonPointer;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.assertj.core.api.AutoCloseableSoftAssertions;
import org.junit.Test;
/**
* Unit tests for {@link org.eclipse.ditto.base.model.headers.metadata.DefaultMetadataHeaderKey}.
*/
public final class DefaultMetadataHeaderKeyTest {
@Test
public void assertImmutability() {
assertInstancesOf(DefaultMetadataHeaderKey.class,
areImmutable(),
provided(JsonPointer.class).isAlsoImmutable());
}
@Test
public void testHashCodeAndEquals() {
EqualsVerifier.forClass(DefaultMetadataHeaderKey.class)
.usingGetClass()
.verify();
}
@Test
public void tryToGetInstanceWithNullPath() {
assertThatNullPointerException()
.isThrownBy(() -> DefaultMetadataHeaderKey.of(null))
.withMessage("The path must not be null!")
.withNoCause();
}
@Test
public void tryToGetInstanceWithEmptyPath() {
assertThatIllegalArgumentException()
.isThrownBy(() -> DefaultMetadataHeaderKey.of(JsonPointer.empty()))
.withMessage("The path of a metadata header key must not be empty!")
.withNoCause();
}
@Test
public void wildcardPathHasTooFewLevels() {
assertThatIllegalArgumentException()
.isThrownBy(() -> DefaultMetadataHeaderKey.of(JsonPointer.of("/*")))
.withMessage("A wildcard path of a metadata header key must have exactly two levels but it had <1>!")
.withNoCause();
}
@Test
public void wildcardPathHasTooManyLevels() {
assertThatIllegalArgumentException()
.isThrownBy(() -> DefaultMetadataHeaderKey.of(JsonPointer.of("/*/foo/meta")))
.withMessage("A wildcard path of a metadata header key must have exactly two levels but it had <3>!")
.withNoCause();
}
@Test
public void tryToParseNullString() {
assertThatNullPointerException()
.isThrownBy(() -> DefaultMetadataHeaderKey.parse(null))
.withMessage("The key must not be null!")
.withNoCause();
}
@Test
public void tryToParseEmptyString() {
assertThatIllegalArgumentException()
.isThrownBy(() -> DefaultMetadataHeaderKey.parse(""))
.withMessage("The argument 'key' must not be empty!")
.withNoCause();
}
@Test
public void parseValidString() {
final JsonPointer path = JsonPointer.of("/foo/bar");
final DefaultMetadataHeaderKey parsed = DefaultMetadataHeaderKey.parse(path);
assertThat(parsed).isEqualTo(DefaultMetadataHeaderKey.of(path));
}
@Test
public void getSpecificPath() {
final JsonPointer path = JsonPointer.of("/foo/bar/baz");
final MetadataHeaderKey underTest = DefaultMetadataHeaderKey.of(path);
assertThat((CharSequence) underTest.getPath()).isEqualTo(path);
}
@Test
public void getPathOfWildcardKey() {
final JsonKey leaf = JsonKey.of("baz");
final JsonPointer path = JsonFactory.newPointer(DefaultMetadataHeaderKey.HIERARCHY_WILDCARD, leaf);
final MetadataHeaderKey underTest = DefaultMetadataHeaderKey.of(path);
assertThat((CharSequence) underTest.getPath()).isEqualTo(leaf.asPointer());
}
@Test
public void pathWithoutLeadingAsteriskDoesNotApplyToAllLeaves() {
final JsonPointer path = JsonPointer.of("/foo/bar/baz");
final MetadataHeaderKey underTest = DefaultMetadataHeaderKey.of(path);
assertThat(underTest.appliesToAllLeaves()).isFalse();
}
@Test
public void pathWithLeadingAsteriskAppliesToAllLeaves() {
final JsonPointer path = JsonPointer.of("/*/baz");
final MetadataHeaderKey underTest = DefaultMetadataHeaderKey.of(path);
assertThat(underTest.appliesToAllLeaves()).isTrue();
}
@Test
public void toStringReturnsExpected() {
final JsonPointer path = JsonPointer.of("/foo/bar/baz");
final DefaultMetadataHeaderKey underTest = DefaultMetadataHeaderKey.of(path);
assertThat(underTest).hasToString(path.toString());
}
@Test
public void compareToWorksAsExpected() {
final DefaultMetadataHeaderKey keyWithSpecificPathB = DefaultMetadataHeaderKey.of(JsonPointer.of("/foo/bar/baz"));
final DefaultMetadataHeaderKey keyWithWildcardPathB = DefaultMetadataHeaderKey.of(JsonPointer.of("/*/baz"));
final DefaultMetadataHeaderKey keyWithSpecificPathC = DefaultMetadataHeaderKey.of(JsonPointer.of("/foo/bar/chumble"));
final DefaultMetadataHeaderKey keyWithWildcardPathC = DefaultMetadataHeaderKey.of(JsonPointer.of("/*/chumble"));
try (final AutoCloseableSoftAssertions softly = new AutoCloseableSoftAssertions()) {
softly.assertThat(keyWithSpecificPathB.compareTo(keyWithSpecificPathB))
.as("[specific path/specific path] key is equal to itself")
.isZero();
softly.assertThat(keyWithSpecificPathB)
.as("[specific path/specific path] key is less than other key")
.isLessThan(keyWithSpecificPathC);
softly.assertThat(keyWithSpecificPathC)
.as("[specific path/specific/path] key is greater than other key")
.isGreaterThan(keyWithSpecificPathB);
softly.assertThat(keyWithWildcardPathB.compareTo(keyWithWildcardPathB))
.as("[wildcard path/wildcard path] key is equal to itself")
.isZero();
softly.assertThat(keyWithWildcardPathB)
.as("[wildcard path/wildcard path] key is less than other key")
.isLessThan(keyWithWildcardPathC);
softly.assertThat(keyWithWildcardPathB)
.as("[wildcard path/wildcard path] key is greater than other key")
.isLessThan(keyWithSpecificPathB);
softly.assertThat(keyWithWildcardPathC)
.as("[wildcard path/specific path] key is less than other key")
.isLessThan(keyWithSpecificPathB);
softly.assertThat(keyWithSpecificPathB)
.as("[specific path/wildcard path] key is greater than other key")
.isGreaterThan(keyWithWildcardPathC);
softly.assertThat(keyWithSpecificPathC)
.as("[specific path/wildcard path] key is greater than other key")
.isGreaterThan(keyWithWildcardPathC);
}
}
}