From: Avi Gaffa Date: Wed, 18 Jul 2018 11:35:02 +0000 (+0000) Subject: Merge "Pagination of workflow list API" X-Git-Tag: 1.3.0~98 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=65aace9081f4fafde2554704ead8d0ce0c285d5e;p=sdc%2Fsdc-workflow-designer.git Merge "Pagination of workflow list API" --- 65aace9081f4fafde2554704ead8d0ce0c285d5e diff --cc workflow/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerTest.java index 454a5d38,7c275052..1f36759d --- a/workflow/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerTest.java +++ b/workflow/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerTest.java @@@ -96,7 -117,117 +117,117 @@@ public class WorkflowControllerTest .andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$.results", hasSize(numOfWorkflows))); } + @Test + public void shouldReturnSortedLimitOffsetAppliedWorkflows() throws Exception { + List 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 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 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 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 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 + /* @Test public void shouldCreateWorkflowWhenCallingPostRESTRequest() throws Exception { Item item = new Item(); item.setId("abc");