1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.bremersee.comparator.model;
18
19 import static org.assertj.core.api.Assertions.assertThat;
20 import static org.assertj.core.api.InstanceOfAssertFactories.list;
21
22 import com.fasterxml.jackson.databind.ObjectMapper;
23 import java.io.StringReader;
24 import java.io.StringWriter;
25 import java.util.List;
26 import jakarta.xml.bind.JAXBContext;
27 import jakarta.xml.bind.JAXBException;
28 import jakarta.xml.bind.Marshaller;
29 import org.assertj.core.api.InstanceOfAssertFactories;
30 import org.assertj.core.api.SoftAssertions;
31 import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
32 import org.junit.jupiter.api.BeforeAll;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35
36
37
38
39
40
41 @ExtendWith(SoftAssertionsExtension.class)
42 class SortOrdersTest {
43
44 private static JAXBContext jaxbContext;
45
46
47
48
49
50
51 @BeforeAll
52 static void createJaxbContext() throws JAXBException {
53 jaxbContext = JAXBContext.newInstance(ObjectFactory.class.getPackage().getName());
54 }
55
56
57
58
59
60
61 @Test
62 void testXmlSortOrders() throws Exception {
63 SortOrder sortOrder0 = new SortOrder("i0", true, true, false);
64 SortOrder sortOrder1 = new SortOrder("i1", false, true, false);
65 SortOrder sortOrder2 = new SortOrder("i2", true, true, false);
66
67 SortOrders sortOrders = new SortOrders(List.of(sortOrder0, sortOrder1, sortOrder2));
68
69 Marshaller marshaller = jaxbContext.createMarshaller();
70 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
71
72 StringWriter sw = new StringWriter();
73
74 marshaller.marshal(sortOrders, sw);
75
76 String xmlStr = sw.toString();
77
78
79 SortOrders readFields = (SortOrders) jaxbContext.createUnmarshaller()
80 .unmarshal(new StringReader(xmlStr));
81
82 assertThat(readFields)
83 .as("Write and read xml of %s", sortOrders)
84 .isEqualTo(sortOrders);
85 }
86
87
88
89
90
91
92 @Test
93 void testJsonSortOrders() throws Exception {
94 SortOrder sortOrder0 = new SortOrder("i0", true, false, true);
95 SortOrder sortOrder1 = new SortOrder("i1", false, true, false);
96
97 SortOrders sortOrders = new SortOrders(List.of(sortOrder0, sortOrder1));
98
99 ObjectMapper om = new ObjectMapper();
100
101 String jsonStr = om.writerWithDefaultPrettyPrinter().writeValueAsString(sortOrders);
102
103
104 SortOrders readFields = om.readValue(jsonStr, SortOrders.class);
105
106 assertThat(readFields)
107 .as("Write and read json of %s", sortOrders)
108 .isEqualTo(sortOrders);
109 }
110
111
112
113
114
115
116 @Test
117 void testEqualsAndHashCode(SoftAssertions softly) {
118 SortOrder sortOrder0 = new SortOrder("i0", true, false, true);
119 SortOrder sortOrder1 = new SortOrder("i1", true, false, true);
120 SortOrder sortOrder2 = new SortOrder("i0", true, false, true);
121 SortOrder sortOrder3 = new SortOrder("i1", true, false, true);
122 SortOrders sortOrders0 = new SortOrders(List.of(sortOrder0, sortOrder1));
123 SortOrders sortOrders2 = new SortOrders(List.of(sortOrder2, sortOrder3));
124
125 softly.assertThat(sortOrders0.hashCode()).isEqualTo(sortOrders2.hashCode());
126
127
128 SortOrders sortOrders1 = sortOrders0;
129
130 softly.assertThat(sortOrders0.equals(sortOrders1)).isTrue();
131 softly.assertThat(sortOrders0.equals(sortOrders2)).isTrue();
132
133 SortOrders sortOrders3 = new SortOrders(List.of(sortOrder1, sortOrder3));
134 softly.assertThat(sortOrders3.equals(sortOrders0)).isFalse();
135
136 softly.assertThat(sortOrders0.equals(sortOrder0)).isFalse();
137
138 softly.assertThat(new SortOrders(null).equals(new SortOrders())).isTrue();
139 }
140
141
142
143
144
145
146 @Test
147 void testGetSortOrdersText(SoftAssertions softly) {
148 SortOrder sortOrder0 = new SortOrder("i0", true, false, true);
149 SortOrder sortOrder1 = new SortOrder("i1", false, true, false);
150 SortOrders sortOrders0 = new SortOrders(List.of(sortOrder0, sortOrder1));
151 String actual = sortOrders0.getSortOrdersText();
152 softly.assertThat(actual)
153 .as("Create sort orders text of %s", sortOrders0)
154 .isEqualTo("i0,asc,false,true;i1,desc,true,false");
155 softly.assertThat(sortOrders0.toString())
156 .as("toString is equal to sort orders text")
157 .isEqualTo(actual);
158
159 actual = sortOrders0.getSortOrdersText(SortOrdersTextProperties.builder()
160 .sortOrderSeparator("&")
161 .sortOrderArgsSeparator(":")
162 .build());
163 softly.assertThat(actual)
164 .as("Create sort orders text with custom properties of %s", sortOrders0)
165 .isEqualTo("i0:asc:false:true&i1:desc:true:false");
166 }
167
168
169
170
171
172
173 @Test
174 void testFromSortOrdersText(SoftAssertions softly) {
175 SortOrders actual = SortOrders.fromSortOrdersText(null);
176 softly.assertThat(actual)
177 .extracting(SortOrders::getSortOrders, list(SortOrder.class))
178 .isEmpty();
179
180 actual = SortOrders.fromSortOrdersText(
181 "field0,asc,true,true");
182 SortOrder sortOrder0 = new SortOrder("field0", true, true, true);
183 List<SortOrder> expected = List.of(sortOrder0);
184 softly.assertThat(actual)
185 .extracting(SortOrders::getSortOrders, list(SortOrder.class))
186 .containsExactlyElementsOf(expected);
187
188 actual = SortOrders.fromSortOrdersText(
189 "field0,asc,true,true"
190 + ";field1,asc,false,true"
191 + ";field2,asc,true,false"
192 + ";field3,asc,false,false"
193 + ";field4,desc,false,true"
194 + ";field5,desc,false,false");
195 SortOrder sortOrder1 = new SortOrder("field1", true, false, true);
196 SortOrder sortOrder2 = new SortOrder("field2", true, true, false);
197 SortOrder sortOrder3 = new SortOrder("field3", true, false, false);
198 SortOrder sortOrder4 = new SortOrder("field4", false, false, true);
199 SortOrder sortOrder5 = new SortOrder("field5", false, false, false);
200 expected = List.of(sortOrder0, sortOrder1, sortOrder2, sortOrder3, sortOrder4, sortOrder5);
201 softly.assertThat(actual)
202 .extracting(SortOrders::getSortOrders, list(SortOrder.class))
203 .containsExactlyElementsOf(expected);
204 }
205
206
207
208
209 @Test
210 void testFromSortOrdersTextWithProperties() {
211 SortOrdersTextProperties properties = SortOrdersTextProperties.builder()
212 .sortOrderArgsSeparator("-:-")
213 .sortOrderSeparator("&&")
214 .caseSensitiveValue("cs")
215 .caseInsensitiveValue("cis")
216 .nullIsFirstValue("nif")
217 .nullIsLastValue("nil")
218 .build();
219
220 SortOrders actual = SortOrders.fromSortOrdersText(
221 "-:-asc-:-cis-:-nif"
222 + "&&field1"
223 + "&&field2-:-desc"
224 + "&&field3-:-desc-:-cs"
225 + "&&field4-:-desc-:-cs-:-nif"
226 + "&&-:-desc",
227 properties
228 );
229
230 SortOrder sortOrder0 = new SortOrder(null, true, true, true);
231 SortOrder sortOrder1 = new SortOrder("field1", true, true, false);
232 SortOrder sortOrder2 = new SortOrder("field2", false, true, false);
233 SortOrder sortOrder3 = new SortOrder("field3", false, false, false);
234 SortOrder sortOrder4 = new SortOrder("field4", false, false, true);
235 SortOrder sortOrder5 = new SortOrder(null, false, true, false);
236 List<SortOrder> expected = List.of(sortOrder0, sortOrder1, sortOrder2, sortOrder3, sortOrder4,
237 sortOrder5);
238
239 assertThat(actual)
240 .extracting(SortOrders::getSortOrders, list(SortOrder.class))
241 .containsExactlyElementsOf(expected);
242 }
243
244
245
246
247 @Test
248 public void testIsEmpty() {
249 assertThat(SortOrders.by())
250 .extracting(SortOrders::isEmpty, InstanceOfAssertFactories.BOOLEAN)
251 .isTrue();
252 }
253
254
255
256
257 @Test
258 public void testIsUnsorted() {
259 assertThat(SortOrders.by())
260 .extracting(SortOrders::isUnsorted, InstanceOfAssertFactories.BOOLEAN)
261 .isTrue();
262 }
263
264
265
266
267 @Test
268 public void testIsSorted() {
269 assertThat(SortOrders.by(SortOrder.by("home")))
270 .extracting(SortOrders::isSorted, InstanceOfAssertFactories.BOOLEAN)
271 .isTrue();
272 }
273
274 }