Add null test to ComponentInstanceBusinessLogic

- Added test in `changeInstanceVersion` for null value

Issue-ID: SDC-2908
Signed-off-by: Chris Andre <chris.andre@yoppworks.com>
Change-Id: I42a428c4b245a55b7229e2ec9b781e9af4403662
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java
index da9e0f9..e943af8 100644
--- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java
+++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java
@@ -2292,8 +2292,11 @@
         return changeInstanceVersion(containerComponent, currentResourceInstance, newComponentInstance, user, containerComponentType );
     }
 
-    public ComponentInstance changeInstanceVersion(org.openecomp.sdc.be.model.Component containerComponent, ComponentInstance currentResourceInstance,
-                                                   ComponentInstance newComponentInstance, User user, final ComponentTypeEnum containerComponentType    ) {
+    public ComponentInstance changeInstanceVersion(org.openecomp.sdc.be.model.Component containerComponent,
+        ComponentInstance currentResourceInstance,
+        ComponentInstance newComponentInstance,
+        User user,
+        final ComponentTypeEnum containerComponentType) {
         boolean failed = false;
         Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus;
 
@@ -2306,10 +2309,14 @@
             }
             String resourceId = newComponentInstance.getComponentUid();
 
-            Either<Boolean, StorageOperationStatus> componentExistsRes = toscaOperationFacade.validateComponentExists(resourceId);
+            Either<Boolean, StorageOperationStatus> componentExistsRes = toscaOperationFacade
+                .validateComponentExists(resourceId);
             if (componentExistsRes.isRight()) {
-                log.debug("Failed to validate existing of the component {}. Status is {} ", resourceId, componentExistsRes.right().value());
-                throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(componentExistsRes.right().value()), resourceId);
+                StorageOperationStatus errorStatus = componentExistsRes.right().value();
+
+                log.debug("Failed to validate existing of the component {}. Status is {} ", resourceId, errorStatus);
+                throw new ByActionStatusComponentException(
+                    componentsUtils.convertFromStorageResponse(errorStatus), resourceId);
             } else if (!componentExistsRes.left().value()) {
                 log.debug("The resource {} not found ", resourceId);
                 throw new ByActionStatusComponentException(ActionStatus.RESOURCE_NOT_FOUND, resourceId);
@@ -2317,60 +2324,79 @@
 
             Component eitherOriginComponent = getInstanceOriginNode(currentResourceInstance);
 
-            DataForMergeHolder dataHolder = compInstMergeDataBL.saveAllDataBeforeDeleting(containerComponent, currentResourceInstance, eitherOriginComponent);
-            ComponentInstance resResourceInfo = deleteComponentInstance(containerComponent, componentInstanceId, containerComponentType);
-            Component origComponent = null;
-            OriginTypeEnum originType = currentResourceInstance.getOriginType();
-            if (originType == OriginTypeEnum.ServiceProxy) {
-                Either<Component, StorageOperationStatus> serviceProxyOrigin = toscaOperationFacade.getLatestByName("serviceProxy");
-                if (isServiceProxyOrigin(serviceProxyOrigin))
-                    throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(serviceProxyOrigin.right().value()));
-                origComponent = serviceProxyOrigin.left().value();
+            DataForMergeHolder dataHolder = compInstMergeDataBL
+                .saveAllDataBeforeDeleting(containerComponent, currentResourceInstance, eitherOriginComponent);
+            ComponentInstance resResourceInfo = deleteComponentInstance(containerComponent, componentInstanceId,
+                containerComponentType);
 
-                StorageOperationStatus fillProxyRes = fillProxyInstanceData(newComponentInstance, origComponent);
+            if (resResourceInfo == null) {
+                log.error("Calling `deleteComponentInstance` for the resource {} returned a null", resourceId);
+                throw new ByActionStatusComponentException(ActionStatus.RESOURCE_NOT_FOUND, resourceId);
+            } else {
+                Component origComponent = null;
+                OriginTypeEnum originType = currentResourceInstance.getOriginType();
+                if (originType == OriginTypeEnum.ServiceProxy) {
+                    Either<Component, StorageOperationStatus> serviceProxyOrigin = toscaOperationFacade
+                        .getLatestByName("serviceProxy");
+                    if (isServiceProxyOrigin(serviceProxyOrigin)) {
+                        throw new ByActionStatusComponentException(
+                            componentsUtils.convertFromStorageResponse(serviceProxyOrigin.right().value()));
+                    }
+                    origComponent = serviceProxyOrigin.left().value();
 
-                if (isFillProxyRes(fillProxyRes))
-                    throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(fillProxyRes));
-                newComponentInstance.setOriginType(originType);
-            }else{
-                origComponent = getOriginComponentFromComponentInstance(newComponentInstance);
-                newComponentInstance.setName(resResourceInfo.getName());
-            }
+                    StorageOperationStatus fillProxyRes = fillProxyInstanceData(newComponentInstance, origComponent);
 
-            newComponentInstance.setInvariantName(resResourceInfo.getInvariantName());
-            newComponentInstance.setPosX(resResourceInfo.getPosX());
-            newComponentInstance.setPosY(resResourceInfo.getPosY());
-            newComponentInstance.setDescription(resResourceInfo.getDescription());
+                    if (isFillProxyRes(fillProxyRes)) {
+                        throw new ByActionStatusComponentException(
+                            componentsUtils.convertFromStorageResponse(fillProxyRes));
+                    }
+                    newComponentInstance.setOriginType(originType);
+                } else {
+                    origComponent = getOriginComponentFromComponentInstance(newComponentInstance);
+                    newComponentInstance.setName(resResourceInfo.getName());
+                }
 
