Clover icon

Magnolia REST Content Delivery 2.1

  1. Project Clover database Fri Mar 16 2018 18:21:08 CET
  2. Package info.magnolia.rest.delivery.jcr

File FilteringConditionTest.java

 

Code metrics

0
125
36
1
452
259
36
0.29
3.47
36
1

Classes

Class Line # Actions
FilteringConditionTest 52 125 0% 36 0
1.0100%
 

Contributing tests

This file is covered by 34 tests. .

Source view

1    /**
2    * This file Copyright (c) 2017-2018 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.rest.delivery.jcr;
35   
36    import static info.magnolia.rest.delivery.jcr.FilteringCondition.*;
37    import static info.magnolia.test.hamcrest.ExceptionMatcher.instanceOf;
38    import static info.magnolia.test.hamcrest.ExecutionMatcher.throwsAnException;
39    import static org.hamcrest.CoreMatchers.containsString;
40    import static org.junit.Assert.*;
41   
42    import java.time.LocalDate;
43    import java.time.LocalTime;
44    import java.time.ZoneId;
45    import java.time.ZonedDateTime;
46    import java.util.Arrays;
47   
48    import javax.ws.rs.BadRequestException;
49   
50    import org.junit.Test;
51   
 
52    public class FilteringConditionTest {
 
53  1 toggle @Test
54    public void withStringValue() {
55    // WHEN
56  1 FilteringCondition filteringCondition = new FilteringCondition("title", "About");
57   
58    // THEN
59  1 assertEquals("[title] = 'About'", filteringCondition.asSqlString());
60    }
61   
 
62  1 toggle @Test
63    public void withNumberValue() {
64    // WHEN
65  1 FilteringCondition filteringCondition = new FilteringCondition("size", "123");
66   
67    // THEN
68  1 assertEquals("[size] = 123", filteringCondition.asSqlString());
69    }
70   
 
71  1 toggle @Test
72    public void withFloatingPointNumberValue() {
73    // WHEN
74  1 FilteringCondition filteringCondition = new FilteringCondition("price", "99.99");
75   
76    // THEN
77  1 assertEquals("[price] = 99.99", filteringCondition.asSqlString());
78    }
79   
 
80  1 toggle @Test
81    public void withBooleanValue() {
82    // WHEN
83  1 FilteringCondition filteringCondition = new FilteringCondition("available", "true");
84   
85    // THEN
86  1 assertEquals("[available] = 'true'", filteringCondition.asSqlString());
87    }
88   
 
89  1 toggle @Test
90    public void withDateTimeValue() {
91    // WHEN
92  1 FilteringCondition filteringCondition = new FilteringCondition("publishedDate", "2015-01-26T23:17:02.000+07:00");
93   
94    // THEN
95  1 assertEquals("[publishedDate] = CAST('2015-01-26T23:17:02.000+07:00' AS DATE)", filteringCondition.asSqlString());
96    }
97   
 
98  1 toggle @Test
99    public void withSingleSimpleDateValue() {
100    // GIVEN
101  1 String dateString = "2018-02-21";
102  1 String startDateTime = convertDateToStartOfIsoDateTime(dateString);
103  1 String endDateTime = convertDateToEndOfIsoDateTime(dateString);
104   
105    // WHEN
106  1 FilteringCondition filteringCondition = new FilteringCondition("birthday", dateString);
107   
108    // THEN
109  1 assertEquals("[birthday] >= CAST('" + startDateTime + "' AS DATE) AND [birthday] <= CAST('" + endDateTime + "' AS DATE)", filteringCondition.asSqlString());
110    }
111   
 
112  1 toggle @Test
113    public void withMultipleNumberValues() {
114    // WHEN
115  1 FilteringCondition filteringCondition = new FilteringCondition("size", "100|200");
116   
117    // THEN
118  1 assertEquals("[size] = 100 OR [size] = 200", filteringCondition.asSqlString());
119    }
120   
 
121  1 toggle @Test
122    public void withPathParam() {
123    // WHEN
124  1 FilteringCondition filteringCondition = new FilteringCondition("@ancestor", "/travel/about");
125   
126    // THEN
127  1 assertEquals("ISDESCENDANTNODE('/travel/about')", filteringCondition.asSqlString());
128    }
129   
 
130  1 toggle @Test
131    public void withNameParam() {
132    // WHEN
133  1 FilteringCondition filteringCondition = new FilteringCondition("@name", "travel");
134   
135    // THEN
136  1 assertEquals("LOWER(LOCALNAME(t)) LIKE 'travel'", filteringCondition.asSqlString());
137    }
138   
 
139  1 toggle @Test
140    public void withNameParamAndValueIsNotActuallyANumber() {
141    // WHEN
142  1 FilteringCondition filteringCondition = new FilteringCondition("@name", "00");
143   
144    // THEN
145  1 assertEquals("LOWER(LOCALNAME(t)) LIKE '00'", filteringCondition.asSqlString());
146    }
147   
 
148  1 toggle @Test
149    public void paramNameContainsOperator() {
150    // WHEN
151  1 FilteringCondition filteringCondition = new FilteringCondition("price[gte]", "100");
152   
153    // THEN
154  1 assertEquals("[price] >= 100", filteringCondition.asSqlString());
155    }
156   
 
157  1 toggle @Test
158    public void paramNameAndOperatorAndMultipleValues() {
159    // WHEN
160  1 FilteringCondition filteringCondition = new FilteringCondition("price[ne]", "100|200");
161   
162    // THEN
163  1 assertEquals("[price] <> 100 OR [price] <> 200", filteringCondition.asSqlString());
164    }
165   
 
166  1 toggle @Test
167    public void inARangeOfNumbers() {
168    // WHEN
169  1 FilteringCondition filteringCondition = new FilteringCondition("price[in]", "-5~10");
170   
171    // THEN
172  1 assertEquals("[price] >= -5 AND [price] <= 10", filteringCondition.asSqlString());
173    }
174   
 
175  1 toggle @Test
176    public void inMultipleRangesOfNumbers() {
177    // WHEN
178  1 FilteringCondition filteringCondition = new FilteringCondition("price[in]", "-5~10|20~30");
179   
180    // THEN
181  1 assertEquals("[price] >= -5 AND [price] <= 10 OR [price] >= 20 AND [price] <= 30", filteringCondition.asSqlString());
182    }
183   
 
184  1 toggle @Test
185    public void inARangeOfDates() {
186    // GIVEN
187  1 String fromDate = "2015-10-20";
188  1 String fromIsoDateTime = convertDateToStartOfIsoDateTime(fromDate);
189  1 String toDate = "2015-10-25";
190  1 String toIsoDateTime = convertDateToEndOfIsoDateTime(toDate);
191  1 String expectedResult = String.format("[mgnl:created] >= CAST('%s' AS DATE) AND [mgnl:created] <= CAST('%s' AS DATE)",
192    fromIsoDateTime, toIsoDateTime);
193   
194    // WHEN
195  1 FilteringCondition filteringCondition = new FilteringCondition("mgnl:created[in]", String.format("%s~%s", fromDate, toDate));
196   
197    // THEN
198  1 assertEquals(expectedResult, filteringCondition.asSqlString());
199    }
200   
 
201  8 toggle private String convertDateToStartOfIsoDateTime(String date) {
202  8 LocalDate localDate = LocalDate.parse(date, DATE_FORMATTER);
203  8 ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
204  8 return zonedDateTime.format(DATE_TIME_FORMATTER);
205    }
206   
 
207  8 toggle private String convertDateToEndOfIsoDateTime(String date) {
208  8 LocalDate localDate = LocalDate.parse(date, DATE_FORMATTER);
209  8 ZonedDateTime zonedDateTime = localDate.atTime(LocalTime.MAX).atZone(ZoneId.systemDefault());
210  8 return zonedDateTime.format(DATE_TIME_FORMATTER);
211    }
212   
 
213  1 toggle @Test
214    public void inMultipleRangesOfDates() {
215    // GIVEN
216  1 String fromDate1 = "2015-10-20";
217  1 String fromIsoDateTime1 = convertDateToStartOfIsoDateTime(fromDate1);
218  1 String toDate1 = "2015-10-25";
219  1 String toIsoDateTime1 = convertDateToEndOfIsoDateTime(toDate1);
220  1 String fromDate2 = "2017-10-20";
221  1 String fromIsoDateTime2 = convertDateToStartOfIsoDateTime(fromDate2);
222  1 String toDate2 = "2017-10-25";
223  1 String toIsoDateTime2 = convertDateToEndOfIsoDateTime(toDate2);
224  1 String expectedResult = String.format("[mgnl:created] >= CAST('%s' AS DATE) AND [mgnl:created] <= CAST('%s' AS DATE) OR [mgnl:created] >= CAST('%s' AS DATE) AND [mgnl:created] <= CAST('%s' AS DATE)",
225    fromIsoDateTime1, toIsoDateTime1, fromIsoDateTime2, toIsoDateTime2);
226   
227    // WHEN
228  1 FilteringCondition filteringCondition = new FilteringCondition("mgnl:created[in]", String.format("%s~%s|%s~%s", fromDate1, toDate1, fromDate2, toDate2));
229   
230    // THEN
231  1 assertEquals(expectedResult, filteringCondition.asSqlString());
232    }
233   
 
234  1 toggle @Test
235    public void inARangeOfDateTimes() {
236    // GIVEN
237  1 String fromDate = "2015-12-26T23:17:02.000+01:00";
238  1 String toDate = "2015-12-26T23:17:02.000+01:00";
239  1 String expectedResult = String.format("[mgnl:created] >= CAST('%s' AS DATE) AND [mgnl:created] <= CAST('%s' AS DATE)", fromDate, toDate);
240   
241    // WHEN
242  1 FilteringCondition filteringCondition = new FilteringCondition("mgnl:created[in]", String.format("%s~%s", fromDate, toDate));
243   
244    // THEN
245  1 assertEquals(expectedResult, filteringCondition.asSqlString());
246    }
247   
 
248  1 toggle @Test
249    public void inMultipleRangesOfDateTimes() {
250    // GIVEN
251  1 String fromDate1 = "2015-12-20T23:17:02.000+01:00";
252  1 String toDate1 = "2015-12-25T23:17:02.000+01:00";
253  1 String fromDate2 = "2017-12-20T23:17:02.000+01:00";
254  1 String toDate2 = "2017-12-25T23:17:02.000+01:00";
255  1 String expectedResult = String.format("[mgnl:created] >= CAST('%s' AS DATE) AND [mgnl:created] <= CAST('%s' AS DATE) OR [mgnl:created] >= CAST('%s' AS DATE) AND [mgnl:created] <= CAST('%s' AS DATE)",
256    fromDate1, toDate1, fromDate2, toDate2);
257   
258    // WHEN
259  1 FilteringCondition filteringCondition = new FilteringCondition("mgnl:created[in]", String.format("%s~%s|%s~%s", fromDate1, toDate1, fromDate2, toDate2));
260   
261    // THEN
262  1 assertEquals(expectedResult, filteringCondition.asSqlString());
263    }
264   
 
265  1 toggle @Test
266    public void notInARangeOfNumbers() {
267    // WHEN
268  1 FilteringCondition filteringCondition = new FilteringCondition("price[not-in]", "-5~10");
269   
270    // THEN
271  1 assertEquals("[price] < -5 OR [price] > 10", filteringCondition.asSqlString());
272    }
273   
 
274  1 toggle @Test
275    public void notInARangeOfDates() {
276    // GIVEN
277  1 String fromDate = "2015-10-20";
278  1 String fromIsoDateTime = convertDateToStartOfIsoDateTime(fromDate);
279  1 String toDate = "2015-10-25";
280  1 String toIsoDateTime = convertDateToEndOfIsoDateTime(toDate);
281  1 String expectedResult = String.format("[mgnl:created] < CAST('%s' AS DATE) OR [mgnl:created] > CAST('%s' AS DATE)",
282    fromIsoDateTime, toIsoDateTime);
283   
284    // WHEN
285  1 FilteringCondition filteringCondition = new FilteringCondition("mgnl:created[not-in]", String.format("%s~%s", fromDate, toDate));
286   
287    // THEN
288  1 assertEquals(expectedResult, filteringCondition.asSqlString());
289    }
290   
 
291  1 toggle @Test
292    public void notInARangeOfDateTimes() {
293    // GIVEN
294  1 String fromDate1 = "2015-12-20T23:17:02.000+01:00";
295  1 String toDate1 = "2015-12-25T23:17:02.000+01:00";
296  1 String fromDate2 = "2017-12-20T23:17:02.000+01:00";
297  1 String toDate2 = "2017-12-25T23:17:02.000+01:00";
298   
299  1 String expectedResult = String.format("[mgnl:created] < CAST('%s' AS DATE) OR [mgnl:created] > CAST('%s' AS DATE)",
300    fromDate1, toDate1, fromDate2, toDate2);
301   
302    // WHEN
303  1 FilteringCondition filteringCondition = new FilteringCondition("mgnl:created[not-in]", String.format("%s~%s", fromDate1, toDate1, fromDate2, toDate2));
304   
305    // THEN
306  1 assertEquals(expectedResult, filteringCondition.asSqlString());
307    }
308   
 
309  1 toggle @Test
310    public void propertyStringIsEmpty() {
311  1 assertThat(() -> new FilteringCondition("", "test"),
312    throwsAnException(instanceOf(IllegalArgumentException.class)
313    .withMessage(containsString("Property string is invalid."))));
314    }
315   
 
316  1 toggle @Test
317    public void valueStringIsEmpty() {
318  1 FilteringCondition filteringCondition = new FilteringCondition("test[in]", "");
319  1 assertEquals("", filteringCondition.asSqlString());
320    }
321   
 
322  1 toggle @Test
323    public void treatEscapedValueWithQuotesAsString() {
324    // WHEN
325  1 FilteringCondition filteringCondition1 = new FilteringCondition("zipCode[ne]", "\"100\"");
326  1 FilteringCondition filteringCondition2 = new FilteringCondition("title[ne]", "\"About\"");
327   
328    // THEN
329  1 assertEquals("[zipCode] <> '100'", filteringCondition1.asSqlString());
330  1 assertEquals("[title] <> 'About'", filteringCondition2.asSqlString());
331    }
332   
 
333  1 toggle @Test
334    public void treatEscapedMultiValuesWithQuotesAsString() {
335    // WHEN
336  1 FilteringCondition filteringCondition1 = new FilteringCondition("zipCode[ne]", "\"100\"|\"200\"");
337  1 FilteringCondition filteringCondition2 = new FilteringCondition("title[ne]", "\"About\"|\"Company\"");
338   
339    // THEN
340  1 assertEquals("[zipCode] <> '100' OR [zipCode] <> '200'", filteringCondition1.asSqlString());
341  1 assertEquals("[title] <> 'About' OR [title] <> 'Company'", filteringCondition2.asSqlString());
342    }
343   
 
344  1 toggle @Test
345    public void stringContainsSingleQuoteOrQuotesIsNotEscaped() {
346    // WHEN
347  1 FilteringCondition filteringCondition1 = new FilteringCondition("price[ne]", "\"abc'abc");
348   
349    // THEN
350  1 assertEquals("[price] <> '\"abc\'abc'", filteringCondition1.asSqlString());
351    }
352   
 
353  1 toggle @Test
354    public void afterADate() {
355    // GIVEN
356  1 String date = "2018-02-21";
357  1 String dateToCompare = convertDateToEndOfIsoDateTime(date);
358   
359    // WHEN
360  1 FilteringCondition filteringCondition = new FilteringCondition("birthday[gt]", date);
361   
362    // THEN
363  1 assertEquals("[birthday] > CAST('" + dateToCompare + "' AS DATE)", filteringCondition.asSqlString());
364    }
365   
 
366  1 toggle @Test
367    public void noEarlierThanADate() {
368    // GIVEN
369  1 String date = "2018-02-21";
370  1 String dateToCompare = convertDateToStartOfIsoDateTime(date);
371   
372    // WHEN
373  1 FilteringCondition filteringCondition = new FilteringCondition("birthday[gte]", date);
374   
375    // THEN
376  1 assertEquals("[birthday] >= CAST('" + dateToCompare + "' AS DATE)", filteringCondition.asSqlString());
377    }
378   
 
379  1 toggle @Test
380    public void beforeADate() {
381    // GIVEN
382  1 String date = "2018-02-21";
383  1 String dateToCompare = convertDateToStartOfIsoDateTime(date);
384   
385    // WHEN
386  1 FilteringCondition filteringCondition = new FilteringCondition("birthday[lt]", date);
387   
388    // THEN
389  1 assertEquals("[birthday] < CAST('" + dateToCompare + "' AS DATE)", filteringCondition.asSqlString());
390    }
391   
 
392  1 toggle @Test
393    public void noLaterThanADate() {
394    // GIVEN
395  1 String date = "2018-02-21";
396  1 String dateToCompare = convertDateToEndOfIsoDateTime(date);
397   
398    // WHEN
399  1 FilteringCondition filteringCondition = new FilteringCondition("birthday[lte]", date);
400   
401    // THEN
402  1 assertEquals("[birthday] <= CAST('" + dateToCompare + "' AS DATE)", filteringCondition.asSqlString());
403    }
404   
 
405  1 toggle @Test
406    public void differentFromADate() {
407    // GIVEN
408  1 String date = "2018-02-21";
409  1 String startDateTime = convertDateToStartOfIsoDateTime(date);
410  1 String endDateTime = convertDateToEndOfIsoDateTime(date);
411   
412    // WHEN
413  1 FilteringCondition filteringCondition = new FilteringCondition("birthday[ne]", date);
414   
415    // THEN
416  1 assertEquals("[birthday] < CAST('" + startDateTime + "' AS DATE) OR [birthday] > CAST('" + endDateTime + "' AS DATE)", filteringCondition.asSqlString());
417    }
418   
 
419  1 toggle @Test
420    public void rangeIsNotSupportedForSomeOperators() {
421    // GIVEN
422  1 String fromDate = "2015-10-20";
423  1 String toDate = "2015-10-25";
424  1 String operators[] = { "gt", "gte", "lt", "lte", "eq", "ne" };
425   
426  1 Arrays.stream(operators).forEach(operator -> {
427    // WHEN
428  6 FilteringCondition filteringCondition = new FilteringCondition(String.format("birthday[%s]", operator), String.format("%s~%s", fromDate, toDate));
429   
430    // THEN
431  6 assertThat(() -> filteringCondition.asSqlString(),
432    throwsAnException(instanceOf(BadRequestException.class)
433    .withMessage(containsString("Range is not supported."))));
434    });
435    }
436   
 
437  1 toggle @Test
438    public void invalidRangeThrowsException() {
439    // WHEN & THEN
440  1 assertThat(() -> new FilteringCondition("birthday[in]", "2018-01-01~"),
441    throwsAnException(instanceOf(BadRequestException.class)
442    .withMessage(containsString("Value string is invalid."))));
443    }
444   
 
445  1 toggle @Test
446    public void mixingRangeOfDatesAndDateThrowsException() {
447    // WHEN & THEN
448  1 assertThat(() -> new FilteringCondition("mgnl:created[in]", "2015-10-20~2015-10-25|2018-03-01"),
449    throwsAnException(instanceOf(BadRequestException.class)
450    .withMessage(containsString("Value string is invalid."))));
451    }
452    }