Improve test coverage
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ComponentInstanceBusinessLogic.java
index ff98163..7cb9858 100644 (file)
@@ -1888,43 +1888,6 @@ public class ComponentInstanceBusinessLogic extends BaseBusinessLogic {
      * @param userId
      * @return
      */
-    public Either<ComponentInstanceProperty, ResponseFormat> createOrUpdateAttributeValue(ComponentTypeEnum componentTypeEnum, String componentId,
-                                                                                          String resourceInstanceId,
-                                                                                          ComponentInstanceProperty attribute, String userId) {
-        Either<ComponentInstanceProperty, ResponseFormat> result = null;
-        Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
-        validateUserExists(userId);
-        if (errorWrapper.isEmpty()) {
-            validateComponentTypeEnum(componentTypeEnum, "CreateOrUpdateAttributeValue", errorWrapper);
-        }
-        if (errorWrapper.isEmpty()) {
-            validateCanWorkOnComponent(componentId, componentTypeEnum, userId, errorWrapper);
-        }
-        if (errorWrapper.isEmpty()) {
-            validateComponentLock(componentId, componentTypeEnum, errorWrapper);
-        }
-        try {
-            if (errorWrapper.isEmpty()) {
-                final boolean isCreate = Objects.isNull(attribute.getValueUniqueUid());
-                if (isCreate) {
-                    result = createAttributeValue(attribute, resourceInstanceId);
-                } else {
-                    result = updateAttributeValue(attribute, resourceInstanceId);
-                }
-            } else {
-                result = Either.right(errorWrapper.getInnerElement());
-            }
-            return result;
-        } finally {
-            if (result == null || result.isRight()) {
-                janusGraphDao.rollback();
-            } else {
-                janusGraphDao.commit();
-            }
-            // unlock resource
-            graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
-        }
-    }
 
     public Either<List<ComponentInstanceProperty>, ResponseFormat> createOrUpdatePropertiesValues(ComponentTypeEnum componentTypeEnum,
                                                                                                   String componentId, String resourceInstanceId,
@@ -2110,19 +2073,6 @@ public class ComponentInstanceBusinessLogic extends BaseBusinessLogic {
         }
     }
 
-    private void setJsonObjectForSubProperty(final JSONObject jObject, final List<String> path, String value) {
-        if (path.size() == 1) {
-            Object valueAsObject = new Yaml().loadAs(value, Object.class);
-            jObject.put(path.get(0), valueAsObject);
-        } else {
-            if (!jObject.has(path.get(0))) {
-                jObject.put(path.get(0), new JSONObject());
-            }
-            final JSONObject jsonObject = jObject.getJSONObject(path.get(0));
-            setJsonObjectForSubProperty(jsonObject, path.subList(1, path.size()), value);
-        }
-    }
-
     public Either<List<ComponentInstanceAttribute>, ResponseFormat> createOrUpdateAttributeValues(final ComponentTypeEnum componentTypeEnum,
                                                                                                   final String componentId,
                                                                                                   final String resourceInstanceId,
@@ -2248,14 +2198,35 @@ public class ComponentInstanceBusinessLogic extends BaseBusinessLogic {
     private ComponentInstanceAttribute validateAttributeExistsOnComponent(final ComponentInstanceAttribute attribute,
                                                                           final Component containerComponent,
                                                                           final ComponentInstance foundResourceInstance) {
-        final List<ComponentInstanceAttribute> instanceProperties =
+        final List<ComponentInstanceAttribute> instanceAttributes =
             containerComponent.getComponentInstancesAttributes().get(foundResourceInstance.getUniqueId());
+        final List<ComponentInstanceProperty> instanceProperties =
+            containerComponent.getComponentInstancesProperties().get(foundResourceInstance.getUniqueId());
         final Optional<ComponentInstanceAttribute> instanceAttribute =
+            instanceAttributes.stream().filter(p -> p.getName().equals(attribute.getName())).findAny();
+        final Optional<ComponentInstanceProperty> instanceProperty =
             instanceProperties.stream().filter(p -> p.getName().equals(attribute.getName())).findAny();
-        if (!instanceAttribute.isPresent()) {
-            throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, attribute.getName());
+        if (instanceAttribute.isPresent()) {
+            return instanceAttribute.get();
+        }
+        if (instanceProperty.isPresent()) {
+            ComponentInstanceAttribute propAttribute = getComponentInstanceAttribute(instanceProperty.get());
+            return propAttribute;
         }
-        return instanceAttribute.get();
+        throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, attribute.getName());
+    }
+
+    private ComponentInstanceAttribute getComponentInstanceAttribute(ComponentInstanceProperty property) {
+        ComponentInstanceAttribute attribute = new ComponentInstanceAttribute();
+        attribute.setParentUniqueId(property.getParentUniqueId());
+        attribute.setName(property.getName());
+        attribute.setOwnerId(property.getOwnerId());
+        attribute.setType(property.getType());
+        attribute.setSchema(property.getSchema());
+        attribute.setUniqueId(property.getUniqueId());
+        attribute.setValue(property.getValue());
+        attribute.setDefaultValue(property.getDefaultValue());
+        return attribute;
     }
 
     private ResponseFormat updateCapabilityPropertyOnContainerComponent(ComponentInstanceProperty property,
@@ -2498,42 +2469,6 @@ public class ComponentInstanceBusinessLogic extends BaseBusinessLogic {
         return Either.left(newValue);
     }
 
-    private <T extends PropertyDefinition> void validateToscaGetFunction(T property, Component parentComponent) {
-        final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) property.getToscaFunction();
-        validateGetToscaFunctionAttributes(toscaGetFunction);
-        validateGetPropertySource(toscaGetFunction.getFunctionType(), toscaGetFunction.getPropertySource());
-        if (toscaGetFunction.getFunctionType() == ToscaGetFunctionType.GET_INPUT) {
-            validateGetFunction(property, parentComponent.getInputs(), parentComponent.getModel());
-            return;
-        }
-        if (toscaGetFunction.getFunctionType() == ToscaGetFunctionType.GET_PROPERTY) {
-            if (toscaGetFunction.getPropertySource() == PropertySource.SELF) {
-                validateGetFunction(property, parentComponent.getProperties(), parentComponent.getModel());
-            } else if (toscaGetFunction.getPropertySource() == PropertySource.INSTANCE) {
-                final ComponentInstance componentInstance =
-                    parentComponent.getComponentInstanceById(toscaGetFunction.getSourceUniqueId())
-                        .orElseThrow(ToscaGetFunctionExceptionSupplier.instanceNotFound(toscaGetFunction.getSourceName()));
-                validateGetFunction(property, componentInstance.getProperties(), parentComponent.getModel());
-            }
-
-            return;
-        }
-        if (toscaGetFunction.getFunctionType() == ToscaGetFunctionType.GET_ATTRIBUTE) {
-            if (toscaGetFunction.getPropertySource() == PropertySource.SELF) {
-                validateGetFunction(property, parentComponent.getAttributes(), parentComponent.getModel());
-            } else if (toscaGetFunction.getPropertySource() == PropertySource.INSTANCE) {
-                final ComponentInstance componentInstance =
-                    parentComponent.getComponentInstanceById(toscaGetFunction.getSourceUniqueId())
-                        .orElseThrow(ToscaGetFunctionExceptionSupplier.instanceNotFound(toscaGetFunction.getSourceName()));
-                validateGetFunction(property, componentInstance.getAttributes(), parentComponent.getModel());
-            }
-
-            return;
-        }
-
-        throw ToscaGetFunctionExceptionSupplier.functionNotSupported(toscaGetFunction.getFunctionType()).get();
-    }
-
     private <T extends PropertyDefinition> void validateGetFunction(final T property,
                                                                     final List<? extends ToscaPropertyData> parentProperties,
                                                                     final String model) {
@@ -3051,7 +2986,8 @@ public class ComponentInstanceBusinessLogic extends BaseBusinessLogic {
             }
 
             Component eitherOriginComponent = getInstanceOriginNode(currentResourceInstance);
-            DataForMergeHolder dataHolder = compInstMergeDataBL.saveAllDataBeforeDeleting(containerComponent, currentResourceInstance, eitherOriginComponent);
+            DataForMergeHolder dataHolder =
+                compInstMergeDataBL.saveAllDataBeforeDeleting(containerComponent, currentResourceInstance, eitherOriginComponent);
             ComponentInstance resResourceInfo = deleteComponentInstance(containerComponent, componentInstanceId,
                 containerComponentType);