diff options
author | avigaffa <avi.gaffa@amdocs.com> | 2018-06-18 20:07:14 +0300 |
---|---|---|
committer | avigaffa <avi.gaffa@amdocs.com> | 2018-06-18 20:10:42 +0300 |
commit | ba255a01b24485fe9793d34841b13a5479f6444a (patch) | |
tree | 98e0f228fb5f6b43a2e761e7a7ef4c6e9c103cc0 /workflow-designer-be/src/test/java | |
parent | 8f07b3287efedba992f037d499c7f55f71f823eb (diff) |
fix mapper unit tests
Change-Id: Ibbc47fe2d59c1fa95ee2a0212ebe6c293f5bdfd2
Issue-ID: SDC-1396
Signed-off-by: avigaffa <avi.gaffa@amdocs.com>
Diffstat (limited to 'workflow-designer-be/src/test/java')
2 files changed, 162 insertions, 0 deletions
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<String, Object> 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})); + } +} |