diff options
author | vempo <vitaliy.emporopulo@amdocs.com> | 2018-07-24 17:34:04 +0300 |
---|---|---|
committer | vempo <vitaliy.emporopulo@amdocs.com> | 2018-07-25 11:39:10 +0300 |
commit | a52d50e788792a63e97a9176ab319d53db7a2853 (patch) | |
tree | b1c2222cacf4b8192aea16d1e0315b1f005c5347 /workflow-designer-be/src/test/java | |
parent | 3c2665debb400aef7f0ed9e235698d2ff9f859db (diff) |
Replaced old implementation at root
Old project files and directories has been moved
under 'deprecated-workflow-designer'. The old project
is not built by the CI anymore, but can be still built manually.
New modules/directories have been moved up and integrated with
the CI system.
Change-Id: I1528c792bcbcce9e50bfc294a1328a20e72c91cf
Issue-ID: SDC-1559
Signed-off-by: vempo <vitaliy.emporopulo@amdocs.com>
Diffstat (limited to 'workflow-designer-be/src/test/java')
12 files changed, 1577 insertions, 0 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 new file mode 100644 index 00000000..7a7e715e --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/RestPath.java @@ -0,0 +1,66 @@ +package org.onap.sdc.workflow; + +import static org.onap.sdc.workflow.api.RestConstants.SIZE_PARAM; +import static org.onap.sdc.workflow.api.RestConstants.PAGE_PARAM; +import static org.onap.sdc.workflow.api.RestConstants.SORT_PARAM; + +public class RestPath { + + private RestPath() { + //Hiding implicit constructor + } + + private static final String WORKFLOWS_URL = "/workflows"; + public static final String WORKFLOWS_WITH_VERSION_STATE_FILTER_URL = WORKFLOWS_URL + "?versionState=%s"; + private static final String WORKFLOW_URL_FORMATTER = WORKFLOWS_URL + "/%s"; + private static final String VERSIONS_URL_FORMATTER = WORKFLOWS_URL + "/%s/versions"; + private static final String VERSION_URL_FORMATTER = WORKFLOWS_URL + "/%s/versions/%s"; + private static final String SORT_QUERY_STRING_FORMATTER = SORT_PARAM + "=%s"; + private static final String SIZE_QUERY_STRING_FORMATTER = SIZE_PARAM + "=%s"; + private static final String OFFSET_QUERY_STRING_FORMATTER = PAGE_PARAM + "=%s"; + private static final String WORKFLOW_URL_FORMATTER_QUERY_PARAMS_ALL = + WORKFLOWS_URL + "?" + SORT_QUERY_STRING_FORMATTER+ "&" + SIZE_QUERY_STRING_FORMATTER + "&" + + OFFSET_QUERY_STRING_FORMATTER; + private static final String WORKFLOW_URL_FORMATTER_QUERY_PARAMS_NO_SORT = + WORKFLOWS_URL + "?" + SIZE_QUERY_STRING_FORMATTER + "&" + OFFSET_QUERY_STRING_FORMATTER; + private static final String WORKFLOW_URL_FORMATTER_QUERY_PARAMS_NO_SORT_AND_SIZE = + WORKFLOWS_URL + "?" + OFFSET_QUERY_STRING_FORMATTER; + private static final String WORKFLOW_URL_FORMATTER_QUERY_PARAMS_NO_SORT_AND_OFFSET = + WORKFLOWS_URL + "?" + SIZE_QUERY_STRING_FORMATTER; + + public static String getWorkflowsPathAllQueryParams(String sort, String size, String offset){ + return String.format(WORKFLOW_URL_FORMATTER_QUERY_PARAMS_ALL, sort, size, offset); + } + + public static String getWorkflowsPathNoSort(String size, String offset){ + return String.format(WORKFLOW_URL_FORMATTER_QUERY_PARAMS_NO_SORT, size, offset); + } + + public static String getWorkflowsPathNoSortAndSize(String offset){ + return String.format(WORKFLOW_URL_FORMATTER_QUERY_PARAMS_NO_SORT_AND_SIZE, offset); + } + + public static String getWorkflowsPathNoSortAndOffset(String size){ + return String.format(WORKFLOW_URL_FORMATTER_QUERY_PARAMS_NO_SORT_AND_OFFSET, size); + } + + public static String getWorkflowsPath() { + 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); + } + + public static String getWorkflowVersions(String workflowId) { + return String.format(VERSIONS_URL_FORMATTER, workflowId); + } + + public static String getWorkflowVersion(String workflowId, String versionId) { + return String.format(VERSION_URL_FORMATTER, workflowId, versionId); + } +} diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/TestUtil.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/TestUtil.java new file mode 100644 index 00000000..e2a566aa --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/TestUtil.java @@ -0,0 +1,35 @@ +package org.onap.sdc.workflow; + +import org.onap.sdc.workflow.persistence.types.Workflow; +import org.openecomp.sdc.versioning.types.Item; + +public class TestUtil { + + private static final String WORKFLOW_TYPE = "WORKFLOW"; + + public static Workflow createWorkflow(int workflowNum, boolean createId) { + Workflow workflow = new Workflow(); + if (createId) { + workflow.setId(String.valueOf(workflowNum)); + } + workflow.setName("Workflow_" + workflowNum); + workflow.setDescription("Description_" + workflowNum); + + return workflow; + } + + public static Item createItem(int itemNum, boolean setType, boolean setId) { + Item item = new Item(); + if (setId) { + item.setId(String.valueOf(itemNum)); + } + item.setName("Workflow_" + itemNum); + item.setDescription("Description_" + itemNum); + if (setType) { + item.setType(WORKFLOW_TYPE); + } + return item; + } + + +} 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 new file mode 100644 index 00000000..5e7df483 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerTest.java @@ -0,0 +1,287 @@ +package org.onap.sdc.workflow.api; + +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; +import static org.onap.sdc.workflow.api.RestConstants.PAGE_DEFAULT; +import static org.onap.sdc.workflow.api.RestConstants.SIZE_DEFAULT; +import static org.onap.sdc.workflow.api.RestConstants.SORT_FIELD_NAME; +import static org.onap.sdc.workflow.api.RestConstants.SORT_PARAM; +import static org.onap.sdc.workflow.api.RestConstants.USER_ID_HEADER_PARAM; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.amdocs.zusammen.datatypes.Id; +import com.amdocs.zusammen.datatypes.item.Item; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.collect.ImmutableSet; +import com.google.gson.Gson; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +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.api.types.CollectionWrapper; +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.springframework.data.web.PageableHandlerMethodArgumentResolver; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +@RunWith(MockitoJUnitRunner.class) +public class WorkflowControllerTest { + + private static final String MISSING_REQUEST_HEADER_ERRROR_FORMAT = + "Missing request header '%s' for method parameter of type String"; + private static final String USER_ID = "userId"; + private static final Gson GSON = new Gson(); + private static final String USER_ID_HEADER = "USER_ID"; + private static final String INVALID_PAGINATION_PARAMETER_FORMAT = "Requested %s: %s %s"; + private static final String PAGINATION_PARAMETER_INVALID_SORT_FIELD_SUFFIX = + "is not supported. Supported values are: "; + private static final String DEFAULT_SORT_VALUE = "name,asc"; + + private MockMvc mockMvc; + + + @InjectMocks + private WorkflowController workflowController; + + @Mock + private WorkflowManager workflowManagerMock; + + @Before + public void setUp() { + mockMvc = MockMvcBuilders.standaloneSetup(workflowController) + .setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver()) + .setControllerAdvice(new CustomizedResponseEntityExceptionHandler()).build(); + } + + @Test + public void shouldReturnErrorWhenMissingUserIdInGetReqHeader() throws Exception { + Workflow workflowMock = createWorkflow(1, true); + MockHttpServletResponse response = + mockMvc.perform(get(RestPath.getWorkflowPath(workflowMock.getId())).contentType(APPLICATION_JSON)) + .andDo(print()).andExpect(status().isBadRequest()).andExpect(status().is(400)).andReturn() + .getResponse(); + assertEquals(String.format(MISSING_REQUEST_HEADER_ERRROR_FORMAT, USER_ID_HEADER), + response.getContentAsString()); + } + + @Test + public void shouldReturnWorkflowDataWhenRequestPathIsOk() throws Exception { + Workflow workflowMock = createWorkflow(1, true); + doReturn(workflowMock).when(workflowManagerMock).get(any(Workflow.class)); + mockMvc.perform(get(RestPath.getWorkflowPath(workflowMock.getId())).header(USER_ID_HEADER_PARAM, USER_ID) + .contentType(APPLICATION_JSON)) + .andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$.id", is(workflowMock.getId()))) + .andExpect(jsonPath("$.name", is(workflowMock.getName()))); + } + + @Test + public void shouldReturnErrorWhenMissingUserIdInListReqHeader() throws Exception { + MockHttpServletResponse response = + mockMvc.perform(get(RestPath.getWorkflowsPath()).contentType(APPLICATION_JSON)).andDo(print()) + .andExpect(status().isBadRequest()).andExpect(status().is(400)).andReturn().getResponse(); + assertEquals(String.format(MISSING_REQUEST_HEADER_ERRROR_FORMAT, USER_ID_HEADER_PARAM), + response.getContentAsString()); + } + + @Test + public void shouldReturn5WorkflowWhen5WorkflowsExists() throws Exception { + int numOfWorkflows = 5; + List<Workflow> workflowMocks = createWorkflows(numOfWorkflows); + doReturn(workflowMocks).when(workflowManagerMock).list(any(), any()); + mockMvc.perform( + get(RestPath.getWorkflowsPath()).header(USER_ID_HEADER_PARAM, USER_ID).contentType(APPLICATION_JSON)) + .andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$.results", hasSize(numOfWorkflows))); + } + + @Test + public void listWithValidVersionStateFilter() throws Exception { + int numOfWorkflows = 3; + List<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_PARAM, USER_ID) + .contentType(APPLICATION_JSON)) + .andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$.total", is(numOfWorkflows))) + .andExpect(jsonPath("$.results", hasSize(numOfWorkflows))); + } + + @Test + public void listWithInvalidVersionStateFilter() throws Exception { + mockMvc.perform( + get(RestPath.getWorkflowsWithVersionStateFilterPath("hasdhf")).header(USER_ID_HEADER_PARAM, USER_ID) + .contentType(APPLICATION_JSON)) + .andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$.total", is(0))); + } + + @Test + public void shouldReturnSortedSizeOffsetAppliedWorkflows() throws Exception { + List<Workflow> workflowMocks = createSize2AndOffset1For5WorkflowList(); + doReturn(workflowMocks).when(workflowManagerMock).list(any(), 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 shouldReturnResultsWithDefaultWhenSizeIsNegative() throws Exception { + List<Workflow> workflowMocks = createSize2AndOffset1For5WorkflowList(); + doReturn(workflowMocks).when(workflowManagerMock).list(any(), 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(SIZE_DEFAULT, workflowListResponse.getSize()); + assertEquals(1, workflowListResponse.getPage()); + 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.getSize()); + assertEquals(PAGE_DEFAULT, workflowListResponse.getPage()); + assertEquals(0, workflowListResponse.getTotal()); + } + + @Test + public void shouldFallbackOnDefaultSizeWhenSizeIsNotAnInteger() 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(SIZE_DEFAULT, workflowListResponse.getSize()); + assertEquals(0, workflowListResponse.getPage()); + 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.getSize()); + assertEquals(PAGE_DEFAULT, workflowListResponse.getPage()); + 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 shouldReturnAscSortedSizeOffsetAppliedWorkflowsWhenSortIsNotSpecified() throws Exception { + List<Workflow> workflowMocks = createSize2AndOffset1For5WorkflowList(); + doReturn(workflowMocks).when(workflowManagerMock).list(any(), 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 shouldReturnDefaultSizeOffsetAppliedWorkflowsWhenSizeIsNotSpecified() throws Exception { + List<Workflow> workflowMocks = createSize2AndOffset1For5WorkflowList(); + doReturn(workflowMocks).when(workflowManagerMock).list(any(), any()); + mockMvc.perform( + get(RestPath.getWorkflowsPathNoSortAndSize("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 = createSize1WorkflowList(); + doReturn(workflowMocks).when(workflowManagerMock).list(any(), 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(new Id("abc")); + Workflow reqWorkflow = createWorkflow(1, false); + mockMvc.perform( + post(RestPath.getWorkflowsPath()).header(USER_ID_HEADER_PARAM, USER_ID).contentType(APPLICATION_JSON) + .content(GSON.toJson(reqWorkflow))).andDo(print()) + .andExpect(status().isCreated()); + verify(workflowManagerMock).create(reqWorkflow); + } + + private List<Workflow> createWorkflows(int numOfWorkflows) { + List<Workflow> workflowList = new ArrayList<>(numOfWorkflows); + for (int i = 0; i < numOfWorkflows; i++) { + workflowList.add(createWorkflow(i, true)); + } + + return workflowList; + } + + private List<Workflow> createSize2AndOffset1For5WorkflowList() { + List<Workflow> workflowList = new ArrayList<>(); + workflowList.add(createWorkflow(2, true)); + workflowList.add(createWorkflow(3, true)); + return workflowList; + } + + private List<Workflow> createSize1WorkflowList() { + List<Workflow> workflowList = new ArrayList<>(); + workflowList.add(createWorkflow(0, true)); + return workflowList; + } + + + private Set<String> getSupportedSortFields() { + return ImmutableSet.of(SORT_FIELD_NAME); + } + +}
\ No newline at end of file diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowVersionControllerTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowVersionControllerTest.java new file mode 100644 index 00000000..ee9a56c8 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowVersionControllerTest.java @@ -0,0 +1,117 @@ +package org.onap.sdc.workflow.api; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.google.gson.Gson; +import java.util.Arrays; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.sdc.workflow.RestPath; +import org.onap.sdc.workflow.persistence.types.WorkflowVersion; +import org.onap.sdc.workflow.services.WorkflowVersionManager; +import org.openecomp.sdc.versioning.dao.types.Version; +import org.springframework.http.HttpStatus; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +@RunWith(MockitoJUnitRunner.class) +public class WorkflowVersionControllerTest { + + private static final String USER_ID = "cs0008"; + private static final String ITEM1_ID = "item_id_1"; + private static final String VERSION1_ID = "version_id_1"; + private static final String VERSION2_ID = "version_id_2"; + private List<Version> versionList; + + private static final Gson GSON = new Gson(); + + private MockMvc mockMvc; + + @Mock + private WorkflowVersionManager workflowVersionManagerMock; + + @InjectMocks + private WorkflowVersionController workflowVersionController; + + @Before + public void setUp() { + versionList = Arrays.asList( new Version(VERSION1_ID),new Version(VERSION2_ID)); + mockMvc = MockMvcBuilders.standaloneSetup(workflowVersionController).build(); + } + + @Test + public void shouldReturnWorkflowVersionListWhenCallingVersionGetREST() throws Exception { + + doReturn(versionList).when(workflowVersionManagerMock).list(ITEM1_ID, null); + mockMvc.perform(get(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID) + .contentType(APPLICATION_JSON)).andExpect(status().isOk()) + .andExpect(jsonPath("$.results", hasSize(2))) + .andExpect(jsonPath("$.results[0].id", equalTo(VERSION1_ID))) + .andExpect(jsonPath("$.results[1].id", equalTo(VERSION2_ID))); + + verify(workflowVersionManagerMock, times(1)).list(ITEM1_ID, null); + } + + + @Test + public void shouldCreateWorkflowVersionWhenCallingVersionsPostREST() throws Exception { + + WorkflowVersion version = new WorkflowVersion(); + version.setDescription("VersionDescription"); + mockMvc.perform(post(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID) + .contentType(APPLICATION_JSON) + .content(GSON.toJson(version))) + .andExpect(status().isCreated()); + + verify(workflowVersionManagerMock, times(1)).create(ITEM1_ID, null, version); + } + + + @Test + public void shouldReturnWorkflowVersionWhenExists() throws Exception { + WorkflowVersion version = new WorkflowVersion(VERSION1_ID); + doReturn(version).when(workflowVersionManagerMock).get(ITEM1_ID, VERSION1_ID); + mockMvc.perform( + get(RestPath.getWorkflowVersion(ITEM1_ID, VERSION1_ID)).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID) + .contentType(APPLICATION_JSON)).andDo(print()) + .andExpect(status().isOk()).andExpect(jsonPath("$.id", is(version.getId()))); + verify(workflowVersionManagerMock, times(1)).get(ITEM1_ID, VERSION1_ID); + } + + @Test + public void shouldUpdateWorkflowVersionWhenCallingPutREST() throws Exception { + WorkflowVersion version = new WorkflowVersion(); + version.setDescription("Updated"); + + MockHttpServletResponse result = mockMvc.perform( + put(RestPath.getWorkflowVersion(ITEM1_ID, VERSION1_ID)).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID) + .contentType(APPLICATION_JSON) + .content(GSON.toJson(version))).andReturn() + .getResponse(); + + assertEquals(HttpStatus.OK.value(), result.getStatus()); + version.setId(VERSION1_ID); + verify(workflowVersionManagerMock, times(1)).update(ITEM1_ID, version); + + } + +} diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/persistence/impl/ArtifactRepositoryTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/persistence/impl/ArtifactRepositoryTest.java new file mode 100644 index 00000000..b86b14b9 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/persistence/impl/ArtifactRepositoryTest.java @@ -0,0 +1,99 @@ +package org.onap.sdc.workflow.persistence.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.isNull; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; +import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement; + +import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement; +import com.amdocs.zusammen.datatypes.Id; +import com.amdocs.zusammen.datatypes.SessionContext; +import com.amdocs.zusammen.datatypes.item.Action; +import com.amdocs.zusammen.datatypes.item.ElementContext; +import java.io.IOException; +import java.io.InputStream; +import java.util.Optional; +import org.apache.commons.io.IOUtils; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.sdc.workflow.persistence.types.ArtifactEntity; +import org.onap.sdc.workflow.persistence.types.WorkflowElementType; +import org.openecomp.core.zusammen.api.ZusammenAdaptor; +import org.openecomp.sdc.common.session.SessionContextProviderFactory; + +@RunWith(MockitoJUnitRunner.class) +public class ArtifactRepositoryTest { + + private static final String FILE_NAME_PROPERTY = "fileName"; + private static final String FILE_NAME = "fileName.txt"; + private static final String ITEM1_ID = "item_id_1"; + private static final String VERSION1_ID = "version_id_1"; + + + @Mock + private ZusammenAdaptor zusammenAdaptorMock; + + @InjectMocks + private ArtifactRepositoryImpl artifactRepository; + + @Before + public void setUp() { + SessionContextProviderFactory.getInstance().createInterface().create("test_user", "workflow"); + } + + @Test + public void shouldUpdateArtifact() throws IOException { + + InputStream inputStreamMock = IOUtils.toInputStream("some test data for my input stream", "UTF-8"); + ArtifactEntity artifactMock = new ArtifactEntity(FILE_NAME, inputStreamMock); + + artifactRepository.update(ITEM1_ID, VERSION1_ID, artifactMock); + verify(zusammenAdaptorMock) + .saveElement(any(SessionContext.class), any(ElementContext.class), any(ZusammenElement.class), + eq("Update WorkflowVersion Artifact Element")); + } + + @Test + public void shouldGetArtifactWhenExist() throws IOException { + + ZusammenElement artifactElement = buildStructuralElement(WorkflowElementType.ARTIFACT.name(), Action.UPDATE); + artifactElement.setData(IOUtils.toInputStream("some test data for my input stream", "UTF-8")); + artifactElement.getInfo().addProperty(FILE_NAME_PROPERTY, FILE_NAME); + Optional<Element> elementOptional = Optional.of(artifactElement); + + doReturn(elementOptional).when(zusammenAdaptorMock) + .getElementByName(any(SessionContext.class), any(ElementContext.class), isNull(Id.class), + eq(WorkflowElementType.ARTIFACT.name())); + + Optional<ArtifactEntity> result = artifactRepository.get(ITEM1_ID, VERSION1_ID); + assertTrue(result.isPresent()); + assertEquals(FILE_NAME,result.get().getFileName()); + verify(zusammenAdaptorMock).getElementByName(any(SessionContext.class), any(ElementContext.class), isNull(Id.class), + eq(WorkflowElementType.ARTIFACT.name())); + } + + @Test + public void shouldCreateArtifactStructure() { + artifactRepository.createStructure(ITEM1_ID, VERSION1_ID); + verify(zusammenAdaptorMock) + .saveElement(any(SessionContext.class), any(ElementContext.class), any(ZusammenElement.class), + eq("Create WorkflowVersion Artifact Element")); + } + + @Test + public void shouldDeleteArtifact(){ + artifactRepository.delete(ITEM1_ID,VERSION1_ID); + verify(zusammenAdaptorMock).saveElement(any(SessionContext.class), any(ElementContext.class), any(ZusammenElement.class), + eq("Delete WorkflowVersion Artifact Data")); + } + +} diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/persistence/impl/ParameterRepositoryTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/persistence/impl/ParameterRepositoryTest.java new file mode 100644 index 00000000..07a42658 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/persistence/impl/ParameterRepositoryTest.java @@ -0,0 +1,166 @@ +package org.onap.sdc.workflow.persistence.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; + +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement; +import com.amdocs.zusammen.datatypes.Id; +import com.amdocs.zusammen.datatypes.SessionContext; +import com.amdocs.zusammen.datatypes.item.ElementContext; +import com.amdocs.zusammen.datatypes.item.Info; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Optional; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mapstruct.ap.internal.util.Collections; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.sdc.workflow.persistence.types.ParameterEntity; +import org.onap.sdc.workflow.persistence.types.ParameterPropertyName; +import org.onap.sdc.workflow.persistence.types.ParameterRole; +import org.onap.sdc.workflow.persistence.types.ParameterType; +import org.onap.sdc.workflow.persistence.types.WorkflowElementType; +import org.openecomp.core.zusammen.api.ZusammenAdaptor; +import org.openecomp.sdc.common.session.SessionContextProviderFactory; + +@RunWith(MockitoJUnitRunner.class) +public class ParameterRepositoryTest { + + private static final String ITEM1_ID = "item_id_1"; + private static final String VERSION1_ID = "version_id_1"; + private static final String PARAMETER1_ID = "parameter_id_1"; + private static final String PARAMETER2_ID = "parameter_id_2"; + private static final String PARAMETERS_PARENT_ID = "parameters_id"; + + + @Mock + private ZusammenAdaptor zusammenAdaptorMock; + @Spy + @InjectMocks + private ParameterRepositoryImpl parameterRepository; + + @Before + public void setUp() { + SessionContextProviderFactory.getInstance().createInterface().create("test_user", "workflow"); + } + + @Test + public void shouldGetParameterById() { + + ElementInfo element = new ElementInfo(); + element.setId(new Id(PARAMETER1_ID)); + Info info = new Info(); + info.setName("testInput"); + info.addProperty(ParameterPropertyName.TYPE.name(), ParameterType.FLOAT.name()); + info.addProperty(ParameterPropertyName.mandatory.name(), true); + element.setInfo(info); + doReturn(Optional.of(element)).when(zusammenAdaptorMock) + .getElementInfo(any(SessionContext.class), any(ElementContext.class), + eq(new Id(PARAMETER1_ID))); + ParameterEntity result = parameterRepository.get(ITEM1_ID, VERSION1_ID, PARAMETER1_ID); + verify(zusammenAdaptorMock) + .getElementInfo(any(SessionContext.class), any(ElementContext.class), eq(new Id(PARAMETER1_ID))); + assertEquals("testInput", result.getName()); + + } + + + @Test + public void shouldUpdateParameter() { + ParameterEntity parameterEntityToUpdate = new ParameterEntity(); + parameterEntityToUpdate.setId(PARAMETER1_ID); + parameterEntityToUpdate.setName("Input1"); + parameterEntityToUpdate.setMandatory(true); + parameterEntityToUpdate.setType(ParameterType.STRING); + + parameterRepository.update(ITEM1_ID, VERSION1_ID, ParameterRole.INPUT, parameterEntityToUpdate); + verify(zusammenAdaptorMock) + .saveElement(any(SessionContext.class), any(ElementContext.class), any(ZusammenElement.class), + eq("Update WorkflowVersion Parameter")); + + } + + @Test + public void shouldCreateParameterStructure() { + parameterRepository.createStructure(ITEM1_ID, VERSION1_ID); + verify(zusammenAdaptorMock) + .saveElement(any(SessionContext.class), any(ElementContext.class), any(ZusammenElement.class), + eq("Create WorkflowVersion INPUTS Element")); + verify(zusammenAdaptorMock) + .saveElement(any(SessionContext.class), any(ElementContext.class), any(ZusammenElement.class), + eq("Create WorkflowVersion OUTPUTS Element")); + } + + @Test + public void shouldDeleteParameter() { + parameterRepository.delete(ITEM1_ID, VERSION1_ID, PARAMETER1_ID); + verify(zusammenAdaptorMock) + .saveElement(any(SessionContext.class), any(ElementContext.class), any(ZusammenElement.class), + eq("Delete Parameter with id parameter_id_1")); + } + + + @Test + public void shouldListParametersByType() { + + ElementInfo parameter1 = new ElementInfo(); + parameter1.setId(new Id(PARAMETER1_ID)); + Info info1 = new Info(); + info1.setName("input1"); + info1.addProperty(ParameterPropertyName.TYPE.name(), "INTEGER"); + info1.addProperty(ParameterPropertyName.mandatory.name(), true); + parameter1.setInfo(info1); + ElementInfo parameter2 = new ElementInfo(); + parameter2.setId(new Id(PARAMETER2_ID)); + Info info2 = new Info(); + info2.setName("input2"); + info2.addProperty(ParameterPropertyName.TYPE.name(), "STRING"); + info2.addProperty(ParameterPropertyName.mandatory.name(), false); + parameter2.setInfo(info2); + Collection<ElementInfo> parameters = Collections.asSet(parameter1, parameter2); + doReturn(parameters).when(zusammenAdaptorMock) + .listElementsByName(any(SessionContext.class), any(ElementContext.class), isNull(), + eq(WorkflowElementType.INPUTS.name())); + Collection<ParameterEntity> results = parameterRepository.list(ITEM1_ID, VERSION1_ID, ParameterRole.INPUT); + + verify(zusammenAdaptorMock).listElementsByName(any(SessionContext.class), any(ElementContext.class), isNull(), + eq(WorkflowElementType.INPUTS.name())); + assertTrue(results.stream().anyMatch(parameterEntity -> parameterEntity.getId().equals(PARAMETER1_ID))); + assertTrue(results.stream().anyMatch(parameterEntity -> parameterEntity.getId().equals(PARAMETER2_ID))); + } + + @Test + public void shouldDeleteAllParametersByType() { + ElementInfo parameterParentElement = new ElementInfo(); + parameterParentElement.setId(new Id(PARAMETERS_PARENT_ID)); + ElementInfo parameter1 = new ElementInfo(); + parameter1.setId(new Id(PARAMETER1_ID)); + ElementInfo parameter2 = new ElementInfo(); + parameter2.setId(new Id(PARAMETER2_ID)); + parameterParentElement.setSubElements(new ArrayList<>()); + parameterParentElement.getSubElements().add(parameter1); + parameterParentElement.getSubElements().add(parameter2); + + Optional<ElementInfo> elementOptional = Optional.of(parameterParentElement); + + doReturn(elementOptional).when(zusammenAdaptorMock) + .getElementInfoByName(any(SessionContext.class), any(ElementContext.class), isNull(), + eq(WorkflowElementType.INPUTS.name())); + + parameterRepository.deleteAll(ITEM1_ID, VERSION1_ID, ParameterRole.INPUT); + verify(zusammenAdaptorMock) + .saveElement(any(SessionContext.class), any(ElementContext.class), any(ZusammenElement.class), + eq("Delete all INPUT")); + } + +} diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/UniqueValueServiceTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/UniqueValueServiceTest.java new file mode 100644 index 00000000..4911060a --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/UniqueValueServiceTest.java @@ -0,0 +1,98 @@ +package org.onap.sdc.workflow.services; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.util.Optional; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; +import org.onap.sdc.workflow.persistence.UniqueValueRepository; +import org.onap.sdc.workflow.persistence.types.UniqueValueEntity; +import org.onap.sdc.workflow.services.exceptions.UniqueValueViolationException; + +public class UniqueValueServiceTest { + + private static final String TYPE = "ss"; + private static final String DUMMY_COMBINATION = "dummy"; + + @Mock + private UniqueValueRepository uniqueValueRepositoryMock; + + @Spy + @InjectMocks + private UniqueValueService uniqueValueService; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void shouldCallRepositoryInsertIfValueUnique(){ + doReturn(Optional.empty()).when(uniqueValueRepositoryMock).findById(any()); + uniqueValueService.createUniqueValue(TYPE, new String[]{DUMMY_COMBINATION}); + verify(uniqueValueRepositoryMock, times(1)).insert(any(UniqueValueEntity.class)); + } + + @Test + public void shouldNotCheckValueIfNoUniqueCombination(){ + uniqueValueService.createUniqueValue(TYPE, null); + verify(uniqueValueRepositoryMock, never()).findById(any(UniqueValueEntity.class)); + } + + @Test(expected = UniqueValueViolationException.class) + public void shouldThrowExceptionIfValueIsNotUnique(){ + doReturn(Optional.of("xxx")).when(uniqueValueRepositoryMock).findById(any()); + uniqueValueService.createUniqueValue(TYPE, new String[]{DUMMY_COMBINATION}); + } + + @Test + public void shouldCallRepositoryDeleteIfValueValid(){ + uniqueValueService.deleteUniqueValue(TYPE, new String[]{DUMMY_COMBINATION}); + verify(uniqueValueRepositoryMock, times(1)).delete(any(UniqueValueEntity.class)); + } + + @Test + public void shouldNotCallRepositoryDeleteIfValueNouniqueCombination(){ + uniqueValueService.deleteUniqueValue(TYPE, new String[]{}); + verify(uniqueValueRepositoryMock, never()).delete(any(UniqueValueEntity.class)); + } + + @Test + public void shouldNotUpdateIfNewAndOldValueAreEqualsCaseIgnore(){ + String value = "value"; + uniqueValueService.updateUniqueValue(TYPE, value, value.toUpperCase()); + verify(uniqueValueService, never()).createUniqueValue(anyString(), any()); + } + + @Test + public void shouldUpdateIfNewAndOldValueAreNotEqualsCaseIgnore(){ + String oldValue = "oldValue"; + String newValue = "newValue"; + uniqueValueService.updateUniqueValue(TYPE, oldValue, newValue); + verify(uniqueValueService, times(1)).createUniqueValue(anyString(), any()); + verify(uniqueValueService, times(1)).deleteUniqueValue(anyString(), any()); + } + + @Test + public void shouldReturnTrueIfValueExist() { + doReturn(Optional.of("xxx")).when(uniqueValueRepositoryMock).findById(any()); + assertTrue(uniqueValueService.isUniqueValueOccupied(TYPE, new String[]{DUMMY_COMBINATION})); + } + + @Test + public void shouldReturnFalseIfValueNotExist() { + doReturn(Optional.empty()).when(uniqueValueRepositoryMock).findById(any()); + assertFalse(uniqueValueService.isUniqueValueOccupied(TYPE, new String[]{DUMMY_COMBINATION})); + } +} 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 new file mode 100644 index 00000000..0105fe83 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImplTest.java @@ -0,0 +1,240 @@ +package org.onap.sdc.workflow.services.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +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.api.RestConstants.SORT_FIELD_NAME; +import static org.openecomp.sdc.versioning.dao.types.VersionStatus.Certified; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +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; +import org.mockito.Mock; +import org.onap.sdc.workflow.persistence.types.Workflow; +import org.onap.sdc.workflow.persistence.types.WorkflowVersionState; +import org.onap.sdc.workflow.services.UniqueValueService; +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.openecomp.sdc.versioning.ItemManager; +import org.openecomp.sdc.versioning.types.Item; +import org.openecomp.sdc.versioning.types.ItemStatus; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +public class WorkflowManagerImplTest { + + private static final String ITEM1_ID = "1"; + private static final String WORKFLOW_TYPE = "WORKFLOW"; + private static final String WORKFLOW_NAME_UNIQUE_TYPE = "WORKFLOW_NAME"; + private static final List<Item> ITEMS; + private static final List<Workflow> MAPPED_WORKFLOWS; + + static { + List<Item> items = new ArrayList<>(); + List<Workflow> mappedWorkflows = new ArrayList<>(); + for (int i = 1; i < 6; i++) { + items.add(createItem(i, true, true)); + mappedWorkflows.add(createWorkflow(i, true)); + } + ITEMS = Collections.unmodifiableList(items); + MAPPED_WORKFLOWS = Collections.unmodifiableList(mappedWorkflows); + } + + @Mock + private ItemManager itemManagerMock; + @Mock + private UniqueValueService uniqueValueServiceMock; + @Mock + private WorkflowMapper workflowMapperMock; + @Mock + private VersionStateMapper versionStateMapperMock; + @InjectMocks + private WorkflowManagerImpl workflowManager; + + @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)); + } + Collection<Workflow> workflows = + workflowManager.list(null, createPageRequest(20, 0, Sort.Direction.ASC, SORT_FIELD_NAME)); + + Map<String, Workflow> workflowById = + workflows.stream().collect(Collectors.toMap(Workflow::getId, Function.identity())); + assertEquals(ITEMS.size(), workflows.size()); + for (int i = 1; i < ITEMS.size() + 1; i++) { + assertTrue(workflowById.containsKey(String.valueOf(i))); + } + } + + @Test + public void listWithVersionStateFilter() { + doReturn(Certified).when(versionStateMapperMock) + .workflowVersionStateToVersionStatus(WorkflowVersionState.CERTIFIED); + doReturn(Arrays.asList(ITEMS.get(0), ITEMS.get(2))).when(itemManagerMock).list(any()); + doReturn(MAPPED_WORKFLOWS.get(0)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(0)); + doReturn(MAPPED_WORKFLOWS.get(2)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(2)); + + Collection<Workflow> workflows = workflowManager.list(Collections.singleton(WorkflowVersionState.CERTIFIED), + createPageRequest(20, 0, Sort.Direction.ASC, SORT_FIELD_NAME)); + + Map<String, Workflow> workflowById = + workflows.stream().collect(Collectors.toMap(Workflow::getId, Function.identity())); + assertEquals(2, workflows.size()); + assertTrue(workflowById.containsKey("1")); + assertTrue(workflowById.containsKey("3")); + } + + @Test(expected = EntityNotFoundException.class) + public void shouldThrowExceptionWhenWorkflowDontExist() { + Workflow nonExistingWorkflow = new Workflow(); + nonExistingWorkflow.setId(ITEM1_ID); + doReturn(null).when(itemManagerMock).get(ITEM1_ID); + workflowManager.get(nonExistingWorkflow); + verify(workflowMapperMock, times(3)).itemToWorkflow(any(Item.class)); + } + + @Test + public void shouldReturnWorkflow() { + Item retrievedItem = createItem(1, true, true); + doReturn(retrievedItem).when(itemManagerMock).get(ITEM1_ID); + Workflow workflow = createWorkflow(1, true); + workflowManager.get(workflow); + verify(itemManagerMock).get(ITEM1_ID); + verify(workflowMapperMock).itemToWorkflow(retrievedItem); + } + + @Test + public void shouldCreateWorkflowItemFromWorkflow() { + Workflow workflowToBeCreated = createWorkflow(1, false); + Item createdWorkflowItem = createItem(1, false, true); + doReturn(createdWorkflowItem).when(workflowMapperMock).workflowToItem(workflowToBeCreated); + doReturn(createdWorkflowItem).when(itemManagerMock).create(createdWorkflowItem); + workflowManager.create(workflowToBeCreated); + verify(uniqueValueServiceMock) + .validateUniqueValue(WORKFLOW_NAME_UNIQUE_TYPE, new String[] {workflowToBeCreated.getName()}); + + createdWorkflowItem.setStatus(ItemStatus.ACTIVE); + createdWorkflowItem.setType(WORKFLOW_TYPE); + verify(itemManagerMock).create(createdWorkflowItem); + verify(uniqueValueServiceMock) + .createUniqueValue(WORKFLOW_NAME_UNIQUE_TYPE, new String[] {workflowToBeCreated.getName()}); + } + + @Test + public void shouldUpdateWorkflow() { + Item workflowItem = createItem(1, true, true); + doReturn(workflowItem).when(itemManagerMock).get(ITEM1_ID); + Workflow workflowToBeUpdated = createWorkflow(1, true); + doReturn(workflowItem).when(workflowMapperMock).workflowToItem(workflowToBeUpdated); + workflowManager.update(workflowToBeUpdated); + verify(itemManagerMock).update(workflowItem); + verify(uniqueValueServiceMock) + .updateUniqueValue(WORKFLOW_NAME_UNIQUE_TYPE, workflowItem.getName(), workflowToBeUpdated.getName()); + + } + + @Test(expected = EntityNotFoundException.class) + public void shouldThrowExceptionWhenWorkflowToUpdateNotFound() { + doReturn(null).when(itemManagerMock).get(ITEM1_ID); + workflowManager.update(createWorkflow(1, true)); + } + + @Test + public void shouldListAllWorkflowsWhenLimitAndOffsetAreValid() { + PageRequest pageRequest = createPageRequest(5, 0, Sort.Direction.ASC, SORT_FIELD_NAME); + 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, pageRequest).size()); + } + + @Test + public void shouldListLimitFilteredWorkflowsInFirstOffsetRange() { + PageRequest pageRequest = createPageRequest(3, 0, Sort.Direction.ASC, SORT_FIELD_NAME); + 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, pageRequest).size()); + } + + @Test + public void shouldListLimitFilteredWorkflowsInSecondOffsetRange() { + PageRequest pageRequest = createPageRequest(3, 1, Sort.Direction.ASC, SORT_FIELD_NAME); + 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, pageRequest).size()); + } + + @Test + public void shouldListAllWorkflowsWhenLimitGreaterThanTotalRecordsAndOffsetInRange() { + PageRequest pageRequest = createPageRequest(10, 0, Sort.Direction.ASC, SORT_FIELD_NAME); + 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, pageRequest).size()); + } + + @Test + public void shouldNotListWorkflowsIfOffsetGreaterThanTotalRecords() { + PageRequest pageRequest = createPageRequest(3, 6, Sort.Direction.ASC, SORT_FIELD_NAME); + 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, pageRequest).size()); + } + + @Test + public void shouldNotListWorkflowsBothLimitAndOffsetGreaterThanTotalRecords() { + PageRequest pageRequest = createPageRequest(10, 10, Sort.Direction.ASC, SORT_FIELD_NAME); + 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, pageRequest).size()); + } + + @Test + public void shouldListLimitOffsetAppliedWorkflowsSortedInDescOrder() { + PageRequest pageRequest = createPageRequest(2, 1, Sort.Direction.DESC, SORT_FIELD_NAME); + 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)); + } + Collection<Workflow> workflows = workflowManager.list(null, pageRequest); + Assert.assertEquals(2, workflows.size()); + Iterator<Workflow> workflowIterator = workflows.iterator(); + Assert.assertEquals("Workflow_3", workflowIterator.next().getName()); + Assert.assertEquals("Workflow_2", workflowIterator.next().getName()); + } + + private PageRequest createPageRequest(int limit, int offset, Sort.Direction sortOrder, String sortField) { + return PageRequest.of(offset, limit, sortOrder, sortField); + } + +}
\ No newline at end of file diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerImplTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerImplTest.java new file mode 100644 index 00000000..7dee5245 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerImplTest.java @@ -0,0 +1,264 @@ +package org.onap.sdc.workflow.services.impl; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.onap.sdc.workflow.persistence.types.WorkflowVersionState.CERTIFIED; +import static org.onap.sdc.workflow.persistence.types.WorkflowVersionState.DRAFT; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import org.apache.commons.io.IOUtils; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.ArgumentMatcher; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.sdc.workflow.persistence.ArtifactRepository; +import org.onap.sdc.workflow.persistence.ParameterRepository; +import org.onap.sdc.workflow.persistence.types.ArtifactEntity; +import org.onap.sdc.workflow.persistence.types.ParameterRole; +import org.onap.sdc.workflow.persistence.types.WorkflowVersion; +import org.onap.sdc.workflow.persistence.types.WorkflowVersionState; +import org.onap.sdc.workflow.services.exceptions.EntityNotFoundException; +import org.onap.sdc.workflow.services.exceptions.VersionCreationException; +import org.onap.sdc.workflow.services.exceptions.VersionStateModificationException; +import org.onap.sdc.workflow.services.impl.mappers.VersionMapper; +import org.onap.sdc.workflow.services.impl.mappers.VersionStateMapper; +import org.openecomp.sdc.versioning.VersioningManager; +import org.openecomp.sdc.versioning.dao.types.Version; +import org.openecomp.sdc.versioning.dao.types.VersionStatus; +import org.openecomp.sdc.versioning.types.VersionCreationMethod; +import org.springframework.mock.web.MockMultipartFile; + +@RunWith(MockitoJUnitRunner.class) +public class WorkflowVersionManagerImplTest { + + private static final String ITEM1_ID = "item_id_1"; + private static final String VERSION1_ID = "version_id_1"; + private static final String VERSION2_ID = "version_id_2"; + + @Mock + private VersioningManager versioningManagerMock; + @Mock + private ParameterRepository parameterRepositoryMock; + @Mock + private ArtifactRepository artifactRepositoryMock; + @Mock + private VersionMapper versionMapperMock; + @Mock + private VersionStateMapper versionStateMapperMock; + @Spy + @InjectMocks + private WorkflowVersionManagerImpl workflowVersionManager; + + @Test(expected = EntityNotFoundException.class) + public void shouldThrowExceptionWhenVersionDontExist() { + Version nonExistingVersion = new Version(VERSION1_ID); + doThrow(new RuntimeException()).when(versioningManagerMock).get(ITEM1_ID, nonExistingVersion); + workflowVersionManager.get(ITEM1_ID, VERSION1_ID); + } + + @Test + public void shouldReturnWorkflowVersionWhenExist() { + Version version = new Version(VERSION1_ID); + WorkflowVersion workflowVersion = new WorkflowVersion(VERSION1_ID); + doReturn(workflowVersion).when(versionMapperMock).versionToWorkflowVersion(any(Version.class)); + doReturn(version).when(versioningManagerMock).get(eq(ITEM1_ID),any(Version.class)); + doReturn(new ArrayList()).when(parameterRepositoryMock).list(eq(ITEM1_ID), eq(VERSION1_ID), any(ParameterRole.class)); + workflowVersionManager.get(ITEM1_ID,VERSION1_ID); + verify(versioningManagerMock).get(ITEM1_ID,version); + } + + @Test + public void shouldReturnWorkflowVersionList() { + List<Version> versionList = Arrays.asList(new Version(VERSION1_ID), new Version(VERSION2_ID)); + doReturn(versionList).when(versioningManagerMock).list(ITEM1_ID); + doReturn(new WorkflowVersion()).when(versionMapperMock).versionToWorkflowVersion(any(Version.class)); + workflowVersionManager.list(ITEM1_ID, null); + verify(versioningManagerMock).list(ITEM1_ID); + verify(versionMapperMock, times(2)).versionToWorkflowVersion(any(Version.class)); + } + + @Test + public void shouldUpdateWorkflowVersion() { + String updatedDescription = "WorkflowVersion description updated"; + Version retrievedVersion = new Version(VERSION1_ID); + retrievedVersion.setName("1.0"); + retrievedVersion.setDescription("WorkflowVersion description"); + retrievedVersion.setStatus(VersionStatus.Draft); + doReturn(retrievedVersion).when(versioningManagerMock).get(eq(ITEM1_ID), eqVersion(VERSION1_ID)); + doReturn(DRAFT).when(versionStateMapperMock).versionStatusToWorkflowVersionState(retrievedVersion.getStatus()); + + WorkflowVersion inputVersion = new WorkflowVersion(VERSION1_ID); + inputVersion.setName("1.0"); + inputVersion.setDescription(updatedDescription); + inputVersion.setInputs(new ArrayList<>()); + inputVersion.setOutputs(new ArrayList<>()); + + Version mappedInputVersion = new Version(VERSION1_ID); + mappedInputVersion.setName("1.0"); + mappedInputVersion.setDescription(updatedDescription); + doReturn(mappedInputVersion).when(versionMapperMock).workflowVersionToVersion(inputVersion); + + ArgumentCaptor<Version> versionArgCaptor = ArgumentCaptor.forClass(Version.class); + workflowVersionManager.update(ITEM1_ID, inputVersion); + + verify(versioningManagerMock).updateVersion(eq(ITEM1_ID), versionArgCaptor.capture()); + Version captorVersion = versionArgCaptor.getValue(); + assertEquals("1.0", captorVersion.getName()); + assertEquals(updatedDescription, captorVersion.getDescription()); + assertEquals(VersionStatus.Draft, captorVersion.getStatus()); + verify(versioningManagerMock).publish(ITEM1_ID, mappedInputVersion, "Update version"); + } + + + @Test + public void shouldCreateWorkflowVersion(){ + Version version = new Version(VERSION1_ID); + version.setDescription("version desc"); + doReturn(version).when(versioningManagerMock).create(ITEM1_ID,version, VersionCreationMethod.major); + WorkflowVersion versionRequest = new WorkflowVersion(); + versionRequest.setDescription("version desc"); + versionRequest.setInputs(new ArrayList<>()); + versionRequest.setOutputs(new ArrayList<>()); + WorkflowVersion workflowVersion = new WorkflowVersion(VERSION1_ID); + doReturn(workflowVersion).when(workflowVersionManager).get(ITEM1_ID,VERSION1_ID); + workflowVersionManager.create(ITEM1_ID, null, versionRequest); + verify(versioningManagerMock).create(ITEM1_ID,version, VersionCreationMethod.major); + } + + @Test(expected = VersionCreationException.class) + public void shouldTrowExceptionWhenDraftVersionExists() { + WorkflowVersion versionRequestDto = new WorkflowVersion(); + + Version baseVersion = new Version(VERSION2_ID); + baseVersion.setStatus(VersionStatus.Draft); + List<Version> versions = Collections.singletonList(baseVersion); + doReturn(versions).when(versioningManagerMock).list(ITEM1_ID); + + workflowVersionManager.create(ITEM1_ID, VERSION2_ID, versionRequestDto); + } + + @Test(expected = EntityNotFoundException.class) + public void getStateOfNonExisting() { + doThrow(new RuntimeException()).when(versioningManagerMock).get(eq(ITEM1_ID), eqVersion(VERSION1_ID)); + workflowVersionManager.getState(ITEM1_ID, VERSION1_ID); + } + + @Test + public void getState() { + Version version = new Version(VERSION1_ID); + version.setStatus(VersionStatus.Certified); + doReturn(version).when(versioningManagerMock).get(eq(ITEM1_ID), eqVersion(VERSION1_ID)); + doReturn(CERTIFIED).when(versionStateMapperMock).versionStatusToWorkflowVersionState(version.getStatus()); + + WorkflowVersionState state = workflowVersionManager.getState(ITEM1_ID, VERSION1_ID); + assertEquals(CERTIFIED, state); + } + + @Test(expected = EntityNotFoundException.class) + public void updateStateOfNonExisting() { + doThrow(new RuntimeException()).when(versioningManagerMock).get(eq(ITEM1_ID), eqVersion(VERSION1_ID)); + workflowVersionManager.updateState(ITEM1_ID, VERSION1_ID, CERTIFIED); + } + + @Test(expected = VersionStateModificationException.class) + public void updateStateToCurrentState() { + Version version = new Version(VERSION1_ID); + version.setStatus(VersionStatus.Draft); + doReturn(version).when(versioningManagerMock).get(eq(ITEM1_ID), eqVersion(VERSION1_ID)); + doReturn(DRAFT).when(versionStateMapperMock).versionStatusToWorkflowVersionState(version.getStatus()); + + workflowVersionManager.updateState(ITEM1_ID, VERSION1_ID, DRAFT); + } + + @Test(expected = VersionStateModificationException.class) + public void updateStateWhenCertified() { + Version version = new Version(VERSION1_ID); + version.setStatus(VersionStatus.Certified); + doReturn(version).when(versioningManagerMock).get(eq(ITEM1_ID), eqVersion(VERSION1_ID)); + doReturn(CERTIFIED).when(versionStateMapperMock).versionStatusToWorkflowVersionState(version.getStatus()); + doThrow(new RuntimeException()).when(versioningManagerMock) + .submit(eq(ITEM1_ID), eqVersion(VERSION1_ID), anyString()); + + workflowVersionManager.updateState(ITEM1_ID, VERSION1_ID, CERTIFIED); + } + + @Test + public void updateState() { + Version retrievedVersion = new Version(VERSION1_ID); + retrievedVersion.setStatus(VersionStatus.Draft); + doReturn(retrievedVersion).when(versioningManagerMock).get(eq(ITEM1_ID), eqVersion(VERSION1_ID)); + doReturn(DRAFT).when(versionStateMapperMock).versionStatusToWorkflowVersionState(VersionStatus.Draft); + + workflowVersionManager.updateState(ITEM1_ID, VERSION1_ID, CERTIFIED); + + verify(versioningManagerMock).submit(eq(ITEM1_ID), eqVersion(VERSION1_ID), anyString()); + } + + @Test + public void shouldUploadArtifact() { + Version version = new Version(VERSION1_ID); + version.setStatus(VersionStatus.Draft); + doReturn(version).when(versioningManagerMock).get(eq(ITEM1_ID), eqVersion(VERSION1_ID)); + doReturn(DRAFT).when(versionStateMapperMock).versionStatusToWorkflowVersionState(version.getStatus()); + + MockMultipartFile mockFile = new MockMultipartFile("data", "filename.txt", "text/plain", "some xml".getBytes()); + workflowVersionManager.uploadArtifact(ITEM1_ID, VERSION1_ID, mockFile); + + verify(artifactRepositoryMock).update(eq(ITEM1_ID), eq(VERSION1_ID), any(ArtifactEntity.class)); + } + + @Test(expected = EntityNotFoundException.class) + public void shouldThrowExceptionWhenArtifactNotFound() { + doReturn(new Version(VERSION1_ID)).when(versioningManagerMock).get(eq(ITEM1_ID), eqVersion(VERSION1_ID)); + + doReturn(Optional.empty()).when(artifactRepositoryMock).get(ITEM1_ID, VERSION1_ID); + workflowVersionManager.getArtifact(ITEM1_ID, VERSION1_ID); + } + + @Test + public void shouldReturnArtifact() throws IOException { + doReturn(new Version(VERSION1_ID)).when(versioningManagerMock).get(eq(ITEM1_ID), eqVersion(VERSION1_ID)); + + InputStream inputStreamMock = IOUtils.toInputStream("some test data for my input stream", "UTF-8"); + ArtifactEntity artifactMock = new ArtifactEntity("fileName.txt", inputStreamMock); + doReturn(Optional.of(artifactMock)).when(artifactRepositoryMock).get(ITEM1_ID, VERSION1_ID); + ArtifactEntity returnedArtifact = workflowVersionManager.getArtifact(ITEM1_ID, VERSION1_ID); + assertEquals(artifactMock, returnedArtifact); + } + + private static Version eqVersion(String versionId) { + return argThat(new EqVersion(versionId)); + } + + private static class EqVersion implements ArgumentMatcher<Version> { + + private final String versionId; + + EqVersion(String versionId) { + this.versionId = versionId; + } + + @Override + public boolean matches(Version version) { + return versionId.equals(version.getId()); + } + } + +}
\ No newline at end of file diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/mappers/VersionMapperTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/mappers/VersionMapperTest.java new file mode 100644 index 00000000..d9790355 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/mappers/VersionMapperTest.java @@ -0,0 +1,80 @@ +package org.onap.sdc.workflow.services.impl.mappers; + +import static org.junit.Assert.assertEquals; + +import java.util.Date; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.sdc.workflow.persistence.types.WorkflowVersion; +import org.onap.sdc.workflow.persistence.types.WorkflowVersionState; +import org.openecomp.sdc.versioning.dao.types.Version; +import org.openecomp.sdc.versioning.dao.types.VersionStatus; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@ContextConfiguration(classes = VersionMapperTest.VersionMapperSpringTestConfig.class) +@RunWith(SpringJUnit4ClassRunner.class) +public class VersionMapperTest { + + @Configuration + @ComponentScan(basePackageClasses = {VersionMapper.class, VersionStateMapper.class}) + public static class VersionMapperSpringTestConfig { } + + @Autowired + VersionMapper versionMapper; + + + @Test + public void shouldMapVersionToWorkflowVersion() { + Version version = createVersion(); + WorkflowVersion mappedWorkflowVersion = versionMapper.versionToWorkflowVersion(version); + assertEquals(mappedWorkflowVersion.getId(), version.getId()); + assertEquals(mappedWorkflowVersion.getBaseId(), version.getBaseId()); + assertEquals(mappedWorkflowVersion.getDescription(), version.getDescription()); + assertEquals(mappedWorkflowVersion.getName(), version.getName()); + assertEquals(mappedWorkflowVersion.getCreationTime(), version.getCreationTime()); + assertEquals(mappedWorkflowVersion.getModificationTime(), version.getModificationTime()); + } + + @Test + public void shouldMapWorkflowVersionToVersion() { + WorkflowVersion workflowVersion = createWorkflowVersion(); + Version mappedVersion = versionMapper.workflowVersionToVersion(workflowVersion); + assertEquals(mappedVersion.getId(), workflowVersion.getId()); + assertEquals(mappedVersion.getBaseId(), workflowVersion.getBaseId()); + assertEquals(mappedVersion.getDescription(), workflowVersion.getDescription()); + assertEquals(mappedVersion.getName(), workflowVersion.getName()); + assertEquals(mappedVersion.getCreationTime(), workflowVersion.getCreationTime()); + assertEquals(mappedVersion.getModificationTime(), workflowVersion.getModificationTime()); + + } + + private Version createVersion() { + Version version = new Version("version_id"); + version.setBaseId("base_version_id"); + version.setName("1.0"); + version.setCreationTime(new Date()); + version.setModificationTime(new Date()); + version.setDescription("version_description"); + version.setStatus(VersionStatus.Draft); + + return version; + + } + + private WorkflowVersion createWorkflowVersion() { + WorkflowVersion workflowVersion = new WorkflowVersion(); + workflowVersion.setId("wf_version_id"); + workflowVersion.setBaseId("wf_base_version_id"); + workflowVersion.setName("1.0"); + workflowVersion.setCreationTime(new Date()); + workflowVersion.setModificationTime(new Date()); + workflowVersion.setDescription("version_description"); + workflowVersion.setState(WorkflowVersionState.CERTIFIED); + + return workflowVersion; + } +}
\ No newline at end of file diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/mappers/VersionStateMapperTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/mappers/VersionStateMapperTest.java new file mode 100644 index 00000000..070c433e --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/mappers/VersionStateMapperTest.java @@ -0,0 +1,75 @@ +package org.onap.sdc.workflow.services.impl.mappers; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.sdc.workflow.persistence.types.WorkflowVersionState; +import org.openecomp.sdc.versioning.dao.types.VersionStatus; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@ContextConfiguration(classes = VersionStateMapperTest.VersionStatusMapperSpringTestConfig.class) +@RunWith(SpringJUnit4ClassRunner.class) +public class VersionStateMapperTest { + + @Configuration + @ComponentScan(basePackageClasses = {VersionStateMapper.class}) + public static class VersionStatusMapperSpringTestConfig { } + + + @Autowired + VersionStateMapper versionStateMapper; + + @Test + public void shouldMapCertifiedVersionStatusToWorkflowVersionStatus() { + WorkflowVersionState mappedVersionStatus = + versionStateMapper.versionStatusToWorkflowVersionState(VersionStatus.Certified); + assertEquals(WorkflowVersionState.CERTIFIED, mappedVersionStatus); + } + + @Test + public void shouldMapDraftVersionStatusToWorkflowVersionStatus() { + WorkflowVersionState mappedVersionStatus = + versionStateMapper.versionStatusToWorkflowVersionState(VersionStatus.Draft); + assertEquals(WorkflowVersionState.DRAFT, mappedVersionStatus); + } + + @Test + public void shouldMapDeletedVersionStatusToWorkflowVersionStatus() { + WorkflowVersionState mappedVersionStatus = + versionStateMapper.versionStatusToWorkflowVersionState(VersionStatus.Deleted); + assertEquals(WorkflowVersionState.DRAFT, mappedVersionStatus); + } + + @Test + public void shouldMapLockedVersionStatusToWorkflowVersionStatus() { + WorkflowVersionState mappedVersionStatus = + versionStateMapper.versionStatusToWorkflowVersionState(VersionStatus.Locked); + assertEquals(WorkflowVersionState.DRAFT, mappedVersionStatus); + } + + @Test + public void shouldMapDeprecatedVersionStatusToWorkflowVersionStatus() { + WorkflowVersionState mappedVersionStatus = + versionStateMapper.versionStatusToWorkflowVersionState(VersionStatus.Deprecated); + assertEquals(WorkflowVersionState.DRAFT, mappedVersionStatus); + } + + @Test + public void shouldMapCertifiedWorkflowVersionStatusToVersionStatus() { + VersionStatus mappedVersionStatus = + versionStateMapper.workflowVersionStateToVersionStatus(WorkflowVersionState.CERTIFIED); + assertEquals(VersionStatus.Certified, mappedVersionStatus); + } + + @Test + public void shouldMapDraftWorkflowVersionStatusToVersionStatus() { + VersionStatus mappedVersionStatus = + versionStateMapper.workflowVersionStateToVersionStatus(WorkflowVersionState.DRAFT); + assertEquals(VersionStatus.Draft, mappedVersionStatus); + } +}
\ No newline at end of file diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/mappers/WorkflowMapperTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/mappers/WorkflowMapperTest.java new file mode 100644 index 00000000..55802cc0 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/mappers/WorkflowMapperTest.java @@ -0,0 +1,50 @@ +package org.onap.sdc.workflow.services.impl.mappers; + + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.onap.sdc.workflow.TestUtil.createItem; +import static org.onap.sdc.workflow.TestUtil.createWorkflow; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.sdc.workflow.persistence.types.Workflow; +import org.openecomp.sdc.versioning.types.Item; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@ContextConfiguration(classes = WorkflowMapperTest.WorkflowMapperSpringTestConfig.class) +@RunWith(SpringJUnit4ClassRunner.class) +public class WorkflowMapperTest { + + @Configuration + @ComponentScan(basePackageClasses = {WorkflowMapper.class}) + public static class WorkflowMapperSpringTestConfig { } + + @Autowired + WorkflowMapper workflowMapper; + + @Test + public void shouldMapItemToWorkflow() { + + Item item = createItem(1,false,true); + Workflow mappedWorkflow = workflowMapper.itemToWorkflow(item); + assertEquals(mappedWorkflow.getId(), item.getId()); + assertEquals(mappedWorkflow.getDescription(), item.getDescription()); + assertEquals(mappedWorkflow.getName(), item.getName()); + } + + @Test + public void shouldMapWorkflowToItem(){ + + Workflow workflow = createWorkflow(1,true); + Item mappedItem = workflowMapper.workflowToItem(workflow); + assertEquals(mappedItem.getId(), workflow.getId()); + assertEquals(mappedItem.getDescription(), workflow.getDescription()); + assertEquals(mappedItem.getName(), workflow.getName()); + } + +}
\ No newline at end of file |