diff options
author | 2018-11-05 14:39:38 +0200 | |
---|---|---|
committer | 2018-11-20 09:44:22 +0000 | |
commit | 0279fb7d90a7a5522e5717049a9e33a9db6392aa (patch) | |
tree | e918176a099ad21aa67aad0cf2b8ca2b5995074f /workflow-designer-be/src/test/java | |
parent | 54226668c7f778a757501087a278d741308dd93c (diff) |
Restrict workflow certify if artifact not present
Support Workflow Archive and restore
SDCNEW-972 Issue & Archive Workflow changes
Change-Id: Ie98501fddaf3543ad1084998153727b6932d449e
Issue-ID: SDC-1901
Signed-off-by: siddharth0905 <siddharth.singh4@amdocs.com>
Diffstat (limited to 'workflow-designer-be/src/test/java')
6 files changed, 292 insertions, 54 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 6c825f9d..9821d510 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 @@ -12,6 +12,7 @@ public class RestPath { private static final String WORKFLOWS_URL = "/wf/workflows"; private static final String WORKFLOW_URL_FORMATTER = WORKFLOWS_URL + "/%s"; + private static final String ARCHIVE_URL_FORMATTER = WORKFLOWS_URL + "/%s/archiving"; 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 + "=%s"; @@ -25,15 +26,15 @@ public class RestPath { private static final String WORKFLOW_URL_FORMATTER_QUERY_PARAMS_NO_SORT_AND_OFFSET = WORKFLOWS_URL + "?" + LIMIT_QUERY_STRING_FORMATTER; - public static String getWorkflowsPathAllQueryParams(String sort, String limit, String offset){ + public static String getWorkflowsPathAllQueryParams(String sort, String limit, String offset) { return String.format(WORKFLOW_URL_FORMATTER_QUERY_PARAMS_ALL, sort, limit, offset); } - public static String getWorkflowsPathNoSortAndLimit(String offset){ + public static String getWorkflowsPathNoSortAndLimit(String offset) { return String.format(WORKFLOW_URL_FORMATTER_QUERY_PARAMS_NO_SORT_AND_LIMIT, offset); } - public static String getWorkflowsPathNoSortAndOffset(String limit){ + public static String getWorkflowsPathNoSortAndOffset(String limit) { return String.format(WORKFLOW_URL_FORMATTER_QUERY_PARAMS_NO_SORT_AND_OFFSET, limit); } @@ -45,6 +46,10 @@ public class RestPath { return String.format(WORKFLOW_URL_FORMATTER, workflowId); } + public static String getArchiveWorkflowPath(String workflowId) { + return String.format(ARCHIVE_URL_FORMATTER, workflowId); + } + public static String getWorkflowVersions(String workflowId) { return String.format(VERSIONS_URL_FORMATTER, workflowId); } diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/ExceptionsHandlerTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/ExceptionsHandlerTest.java index e4008bb0..fc35c4a6 100644 --- a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/ExceptionsHandlerTest.java +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/ExceptionsHandlerTest.java @@ -14,6 +14,8 @@ import org.onap.sdc.workflow.api.types.ErrorResponse; import org.onap.sdc.workflow.api.types.UnexpectedErrorResponse; import org.onap.sdc.workflow.services.exceptions.EntityNotFoundException; import org.onap.sdc.workflow.services.exceptions.VersionModificationException; +import org.onap.sdc.workflow.services.exceptions.VersionStateModificationMissingArtifactException; +import org.onap.sdc.workflow.services.types.WorkflowVersionState; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -42,6 +44,17 @@ public class ExceptionsHandlerTest { } @Test + public void handleUnprocessableEntityVersionStateModificationMissingArtifactException() { + VersionStateModificationMissingArtifactException exception = + new VersionStateModificationMissingArtifactException("WF_ID", "Version_id", + WorkflowVersionState.DRAFT, WorkflowVersionState.CERTIFIED); + ResponseEntity<ErrorResponse> response = exceptionsHandler.handleUnprocessableEntityException(exception); + + assertEquals(UNPROCESSABLE_ENTITY, response.getStatusCode()); + assertEquals(exception.getMessage(), response.getBody().getMessage()); + } + + @Test public void handleUnexpectedException() { Exception exception = new Exception("message"); ResponseEntity<UnexpectedErrorResponse> response = exceptionsHandler.handleUnexpectedException(exception); 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 9c481282..0c48fd64 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 @@ -40,6 +40,7 @@ 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.onap.sdc.workflow.services.types.Workflow; +import org.onap.sdc.workflow.services.types.WorkflowStatus; import org.onap.sdc.workflow.services.utilities.JsonUtil; import org.springframework.data.web.PageableHandlerMethodArgumentResolver; import org.springframework.test.web.servlet.MockMvc; @@ -95,6 +96,22 @@ public class WorkflowControllerTest { } @Test + public void shouldReturnOkWhenArchivingWorkflow() throws Exception { + Workflow workflowMock = createWorkflow(1, true); + mockMvc.perform(post(RestPath.getArchiveWorkflowPath(workflowMock.getId())).header(USER_ID_HEADER, USER_ID) + .contentType(APPLICATION_JSON).content("{\"status\": \"ARCHIVED\"}")).andExpect(status().isOk()); + verify(workflowManagerMock).updateStatus(workflowMock.getId(),WorkflowStatus.ARCHIVED); + } + + @Test + public void shouldReturnOkWhenRestoringWorkflow() throws Exception { + Workflow workflowMock = createWorkflow(1, true); + mockMvc.perform(post(RestPath.getArchiveWorkflowPath(workflowMock.getId())).header(USER_ID_HEADER, USER_ID) + .contentType(APPLICATION_JSON).content("{\"status\": \"ACTIVE\"}")).andExpect(status().isOk()); + verify(workflowManagerMock).updateStatus(workflowMock.getId(),WorkflowStatus.ACTIVE); + } + + @Test public void listWhenExist() throws Exception { mockManagerList3(); ResultActions result = mockMvc.perform( @@ -105,7 +122,7 @@ public class WorkflowControllerTest { result.andExpect(jsonPath(String.format("$.items[%s].id", i), is(String.valueOf(i + 1)))); } - verify(workflowManagerMock).list(any(), any(), requestSpecArg.capture()); + verify(workflowManagerMock).list(any(), any(), any(), requestSpecArg.capture()); assertRequestSpec(requestSpecArg.getValue(), DEFAULT_OFFSET, DEFAULT_LIMIT, Collections.emptyList()); } @@ -115,7 +132,7 @@ public class WorkflowControllerTest { mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "2", "1")) .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)) .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3))); - verify(workflowManagerMock).list(any(), any(), requestSpecArg.capture()); + verify(workflowManagerMock).list(any(), any(), any(), requestSpecArg.capture()); assertRequestSpec(requestSpecArg.getValue(), 1, 2, Collections.singletonList(new Sort("name", true))); } @@ -125,7 +142,7 @@ public class WorkflowControllerTest { mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "-2", "1")) .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)) .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3))); - verify(workflowManagerMock).list(any(), any(), requestSpecArg.capture()); + verify(workflowManagerMock).list(any(), any(), any(), requestSpecArg.capture()); assertRequestSpec(requestSpecArg.getValue(), 1, DEFAULT_LIMIT, Collections.singletonList(new Sort("name", true))); } @@ -136,7 +153,7 @@ public class WorkflowControllerTest { mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "2", "-1")) .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)) .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3))); - verify(workflowManagerMock).list(any(), any(), requestSpecArg.capture()); + verify(workflowManagerMock).list(any(), any(), any(), requestSpecArg.capture()); assertRequestSpec(requestSpecArg.getValue(), DEFAULT_OFFSET, 2, Collections.singletonList(new Sort("name", true))); } @@ -147,7 +164,7 @@ public class WorkflowControllerTest { mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "abc", "0")) .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)) .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3))); - verify(workflowManagerMock).list(any(), any(), requestSpecArg.capture()); + verify(workflowManagerMock).list(any(), any(), any(), requestSpecArg.capture()); assertRequestSpec(requestSpecArg.getValue(), 0, DEFAULT_LIMIT, Collections.singletonList(new Sort("name", true))); } @@ -158,7 +175,7 @@ public class WorkflowControllerTest { mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "2", "abc")) .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)) .andExpect(jsonPath("$.items", hasSize(3))); - verify(workflowManagerMock).list(any(), any(), requestSpecArg.capture()); + verify(workflowManagerMock).list(any(), any(), any(), requestSpecArg.capture()); assertRequestSpec(requestSpecArg.getValue(), DEFAULT_OFFSET, 2, Collections.singletonList(new Sort("name", true))); } @@ -169,7 +186,7 @@ public class WorkflowControllerTest { mockMvc.perform(get(RestPath.getWorkflowsPathNoSortAndLimit("1")).header(USER_ID_HEADER, USER_ID) .contentType(APPLICATION_JSON)) .andExpect(jsonPath("$.items", hasSize(3))); - verify(workflowManagerMock).list(any(), any(), requestSpecArg.capture()); + verify(workflowManagerMock).list(any(), any(), any(), requestSpecArg.capture()); assertRequestSpec(requestSpecArg.getValue(), 1, DEFAULT_LIMIT, Collections.emptyList()); } @@ -179,7 +196,7 @@ public class WorkflowControllerTest { mockMvc.perform(get(RestPath.getWorkflowsPathNoSortAndOffset("1")).header(USER_ID_HEADER, USER_ID) .contentType(APPLICATION_JSON)) .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3))); - verify(workflowManagerMock).list(any(), any(), requestSpecArg.capture()); + verify(workflowManagerMock).list(any(), any(), any(), requestSpecArg.capture()); assertRequestSpec(requestSpecArg.getValue(), DEFAULT_OFFSET, 1, Collections.emptyList()); } @@ -258,7 +275,7 @@ public class WorkflowControllerTest { 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(), any()); + .list(any(), any(), any(), any()); } private static void assertRequestSpec(RequestSpec actual, int expectedOffset, int expectedLimit, diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/validation/WorkflowStatusVelidatorTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/validation/WorkflowStatusVelidatorTest.java new file mode 100644 index 00000000..289c9353 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/validation/WorkflowStatusVelidatorTest.java @@ -0,0 +1,91 @@ +/* + * Copyright © 2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.sdc.workflow.api.validation; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import javax.validation.ConstraintValidatorContext; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +public class WorkflowStatusVelidatorTest { + + class AnnotationWrapper { + + @ValidName(message = "test message") + public String status; + } + + @Mock + private ConstraintValidatorContext context; + @Mock + private ConstraintValidatorContext.ConstraintViolationBuilder constraintViolationBuilder; + @Mock + private ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext nodeBuilderCustomizableContext; + + private WorkflowStatusValidator validator; + + @Before + public void setup() throws NoSuchFieldException { + MockitoAnnotations.initMocks(this); + when(context.buildConstraintViolationWithTemplate(anyString())).thenReturn(constraintViolationBuilder); + when(constraintViolationBuilder.addPropertyNode(anyString())).thenReturn(nodeBuilderCustomizableContext); + validator = initializeValidator(WorkflowStatusVelidatorTest.AnnotationWrapper.class); + } + + @Test + public void shouldFailIfValueIsNull() { + assertFalse(validator.isValid(null, context)); + } + + @Test + public void shouldFailIfValueInvalid() { + assertFalse(validator.isValid("blahblah", context)); + } + + @Test + public void shouldPassIfValueIsActive() { + assertTrue(validator.isValid("ACTIVE", context)); + } + + @Test + public void shouldPassIfValueIsArchived() { + assertTrue(validator.isValid("ARCHIVED", context)); + } + + + + + private WorkflowStatusValidator initializeValidator(Class<?> classWithAnnotation) + throws NoSuchFieldException { + ValidStatus constraint = classWithAnnotation.getField("status").getAnnotation(ValidStatus.class); + WorkflowStatusValidator validator = new WorkflowStatusValidator(); + validator.initialize(constraint); + return validator; + } + + +} 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 e2df4ec0..702da4c8 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 @@ -4,6 +4,7 @@ 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.doThrow; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.onap.sdc.workflow.TestUtil.createItem; @@ -27,6 +28,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.onap.sdc.workflow.services.UniqueValueService; import org.onap.sdc.workflow.services.exceptions.EntityNotFoundException; +import org.onap.sdc.workflow.services.exceptions.WorkflowStatusModificationException; 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; @@ -36,7 +38,11 @@ import org.onap.sdc.workflow.services.types.RequestSpec; import org.onap.sdc.workflow.services.types.Sort; import org.onap.sdc.workflow.services.types.SortingRequest; import org.onap.sdc.workflow.services.types.Workflow; +import org.onap.sdc.workflow.services.types.WorkflowStatus; import org.onap.sdc.workflow.services.types.WorkflowVersionState; +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.common.errors.ErrorCategory; +import org.openecomp.sdc.common.errors.ErrorCode; import org.openecomp.sdc.versioning.ItemManager; import org.openecomp.sdc.versioning.types.Item; import org.openecomp.sdc.versioning.types.ItemStatus; @@ -80,7 +86,7 @@ public class WorkflowManagerImplTest { doReturn(ITEMS).when(itemManagerMock).list(any()); mockItemToWorkflowMaps(); RequestSpec requestSpec = createRequestSpec(0, 20, true); - Page<Workflow> workflows = workflowManager.list(null,null, requestSpec); + Page<Workflow> workflows = workflowManager.list(null,null,null, requestSpec); Map<String, Workflow> workflowById = workflows.getItems().stream().collect(Collectors.toMap(Workflow::getId, Function.identity())); @@ -102,7 +108,7 @@ public class WorkflowManagerImplTest { RequestSpec requestSpec = createRequestSpec(0, 20, true); Page<Workflow> workflows = - workflowManager.list(null,Collections.singleton(WorkflowVersionState.CERTIFIED), requestSpec); + workflowManager.list(null,null,Collections.singleton(WorkflowVersionState.CERTIFIED), requestSpec); Map<String, Workflow> workflowById = workflows.getItems().stream().collect(Collectors.toMap(Workflow::getId, Function.identity())); @@ -114,6 +120,42 @@ public class WorkflowManagerImplTest { } @Test(expected = EntityNotFoundException.class) + public void shouldThrowExceptionWhenWorkflowDontExistTryingToUpdateStatus() { + doReturn(null).when(itemManagerMock).get(ITEM1_ID); + workflowManager.updateStatus(ITEM1_ID,WorkflowStatus.ARCHIVED); + } + + @Test(expected = WorkflowStatusModificationException.class) + public void shouldThrowExceptionWhenFailingToUpdateStatus() { + Item mockItem = new Item(); + mockItem.setId(ITEM1_ID); + mockItem.setStatus(ItemStatus.ARCHIVED); + doReturn(mockItem).when(itemManagerMock).get(ITEM1_ID); + doThrow(new CoreException(new ErrorCode.ErrorCodeBuilder().build())).when(itemManagerMock).archive(mockItem); + workflowManager.updateStatus(ITEM1_ID,WorkflowStatus.ARCHIVED); + } + + @Test + public void shouldArchiveWorkflow() { + Item mockItem = new Item(); + mockItem.setId(ITEM1_ID); + mockItem.setStatus(ItemStatus.ACTIVE); + doReturn(mockItem).when(itemManagerMock).get(ITEM1_ID); + workflowManager.updateStatus(ITEM1_ID,WorkflowStatus.ARCHIVED); + verify(itemManagerMock).archive(mockItem); + } + + @Test + public void shouldRestoreWorkflow() { + Item mockItem = new Item(); + mockItem.setId(ITEM1_ID); + mockItem.setStatus(ItemStatus.ARCHIVED); + doReturn(mockItem).when(itemManagerMock).get(ITEM1_ID); + workflowManager.updateStatus(ITEM1_ID,WorkflowStatus.ACTIVE); + verify(itemManagerMock).restore(mockItem); + } + + @Test(expected = EntityNotFoundException.class) public void shouldThrowExceptionWhenWorkflowDontExist() { Workflow nonExistingWorkflow = new Workflow(); nonExistingWorkflow.setId(ITEM1_ID); @@ -172,7 +214,7 @@ public class WorkflowManagerImplTest { public void listWhenRequestSpecIsNull() { doReturn(ITEMS).when(itemManagerMock).list(any()); mockItemToWorkflowMaps(); - Page<Workflow> workflows = workflowManager.list(null,null, null); + Page<Workflow> workflows = workflowManager.list(null,null,null, null); assertEquals(ITEMS.size(), workflows.getItems().size()); assertPaging(workflows.getPaging(), DEFAULT_OFFSET, DEFAULT_LIMIT, ITEMS.size()); @@ -187,7 +229,7 @@ public class WorkflowManagerImplTest { public void listWhenPagingIsNull() { doReturn(ITEMS).when(itemManagerMock).list(any()); mockItemToWorkflowMaps(); - Page<Workflow> workflows = workflowManager.list(null,null, new RequestSpec(null, + Page<Workflow> workflows = workflowManager.list(null,null,null, new RequestSpec(null, SortingRequest.builder().sort(new Sort(SORT_FIELD_NAME, true)).build())); assertEquals(ITEMS.size(), workflows.getItems().size()); @@ -200,7 +242,7 @@ public class WorkflowManagerImplTest { 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,null, requestSpec); + Page<Workflow> workflows = workflowManager.list(null,null,null, requestSpec); assertEquals(ITEMS.size(), workflows.getItems().size()); assertPaging(workflows.getPaging(), DEFAULT_OFFSET, DEFAULT_LIMIT, ITEMS.size()); @@ -211,7 +253,7 @@ public class WorkflowManagerImplTest { doReturn(ITEMS).when(itemManagerMock).list(any()); mockItemToWorkflowMaps(); RequestSpec requestSpec = new RequestSpec(new PagingRequest(2, 8), null); - Page<Workflow> workflows = workflowManager.list(null,null, requestSpec); + Page<Workflow> workflows = workflowManager.list(null,null,null, requestSpec); assertEquals(3, workflows.getItems().size()); assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), requestSpec.getPaging().getLimit(), @@ -228,7 +270,7 @@ public class WorkflowManagerImplTest { doReturn(ITEMS).when(itemManagerMock).list(any()); mockItemToWorkflowMaps(); RequestSpec requestSpec = new RequestSpec(new PagingRequest(2, 8), SortingRequest.builder().build()); - Page<Workflow> workflows = workflowManager.list(null,null, requestSpec); + Page<Workflow> workflows = workflowManager.list(null,null,null, requestSpec); assertEquals(3, workflows.getItems().size()); assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), requestSpec.getPaging().getLimit(), @@ -245,7 +287,7 @@ public class WorkflowManagerImplTest { RequestSpec requestSpec = createRequestSpec(0, 5, true); doReturn(ITEMS).when(itemManagerMock).list(any()); mockItemToWorkflowMaps(); - Page<Workflow> workflows = workflowManager.list(null,null, requestSpec); + Page<Workflow> workflows = workflowManager.list(null,null,null, requestSpec); assertEquals(5, workflows.getItems().size()); assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), requestSpec.getPaging().getLimit(), @@ -257,7 +299,7 @@ public class WorkflowManagerImplTest { RequestSpec requestSpec = createRequestSpec(0, 3, true); doReturn(ITEMS).when(itemManagerMock).list(any()); mockItemToWorkflowMaps(); - Page<Workflow> workflows = workflowManager.list(null,null, requestSpec); + Page<Workflow> workflows = workflowManager.list(null,null,null, requestSpec); assertEquals(3, workflows.getItems().size()); assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), requestSpec.getPaging().getLimit(), ITEMS.size()); @@ -269,7 +311,7 @@ public class WorkflowManagerImplTest { RequestSpec requestSpec = createRequestSpec(3, 1, true); doReturn(ITEMS).when(itemManagerMock).list(any()); mockItemToWorkflowMaps(); - Page<Workflow> workflows = workflowManager.list(null,null, requestSpec); + Page<Workflow> workflows = workflowManager.list(null,null,null, requestSpec); assertEquals(1, workflows.getItems().size()); assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), requestSpec.getPaging().getLimit(), ITEMS.size()); @@ -280,7 +322,7 @@ public class WorkflowManagerImplTest { RequestSpec requestSpec = createRequestSpec(0, 10, true); doReturn(ITEMS).when(itemManagerMock).list(any()); mockItemToWorkflowMaps(); - Page<Workflow> workflows = workflowManager.list(null,null, requestSpec); + Page<Workflow> workflows = workflowManager.list(null,null,null, requestSpec); assertEquals(5, workflows.getItems().size()); assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), requestSpec.getPaging().getLimit(), ITEMS.size()); @@ -291,7 +333,7 @@ public class WorkflowManagerImplTest { RequestSpec requestSpec = createRequestSpec(6, 3, true); doReturn(ITEMS).when(itemManagerMock).list(any()); mockItemToWorkflowMaps(); - Page<Workflow> workflows = workflowManager.list(null,null, requestSpec); + Page<Workflow> workflows = workflowManager.list(null,null,null, requestSpec); assertEquals(0, workflows.getItems().size()); assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), requestSpec.getPaging().getLimit(), ITEMS.size()); @@ -302,7 +344,7 @@ public class WorkflowManagerImplTest { doReturn(ITEMS).when(itemManagerMock).list(any()); mockItemToWorkflowMaps(); RequestSpec requestSpec = createRequestSpec(0, 5555, true); - Page<Workflow> workflows = workflowManager.list(null,null, requestSpec); + Page<Workflow> workflows = workflowManager.list(null,null,null, requestSpec); assertEquals(ITEMS.size(), workflows.getItems().size()); assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), MAX_LIMIT, ITEMS.size()); @@ -313,7 +355,7 @@ public class WorkflowManagerImplTest { RequestSpec requestSpec = createRequestSpec(10, 10, true); doReturn(ITEMS).when(itemManagerMock).list(any()); mockItemToWorkflowMaps(); - Page<Workflow> workflows = workflowManager.list(null,null, requestSpec); + Page<Workflow> workflows = workflowManager.list(null,null,null, requestSpec); assertEquals(0, workflows.getItems().size()); assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), requestSpec.getPaging().getLimit(), ITEMS.size()); @@ -324,7 +366,7 @@ public class WorkflowManagerImplTest { RequestSpec requestSpec = createRequestSpec(2, 1, false); doReturn(ITEMS).when(itemManagerMock).list(any()); mockItemToWorkflowMaps(); - Page<Workflow> workflows = workflowManager.list(null,null, requestSpec); + Page<Workflow> workflows = workflowManager.list(null,null,null, requestSpec); assertEquals(1, workflows.getItems().size()); assertPaging(workflows.getPaging(), requestSpec.getPaging().getOffset(), requestSpec.getPaging().getLimit(), ITEMS.size()); 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 index 8b09e15d..640d3e8b 100644 --- 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 @@ -1,11 +1,11 @@ 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.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.times; @@ -15,15 +15,16 @@ import static org.onap.sdc.workflow.services.types.WorkflowVersionState.DRAFT; import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Optional; -import java.util.Set; + import org.apache.commons.io.IOUtils; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.InjectMocks; @@ -39,16 +40,20 @@ import org.onap.sdc.workflow.services.exceptions.EntityNotFoundException; import org.onap.sdc.workflow.services.exceptions.VersionCreationException; import org.onap.sdc.workflow.services.exceptions.VersionModificationException; import org.onap.sdc.workflow.services.exceptions.VersionStateModificationException; +import org.onap.sdc.workflow.services.exceptions.VersionStateModificationMissingArtifactException; +import org.onap.sdc.workflow.services.exceptions.WorkflowModificationException; import org.onap.sdc.workflow.services.impl.mappers.VersionMapper; import org.onap.sdc.workflow.services.impl.mappers.VersionStateMapper; import org.onap.sdc.workflow.services.types.WorkflowVersion; import org.onap.sdc.workflow.services.types.WorkflowVersionState; +import org.openecomp.sdc.versioning.ItemManager; import org.openecomp.sdc.versioning.VersioningManager; import org.openecomp.sdc.versioning.dao.types.Version; import org.openecomp.sdc.versioning.dao.types.VersionState; import org.openecomp.sdc.versioning.dao.types.VersionStatus; +import org.openecomp.sdc.versioning.types.Item; +import org.openecomp.sdc.versioning.types.ItemStatus; import org.openecomp.sdc.versioning.types.VersionCreationMethod; -import org.springframework.boot.test.autoconfigure.web.client.RestClientTest; import org.springframework.mock.web.MockMultipartFile; @RunWith(MockitoJUnitRunner.class) @@ -59,6 +64,8 @@ public class WorkflowVersionManagerImplTest { private static final String VERSION2_ID = "version_id_2"; @Mock + private ItemManager itemManagerMock; + @Mock private VersioningManager versioningManagerMock; @Mock private ParameterRepository parameterRepositoryMock; @@ -68,6 +75,8 @@ public class WorkflowVersionManagerImplTest { private VersionMapper versionMapperMock; @Mock private VersionStateMapper versionStateMapperMock; + @Rule + public ExpectedException exceptionRule = ExpectedException.none(); @Spy @InjectMocks private WorkflowVersionManagerImpl workflowVersionManager; @@ -79,6 +88,43 @@ public class WorkflowVersionManagerImplTest { workflowVersionManager.get(ITEM1_ID, VERSION1_ID); } + @Test(expected = WorkflowModificationException.class) + public void shouldThrowExceptionWhenCreatingVersionForArchivedWorkflow() { + Item mockItem = new Item(); + mockItem.setId(ITEM1_ID); + mockItem.setStatus(ItemStatus.ARCHIVED); + doReturn(mockItem).when(itemManagerMock).get(ITEM1_ID); + workflowVersionManager.create(ITEM1_ID, null, new WorkflowVersion(VERSION1_ID)); + } + + @Test(expected = WorkflowModificationException.class) + public void shouldThrowExceptionWhenUpdatingVersionForArchivedWorkflow() { + Item mockItem = new Item(); + mockItem.setId(ITEM1_ID); + mockItem.setStatus(ItemStatus.ARCHIVED); + doReturn(mockItem).when(itemManagerMock).get(ITEM1_ID); + workflowVersionManager.update(ITEM1_ID, new WorkflowVersion(VERSION1_ID)); + } + + @Test(expected = WorkflowModificationException.class) + public void shouldThrowExceptionWhenUploadingArtifactForArchivedWorkflow() { + Item mockItem = new Item(); + mockItem.setId(ITEM1_ID); + mockItem.setStatus(ItemStatus.ARCHIVED); + doReturn(mockItem).when(itemManagerMock).get(ITEM1_ID); + MockMultipartFile mockFile = new MockMultipartFile("data", "filename.txt", "text/plain", "some xml".getBytes()); + workflowVersionManager.uploadArtifact(ITEM1_ID, VERSION1_ID, mockFile); + } + + @Test(expected = WorkflowModificationException.class) + public void shouldThrowExceptionWhenDeletingArtifactForArchivedWorkflow() { + Item mockItem = new Item(); + mockItem.setId(ITEM1_ID); + mockItem.setStatus(ItemStatus.ARCHIVED); + doReturn(mockItem).when(itemManagerMock).get(ITEM1_ID); + workflowVersionManager.deleteArtifact(ITEM1_ID, VERSION1_ID); + } + @Test public void shouldReturnWorkflowVersionWhenExist() { Version version = new Version(VERSION1_ID); @@ -95,7 +141,7 @@ public class WorkflowVersionManagerImplTest { public void shouldReturnWorkflowVersionList() { Version version1 = new Version(VERSION1_ID); Version version2 = new Version(VERSION2_ID); - List<Version> versionList = Arrays.asList(version1,version2); + List<Version> versionList = Arrays.asList(version1, version2); doReturn(versionList).when(versioningManagerMock).list(ITEM1_ID); WorkflowVersion workflowVersion1 = new WorkflowVersion(); workflowVersion1.setId(VERSION1_ID); @@ -104,8 +150,10 @@ public class WorkflowVersionManagerImplTest { workflowVersion2.setId(VERSION2_ID); workflowVersion2.setName(VERSION2_ID); doReturn(workflowVersion2).when(versionMapperMock).versionToWorkflowVersion(version2); - doReturn(Collections.emptyList()).when(parameterRepositoryMock).list(eq(ITEM1_ID),anyString(),eq(ParameterRole.INPUT)); - doReturn(Collections.emptyList()).when(parameterRepositoryMock).list(eq(ITEM1_ID),anyString(),eq(ParameterRole.OUTPUT)); + doReturn(Collections.emptyList()).when(parameterRepositoryMock) + .list(eq(ITEM1_ID), anyString(), eq(ParameterRole.INPUT)); + doReturn(Collections.emptyList()).when(parameterRepositoryMock) + .list(eq(ITEM1_ID), anyString(), eq(ParameterRole.OUTPUT)); workflowVersionManager.list(ITEM1_ID, null); verify(versioningManagerMock).list(ITEM1_ID); verify(versionMapperMock, times(2)).versionToWorkflowVersion(any(Version.class)); @@ -127,14 +175,15 @@ public class WorkflowVersionManagerImplTest { workflowVersion2.setName(VERSION2_ID); doReturn(workflowVersion1).when(versionMapperMock).versionToWorkflowVersion(version1); doReturn(Collections.emptyList()).when(parameterRepositoryMock) - .list(eq(ITEM1_ID), anyString(), eq(ParameterRole.INPUT)); + .list(eq(ITEM1_ID), anyString(), eq(ParameterRole.INPUT)); doReturn(Collections.emptyList()).when(parameterRepositoryMock) - .list(eq(ITEM1_ID), anyString(), eq(ParameterRole.OUTPUT)); + .list(eq(ITEM1_ID), anyString(), eq(ParameterRole.OUTPUT)); doReturn(VersionStatus.Certified).when(versionStateMapperMock) - .workflowVersionStateToVersionStatus( - WorkflowVersionState.CERTIFIED); + .workflowVersionStateToVersionStatus( + WorkflowVersionState.CERTIFIED); - assertEquals(1, workflowVersionManager.list(ITEM1_ID, Collections.singleton(WorkflowVersionState.CERTIFIED)).size()); + assertEquals(1, + workflowVersionManager.list(ITEM1_ID, Collections.singleton(WorkflowVersionState.CERTIFIED)).size()); verify(versioningManagerMock).list(ITEM1_ID); verify(versionMapperMock, times(1)).versionToWorkflowVersion(any(Version.class)); @@ -142,6 +191,7 @@ public class WorkflowVersionManagerImplTest { @Test public void shouldUpdateWorkflowVersion() { + doNothing().when(workflowVersionManager).validateWorkflowStatus(ITEM1_ID); String updatedDescription = "WorkflowVersion description updated"; Version retrievedVersion = new Version(VERSION1_ID); retrievedVersion.setName("1.0"); @@ -160,11 +210,12 @@ public class WorkflowVersionManagerImplTest { inputVersion.setInputs(Collections.singleton(toBeCreated)); ParameterEntity toBeUpdated = new ParameterEntity("Output1"); inputVersion.setOutputs(Collections.singleton(toBeUpdated)); - doReturn(Collections.emptyList()).when(parameterRepositoryMock).list(ITEM1_ID,VERSION1_ID,ParameterRole.INPUT); + doReturn(Collections.emptyList()).when(parameterRepositoryMock) + .list(ITEM1_ID, VERSION1_ID, ParameterRole.INPUT); ParameterEntity toBeDeleted = new ParameterEntity("Output2"); toBeDeleted.setId("parameter_id_1"); Collection<ParameterEntity> currentOutputs = Arrays.asList(toBeDeleted, toBeUpdated); - doReturn(currentOutputs).when(parameterRepositoryMock).list(ITEM1_ID,VERSION1_ID,ParameterRole.OUTPUT); + doReturn(currentOutputs).when(parameterRepositoryMock).list(ITEM1_ID, VERSION1_ID, ParameterRole.OUTPUT); Version mappedInputVersion = new Version(VERSION1_ID); mappedInputVersion.setName("1.0"); @@ -181,15 +232,16 @@ public class WorkflowVersionManagerImplTest { assertEquals(VersionStatus.Draft, captorVersion.getStatus()); verify(versioningManagerMock).publish(ITEM1_ID, mappedInputVersion, "Update version"); - verify(parameterRepositoryMock).delete(ITEM1_ID,VERSION1_ID,"parameter_id_1"); - verify(parameterRepositoryMock).create(ITEM1_ID,VERSION1_ID,ParameterRole.INPUT,toBeCreated); - verify(parameterRepositoryMock).update(ITEM1_ID,VERSION1_ID,ParameterRole.OUTPUT,toBeUpdated); + verify(parameterRepositoryMock).delete(ITEM1_ID, VERSION1_ID, "parameter_id_1"); + verify(parameterRepositoryMock).create(ITEM1_ID, VERSION1_ID, ParameterRole.INPUT, toBeCreated); + verify(parameterRepositoryMock).update(ITEM1_ID, VERSION1_ID, ParameterRole.OUTPUT, toBeUpdated); } @Test public void shouldCreateWorkflowVersion() { + doNothing().when(workflowVersionManager).validateWorkflowStatus(ITEM1_ID); Version version = new Version(VERSION1_ID); version.setDescription("version desc"); doReturn(version).when(versioningManagerMock).create(ITEM1_ID, version, VersionCreationMethod.major); @@ -205,6 +257,7 @@ public class WorkflowVersionManagerImplTest { @Test(expected = VersionCreationException.class) public void shouldTrowExceptionWhenDraftVersionExists() { + doNothing().when(workflowVersionManager).validateWorkflowStatus(ITEM1_ID); WorkflowVersion versionRequestDto = new WorkflowVersion(); Version baseVersion = new Version(VERSION2_ID); @@ -217,6 +270,7 @@ public class WorkflowVersionManagerImplTest { @Test(expected = VersionCreationException.class) public void shouldTrowExceptionWhenInputsSupplied() { + doNothing().when(workflowVersionManager).validateWorkflowStatus(ITEM1_ID); WorkflowVersion versionRequestDto = new WorkflowVersion(); versionRequestDto.setInputs(Collections.singleton(new ParameterEntity())); Version baseVersion = new Version(VERSION2_ID); @@ -260,32 +314,46 @@ public class WorkflowVersionManagerImplTest { workflowVersionManager.updateState(ITEM1_ID, VERSION1_ID, DRAFT); } - @Test(expected = VersionStateModificationException.class) + @Test(expected = VersionStateModificationMissingArtifactException.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() { + public void shouldFailUpdateStateWhenNoArtifact() { 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); + exceptionRule.expect(VersionStateModificationMissingArtifactException.class); + exceptionRule.expectMessage(String.format( + VersionStateModificationMissingArtifactException.WORKFLOW_MODIFICATION_STATE_MISSING_ARTIFACT_TEMPLATE, + ITEM1_ID, VERSION1_ID, DRAFT, CERTIFIED)); workflowVersionManager.updateState(ITEM1_ID, VERSION1_ID, CERTIFIED); verify(versioningManagerMock).submit(eq(ITEM1_ID), eqVersion(VERSION1_ID), anyString()); } @Test + public void shouldSuccessUpdateStateWhenArtifactExist() { + 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); + doReturn(true).when(artifactRepositoryMock).isExist(ITEM1_ID, VERSION1_ID); + workflowVersionManager.updateState(ITEM1_ID, VERSION1_ID, CERTIFIED); + verify(versioningManagerMock).submit(eq(ITEM1_ID), eqVersion(VERSION1_ID), anyString()); + } + + @Test public void shouldUploadArtifact() { + doNothing().when(workflowVersionManager).validateWorkflowStatus(ITEM1_ID); Version version = new Version(VERSION1_ID); version.setStatus(VersionStatus.Draft); VersionState versionState = new VersionState(); @@ -321,23 +389,25 @@ public class WorkflowVersionManagerImplTest { @Test(expected = VersionModificationException.class) public void shouldThrowExceptionInDeleteArtifactWhenVersionIsCertified() { + doNothing().when(workflowVersionManager).validateWorkflowStatus(ITEM1_ID); Version version = new Version(VERSION1_ID); - doReturn(version).when(versioningManagerMock).get(ITEM1_ID,version); + doReturn(version).when(versioningManagerMock).get(ITEM1_ID, version); WorkflowVersion workflowVersion = new WorkflowVersion(VERSION1_ID); workflowVersion.setState(WorkflowVersionState.CERTIFIED); doReturn(workflowVersion).when(versionMapperMock).versionToWorkflowVersion(version); - workflowVersionManager.deleteArtifact(ITEM1_ID,VERSION1_ID); + workflowVersionManager.deleteArtifact(ITEM1_ID, VERSION1_ID); } @Test public void shouldDeleteArtifact() { + doNothing().when(workflowVersionManager).validateWorkflowStatus(ITEM1_ID); Version version = new Version(VERSION1_ID); - doReturn(version).when(versioningManagerMock).get(ITEM1_ID,version); + doReturn(version).when(versioningManagerMock).get(ITEM1_ID, version); WorkflowVersion workflowVersion = new WorkflowVersion(VERSION1_ID); doReturn(workflowVersion).when(versionMapperMock).versionToWorkflowVersion(version); - workflowVersionManager.deleteArtifact(ITEM1_ID,VERSION1_ID); - verify(artifactRepositoryMock).delete(ITEM1_ID,VERSION1_ID); - verify(versioningManagerMock).publish(ITEM1_ID,version,"Delete Artifact"); + workflowVersionManager.deleteArtifact(ITEM1_ID, VERSION1_ID); + verify(artifactRepositoryMock).delete(ITEM1_ID, VERSION1_ID); + verify(versioningManagerMock).publish(ITEM1_ID, version, "Delete Artifact"); } private static Version eqVersion(String versionId) { |