From ba255a01b24485fe9793d34841b13a5479f6444a Mon Sep 17 00:00:00 2001 From: avigaffa Date: Mon, 18 Jun 2018 20:07:14 +0300 Subject: fix mapper unit tests Change-Id: Ibbc47fe2d59c1fa95ee2a0212ebe6c293f5bdfd2 Issue-ID: SDC-1396 Signed-off-by: avigaffa --- workflow-designer-be/pom.xml | 13 ++- .../workflow/api/mapping/WorkflowMapperTest.java | 64 ++++++++++++ .../workflow/services/UniqueValueServiceTest.java | 98 ++++++++++++++++++ .../workflow/api/mapping/WorkflowMapperTest.java | 64 ------------ .../workflow/services/UniqueValueServiceTest.java | 111 --------------------- 5 files changed, 168 insertions(+), 182 deletions(-) create mode 100644 workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/mapping/WorkflowMapperTest.java create mode 100644 workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/UniqueValueServiceTest.java delete mode 100644 workflow-designer-be/src/test/org/onap/sdc/workflow/api/mapping/WorkflowMapperTest.java delete mode 100644 workflow-designer-be/src/test/org/onap/sdc/workflow/services/UniqueValueServiceTest.java diff --git a/workflow-designer-be/pom.xml b/workflow-designer-be/pom.xml index 65f239ad..226bfef6 100644 --- a/workflow-designer-be/pom.xml +++ b/workflow-designer-be/pom.xml @@ -76,6 +76,12 @@ org.openecomp.sdc openecomp-sdc-versioning-api ${onap.version} + + + org.testng + testng + + org.openecomp.sdc @@ -105,13 +111,6 @@ mapstruct-jdk8 ${org.mapstruct.version} - - org.junit.jupiter - junit-jupiter-api - RELEASE - test - - diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/mapping/WorkflowMapperTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/mapping/WorkflowMapperTest.java new file mode 100644 index 00000000..5371fd70 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/mapping/WorkflowMapperTest.java @@ -0,0 +1,64 @@ +package org.onap.sdc.workflow.api.mapping; + + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.HashMap; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.sdc.workflow.persistence.types.Workflow; +import org.onap.sdc.workflow.persistence.types.WorkflowProperty; +import org.onap.sdc.workflow.services.mappers.WorkflowMapper; +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 shouldMapItemPropertyToWorkflowCategory() { + + Item item = createMockItem(); + HashMap properties = new HashMap<>(); + properties.put(WorkflowProperty.CATEGORY, "category"); + item.setProperties(properties); + + Workflow mappedWorkflow = workflowMapper.itemToWorkflow(item); + assertEquals(mappedWorkflow.getId(), item.getId()); + assertEquals(mappedWorkflow.getDescription(), item.getDescription()); + assertEquals(mappedWorkflow.getName(), item.getName()); + assertEquals(mappedWorkflow.getCategory(), properties.get(WorkflowProperty.CATEGORY)); + } + + @Test + public void shouldAddWorkflowCategoryToItemProperties(){ + Workflow workflow = new Workflow(); + workflow.setId("id"); + workflow.setCategory("cat"); + + Item item = workflowMapper.workflowToItem(workflow); + assertNotNull(item.getProperties().get(WorkflowProperty.CATEGORY)); + } + + private Item createMockItem() { + Item item = new Item(); + item.setId("id"); + item.setDescription("item description"); + item.setName("item name"); + return item; + } + +} \ No newline at end of file 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..50a19655 --- /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.errors.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/org/onap/sdc/workflow/api/mapping/WorkflowMapperTest.java b/workflow-designer-be/src/test/org/onap/sdc/workflow/api/mapping/WorkflowMapperTest.java deleted file mode 100644 index 3d5784bc..00000000 --- a/workflow-designer-be/src/test/org/onap/sdc/workflow/api/mapping/WorkflowMapperTest.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.onap.sdc.workflow.api.mapping; - - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import java.util.HashMap; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.onap.sdc.workflow.persistence.types.Workflow; -import org.onap.sdc.workflow.persistence.types.WorkflowProperty; -import org.onap.sdc.workflow.services.mappers.WorkflowMapper; -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 = WorkflowMapperTest.class) - public static class WorkflowMapperSpringTestConfig { } - - @Autowired - WorkflowMapper workflowMapper; - - @Test - public void shouldMapItemPropertyToWorkflowCategory() { - - Item item = createMockItem(); - HashMap properties = new HashMap<>(); - properties.put(WorkflowProperty.CATEGORY, "category"); - item.setProperties(properties); - - Workflow mappedWorkflow = workflowMapper.itemToWorkflow(item); - assertEquals(mappedWorkflow.getId(), item.getId()); - assertEquals(mappedWorkflow.getDescription(), item.getDescription()); - assertEquals(mappedWorkflow.getName(), item.getName()); - assertEquals(mappedWorkflow.getCategory(), properties.get(WorkflowProperty.CATEGORY)); - } - - @Test - public void shouldAddWorkflowCategoryToItemProperties(){ - Workflow workflow = new Workflow(); - workflow.setId("id"); - workflow.setCategory("cat"); - - Item item = workflowMapper.workflowToItem(workflow); - assertNotNull(item.getProperties().get(WorkflowProperty.CATEGORY)); - } - - private Item createMockItem() { - Item item = new Item(); - item.setId("id"); - item.setDescription("item description"); - item.setName("item name"); - return item; - } - -} \ No newline at end of file diff --git a/workflow-designer-be/src/test/org/onap/sdc/workflow/services/UniqueValueServiceTest.java b/workflow-designer-be/src/test/org/onap/sdc/workflow/services/UniqueValueServiceTest.java deleted file mode 100644 index 57d06fcb..00000000 --- a/workflow-designer-be/src/test/org/onap/sdc/workflow/services/UniqueValueServiceTest.java +++ /dev/null @@ -1,111 +0,0 @@ -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.ArgumentMatchers.eq; -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.errors.UniqueValueViolationException; - -public class UniqueValueServiceTest { - - public static final String TYPE = "ss"; - public static final String DUMMY_COMBINATION = "dummy"; - - @Mock - private UniqueValueRepository uniqueValueRepositoryMock; - - @Spy - @InjectMocks - private UniqueValueService uniqueValueService; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - } - - // testing create unique value- START - @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}); - } - // testing create unique value- END - - // testing delete unique value- START - @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)); - } - - // testing delete unique value- END - - // testing update unique value- START - @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()); - } - // testing update unique value- END - - // testing validateUniqueValue- START - @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})); - } - - // testing validate unique value- END - - -} -- cgit 1.2.3-korg