|
| 1 | +/* |
| 2 | + * Copyright (c) 2004-2022, 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; |
| 29 | + |
| 30 | +import static org.hamcrest.core.StringContains.containsString; |
| 31 | +import static org.hisp.dhis.dxf2.webmessage.WebMessageUtils.ok; |
| 32 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 33 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 34 | + |
| 35 | +import java.util.List; |
| 36 | +import org.hisp.dhis.dxf2.webmessage.WebMessage; |
| 37 | +import org.hisp.dhis.webapi.controller.event.webrequest.OrderCriteria; |
| 38 | +import org.junit.jupiter.api.BeforeEach; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | +import org.springframework.stereotype.Controller; |
| 41 | +import org.springframework.test.web.servlet.MockMvc; |
| 42 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 43 | +import org.springframework.web.bind.annotation.GetMapping; |
| 44 | +import org.springframework.web.bind.annotation.RequestParam; |
| 45 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 46 | + |
| 47 | +class OrderBindingTest { |
| 48 | + private static final String ENDPOINT = "/ordering"; |
| 49 | + |
| 50 | + private MockMvc mockMvc; |
| 51 | + |
| 52 | + @BeforeEach |
| 53 | + public void setUp() { |
| 54 | + mockMvc = |
| 55 | + MockMvcBuilders.standaloneSetup(new OrderingController()) |
| 56 | + .setControllerAdvice(new CrudControllerAdvice()) |
| 57 | + .build(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void shouldReturnDefaultSortDirectionWhenNoSortDirectionIsPassedAsParameter() throws Exception { |
| 62 | + mockMvc |
| 63 | + .perform(get(ENDPOINT).param("order", "field")) |
| 64 | + .andExpect(content().string(containsString("OK"))) |
| 65 | + .andExpect(content().string(containsString("field"))) |
| 66 | + .andExpect(content().string(containsString("ASC"))); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void shouldReturnAscSortDirectionWhenAscSortDirectionIsPassedAsParameter() throws Exception { |
| 71 | + mockMvc |
| 72 | + .perform(get(ENDPOINT).param("order", "field:asc")) |
| 73 | + .andExpect(content().string(containsString("OK"))) |
| 74 | + .andExpect(content().string(containsString("field"))) |
| 75 | + .andExpect(content().string(containsString("ASC"))); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void shouldReturnDescSortDirectionWhenDescSortDirectionIsPassedAsParameter() throws Exception { |
| 80 | + mockMvc |
| 81 | + .perform(get(ENDPOINT).param("order", "field:desc")) |
| 82 | + .andExpect(content().string(containsString("OK"))) |
| 83 | + .andExpect(content().string(containsString("field"))) |
| 84 | + .andExpect(content().string(containsString("DESC"))); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void shouldReturnABadRequestWhenInvalidSortDirectionIsPassedAsParameter() throws Exception { |
| 89 | + mockMvc |
| 90 | + .perform(get(ENDPOINT).param("order", "field:wrong")) |
| 91 | + .andExpect(content().string(containsString("Bad Request"))) |
| 92 | + .andExpect( |
| 93 | + content() |
| 94 | + .string( |
| 95 | + containsString( |
| 96 | + "'wrong' is not a valid sort direction. Valid values are: [ASC, DESC]"))); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void shouldReturnABadRequestWhenInvalidSortDirectionIsPassedAsParameterInAListOfOrders() |
| 101 | + throws Exception { |
| 102 | + mockMvc |
| 103 | + .perform(get(ENDPOINT).param("order", "field1:wrong").param("order", "field2:asc")) |
| 104 | + .andExpect(content().string(containsString("Bad Request"))) |
| 105 | + .andExpect( |
| 106 | + content() |
| 107 | + .string( |
| 108 | + containsString( |
| 109 | + "'wrong' is not a valid sort direction. Valid values are: [ASC, DESC]"))); |
| 110 | + } |
| 111 | + |
| 112 | + @Controller |
| 113 | + private static class OrderingController extends CrudControllerAdvice { |
| 114 | + @GetMapping(value = ENDPOINT) |
| 115 | + public @ResponseBody WebMessage getOrder(@RequestParam List<OrderCriteria> order) { |
| 116 | + return ok(order.toString()); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments