Fix ability to add property to data type of different model 55/132955/5
authorJvD_Ericsson <jeff.van.dam@est.tech>
Thu, 19 Jan 2023 14:43:33 +0000 (14:43 +0000)
committerMichael Morris <michael.morris@est.tech>
Mon, 23 Jan 2023 15:44:25 +0000 (15:44 +0000)
Issue-ID: SDC-4339
Signed-off-by: JvD_Ericsson <jeff.van.dam@est.tech>
Change-Id: Ibc5b57a042ffc4e7f914d46ad264a0e16081b863

catalog-be/src/main/java/org/openecomp/sdc/be/servlets/DataTypeServlet.java
catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperation.java
catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperationTest.java
catalog-ui/src/app/ng2/pages/type-workspace/type-workspace-properties/type-workspace-properties.component.ts

index e081110..8139237 100644 (file)
@@ -144,6 +144,20 @@ public class DataTypeServlet extends BeGenericServlet {
     public Response createProperty(@Parameter(in = ParameterIn.PATH, required = true, description = "The data type id")
                                    @PathParam("id") final String id,
                                    @RequestBody(description = "Property to add", required = true) final PropertyDefinitionDto propertyDefinitionDto) {
+        Optional<DataTypeDataDefinition> dataType = dataTypeOperation.getDataTypeByUid(id);
+        dataType.ifPresentOrElse(dt -> {
+            String model = dt.getModel();
+            Optional<DataTypeDataDefinition> propertyDataType = dataTypeOperation.getDataTypeByNameAndModel(propertyDefinitionDto.getType(), model);
+            if (propertyDataType.isEmpty()) {
+                if (model == null || model.isEmpty()) {
+                    model = "SDC AID";
+                }
+                throw new OperationException(ActionStatus.INVALID_MODEL,
+                        String.format("Property model is not the same as the data type model. Must be be '%s'", model));
+            }
+        }, () -> {
+            throw new OperationException(ActionStatus.DATA_TYPE_NOT_FOUND, String.format("Failed to find data type '%s'", id));
+        });
         final PropertyDefinitionDto property = dataTypeOperation.createProperty(id, propertyDefinitionDto);
         dataTypeBusinessLogic.updateApplicationDataTypeCache(id);
         return Response.status(Status.CREATED).entity(property).build();
index d75302f..36dcaf4 100644 (file)
@@ -189,6 +189,22 @@ public class DataTypeOperation extends AbstractOperation {
         return Optional.of(dataTypeEither.left().value().getDataTypeDataDefinition());
     }
 
+    public Optional<DataTypeDataDefinition> getDataTypeByNameAndModel(final String name, String model) {
+        final Either<DataTypeData, JanusGraphOperationStatus> dataTypeEither = janusGraphGenericDao
+            .getNode("name", name, DataTypeData.class, model);
+        if (dataTypeEither.isRight()) {
+            if (JanusGraphOperationStatus.NOT_FOUND.equals(dataTypeEither.right().value())) {
+                return Optional.empty();
+            }
+            final StorageOperationStatus storageOperationStatus
+                = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(dataTypeEither.right().value());
+            LOGGER.warn("Failed to fetch data type '{}' from JanusGraph. Status is: {}", name, storageOperationStatus);
+            throw new OperationException(ActionStatus.GENERAL_ERROR,
+                String.format("Failed to fetch data type '%s' from JanusGraph. Status is: %s", name, storageOperationStatus));
+        }
+        return Optional.of(dataTypeEither.left().value().getDataTypeDataDefinition());
+    }
+
     public List<PropertyDefinition> findAllProperties(final String uniqueId) {
         final Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> propertiesEither =
             propertyOperation.findPropertiesOfNode(NodeTypeEnum.DataType, uniqueId);
index 034269b..5d68bf9 100644 (file)
@@ -113,6 +113,39 @@ class DataTypeOperationTest {
         assertThat(dataTypesFound.containsAll(dataTypesWithoutModel)).isFalse();
     }
 
+    @Test
+    void getDataTypeByNameAndModelEtsiTest() {
+        final DataTypeData dataType = dataTypesWithModel.get(0);
+        final String dataTypeName = dataType.getDataTypeDataDefinition().getName();
+        final String dataTypeUid = dataType.getDataTypeDataDefinition().getUniqueId();
+        when(janusGraphGenericDao.getNode("name", dataTypeName, DataTypeData.class, modelName))
+                .thenReturn(Either.left(dataType));
+        final var dataTypeFound = dataTypeOperation.getDataTypeByNameAndModel(dataTypeName, model.getName());
+        assertTrue(dataTypeFound.isPresent());
+        DataTypeDataDefinition foundDataType = dataTypeFound.get();
+        assertEquals(modelName ,foundDataType.getModel());
+        assertEquals(dataTypeUid ,foundDataType.getUniqueId());
+    }
+
+    @Test
+    void getDataTypeByNameAndModelNotFoundTest() {
+        when(janusGraphGenericDao.getNode("name", "notReal", DataTypeData.class, modelName))
+                .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
+        final var dataTypesFound = dataTypeOperation.getDataTypeByNameAndModel("notReal", modelName);
+        assertTrue(dataTypesFound.isEmpty());
+    }
+
+    @Test
+    void getDataTypeByNameAndModelGeneralErrorTest() {
+        when(janusGraphGenericDao.getNode("name", "notReal", DataTypeData.class, modelName))
+                .thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR));
+        final OperationException actualException =
+                assertThrows(OperationException.class, () -> dataTypeOperation.getDataTypeByNameAndModel("notReal", modelName));
+        final OperationException expectedException =
+                DataTypeOperationExceptionSupplier.unexpectedErrorWhileFetchingProperties("notReal").get();
+        assertEquals(expectedException.getMessage(), actualException.getMessage());
+    }
+
     @Test
     void getAllDataTypeNodesWithValidationErrorTest() {
         when(janusGraphGenericDao.getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class))
index 90bc89a..f53ad5b 100644 (file)
@@ -173,7 +173,8 @@ export class TypeWorkspacePropertiesComponent implements OnInit {
 
         this.modalService.addDynamicContentToModalAndBindInputs(modal, AddPropertyComponent, {
             'readOnly': readOnly,
-            'property': property
+            'property': property,
+            'model': this.dataType.model
         });
         modal.instance.dynamicContent.instance.onValidityChange.subscribe((validationEvent: PropertyValidationEvent) => {
             disableSaveButtonFlag = !validationEvent.isValid;