diff options
Diffstat (limited to 'workflow-designer-be/src/test')
6 files changed, 414 insertions, 162 deletions
diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/RestPath.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/RestPath.java index a9408f15..4301feb8 100644 --- a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/RestPath.java +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/RestPath.java @@ -48,10 +48,6 @@ public class RestPath { return WORKFLOWS_URL; } - public static String getWorkflowsWithVersionStateFilterPath(String versionState) { - return String.format(WORKFLOWS_WITH_VERSION_STATE_FILTER_URL, versionState); - } - public static String getWorkflowPath(String workflowId) { return String.format(WORKFLOW_URL_FORMATTER, workflowId); } diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerTest.java index 95f7fffa..f8d2aec8 100644 --- a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerTest.java +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerTest.java @@ -4,7 +4,6 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.collection.IsCollectionWithSize.hasSize; import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.verify; import static org.onap.sdc.workflow.TestUtil.createWorkflow; @@ -21,25 +20,29 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import com.amdocs.zusammen.datatypes.Id; import com.amdocs.zusammen.datatypes.item.Item; import com.google.gson.Gson; -import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.onap.sdc.workflow.RestPath; import org.onap.sdc.workflow.api.exceptionshandlers.CustomizedResponseEntityExceptionHandler; import org.onap.sdc.workflow.persistence.types.Workflow; -import org.onap.sdc.workflow.persistence.types.WorkflowVersionState; import org.onap.sdc.workflow.services.WorkflowManager; import org.onap.sdc.workflow.services.types.Page; import org.onap.sdc.workflow.services.types.PagingRequest; +import org.onap.sdc.workflow.services.types.RequestSpec; +import org.onap.sdc.workflow.services.types.Sort; import org.springframework.data.web.PageableHandlerMethodArgumentResolver; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @RunWith(MockitoJUnitRunner.class) @@ -55,11 +58,13 @@ public class WorkflowControllerTest { private MockMvc mockMvc; + @Mock + private WorkflowManager workflowManagerMock; + @Captor + private ArgumentCaptor<RequestSpec> requestSpecArg; @InjectMocks private WorkflowController workflowController; - @Mock - private WorkflowManager workflowManagerMock; @Before public void setUp() { @@ -99,109 +104,92 @@ public class WorkflowControllerTest { } @Test - public void shouldReturn5WorkflowWhen5WorkflowsExists() throws Exception { - int numOfWorkflows = 5; - Page<Workflow> workflowMocks = createWorkflows(numOfWorkflows); - doReturn(workflowMocks).when(workflowManagerMock).list(any(), any()); - mockMvc.perform(get(RestPath.getWorkflowsPath()).header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)) - .andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(numOfWorkflows))); - } - - @Test - public void listWithValidVersionStateFilter() throws Exception { - int numOfWorkflows = 3; - Page<Workflow> workflows = createWorkflows(numOfWorkflows); - doReturn(workflows).when(workflowManagerMock) - .list(eq(Collections.singleton(WorkflowVersionState.CERTIFIED)), any()); - mockMvc.perform( - get(RestPath.getWorkflowsWithVersionStateFilterPath("CERTIFIED")).header(USER_ID_HEADER, USER_ID) - .contentType(APPLICATION_JSON)) - .andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$.paging.total", is(numOfWorkflows))) - .andExpect(jsonPath("$.items", hasSize(numOfWorkflows))); - } - - @Test - public void listWithInvalidVersionStateFilter() throws Exception { - int numOfWorkflows = 0; - Page<Workflow> workflows = createWorkflows(numOfWorkflows); - doReturn(workflows).when(workflowManagerMock).list(eq(Collections.emptySet()), any()); + public void listWhenExist() throws Exception { + mockManagerList3(); + ResultActions result = mockMvc.perform( + get(RestPath.getWorkflowsPath()).header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.items", hasSize(3))); + for (int i = 0; i < 3; i++) { + result.andExpect(jsonPath(String.format("$.items[%s].id", i), is(String.valueOf(i + 1)))); + } - mockMvc.perform( - get(RestPath.getWorkflowsWithVersionStateFilterPath("gibberish")).header(USER_ID_HEADER, USER_ID) - .contentType(APPLICATION_JSON)) - .andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$.paging.total", is(numOfWorkflows))); + verify(workflowManagerMock).list(any(), requestSpecArg.capture()); + assertRequestSpec(requestSpecArg.getValue(), DEFAULT_OFFSET, DEFAULT_LIMIT, Collections.emptyList()); } @Test - public void shouldReturnSortedLimitOffsetAppliedWorkflows() throws Exception { - Page<Workflow> workflowMocks = createLimit2AndOffset1For5WorkflowList(); - doReturn(workflowMocks).when(workflowManagerMock).list(any(), any()); + public void listWhenPagingAndSortingAreSet() throws Exception { + mockManagerList3(); mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "2", "1")) .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)).andDo(print()) - .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(2))); + .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3))); + verify(workflowManagerMock).list(any(), requestSpecArg.capture()); + assertRequestSpec(requestSpecArg.getValue(), 1, 2, Collections.singletonList(new Sort("name", true))); } -/* @Test + @Test public void shouldReturnResultsWithDefaultWhenLimitIsNegative() throws Exception { - Page<Workflow> workflowMocks = createLimit2AndOffset1For5WorkflowList(); - doReturn(workflowMocks).when(workflowManagerMock).list(any(), any()); + mockManagerList3(); mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "-2", "1")) .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)).andDo(print()) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.paging.offset", is(1))) - .andExpect(jsonPath("$.paging.limit", is(DEFAULT_LIMIT))) - .andExpect(jsonPath("$.paging.total", is(2))); - }*/ + .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3))); + verify(workflowManagerMock).list(any(), requestSpecArg.capture()); + assertRequestSpec(requestSpecArg.getValue(), 1, DEFAULT_LIMIT, + Collections.singletonList(new Sort("name", true))); + } -/* @Test + @Test public void shouldFallbackOnDefaultOffsetWhenOffsetIsNegative() throws Exception { - mockMvc.perform( - get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "2", "-1")) - .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)).andDo(print()) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.paging.offset", is(DEFAULT_OFFSET))) - .andExpect(jsonPath("$.paging.limit", is(2))) - .andExpect(jsonPath("$.paging.total", is(0))); - }*/ - -/* @Test + mockManagerList3(); + mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "2", "-1")) + .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)).andDo(print()) + .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3))); + verify(workflowManagerMock).list(any(), requestSpecArg.capture()); + assertRequestSpec(requestSpecArg.getValue(), DEFAULT_OFFSET, 2, + Collections.singletonList(new Sort("name", true))); + } + + @Test public void shouldFallbackOnDefaultLimitWhenLimitIsNotAnInteger() throws Exception { - mockMvc.perform( - get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "abc", "0")) - .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)).andDo(print()) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.paging.offset", is(0))) - .andExpect(jsonPath("$.paging.limit", is(DEFAULT_LIMIT))) - .andExpect(jsonPath("$.paging.total", is(0))); - }*/ - -/* @Test + mockManagerList3(); + mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "abc", "0")) + .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)).andDo(print()) + .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3))); + verify(workflowManagerMock).list(any(), requestSpecArg.capture()); + assertRequestSpec(requestSpecArg.getValue(), 0, DEFAULT_LIMIT, + Collections.singletonList(new Sort("name", true))); + } + + @Test public void shouldFallbackOnDefaultOffsetWhenOffsetIsNotAnInteger() throws Exception { - mockMvc.perform( - get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "2", "abc")) - .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)).andDo(print()) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.paging.offset", is(DEFAULT_OFFSET))) - .andExpect(jsonPath("$.paging.limit", is(2))) - .andExpect(jsonPath("$.paging.total", is(0))); - }*/ + mockManagerList3(); + mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "2", "abc")) + .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)).andDo(print()) + .andExpect(jsonPath("$.items", hasSize(3))); + verify(workflowManagerMock).list(any(), requestSpecArg.capture()); + assertRequestSpec(requestSpecArg.getValue(), DEFAULT_OFFSET, 2, + Collections.singletonList(new Sort("name", true))); + } @Test public void shouldReturnDefaultLimitOffsetAppliedWorkflowsWhenLimitIsNotSpecified() throws Exception { - Page<Workflow> workflowMocks = createLimit2AndOffset1For5WorkflowList(); - doReturn(workflowMocks).when(workflowManagerMock).list(any(), any()); + mockManagerList3(); mockMvc.perform(get(RestPath.getWorkflowsPathNoSortAndLimit("1")).header(USER_ID_HEADER, USER_ID) .contentType(APPLICATION_JSON)).andDo(print()) - .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(2))); + .andExpect(jsonPath("$.items", hasSize(3))); + verify(workflowManagerMock).list(any(), requestSpecArg.capture()); + assertRequestSpec(requestSpecArg.getValue(), 1, DEFAULT_LIMIT, Collections.emptyList()); } @Test public void shouldReturnDefaultOffsetAppliedWorkflowsWhenOffsetIsNotSpecified() throws Exception { - Page<Workflow> workflowMocks = createLimit1WorkflowList(); - doReturn(workflowMocks).when(workflowManagerMock).list(any(), any()); + mockManagerList3(); mockMvc.perform(get(RestPath.getWorkflowsPathNoSortAndOffset("1")).header(USER_ID_HEADER, USER_ID) .contentType(APPLICATION_JSON)).andDo(print()) - .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(1))); + .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3))); + verify(workflowManagerMock).list(any(), requestSpecArg.capture()); + assertRequestSpec(requestSpecArg.getValue(), DEFAULT_OFFSET, 1, Collections.emptyList()); } @Test @@ -227,24 +215,25 @@ public class WorkflowControllerTest { assertEquals("Workflow name must contain only letters, digits and underscores", response.getContentAsString()); } - private Page<Workflow> createWorkflows(int numOfWorkflows) { - List<Workflow> workflows = new ArrayList<>(numOfWorkflows); - for (int i = 0; i < numOfWorkflows; i++) { - workflows.add(createWorkflow(i, true)); - } - return new Page<>(workflows, new PagingRequest(0, 200), numOfWorkflows); + private void mockManagerList3() { + doReturn(new Page<>(Arrays.asList( + createWorkflow(1, true), + createWorkflow(2, true), + createWorkflow(3, true)), + new PagingRequest(DEFAULT_OFFSET, DEFAULT_LIMIT), 3)) + .when(workflowManagerMock).list(any(), any()); } - private Page<Workflow> createLimit2AndOffset1For5WorkflowList() { - List<Workflow> workflows = new ArrayList<>(); - workflows.add(createWorkflow(2, true)); - workflows.add(createWorkflow(3, true)); - return new Page<>(workflows, new PagingRequest(1, 200), 5); - } - - private Page<Workflow> createLimit1WorkflowList() { - List<Workflow> workflows = new ArrayList<>(); - workflows.add(createWorkflow(0, true)); - return new Page<>(workflows, new PagingRequest(0, 1), 1); + private static void assertRequestSpec(RequestSpec actual, int expectedOffset, int expectedLimit, + List<Sort> expectedSorts) { + assertEquals(Integer.valueOf(expectedOffset), actual.getPaging().getOffset()); + assertEquals(Integer.valueOf(expectedLimit), actual.getPaging().getLimit()); + if (expectedSorts.isEmpty()) { + assertEquals(expectedSorts, actual.getSorting().getSorts()); + } else { + for (int i = 0; i < expectedSorts.size(); i++) { + assertEquals(expectedSorts.get(i), actual.getSorting().getSorts().get(i)); + } + } } }
\ No newline at end of file diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/types/PagingTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/types/PagingTest.java new file mode 100644 index 00000000..c2b6cef3 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/types/PagingTest.java @@ -0,0 +1,71 @@ +package org.onap.sdc.workflow.api.types; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.onap.sdc.workflow.services.types.PagingConstants.MAX_LIMIT; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +public class PagingTest { + + @InjectMocks + private Paging paging; + + @Test + public void setOffsetNotNumber() { + paging.setOffset("aaa"); + assertNull(paging.getOffset()); + } + + @Test + public void setOffsetNegative() { + paging.setOffset("-5"); + assertNull(paging.getOffset()); + } + + @Test + public void setOffsetZero() { + paging.setOffset("0"); + assertEquals(Integer.valueOf(0), paging.getOffset()); + } + + @Test + public void setOffsetPositive() { + paging.setOffset("8"); + assertEquals(Integer.valueOf(8), paging.getOffset()); + } + + @Test + public void setLimitNotNumber() { + paging.setLimit("aaa"); + assertNull(paging.getLimit()); + } + + @Test + public void setLimitNegative() { + paging.setLimit("-5"); + assertNull(paging.getLimit()); + } + + @Test + public void setLimitZero() { + paging.setLimit("0"); + assertNull(paging.getLimit()); + } + + @Test + public void setLimitPositive() { + paging.setLimit("8"); + assertEquals(Integer.valueOf(8), paging.getLimit()); + } + + @Test + public void setLimitGreaterThanMax() { + paging.setLimit("7000"); + assertEquals(Integer.valueOf(MAX_LIMIT), paging.getLimit()); + } +}
\ No newline at end of file diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/types/SortingTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/types/SortingTest.java new file mode 100644 index 00000000..29436449 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/types/SortingTest.java @@ -0,0 +1,42 @@ +package org.onap.sdc.workflow.api.types; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.Collections; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.onap.sdc.workflow.services.types.Sort; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +public class SortingTest { + + @InjectMocks + private Sorting sorting; + + @Test + public void setSortInvalid() { + sorting.setSort("a"); + assertEquals(Collections.emptyList(), sorting.getSorts()); + } + + @Test + public void setSortAsc() { + sorting.setSort("name:asc"); + assertEquals(Collections.singletonList(new Sort("name", true)), sorting.getSorts()); + } + + @Test + public void setSortDesc() { + sorting.setSort("name:desc"); + assertEquals(Collections.singletonList(new Sort("name", false)), sorting.getSorts()); + } + + @Test + public void setSortMoreThanOne() { + sorting.setSort("name:asc,date:desc"); + assertEquals(Arrays.asList(new Sort("name", true), new Sort("date", false)), sorting.getSorts()); + } +}
\ No newline at end of file diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/types/VersionStatesFormatterTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/types/VersionStatesFormatterTest.java new file mode 100644 index 00000000..c57bd9c3 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/types/VersionStatesFormatterTest.java @@ -0,0 +1,45 @@ +package org.onap.sdc.workflow.api.types; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.onap.sdc.workflow.persistence.types.WorkflowVersionState.CERTIFIED; +import static org.onap.sdc.workflow.persistence.types.WorkflowVersionState.DRAFT; + +import java.util.Collections; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +public class VersionStatesFormatterTest { + + @InjectMocks + private VersionStatesFormatter versionStateSet; + + @Test + public void setVersionStateInvalid() { + versionStateSet.setVersionState("aaa"); + assertNull(versionStateSet.getVersionStates()); + } + + @Test + public void setVersionStateDraft() { + versionStateSet.setVersionState("DRAFT"); + assertEquals(Collections.singleton(DRAFT), versionStateSet.getVersionStates()); + } + + @Test + public void setVersionStateCertified() { + versionStateSet.setVersionState("CERTIFIED"); + assertEquals(Collections.singleton(CERTIFIED), versionStateSet.getVersionStates()); + } + + @Test + public void setVersionStateBoth() { + versionStateSet.setVersionState("DRAFT,CERTIFIED"); + assertEquals(Stream.of(DRAFT, CERTIFIED).collect(Collectors.toSet()), versionStateSet.getVersionStates()); + } +}
\ No newline at end of file diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImplTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImplTest.java index 0797b648..de17a049 100644 --- a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImplTest.java +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImplTest.java @@ -8,6 +8,9 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.onap.sdc.workflow.TestUtil.createItem; import static org.onap.sdc.workflow.TestUtil.createWorkflow; +import static org.onap.sdc.workflow.services.types.PagingConstants.DEFAULT_LIMIT; +import static org.onap.sdc.workflow.services.types.PagingConstants.DEFAULT_OFFSET; +import static org.onap.sdc.workflow.services.types.PagingConstants.MAX_LIMIT; import static org.openecomp.sdc.versioning.dao.types.VersionStatus.Certified; import java.util.ArrayList; @@ -18,7 +21,6 @@ import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; -import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; @@ -30,6 +32,7 @@ import org.onap.sdc.workflow.services.exceptions.EntityNotFoundException; import org.onap.sdc.workflow.services.impl.mappers.VersionStateMapper; import org.onap.sdc.workflow.services.impl.mappers.WorkflowMapper; import org.onap.sdc.workflow.services.types.Page; +import org.onap.sdc.workflow.services.types.Paging; import org.onap.sdc.workflow.services.types.PagingRequest; import org.onap.sdc.workflow.services.types.RequestSpec; import org.onap.sdc.workflow.services.types.Sort; @@ -53,7 +56,7 @@ public class WorkflowManagerImplTest { static { List<Item> items = new ArrayList<>(); List<Workflow> mappedWorkflows = new ArrayList<>(); - for (int i = 1; i < 6; i++) { + for (int i = 0; i < 5; i++) { items.add(createItem(i, true, true)); mappedWorkflows.add(createWorkflow(i, true)); } @@ -75,17 +78,18 @@ public class WorkflowManagerImplTest { @Test public void shouldReturnWorkflowVersionList() { doReturn(ITEMS).when(itemManagerMock).list(any()); - for (int i = 0; i < ITEMS.size(); i++) { - doReturn(MAPPED_WORKFLOWS.get(i)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(i)); - } - Page<Workflow> workflows = workflowManager.list(null, createRequestSpec(20, 0, true, SORT_FIELD_NAME)); + mockItemToWorkflowMaps(); + RequestSpec requestSpec = createRequestSpec(0, 20, true); + Page<Workflow> workflows = workflowManager.list(null, requestSpec); Map<String, Workflow> workflowById = workflows.getItems().stream().collect(Collectors.toMap(Workflow::getId, Function.identity())); assertEquals(ITEMS.size(), workflows.getItems().size()); - for (int i = 1; i < ITEMS.size() + 1; i++) { + for (int i = 0; i < ITEMS.size(); i++) { assertTrue(workflowById.containsKey(String.valueOf(i))); } + assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), requestSpec.getPaging().getLimit(), + ITEMS.size()); } @Test @@ -96,14 +100,17 @@ public class WorkflowManagerImplTest { doReturn(MAPPED_WORKFLOWS.get(0)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(0)); doReturn(MAPPED_WORKFLOWS.get(2)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(2)); - Page<Workflow> workflows = workflowManager.list(Collections.singleton(WorkflowVersionState.CERTIFIED), - createRequestSpec(20, 0, true, SORT_FIELD_NAME)); + RequestSpec requestSpec = createRequestSpec(0, 20, true); + Page<Workflow> workflows = + workflowManager.list(Collections.singleton(WorkflowVersionState.CERTIFIED), requestSpec); Map<String, Workflow> workflowById = workflows.getItems().stream().collect(Collectors.toMap(Workflow::getId, Function.identity())); assertEquals(2, workflows.getItems().size()); - assertTrue(workflowById.containsKey("1")); - assertTrue(workflowById.containsKey("3")); + assertTrue(workflowById.containsKey("0")); + assertTrue(workflowById.containsKey("2")); + + assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), requestSpec.getPaging().getLimit(), 2); } @Test(expected = EntityNotFoundException.class) @@ -162,81 +169,183 @@ public class WorkflowManagerImplTest { } @Test - public void shouldListAllWorkflowsWhenLimitAndOffsetAreValid() { - RequestSpec requestSpec = createRequestSpec(5, 0, true, SORT_FIELD_NAME); + public void listWhenRequestSpecIsNull() { doReturn(ITEMS).when(itemManagerMock).list(any()); - for (int i = 0; i < ITEMS.size(); i++) { - doReturn(MAPPED_WORKFLOWS.get(i)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(i)); + mockItemToWorkflowMaps(); + Page<Workflow> workflows = workflowManager.list(null, null); + + assertEquals(ITEMS.size(), workflows.getItems().size()); + assertPaging(workflows.getPaging(), DEFAULT_OFFSET, DEFAULT_LIMIT, ITEMS.size()); + + // verify sorted ascending by name + for (int i = DEFAULT_OFFSET; i < ITEMS.size(); i++) { + assertEquals("Workflow_" + i, workflows.getItems().get(i).getName()); } - Assert.assertEquals(5, workflowManager.list(null, requestSpec).getItems().size()); } @Test - public void shouldListLimitFilteredWorkflowsInFirstOffsetRange() { - RequestSpec requestSpec = createRequestSpec(3, 0, true, SORT_FIELD_NAME); + public void listWhenPagingIsNull() { doReturn(ITEMS).when(itemManagerMock).list(any()); - for (int i = 0; i < ITEMS.size(); i++) { - doReturn(MAPPED_WORKFLOWS.get(i)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(i)); - } - Assert.assertEquals(3, workflowManager.list(null, requestSpec).getItems().size()); + mockItemToWorkflowMaps(); + Page<Workflow> workflows = workflowManager.list(null, new RequestSpec(null, + SortingRequest.builder().sort(new Sort(SORT_FIELD_NAME, true)).build())); + + assertEquals(ITEMS.size(), workflows.getItems().size()); + assertPaging(workflows.getPaging(), DEFAULT_OFFSET, DEFAULT_LIMIT, ITEMS.size()); } -/* @Test - public void shouldListLimitFilteredWorkflowsInSecondOffsetRange() { - RequestSpec requestSpec = createRequestSpec(3, 1, true, SORT_FIELD_NAME); + @Test + public void listWhenOffsetAndLimitAreNull() { doReturn(ITEMS).when(itemManagerMock).list(any()); - for (int i = 0; i < ITEMS.size(); i++) { - doReturn(MAPPED_WORKFLOWS.get(i)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(i)); - } - Assert.assertEquals(2, workflowManager.list(null, requestSpec).getItems().size()); - }*/ + mockItemToWorkflowMaps(); + RequestSpec requestSpec = new RequestSpec(new PagingRequest(-2, -8), + SortingRequest.builder().sort(new Sort(SORT_FIELD_NAME, true)).build()); + Page<Workflow> workflows = workflowManager.list(null, requestSpec); + + assertEquals(ITEMS.size(), workflows.getItems().size()); + assertPaging(workflows.getPaging(), DEFAULT_OFFSET, DEFAULT_LIMIT, ITEMS.size()); + } @Test - public void shouldListAllWorkflowsWhenLimitGreaterThanTotalRecordsAndOffsetInRange() { - RequestSpec requestSpec = createRequestSpec(10, 0, true, SORT_FIELD_NAME); + public void listWhenSortingIsNull() { doReturn(ITEMS).when(itemManagerMock).list(any()); - for (int i = 0; i < ITEMS.size(); i++) { - doReturn(MAPPED_WORKFLOWS.get(i)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(i)); - } - Assert.assertEquals(5, workflowManager.list(null, requestSpec).getItems().size()); + mockItemToWorkflowMaps(); + RequestSpec requestSpec = new RequestSpec(new PagingRequest(2, 8), null); + Page<Workflow> workflows = workflowManager.list(null, requestSpec); + + assertEquals(3, workflows.getItems().size()); + assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), requestSpec.getPaging().getLimit(), + ITEMS.size()); + + // verify sorted ascending by name + assertEquals("Workflow_2", workflows.getItems().get(0).getName()); + assertEquals("Workflow_3", workflows.getItems().get(1).getName()); + assertEquals("Workflow_4", workflows.getItems().get(2).getName()); } @Test - public void shouldNotListWorkflowsIfOffsetGreaterThanTotalRecords() { - RequestSpec requestSpec = createRequestSpec(3, 6, true, SORT_FIELD_NAME); + public void listWhenSortingIsEmpty() { doReturn(ITEMS).when(itemManagerMock).list(any()); - for (int i = 0; i < ITEMS.size(); i++) { - doReturn(MAPPED_WORKFLOWS.get(i)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(i)); - } - Assert.assertEquals(0, workflowManager.list(null, requestSpec).getItems().size()); + mockItemToWorkflowMaps(); + RequestSpec requestSpec = new RequestSpec(new PagingRequest(2, 8), SortingRequest.builder().build()); + Page<Workflow> workflows = workflowManager.list(null, requestSpec); + + assertEquals(3, workflows.getItems().size()); + assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), requestSpec.getPaging().getLimit(), + ITEMS.size()); + + // verify sorted ascending by name + assertEquals("Workflow_2", workflows.getItems().get(0).getName()); + assertEquals("Workflow_3", workflows.getItems().get(1).getName()); + assertEquals("Workflow_4", workflows.getItems().get(2).getName()); } @Test - public void shouldNotListWorkflowsBothLimitAndOffsetGreaterThanTotalRecords() { - RequestSpec requestSpec = createRequestSpec(10, 10, true, SORT_FIELD_NAME); + public void listWhenRequestSpecIsValid() { + RequestSpec requestSpec = createRequestSpec(0, 5, true); doReturn(ITEMS).when(itemManagerMock).list(any()); - for (int i = 0; i < ITEMS.size(); i++) { - doReturn(MAPPED_WORKFLOWS.get(i)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(i)); - } - Assert.assertEquals(0, workflowManager.list(null, requestSpec).getItems().size()); + mockItemToWorkflowMaps(); + Page<Workflow> workflows = workflowManager.list(null, requestSpec); + + assertEquals(5, workflows.getItems().size()); + assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), requestSpec.getPaging().getLimit(), + ITEMS.size()); + } + + @Test + public void listWhenLimitIsLessThanTotal() { + RequestSpec requestSpec = createRequestSpec(0, 3, true); + doReturn(ITEMS).when(itemManagerMock).list(any()); + mockItemToWorkflowMaps(); + Page<Workflow> workflows = workflowManager.list(null, requestSpec); + assertEquals(3, workflows.getItems().size()); + assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), requestSpec.getPaging().getLimit(), + ITEMS.size()); + } + + + @Test + public void listWhenOffsetIsNotFirst() { + RequestSpec requestSpec = createRequestSpec(3, 1, true); + doReturn(ITEMS).when(itemManagerMock).list(any()); + mockItemToWorkflowMaps(); + Page<Workflow> workflows = workflowManager.list(null, requestSpec); + assertEquals(1, workflows.getItems().size()); + assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), requestSpec.getPaging().getLimit(), + ITEMS.size()); + } + + @Test + public void listWhenLimitIsMoreThanTotal() { + RequestSpec requestSpec = createRequestSpec(0, 10, true); + doReturn(ITEMS).when(itemManagerMock).list(any()); + mockItemToWorkflowMaps(); + Page<Workflow> workflows = workflowManager.list(null, requestSpec); + assertEquals(5, workflows.getItems().size()); + assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), requestSpec.getPaging().getLimit(), + ITEMS.size()); + } + + @Test + public void listWhenOffsetIsMoreThanTotal() { + RequestSpec requestSpec = createRequestSpec(6, 3, true); + doReturn(ITEMS).when(itemManagerMock).list(any()); + mockItemToWorkflowMaps(); + Page<Workflow> workflows = workflowManager.list(null, requestSpec); + assertEquals(0, workflows.getItems().size()); + assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), requestSpec.getPaging().getLimit(), + ITEMS.size()); + } + + @Test + public void listWhenOffsetIsMoreThanMax() { + doReturn(ITEMS).when(itemManagerMock).list(any()); + mockItemToWorkflowMaps(); + RequestSpec requestSpec = createRequestSpec(0, 5555, true); + Page<Workflow> workflows = workflowManager.list(null, requestSpec); + + assertEquals(ITEMS.size(), workflows.getItems().size()); + assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), MAX_LIMIT, ITEMS.size()); } -/* @Test - public void shouldListLimitOffsetAppliedWorkflowsSortedInDescOrder() { - RequestSpec requestSpec = createRequestSpec(2, 1, false, SORT_FIELD_NAME); + @Test + public void listWhenOffsetAndLimitAreMoreThanTotal() { + RequestSpec requestSpec = createRequestSpec(10, 10, true); doReturn(ITEMS).when(itemManagerMock).list(any()); + mockItemToWorkflowMaps(); + Page<Workflow> workflows = workflowManager.list(null, requestSpec); + assertEquals(0, workflows.getItems().size()); + assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), requestSpec.getPaging().getLimit(), + ITEMS.size()); + } + + @Test + public void listWhenSortedDesc() { + RequestSpec requestSpec = createRequestSpec(2, 1, false); + doReturn(ITEMS).when(itemManagerMock).list(any()); + mockItemToWorkflowMaps(); + Page<Workflow> workflows = workflowManager.list(null, requestSpec); + assertEquals(1, workflows.getItems().size()); + assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), requestSpec.getPaging().getLimit(), + ITEMS.size()); + Iterator<Workflow> workflowIterator = workflows.getItems().iterator(); + assertEquals("Workflow_2", workflowIterator.next().getName()); + } + + private void mockItemToWorkflowMaps() { for (int i = 0; i < ITEMS.size(); i++) { doReturn(MAPPED_WORKFLOWS.get(i)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(i)); } - Page<Workflow> workflows = workflowManager.list(null, requestSpec); - Assert.assertEquals(2, workflows.getItems().size()); - Iterator<Workflow> workflowIterator = workflows.getItems().iterator(); - Assert.assertEquals("Workflow_3", workflowIterator.next().getName()); - Assert.assertEquals("Workflow_2", workflowIterator.next().getName()); - }*/ + } - private RequestSpec createRequestSpec(int limit, int offset, boolean isAscending, String sortField) { + private static RequestSpec createRequestSpec(int offset, int limit, boolean isAscending) { return new RequestSpec(new PagingRequest(offset, limit), - SortingRequest.builder().sort(new Sort(sortField, isAscending)).build()); + SortingRequest.builder().sort(new Sort(SORT_FIELD_NAME, isAscending)).build()); + } + + private static void assertPaging(Paging paging, int expectedOffset, int expectedLimit, int expectedTotal) { + assertEquals(expectedOffset, paging.getOffset()); + assertEquals(expectedLimit, paging.getLimit()); + assertEquals(expectedTotal, paging.getTotal()); } }
\ No newline at end of file |