Skip to content

Commit

Permalink
test: asssert on actual types and their order
Browse files Browse the repository at this point in the history
instead of strings. Add tests for passing comma separated values in one
order param. Add test for the mixed case of comma separated values in one
order param with a repeated param
  • Loading branch information
teleivo committed Mar 1, 2024
1 parent d245c75 commit 3dfda6b
Showing 1 changed file with 51 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@

import static org.hamcrest.core.StringContains.containsString;
import static org.hisp.dhis.dxf2.webmessage.WebMessageUtils.ok;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.List;
import org.hisp.dhis.common.SortDirection;
import org.hisp.dhis.dxf2.webmessage.WebMessage;
import org.hisp.dhis.webapi.controller.event.webrequest.OrderCriteria;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.stereotype.Controller;
import org.springframework.test.web.servlet.MockMvc;
Expand All @@ -46,8 +50,8 @@

class OrderBindingTest {
private static final String ENDPOINT = "/ordering";

private MockMvc mockMvc;
private List<OrderCriteria> actual;

@BeforeEach
public void setUp() {
Expand All @@ -59,29 +63,59 @@ public void setUp() {

@Test
void shouldReturnDefaultSortDirectionWhenNoSortDirectionIsPassedAsParameter() throws Exception {
mockMvc
.perform(get(ENDPOINT).param("order", "field"))
.andExpect(content().string(containsString("OK")))
.andExpect(content().string(containsString("field")))
.andExpect(content().string(containsString("ASC")));
mockMvc.perform(get(ENDPOINT).param("order", "field")).andExpect(status().isOk());

assertEquals(List.of(OrderCriteria.of("field", SortDirection.ASC)), actual);
}

@Test
void shouldReturnAscSortDirectionWhenAscSortDirectionIsPassedAsParameter() throws Exception {
mockMvc
.perform(get(ENDPOINT).param("order", "field:asc"))
.andExpect(content().string(containsString("OK")))
.andExpect(content().string(containsString("field")))
.andExpect(content().string(containsString("ASC")));
mockMvc.perform(get(ENDPOINT).param("order", "field:asc")).andExpect(status().isOk());

assertEquals(List.of(OrderCriteria.of("field", SortDirection.ASC)), actual);
}

@Test
void shouldReturnDescSortDirectionWhenDescSortDirectionIsPassedAsParameter() throws Exception {
mockMvc.perform(get(ENDPOINT).param("order", "field:desc")).andExpect(status().isOk());

assertEquals(List.of(OrderCriteria.of("field", SortDirection.DESC)), actual);
}

@Test
void shouldHandleMultipleOrderComponentsGivenViaOneParameter() throws Exception {
mockMvc
.perform(get(ENDPOINT).param("order", "field1:desc,field2:asc"))
.andExpect(status().isOk());

assertEquals(
List.of(
OrderCriteria.of("field1", SortDirection.DESC),
OrderCriteria.of("field2", SortDirection.ASC)),
actual);
}

@Test
void shouldHandleMultipleOrderComponentsGivenViaMultipleParameters() throws Exception {
mockMvc
.perform(get(ENDPOINT).param("order", "field1:desc").param("order", "field2:asc"))
.andExpect(status().isOk());

assertEquals(
List.of(
OrderCriteria.of("field1", SortDirection.DESC),
OrderCriteria.of("field2", SortDirection.ASC)),
actual);
}

@Disabled
@Test
void shouldReturnABadRequestWhenMixingRepeatedParameterAndCommaSeparatedValues()
throws Exception {
mockMvc
.perform(get(ENDPOINT).param("order", "field:desc"))
.andExpect(content().string(containsString("OK")))
.andExpect(content().string(containsString("field")))
.andExpect(content().string(containsString("DESC")));
.perform(
get(ENDPOINT).param("order", "field1:desc").param("order", "field2:asc,field3:desc"))
.andExpect(status().isOk());
}

@Test
Expand Down Expand Up @@ -110,9 +144,10 @@ void shouldReturnABadRequestWhenInvalidSortDirectionIsPassedAsParameterInAListOf
}

@Controller
private static class OrderingController extends CrudControllerAdvice {
private class OrderingController extends CrudControllerAdvice {
@GetMapping(value = ENDPOINT)
public @ResponseBody WebMessage getOrder(@RequestParam List<OrderCriteria> order) {
actual = order;
return ok(order.toString());
}
}
Expand Down

0 comments on commit 3dfda6b

Please sign in to comment.