|
| 1 | +/* |
| 2 | + * Copyright (c) 2004-2023, University of Oslo |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * Redistributions of source code must retain the above copyright notice, this |
| 8 | + * list of conditions and the following disclaimer. |
| 9 | + * |
| 10 | + * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer in the documentation |
| 12 | + * and/or other materials provided with the distribution. |
| 13 | + * Neither the name of the HISP project nor the names of its contributors may |
| 14 | + * be used to endorse or promote products derived from this software without |
| 15 | + * specific prior written permission. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 18 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 21 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 22 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 24 | + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 26 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + */ |
| 28 | +package org.hisp.dhis.webapi.controller.event.webrequest; |
| 29 | + |
| 30 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 33 | + |
| 34 | +import java.util.List; |
| 35 | +import org.hisp.dhis.webapi.controller.event.mapper.SortDirection; |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | +import org.junit.jupiter.params.ParameterizedTest; |
| 38 | +import org.junit.jupiter.params.provider.ValueSource; |
| 39 | + |
| 40 | +class OrderCriteriaTest { |
| 41 | + |
| 42 | + @Test |
| 43 | + void fromOrderString() { |
| 44 | + List<OrderCriteria> order = OrderCriteria.fromOrderString("one:desc,, two:asc ,three "); |
| 45 | + |
| 46 | + assertEquals( |
| 47 | + List.of( |
| 48 | + OrderCriteria.of("one", SortDirection.DESC), |
| 49 | + OrderCriteria.of("two", SortDirection.ASC), |
| 50 | + OrderCriteria.of("three", SortDirection.ASC)), |
| 51 | + order); |
| 52 | + } |
| 53 | + |
| 54 | + @ValueSource(strings = {"one:desc", "one:Desc", "one:DESC"}) |
| 55 | + @ParameterizedTest |
| 56 | + void fromOrderStringSortDirectionParsingIsCaseInsensitive(String source) { |
| 57 | + List<OrderCriteria> order = OrderCriteria.fromOrderString(source); |
| 58 | + |
| 59 | + assertEquals(List.of(OrderCriteria.of("one", SortDirection.DESC)), order); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void fromOrderStringDefaultsToAscGivenFieldAndColon() { |
| 64 | + List<OrderCriteria> order = OrderCriteria.fromOrderString("one:"); |
| 65 | + |
| 66 | + assertEquals(List.of(OrderCriteria.of("one", SortDirection.ASC)), order); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void failGivenAnUnknownSortDirection() { |
| 71 | + assertThrows(IllegalArgumentException.class, () -> OrderCriteria.fromOrderString("one:wrong")); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void fromOrderStringReturnsEmptyListGivenEmptyOrder() { |
| 76 | + List<OrderCriteria> orderCriteria = OrderCriteria.fromOrderString(" "); |
| 77 | + |
| 78 | + assertNotNull(orderCriteria); |
| 79 | + assertEquals( |
| 80 | + 0, orderCriteria.size(), String.format("Expected 0 items, instead got %s", orderCriteria)); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void failGivenMoreThanTwoColons() { |
| 85 | + assertThrows( |
| 86 | + IllegalArgumentException.class, () -> OrderCriteria.fromOrderString("one:desc:wrong")); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void failGivenEmptyField() { |
| 91 | + assertThrows(IllegalArgumentException.class, () -> OrderCriteria.valueOf(" :desc")); |
| 92 | + } |
| 93 | +} |
0 commit comments