diff options
Diffstat (limited to 'catalog-be/src/test/java/org/openecomp/sdc/be/components/PropertyBusinessLogicTest.java')
-rw-r--r-- | catalog-be/src/test/java/org/openecomp/sdc/be/components/PropertyBusinessLogicTest.java | 226 |
1 files changed, 167 insertions, 59 deletions
diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/PropertyBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/PropertyBusinessLogicTest.java index 95e6cceb78..aeccf7d817 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/PropertyBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/PropertyBusinessLogicTest.java @@ -29,9 +29,9 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import org.openecomp.sdc.be.components.impl.BaseBusinessLogic; import org.openecomp.sdc.be.components.impl.BaseBusinessLogicMock; import org.openecomp.sdc.be.components.impl.PropertyBusinessLogic; +import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException; import org.openecomp.sdc.be.components.validation.UserValidations; import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.dao.api.ActionStatus; @@ -40,9 +40,11 @@ import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.impl.WebAppContextWrapper; import org.openecomp.sdc.be.model.*; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ToscaOperationException; import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation; import org.openecomp.sdc.be.model.operations.api.IPropertyOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; import org.openecomp.sdc.be.user.Role; import org.openecomp.sdc.be.user.UserBusinessLogic; import org.openecomp.sdc.common.api.ConfigurationSource; @@ -51,21 +53,25 @@ import org.openecomp.sdc.common.impl.ExternalConfiguration; import org.openecomp.sdc.common.impl.FSConfigurationSource; import org.openecomp.sdc.exception.ResponseFormat; import org.openecomp.sdc.test.utils.InterfaceOperationTestUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.web.context.WebApplicationContext; import javax.servlet.ServletContext; import java.lang.reflect.Field; import java.util.*; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItems; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.when; public class PropertyBusinessLogicTest extends BaseBusinessLogicMock { - private static final Logger log = LoggerFactory.getLogger(PropertyBusinessLogicTest.class); @Mock private ServletContext servletContext; @Mock @@ -88,7 +94,7 @@ public class PropertyBusinessLogicTest extends BaseBusinessLogicMock { JanusGraphDao janusGraphDao; @InjectMocks - private PropertyBusinessLogic bl = new PropertyBusinessLogic(elementDao, groupOperation, groupInstanceOperation, + private PropertyBusinessLogic propertyBusinessLogic = new PropertyBusinessLogic(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation); private User user = null; private String resourceId = "resourceforproperty.0.1"; @@ -123,8 +129,6 @@ public class PropertyBusinessLogicTest extends BaseBusinessLogicMock { when(servletContext.getAttribute(Constants.PROPERTY_OPERATION_MANAGER)).thenReturn(propertyOperation); when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper); when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext); - - } @Test @@ -137,7 +141,8 @@ public class PropertyBusinessLogicTest extends BaseBusinessLogicMock { resource.setUniqueId(resourceId); Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource)); - Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> nonExistingProperty = bl.getComponentProperty(resourceId, "NonExistingProperty", user.getUserId()); + Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> nonExistingProperty = propertyBusinessLogic + .getComponentProperty(resourceId, "NonExistingProperty", user.getUserId()); assertTrue(nonExistingProperty.isRight()); Mockito.verify(componentsUtils).getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, ""); } @@ -151,7 +156,8 @@ public class PropertyBusinessLogicTest extends BaseBusinessLogicMock { resource.setUniqueId(resourceId); Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource)); - Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> notFoundProperty = bl.getComponentProperty(resourceId, "invalidId", user.getUserId()); + Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> notFoundProperty = propertyBusinessLogic + .getComponentProperty(resourceId, "invalidId", user.getUserId()); assertTrue(notFoundProperty.isRight()); Mockito.verify(componentsUtils).getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, ""); } @@ -164,7 +170,8 @@ public class PropertyBusinessLogicTest extends BaseBusinessLogicMock { resource.setProperties(Arrays.asList(property1)); Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource)); - Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> foundProperty = bl.getComponentProperty(resourceId, property1.getUniqueId(), user.getUserId()); + Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> foundProperty = propertyBusinessLogic + .getComponentProperty(resourceId, property1.getUniqueId(), user.getUserId()); assertTrue(foundProperty.isLeft()); assertEquals(foundProperty.left().value().getValue().getUniqueId(), property1.getUniqueId()); } @@ -179,7 +186,7 @@ public class PropertyBusinessLogicTest extends BaseBusinessLogicMock { Mockito.when(toscaOperationFacade.getToscaElement(serviceId)).thenReturn(Either.left(service)); Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> serviceProperty = - bl.getComponentProperty(serviceId, property1.getUniqueId(), user.getUserId()); + propertyBusinessLogic.getComponentProperty(serviceId, property1.getUniqueId(), user.getUserId()); assertTrue(serviceProperty.isLeft()); assertEquals(serviceProperty.left().value().getValue().getUniqueId(), property1.getUniqueId()); @@ -195,7 +202,7 @@ public class PropertyBusinessLogicTest extends BaseBusinessLogicMock { Mockito.when(toscaOperationFacade.getToscaElement(serviceId)).thenReturn(Either.left(service)); Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> serviceProperty = - bl.getComponentProperty(serviceId, "notExistingPropId", user.getUserId()); + propertyBusinessLogic.getComponentProperty(serviceId, "notExistingPropId", user.getUserId()); assertTrue(serviceProperty.isRight()); } @@ -208,12 +215,12 @@ public class PropertyBusinessLogicTest extends BaseBusinessLogicMock { PropertyDefinition propDef1 = new PropertyDefinition(); propDef1.setUniqueId("ComponentInput1_uniqueId"); - assertTrue(bl.isPropertyUsedByOperation(service, propDef1)); + assertTrue(propertyBusinessLogic.isPropertyUsedByOperation(service, propDef1)); PropertyDefinition propDef2 = new PropertyDefinition(); propDef1.setUniqueId("inputId2"); Mockito.when(toscaOperationFacade.getParentComponents(serviceId)).thenReturn(Either.left(new ArrayList<>())); - assertFalse(bl.isPropertyUsedByOperation(service, propDef2)); + assertFalse(propertyBusinessLogic.isPropertyUsedByOperation(service, propDef2)); } @Test @@ -230,12 +237,12 @@ public class PropertyBusinessLogicTest extends BaseBusinessLogicMock { PropertyDefinition propDef1 = new PropertyDefinition(); propDef1.setUniqueId("ComponentInput1_uniqueId"); - assertTrue(bl.isPropertyUsedByOperation(service, propDef1)); + assertTrue(propertyBusinessLogic.isPropertyUsedByOperation(service, propDef1)); PropertyDefinition propDef2 = new PropertyDefinition(); propDef1.setUniqueId("inputId2"); Mockito.when(toscaOperationFacade.getParentComponents(serviceId)).thenReturn(Either.left(new ArrayList<>())); - assertFalse(bl.isPropertyUsedByOperation(service, propDef2)); + assertFalse(propertyBusinessLogic.isPropertyUsedByOperation(service, propDef2)); } @Test @@ -254,17 +261,16 @@ public class PropertyBusinessLogicTest extends BaseBusinessLogicMock { PropertyDefinition propDef1 = new PropertyDefinition(); propDef1.setUniqueId("ComponentInput1_uniqueId"); Mockito.when(toscaOperationFacade.getParentComponents(serviceId)).thenReturn(Either.left(Arrays.asList(parentService))); - assertTrue(bl.isPropertyUsedByOperation(childService, propDef1)); + assertTrue(propertyBusinessLogic.isPropertyUsedByOperation(childService, propDef1)); PropertyDefinition propDef2 = new PropertyDefinition(); propDef1.setUniqueId("inputId2"); Mockito.when(toscaOperationFacade.getParentComponents(serviceId)).thenReturn(Either.left(new ArrayList<>())); - assertFalse(bl.isPropertyUsedByOperation(childService, propDef2)); + assertFalse(propertyBusinessLogic.isPropertyUsedByOperation(childService, propDef2)); } - - private PropertyDefinition createPropertyObject(String propertyName, String resourceId) { - PropertyDefinition pd = new PropertyDefinition(); + private PropertyDefinition createPropertyObject(final String propertyName, final String resourceId) { + final PropertyDefinition pd = new PropertyDefinition(); pd.setConstraints(null); pd.setDefaultValue("100"); pd.setDescription("Size of thasdasdasdasde local disk, in Gigabytes (GB), available to applications running on the Compute node"); @@ -272,6 +278,7 @@ public class PropertyBusinessLogicTest extends BaseBusinessLogicMock { pd.setRequired(true); pd.setType("Integer"); pd.setOwnerId(resourceId); + pd.setName(propertyName); pd.setUniqueId(resourceId + "." + propertyName); return pd; } @@ -281,7 +288,7 @@ public class PropertyBusinessLogicTest extends BaseBusinessLogicMock { StorageOperationStatus lockResult = StorageOperationStatus.CONNECTION_FAILURE; when(graphLockOperation.lockComponent(any(), any())).thenReturn(lockResult); when(toscaOperationFacade.getToscaElement(anyString())).thenReturn(Either.left(new Resource())); - assertTrue(bl.deletePropertyFromComponent("resourceforproperty.0.1", "someProperty","i726").isRight()); + assertTrue(propertyBusinessLogic.deletePropertyFromComponent("resourceforproperty.0.1", "someProperty","i726").isRight()); } @Test @@ -295,18 +302,14 @@ public class PropertyBusinessLogicTest extends BaseBusinessLogicMock { resource.setUniqueId(resourceId); Field baseBusinessLogic3; - baseBusinessLogic3 = bl.getClass().getSuperclass().getDeclaredField("janusGraphDao"); + baseBusinessLogic3 = propertyBusinessLogic.getClass().getSuperclass().getDeclaredField("janusGraphDao"); baseBusinessLogic3.setAccessible(true); - baseBusinessLogic3.set(bl, janusGraphDao); - + baseBusinessLogic3.set(propertyBusinessLogic, janusGraphDao); Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource)); StorageOperationStatus lockResult = StorageOperationStatus.OK; when(graphLockOperation.lockComponent(any(), any())).thenReturn(lockResult); - //doNothing().when(janusGraphDao).commit(); - - Either<PropertyDefinition, ResponseFormat> result; Component resourcereturn= new Resource(); resourcereturn.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); @@ -316,13 +319,11 @@ public class PropertyBusinessLogicTest extends BaseBusinessLogicMock { Either<Component, StorageOperationStatus> toscastatus=Either.left(resource); when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus); - - assertTrue(bl.deletePropertyFromComponent("RES01", "someProperty","i726").isRight()); + assertTrue(propertyBusinessLogic.deletePropertyFromComponent("RES01", "someProperty","i726").isRight()); } @Test public void deleteProperty_RESTRICTED_OPERATION() throws Exception { - Resource resource = new Resource(); PropertyDefinition property1 = createPropertyObject("someProperty", "someResource"); @@ -331,20 +332,15 @@ public class PropertyBusinessLogicTest extends BaseBusinessLogicMock { resource.setUniqueId(resourceId); Field baseBusinessLogic3; - baseBusinessLogic3 = bl.getClass().getSuperclass().getDeclaredField("janusGraphDao"); + baseBusinessLogic3 = propertyBusinessLogic.getClass().getSuperclass().getDeclaredField("janusGraphDao"); baseBusinessLogic3.setAccessible(true); - baseBusinessLogic3.set(bl, janusGraphDao); - + baseBusinessLogic3.set(propertyBusinessLogic, janusGraphDao); Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource)); StorageOperationStatus lockResult = StorageOperationStatus.OK; when(graphLockOperation.lockComponent(any(), any())).thenReturn(lockResult); - //doNothing().when(janusGraphDao).commit(); - - Either<PropertyDefinition, ResponseFormat> result; - Component resourcereturn= new Resource(); resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); resource.setIsDeleted(false); resource.setLastUpdaterUserId("USR01"); @@ -353,43 +349,155 @@ public class PropertyBusinessLogicTest extends BaseBusinessLogicMock { when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus); - assertTrue(bl.deletePropertyFromComponent("RES01", "someProperty","i726").isRight()); + assertTrue(propertyBusinessLogic.deletePropertyFromComponent("RES01", "someProperty","i726").isRight()); } @Test public void deleteProperty_RESTRICTED_() throws Exception { - - Resource resource = new Resource(); - PropertyDefinition property1 = createPropertyObject("PROP", "RES01"); - property1.setUniqueId("PROP"); - resource.setProperties(Arrays.asList(property1)); - String resourceId = "myResource"; + final PropertyDefinition property1 = createPropertyObject("PROP", "RES01"); + final Resource resource = new Resource(); + final String resourceId = "myResource"; resource.setUniqueId(resourceId); + resource.setProperties(Arrays.asList(property1)); - Field baseBusinessLogic3; - baseBusinessLogic3 = bl.getClass().getSuperclass().getDeclaredField("janusGraphDao"); + final Field baseBusinessLogic3 = + propertyBusinessLogic.getClass().getSuperclass().getDeclaredField("janusGraphDao"); baseBusinessLogic3.setAccessible(true); - baseBusinessLogic3.set(bl, janusGraphDao); - + baseBusinessLogic3.set(propertyBusinessLogic, janusGraphDao); Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource)); - StorageOperationStatus lockResult = StorageOperationStatus.OK; - when(graphLockOperation.lockComponent(any(), any())).thenReturn(lockResult); - //doNothing().when(janusGraphDao).commit(); - - Either<PropertyDefinition, ResponseFormat> result; + when(graphLockOperation.lockComponent(any(), any())).thenReturn(StorageOperationStatus.OK); - Component resourcereturn= new Resource(); resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); resource.setIsDeleted(false); resource.setLastUpdaterUserId("USR01"); - Either<Component, StorageOperationStatus> toscastatus=Either.left(resource); - when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus); - when(toscaOperationFacade.deletePropertyOfComponent(anyObject(),anyString())).thenReturn(StorageOperationStatus.OK); + when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(Either.left(resource)); + when(toscaOperationFacade.deletePropertyOfComponent(anyObject(), anyString())).thenReturn(StorageOperationStatus.OK); when(toscaOperationFacade.getParentComponents(anyString())).thenReturn(Either.left(new ArrayList<>())); - assertTrue(bl.deletePropertyFromComponent("RES01", "PROP","USR01").isRight()); + assertTrue(propertyBusinessLogic.deletePropertyFromComponent("RES01", "PROP","USR01").isRight()); + } + + @Test + public void findComponentByIdTest() throws BusinessLogicException { + //give + final Resource resource = new Resource(); + resource.setUniqueId(resourceId); + Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource)); + //when + final Component actualResource = propertyBusinessLogic.findComponentById(resourceId).orElse(null); + //then + assertThat("Actual resource should not be null", actualResource, is(notNullValue())); + assertThat("Actual resource must have the expected id", + actualResource.getUniqueId(), is(equalTo(resource.getUniqueId()))); + } + + @Test(expected = BusinessLogicException.class) + public void findComponentById_resourceNotFoundTest() throws BusinessLogicException { + //given + Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.right(null)); + Mockito.when(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, "")).thenReturn(new ResponseFormat()); + //when + propertyBusinessLogic.findComponentById(resourceId); + } + + @Test + public void updateComponentPropertyTest() throws BusinessLogicException { + //given + final Resource resource = new Resource(); + resource.setUniqueId(resourceId); + final PropertyDefinition propertyDefinition = createPropertyObject("testProperty", resourceId); + Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource)); + when(toscaOperationFacade.updatePropertyOfComponent(resource, propertyDefinition)).thenReturn(Either.left(propertyDefinition)); + //when + final PropertyDefinition actualPropertyDefinition = propertyBusinessLogic + .updateComponentProperty(resourceId, propertyDefinition); + //then + assertThat("Actual property definition should not be null", actualPropertyDefinition, is(notNullValue())); + assertThat("Actual property definition must have the expected id", + actualPropertyDefinition.getOwnerId(), is(equalTo(resource.getUniqueId()))); + assertThat("Actual property definition must have the expected id", + actualPropertyDefinition.getName(), is(equalTo(propertyDefinition.getName()))); + } + + @Test(expected = BusinessLogicException.class) + public void updateComponentProperty_updateFailedTest() throws BusinessLogicException { + //given + final Resource resource = new Resource(); + resource.setUniqueId(resourceId); + final PropertyDefinition propertyDefinition = createPropertyObject("testProperty", resourceId); + Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource)); + when(toscaOperationFacade.updatePropertyOfComponent(resource, propertyDefinition)).thenReturn(Either.right(null)); + when(componentsUtils.getResponseFormatByResource(Mockito.any(), Mockito.anyString())).thenReturn(new ResponseFormat()); + when(componentsUtils.convertFromStorageResponse(Mockito.any())).thenReturn(null); + //when + propertyBusinessLogic.updateComponentProperty(resourceId, propertyDefinition); + } + + @Test + public void copyPropertyToComponentTest() throws ToscaOperationException { + //given + final Resource expectedResource = new Resource(); + expectedResource.setUniqueId(resourceId); + final List<PropertyDefinition> propertiesToCopyList = new ArrayList<>(); + final PropertyDefinition property1 = createPropertyObject("property1", resourceId); + propertiesToCopyList.add(property1); + final PropertyDefinition property2 = createPropertyObject("property2", resourceId); + propertiesToCopyList.add(property2); + + final PropertyDefinition copiedProperty1 = new PropertyDefinition(property1); + copiedProperty1.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(resourceId, copiedProperty1.getName())); + expectedResource.addProperty(copiedProperty1); + final PropertyDefinition copiedProperty2 = new PropertyDefinition(property2); + copiedProperty2.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(resourceId, copiedProperty2.getName())); + expectedResource.addProperty(copiedProperty2); + + Mockito.when(toscaOperationFacade + .addPropertyToComponent(eq(property1.getName()), Mockito.any(PropertyDefinition.class), eq(expectedResource))) + .thenReturn(Either.left(copiedProperty1)); + Mockito.when(toscaOperationFacade + .addPropertyToComponent(eq(property2.getName()), Mockito.any(PropertyDefinition.class), eq(expectedResource))) + .thenReturn(Either.left(copiedProperty2)); + Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(expectedResource)); + //when + final Component actualComponent = propertyBusinessLogic.copyPropertyToComponent(expectedResource, propertiesToCopyList, true); + //then + assertThat("Actual component should not be null", actualComponent, is(notNullValue())); + assertThat("Actual component should be an instance of Resource", actualComponent, is(instanceOf(Resource.class))); + assertThat("Actual component should have the expected id", actualComponent.getUniqueId(), is(equalTo(expectedResource.getUniqueId()))); + assertThat("Actual component should have 2 properties", actualComponent.getProperties(), hasSize(2)); + assertThat("Actual component should have the expected properties", actualComponent.getProperties(), hasItems(copiedProperty1, copiedProperty2)); + } + + @Test + public void copyPropertyToComponent1() throws ToscaOperationException { + //given + final Resource expectedResource = new Resource(); + expectedResource.setUniqueId(resourceId); + //when + final Component actualComponent = propertyBusinessLogic.copyPropertyToComponent(expectedResource, null); + //then + assertThat("Actual component should not be null", actualComponent, is(notNullValue())); + assertThat("Actual component should be an instance of Resource", actualComponent, is(instanceOf(Resource.class))); + assertThat("Actual component should have the expected id", actualComponent.getUniqueId(), is(equalTo(expectedResource.getUniqueId()))); + assertThat("Actual component should have no properties", actualComponent.getProperties(), is(nullValue())); + } + + @Test(expected = ToscaOperationException.class) + public void copyPropertyToComponent_copyFailed() throws ToscaOperationException { + //given + final Resource expectedResource = new Resource(); + expectedResource.setUniqueId(resourceId); + final List<PropertyDefinition> propertiesToCopyList = new ArrayList<>(); + final PropertyDefinition property1 = createPropertyObject("property1", resourceId); + propertiesToCopyList.add(property1); + Mockito.when(toscaOperationFacade + .addPropertyToComponent(eq(property1.getName()), Mockito.any(PropertyDefinition.class), eq(expectedResource))) + .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR)); + Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(expectedResource)); + //when + propertyBusinessLogic.copyPropertyToComponent(expectedResource, propertiesToCopyList, true); } } |