.andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$.results", hasSize(numOfWorkflows)));
}
- @Test
+ @Test
+ public void shouldReturnSortedLimitOffsetAppliedWorkflows() throws Exception {
+ List<Workflow> workflowMocks = createLimit2AndOffset1For5WorkflowList();
+ doReturn(workflowMocks).when(workflowManagerMock).list(any());
+ mockMvc.perform(
+ get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "2", "1"))
+ .header(RestConstants.USER_ID_HEADER_PARAM, USER_ID).contentType(APPLICATION_JSON))
+ .andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$.results", hasSize(2)));
+ }
+
+ @Test
+ public void shouldReturnResultsWithDefaultWhenLimitIsNegative() throws Exception {
+ List<Workflow> workflowMocks = createLimit2AndOffset1For5WorkflowList();
+ doReturn(workflowMocks).when(workflowManagerMock).list(any());
+ MockHttpServletResponse response = mockMvc.perform(
+ get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "-2", "1"))
+ .header(RestConstants.USER_ID_HEADER_PARAM, USER_ID).contentType(APPLICATION_JSON))
+ .andDo(print()).andExpect(status().isOk()).andExpect(status().is(200)).andReturn()
+ .getResponse();
+ CollectionWrapper workflowListResponse =
+ new ObjectMapper().readValue(response.getContentAsString(), CollectionWrapper.class);
+ assertEquals(LIMIT_DEFAULT, workflowListResponse.getLimit());
+ assertEquals(1, workflowListResponse.getOffset());
+ assertEquals(2, workflowListResponse.getTotal());
+ }
+
+ @Test
+ public void shouldFallbackOnDefaultOffsetWhenOffsetIsNegative() throws Exception {
+ MockHttpServletResponse response = mockMvc.perform(
+ get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "2", "-1"))
+ .header(RestConstants.USER_ID_HEADER_PARAM, USER_ID).contentType(APPLICATION_JSON))
+ .andDo(print()).andExpect(status().isOk()).andExpect(status().is(200)).andReturn()
+ .getResponse();
+ CollectionWrapper workflowListResponse =
+ new ObjectMapper().readValue(response.getContentAsString(), CollectionWrapper.class);
+ assertEquals(2, workflowListResponse.getLimit());
+ assertEquals(OFFSET_DEFAULT, workflowListResponse.getOffset());
+ assertEquals(0, workflowListResponse.getTotal());
+ }
+
+ @Test
+ public void shouldFallbackOnDefaultLimitWhenLimitIsNotAnInteger() throws Exception {
+ MockHttpServletResponse response = mockMvc.perform(
+ get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "abc", "0"))
+ .header(RestConstants.USER_ID_HEADER_PARAM, USER_ID).contentType(APPLICATION_JSON))
+ .andDo(print()).andExpect(status().isOk()).andExpect(status().is(200)).andReturn()
+ .getResponse();
+ CollectionWrapper workflowListResponse =
+ new ObjectMapper().readValue(response.getContentAsString(), CollectionWrapper.class);
+ assertEquals(LIMIT_DEFAULT, workflowListResponse.getLimit());
+ assertEquals(0, workflowListResponse.getOffset());
+ assertEquals(0, workflowListResponse.getTotal());
+ }
+
+ @Test
+ public void shouldFallbackOnDefaultOffsetWhenOffsetIsNotAnInteger() throws Exception {
+ MockHttpServletResponse response = mockMvc.perform(
+ get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "2", "abc"))
+ .header(RestConstants.USER_ID_HEADER_PARAM, USER_ID).contentType(APPLICATION_JSON))
+ .andDo(print()).andExpect(status().isOk()).andExpect(status().is(200)).andReturn()
+ .getResponse();
+ CollectionWrapper workflowListResponse =
+ new ObjectMapper().readValue(response.getContentAsString(), CollectionWrapper.class);
+ assertEquals(2, workflowListResponse.getLimit());
+ assertEquals(OFFSET_DEFAULT, workflowListResponse.getOffset());
+ assertEquals(0, workflowListResponse.getTotal());
+ }
+
+ @Test
+ public void shouldThrowExceptionWhenSortFieldIsInvalid() throws Exception {
+ MockHttpServletResponse response = mockMvc.perform(
+ get(RestPath.getWorkflowsPathAllQueryParams("invalidSortField,asc", "2", "1"))
+ .header(RestConstants.USER_ID_HEADER_PARAM, USER_ID).contentType(APPLICATION_JSON))
+ .andDo(print()).andExpect(status().isBadRequest()).andExpect(status().is(400)).andReturn()
+ .getResponse();
+ assertEquals(String.format(INVALID_PAGINATION_PARAMETER_FORMAT, SORT_PARAM, "invalidSortField",
+ PAGINATION_PARAMETER_INVALID_SORT_FIELD_SUFFIX + getSupportedSortFields()),
+ response.getContentAsString());
+ }
+
+ @Test
+ public void shouldReturnAscSortedLimitOffsetAppliedWorkflowsWhenSortIsNotSpecified() throws Exception {
+ List<Workflow> workflowMocks = createLimit2AndOffset1For5WorkflowList();
+ doReturn(workflowMocks).when(workflowManagerMock).list(any());
+ mockMvc.perform(
+ get(RestPath.getWorkflowsPathNoSort("2", "1"))
+ .header(RestConstants.USER_ID_HEADER_PARAM, USER_ID).contentType(APPLICATION_JSON))
+ .andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$.results", hasSize(2)));
+ }
+
+ @Test
+ public void shouldReturnDefaultLimitOffsetAppliedWorkflowsWhenLimitIsNotSpecified() throws Exception {
+ List<Workflow> workflowMocks = createLimit2AndOffset1For5WorkflowList();
+ doReturn(workflowMocks).when(workflowManagerMock).list(any());
+ mockMvc.perform(
+ get(RestPath.getWorkflowsPathNoSortAndLimit("1"))
+ .header(RestConstants.USER_ID_HEADER_PARAM, USER_ID).contentType(APPLICATION_JSON))
+ .andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$.results", hasSize(2)));
+ }
+
+ @Test
+ public void shouldReturnDefaultOffsetAppliedWorkflowsWhenOffsetIsNotSpecified() throws Exception {
+ List<Workflow> workflowMocks = createLimit1WorkflowList();
+ doReturn(workflowMocks).when(workflowManagerMock).list(any());
+ mockMvc.perform(
+ get(RestPath.getWorkflowsPathNoSortAndOffset("1"))
+ .header(RestConstants.USER_ID_HEADER_PARAM, USER_ID).contentType(APPLICATION_JSON))
+ .andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$.results", hasSize(1)));
+ }
+
+ /* @Test
public void shouldCreateWorkflowWhenCallingPostRESTRequest() throws Exception {
Item item = new Item();
item.setId("abc");