diff options
Diffstat (limited to 'workflow-designer-be/src/test/java/org/onap')
3 files changed, 117 insertions, 96 deletions
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 index ec27c4be..65dae726 100644 --- 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 @@ -2,9 +2,6 @@ package org.onap.sdc.workflow.api; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -18,6 +15,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import com.google.gson.Gson; import java.util.Arrays; +import java.util.Collection; import java.util.List; import org.junit.Before; import org.junit.Test; @@ -26,7 +24,8 @@ 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.types.WorkflowVersionValidator; +import org.onap.sdc.workflow.persistence.types.ParameterEntity; +import org.onap.sdc.workflow.persistence.types.ParameterType; import org.onap.sdc.workflow.persistence.types.WorkflowVersion; import org.onap.sdc.workflow.services.WorkflowVersionManager; import org.openecomp.sdc.versioning.dao.types.Version; @@ -42,7 +41,6 @@ public class WorkflowVersionControllerTest { 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(); @@ -51,38 +49,19 @@ public class WorkflowVersionControllerTest { @Mock private WorkflowVersionManager workflowVersionManagerMock; - @Mock - private WorkflowVersionValidator versionValidator; - @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(RestParams.USER_ID_HEADER, 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"); - doNothing().when(versionValidator).validate(eq(ITEM1_ID),any(WorkflowVersion.class)); mockMvc.perform(post(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestParams.USER_ID_HEADER, USER_ID) .contentType(APPLICATION_JSON) .content(GSON.toJson(version))) @@ -91,6 +70,21 @@ public class WorkflowVersionControllerTest { verify(workflowVersionManagerMock, times(1)).create(ITEM1_ID, null, version); } + @Test + public void shouldFailCreateWorkflowVersionWhenCallingVersionsPostRESTWithDuplicateInput() throws Exception { + + WorkflowVersion version = new WorkflowVersion(); + Collection<ParameterEntity> inputs = + Arrays.asList(createParameterEntity("name1"), createParameterEntity("name1")); + version.setInputs(inputs); + version.setDescription("VersionDescription"); + mockMvc.perform(post(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestParams.USER_ID_HEADER, USER_ID) + .contentType(APPLICATION_JSON) + .content(GSON.toJson(version))) + .andExpect(status().isBadRequest()); + + } + @Test public void shouldReturnWorkflowVersionWhenExists() throws Exception { @@ -107,7 +101,6 @@ public class WorkflowVersionControllerTest { public void shouldUpdateWorkflowVersionWhenCallingPutREST() throws Exception { WorkflowVersion version = new WorkflowVersion(); version.setDescription("Updated"); - doNothing().when(versionValidator).validate(eq(ITEM1_ID),any(WorkflowVersion.class)); MockHttpServletResponse result = mockMvc.perform( put(RestPath.getWorkflowVersion(ITEM1_ID, VERSION1_ID)).header(RestParams.USER_ID_HEADER, USER_ID) @@ -121,4 +114,12 @@ public class WorkflowVersionControllerTest { } + private ParameterEntity createParameterEntity(String name) { + ParameterEntity parameterEntity = new ParameterEntity(); + parameterEntity.setName(name); + parameterEntity.setMandatory(false); + parameterEntity.setType(ParameterType.STRING); + return parameterEntity; + } + } diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/types/WorkflowVersionValidatorTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/types/WorkflowVersionValidatorTest.java deleted file mode 100644 index 9ea007ce..00000000 --- a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/types/WorkflowVersionValidatorTest.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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.types; - -import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.fail; - -import java.util.Arrays; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.onap.sdc.workflow.persistence.types.ParameterEntity; -import org.onap.sdc.workflow.persistence.types.WorkflowVersion; -import org.onap.sdc.workflow.services.exceptions.VersionValidationException; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -@RunWith(SpringJUnit4ClassRunner.class) -public class WorkflowVersionValidatorTest { - - private static final String ITEM1_ID = "item_id_1"; - - @InjectMocks - private WorkflowVersionValidator versionValidator; - - @Test - public void invalidInputs() { - WorkflowVersion workflowVersion = new WorkflowVersion(); - workflowVersion.setDescription("version description"); - ParameterEntity input = new ParameterEntity(); - input.setName("input1"); - workflowVersion.setInputs(Arrays.asList(input, input)); - try { - versionValidator.validate(ITEM1_ID, workflowVersion); - fail("Should have thrown VersionValidationException but did not!"); - - } catch (VersionValidationException ex) { - assertEquals(String.format("Error creating or modifying version for workflow with id %s: %s", ITEM1_ID, - "Input name must be unique"), ex.getMessage()); - } - } - - @Test - public void invalidOtputs(){ - WorkflowVersion workflowVersion = new WorkflowVersion(); - workflowVersion.setDescription("version description"); - ParameterEntity output = new ParameterEntity(); - output.setName("output1"); - workflowVersion.setOutputs(Arrays.asList(output, output)); - try { - versionValidator.validate(ITEM1_ID, workflowVersion); - fail("Should have thrown VersionValidationException but did not!"); - - } catch (VersionValidationException ex) { - assertEquals(String.format("Error creating or modifying version for workflow with id %s: %s", ITEM1_ID, - "Output name must be unique"), ex.getMessage()); - } - } -} diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/validation/NoDuplicatesValidatorTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/validation/NoDuplicatesValidatorTest.java new file mode 100644 index 00000000..bfc7dc36 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/validation/NoDuplicatesValidatorTest.java @@ -0,0 +1,91 @@ +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.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import javax.validation.ConstraintValidatorContext; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.sdc.workflow.persistence.types.ParameterEntity; + +public class NoDuplicatesValidatorTest { + + class AnnotationWrapper { + + @NoDuplicates(message = "test message") + public Collection<ParameterEntity> collection; + } + + private NoDuplicatesValidator noDuplicatesValidator; + + @Mock + private ConstraintValidatorContext context; + @Mock + private ConstraintValidatorContext.ConstraintViolationBuilder constraintViolationBuilder; + @Mock + private ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext nodeBuilderCustomizableContext; + + @Before + public void init() throws NoSuchFieldException { + MockitoAnnotations.initMocks(this); + when(context.buildConstraintViolationWithTemplate(anyString())).thenReturn(constraintViolationBuilder); + when(constraintViolationBuilder.addPropertyNode(anyString())).thenReturn(nodeBuilderCustomizableContext); + noDuplicatesValidator = initializeValidator(AnnotationWrapper.class); + } + + @Test + public void shouldFailIfCollectionHaveMoreThen1ParameterEntityWithSameName() { + Collection<ParameterEntity> inputs = + Arrays.asList(createParameterEntity("name1"), createParameterEntity("name1")); + + assertFalse(noDuplicatesValidator.isValid(inputs, context)); + } + + @Test + public void shouldPassIfCollectionDontHaveMoreThen1ParameterEntityWithSameName() { + Collection<ParameterEntity> inputs = + Arrays.asList(createParameterEntity("name2"), createParameterEntity("name1")); + + assertTrue(noDuplicatesValidator.isValid(inputs, context)); + } + + @Test + public void shouldPassIfCollectionContainsOnlyOneObject() { + Collection<ParameterEntity> inputs = + Arrays.asList(createParameterEntity("name2")); + + assertTrue(noDuplicatesValidator.isValid(inputs, context)); + } + + @Test + public void shouldPassIfCollectionIsNull() { + assertTrue(noDuplicatesValidator.isValid(null, context)); + } + + @Test + public void shouldPassIfCollectionIsEmpty() { + assertTrue(noDuplicatesValidator.isValid(new ArrayList<>(), context)); + } + + private NoDuplicatesValidator initializeValidator(Class<?> classWithAnnotation) throws NoSuchFieldException { + NoDuplicates constraint = classWithAnnotation.getField("collection").getAnnotation(NoDuplicates.class); + NoDuplicatesValidator validator = new NoDuplicatesValidator(); + validator.initialize(constraint); + return validator; + } + + private ParameterEntity createParameterEntity(String name) { + ParameterEntity parameterEntity = new ParameterEntity(); + parameterEntity.setName(name); + return parameterEntity; + } +}
\ No newline at end of file |