-            ComponentInstance updatedComponentInstance = createComponentInstanceOnGraph(containerComponent, origComponent, newComponentInstance, user);
-            dataHolder.setCurrInstanceNode(origComponent);
-            Component mergeStatusEither = compInstMergeDataBL.mergeComponentUserOrigData(user, dataHolder, containerComponent, containerComponentId, newComponentInstance.getUniqueId());
+                newComponentInstance.setInvariantName(resResourceInfo.getInvariantName());
+                newComponentInstance.setPosX(resResourceInfo.getPosX());
+                newComponentInstance.setPosY(resResourceInfo.getPosY());
+                newComponentInstance.setDescription(resResourceInfo.getDescription());
 
-            ActionStatus postChangeVersionResult = onChangeInstanceOperationOrchestrator.doPostChangeVersionOperations(containerComponent, currentResourceInstance, newComponentInstance);
-            if (postChangeVersionResult != ActionStatus.OK) {
-                throw new ByActionStatusComponentException(postChangeVersionResult);
-            }
+                ComponentInstance updatedComponentInstance = createComponentInstanceOnGraph(containerComponent,
+                    origComponent, newComponentInstance, user);
+                dataHolder.setCurrInstanceNode(origComponent);
+                Component mergeStatusEither = compInstMergeDataBL
+                    .mergeComponentUserOrigData(user, dataHolder, containerComponent, containerComponentId,
+                        newComponentInstance.getUniqueId());
 
-            ComponentParametersView filter = new ComponentParametersView(true);
-            filter.setIgnoreComponentInstances(false);
-            Either<Component, StorageOperationStatus> updatedComponentRes = toscaOperationFacade.getToscaElement(containerComponentId, filter);
-            if (updatedComponentRes.isRight()) {
-                StorageOperationStatus storageOperationStatus = updatedComponentRes.right().value();
-                ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(storageOperationStatus, containerComponent.getComponentType());
-                log.debug("Component with id {} was not found", containerComponentId);
-                throw new ByActionStatusComponentException(actionStatus, Constants.EMPTY_STRING);
-            }
-            resourceInstanceStatus = getResourceInstanceById(updatedComponentRes.left().value(), updatedComponentInstance.getUniqueId());
-            if (resourceInstanceStatus.isRight()) {
-                throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse
+                ActionStatus postChangeVersionResult = onChangeInstanceOperationOrchestrator
+                    .doPostChangeVersionOperations(containerComponent, currentResourceInstance, newComponentInstance);
+                if (postChangeVersionResult != ActionStatus.OK) {
+                    throw new ByActionStatusComponentException(postChangeVersionResult);
+                }
+
+                ComponentParametersView filter = new ComponentParametersView(true);
+                filter.setIgnoreComponentInstances(false);
+                Either<Component, StorageOperationStatus> updatedComponentRes = toscaOperationFacade
+                    .getToscaElement(containerComponentId, filter);
+                if (updatedComponentRes.isRight()) {
+                    StorageOperationStatus storageOperationStatus = updatedComponentRes.right().value();
+                    ActionStatus actionStatus = componentsUtils
+                        .convertFromStorageResponse(storageOperationStatus, containerComponent.getComponentType());
+                    log.debug("Component with id {} was not found", containerComponentId);
+                    throw new ByActionStatusComponentException(actionStatus, Constants.EMPTY_STRING);
+                }
+                resourceInstanceStatus = getResourceInstanceById(updatedComponentRes.left().value(),
+                    updatedComponentInstance.getUniqueId());
+                if (resourceInstanceStatus.isRight()) {
+                    throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse
                         (resourceInstanceStatus.right().value()), updatedComponentInstance.getUniqueId());
+                }
+                return resourceInstanceStatus.left().value();
             }
-            return  resourceInstanceStatus.left().value();
-
-        }catch (ComponentException e){
+        } catch (ComponentException e) {
             failed = true;
             throw e;
-        }finally {
+        } finally {
             unlockComponent(failed, containerComponent);
         }
     }