Expose generic Service properties as properties

During the Service creation, the properties declared in the Generic
Service Type are being parsed to inputs, but not to properties.
The change parses the properties also to properties, keeping the
previous input logic.

Change-Id: I3ceba3adeaab56cd49fc8fbd657b02aa7c93e79b
Issue-ID: SDC-3061
Signed-off-by: andre.schmid <andre.schmid@est.tech>
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java
index f4ccb86..552fbb0 100644
--- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java
+++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java
@@ -845,7 +845,7 @@
         if (createServiceResponse.isRight()) {
             return createServiceResponse;
         }
-        return createServiceByDao(service, AuditingActionEnum.CREATE_RESOURCE, user)
+        return createServiceByDao(service, user)
                 .left()
                 .bind(c -> updateCatalog(c, ChangeTypeEnum.LIFECYCLE)
                         .left()
@@ -859,42 +859,42 @@
         }
     }
 
-    private Either<Service, ResponseFormat> createServiceByDao(Service service, AuditingActionEnum actionEnum, User user) {
+    private Either<Service, ResponseFormat> createServiceByDao(final Service service, final User user) {
         log.debug("send service {} to dao for create", service.getComponentMetadataDefinition().getMetadataDataDefinition().getName());
 
         Either<Boolean, ResponseFormat> lockResult = lockComponentByName(service.getSystemName(), service, "Create Service");
         if (lockResult.isRight()) {
             ResponseFormat responseFormat = lockResult.right().value();
-            componentsUtils.auditComponentAdmin(responseFormat, user, service, actionEnum, ComponentTypeEnum.SERVICE);
+            componentsUtils.auditComponentAdmin(responseFormat, user, service, AuditingActionEnum.CREATE_RESOURCE,
+                ComponentTypeEnum.SERVICE);
             return Either.right(responseFormat);
         }
 
         log.debug("System name locked is {}, status = {}", service.getSystemName(), lockResult);
 
         try {
-
             createMandatoryArtifactsData(service, user);
             createServiceApiArtifactsData(service, user);
             setToscaArtifactsPlaceHolders(service, user);
-            generateAndAddInputsFromGenericTypeProperties(service, fetchAndSetDerivedFromGenericType(service));
+            final Resource genericType = fetchAndSetDerivedFromGenericType(service);
+            generatePropertiesFromGenericType(service, genericType);
+            generateAndAddInputsFromGenericTypeProperties(service, genericType);
             beforeCreate(service);
 
             Either<Service, StorageOperationStatus> dataModelResponse = toscaOperationFacade.createToscaComponent(service);
-
-            // service created successfully!!!
             if (dataModelResponse.isLeft()) {
-                log.debug("Service created successfully!!!");
+                log.debug("Service '{}' created successfully", service.getName());
                 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.CREATED);
-                componentsUtils.auditComponentAdmin(responseFormat, user, service, actionEnum, ComponentTypeEnum.SERVICE);
+                componentsUtils.auditComponentAdmin(responseFormat, user, service, AuditingActionEnum.CREATE_RESOURCE,
+                    ComponentTypeEnum.SERVICE);
                 ASDCKpiApi.countCreatedServicesKPI();
                 return Either.left(dataModelResponse.left().value());
             }
-
             ResponseFormat responseFormat = componentsUtils.getResponseFormatByComponent(componentsUtils.convertFromStorageResponse(dataModelResponse.right().value()), service, ComponentTypeEnum.SERVICE);
             log.debug(AUDIT_BEFORE_SENDING_RESPONSE);
-            componentsUtils.auditComponentAdmin(responseFormat, user, service, actionEnum, ComponentTypeEnum.SERVICE);
+            componentsUtils.auditComponentAdmin(responseFormat, user, service, AuditingActionEnum.CREATE_RESOURCE,
+                ComponentTypeEnum.SERVICE);
             return Either.right(responseFormat);
-
         } finally {
             graphLockOperation.unlockComponentByName(service.getSystemName(), service.getUniqueId(), NodeTypeEnum.Service);
         }
@@ -916,6 +916,27 @@
             });
     }
 
+    private void generatePropertiesFromGenericType(final Service service, final Resource genericType) {
+        if (CollectionUtils.isEmpty(genericType.getProperties())) {
+            return;
+        }
+        final List<PropertyDefinition> genericTypePropertyList = genericType.getProperties().stream()
+            .map(PropertyDefinition::new)
+            .peek(propertyDefinition -> propertyDefinition.setUniqueId(null))
+            .collect(Collectors.toList());
+        if (service.getProperties() == null) {
+            service.setProperties(new ArrayList<>(genericTypePropertyList));
+        } else {
+            List<PropertyDefinition> servicePropertyList = service.getProperties();
+            genericTypePropertyList.stream()
+                .filter(property -> servicePropertyList.stream()
+                    .noneMatch(property1 -> property1.getName().equals(property.getName())))
+                .forEach(servicePropertyList::add);
+        }
+
+        service.getProperties().forEach(propertyDefinition -> propertyDefinition.setUniqueId(null));
+    }
+
     @SuppressWarnings("unchecked")
     private void createServiceApiArtifactsData(Service service, User user) {
         // create mandatory artifacts
diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogicTest.java
index 7fc92fb..42d24bb 100644
--- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogicTest.java
+++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogicTest.java
@@ -21,6 +21,9 @@
 package org.openecomp.sdc.be.components.impl;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
@@ -38,8 +41,6 @@
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
 import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.junit.Assert;
 import org.junit.Test;
@@ -56,6 +57,7 @@
 import org.openecomp.sdc.be.model.ComponentInstanceInterface;
 import org.openecomp.sdc.be.model.GroupInstance;
 import org.openecomp.sdc.be.model.Operation;
+import org.openecomp.sdc.be.model.PropertyDefinition;
 import org.openecomp.sdc.be.model.Resource;
 import org.openecomp.sdc.be.model.Service;
 import org.openecomp.sdc.be.model.category.CategoryDefinition;
@@ -93,7 +95,6 @@
     @Test
     public void testHappyScenario() {
         Service service = createServiceObject(false);
-        validateUserRoles(Role.ADMIN, Role.DESIGNER);
         when(genericTypeBusinessLogic.fetchDerivedFromGenericType(service)).thenReturn(Either.left(genericService));
         Either<Service, ResponseFormat> createResponse = bl.createService(service, user);
 
@@ -135,11 +136,58 @@
         assertTrue(createResponse.isLeft());
     }
 
+
+    @Test
+    public void testCreateServiceWhenGenericTypeHasProperties() {
+        final Service service = createServiceObject(false);
+
+        final Resource genericTypeResource = mockGenericTypeResource();
+
+        when(genericTypeBusinessLogic.fetchDerivedFromGenericType(service)).thenReturn(Either.left(genericTypeResource));
+        final Service expectedService = createServiceObject(true);
+        expectedService.setProperties(mockPropertyList());
+        when(toscaOperationFacade.createToscaComponent(service)).thenReturn(Either.left(expectedService));
+        Either<Service, ResponseFormat> createResponse = bl.createService(service, user);
+
+        org.hamcrest.MatcherAssert.assertThat("Service creation should be successful",
+            createResponse.isLeft(), is(true));
+        final Service actualService = createResponse.left().value();
+        org.hamcrest.MatcherAssert.assertThat("Service should not be null", service, is(notNullValue()));
+
+        assertEqualsServiceObject(expectedService, actualService);
+    }
+
+    @Test
+    public void testCreateServiceWhenGenericTypeAndServiceHasProperties() {
+        final Service service = createServiceObject(false);
+        service.setProperties(mockPropertyList());
+        service.getProperties().remove(0);
+        final PropertyDefinition serviceProperty = new PropertyDefinition();
+        serviceProperty.setName("aServiceProperty");
+        service.getProperties().add(serviceProperty);
+
+        final Resource genericTypeResource = mockGenericTypeResource();
+
+        when(genericTypeBusinessLogic.fetchDerivedFromGenericType(service)).thenReturn(Either.left(genericTypeResource));
+        final Service expectedService = createServiceObject(true);
+        expectedService.setProperties(mockPropertyList());
+        expectedService.getProperties().add(serviceProperty);
+        when(toscaOperationFacade.createToscaComponent(service)).thenReturn(Either.left(expectedService));
+        Either<Service, ResponseFormat> createResponse = bl.createService(service, user);
+
+        org.hamcrest.MatcherAssert.assertThat("Service creation should be successful",
+            createResponse.isLeft(), is(true));
+        final Service actualService = createResponse.left().value();
+        org.hamcrest.MatcherAssert.assertThat("Service should not be null", service, is(notNullValue()));
+
+
+        assertEqualsServiceObject(expectedService, actualService);
+    }
+
     @Test
     public void testHappyScenarioCRNullProjectCode() {
         Service service = createServiceObject(false);
         service.setProjectCode(null);
-        validateUserRoles(Role.ADMIN, Role.DESIGNER);
         when(genericTypeBusinessLogic.fetchDerivedFromGenericType(service)).thenReturn(Either.left(genericService));
         Either<Service, ResponseFormat> createResponse = bl.createService(service, user);
 
@@ -148,12 +196,12 @@
         }
         assertEqualsServiceObject(createServiceObject(true), createResponse.left().value());
     }
+
     @Test
     public void testHappyScenarioCREmptyStringProjectCode() {
         createServiceValidator();
         Service service = createServiceObject(false);
         service.setProjectCode("");
-        validateUserRoles(Role.ADMIN, Role.DESIGNER);
         when(genericTypeBusinessLogic.fetchDerivedFromGenericType(service)).thenReturn(Either.left(genericService));
         Either<Service, ResponseFormat> createResponse = bl.createService(service, user);
 
@@ -162,36 +210,37 @@
         }
         assertEqualsServiceObject(createServiceObject(true), createResponse.left().value());
     }
-
-    private void validateUserRoles(Role ... roles) {
-        List<Role> listOfRoles = Stream.of(roles).collect(Collectors.toList());
+    private void assertEqualsServiceObject(final Service expectedService, final Service actualService) {
+        assertEquals(expectedService.getContactId(), actualService.getContactId());
+        assertEquals(expectedService.getCategories(), actualService.getCategories());
+        assertEquals(expectedService.getCreatorUserId(), actualService.getCreatorUserId());
+        assertEquals(expectedService.getCreatorFullName(), actualService.getCreatorFullName());
+        assertEquals(expectedService.getDescription(), actualService.getDescription());
+        assertEquals(expectedService.getIcon(), actualService.getIcon());
+        assertEquals(expectedService.getLastUpdaterUserId(), actualService.getLastUpdaterUserId());
+        assertEquals(expectedService.getLastUpdaterFullName(), actualService.getLastUpdaterFullName());
+        assertEquals(expectedService.getName(), actualService.getName());
+        assertEquals(expectedService.getUniqueId(), actualService.getUniqueId());
+        assertEquals(expectedService.getVersion(), actualService.getVersion());
+        assertEquals(expectedService.getArtifacts(), actualService.getArtifacts());
+        assertEquals(expectedService.getCreationDate(), actualService.getCreationDate());
+        assertEquals(expectedService.getLastUpdateDate(), actualService.getLastUpdateDate());
+        assertEquals(expectedService.getLifecycleState(), actualService.getLifecycleState());
+        assertEquals(expectedService.getTags(), actualService.getTags());
+        if (expectedService.getProperties() == null) {
+            org.hamcrest.MatcherAssert.assertThat("Service properties should be null",
+                actualService.getProperties(), is(nullValue()));
+            return;
+        }
+        org.hamcrest.MatcherAssert.assertThat("Service properties should be as expected",
+            actualService.getProperties(), is(expectedService.getProperties()));
     }
 
-    private void assertEqualsServiceObject(Service origService, Service newService) {
-        assertEquals(origService.getContactId(), newService.getContactId());
-        assertEquals(origService.getCategories(), newService.getCategories());
-        assertEquals(origService.getCreatorUserId(), newService.getCreatorUserId());
-        assertEquals(origService.getCreatorFullName(), newService.getCreatorFullName());
-        assertEquals(origService.getDescription(), newService.getDescription());
-        assertEquals(origService.getIcon(), newService.getIcon());
-        assertEquals(origService.getLastUpdaterUserId(), newService.getLastUpdaterUserId());
-        assertEquals(origService.getLastUpdaterFullName(), newService.getLastUpdaterFullName());
-        assertEquals(origService.getName(), newService.getName());
-        assertEquals(origService.getName(), newService.getName());
-        assertEquals(origService.getUniqueId(), newService.getUniqueId());
-        assertEquals(origService.getVersion(), newService.getVersion());
-        assertEquals(origService.getArtifacts(), newService.getArtifacts());
-        assertEquals(origService.getCreationDate(), newService.getCreationDate());
-        assertEquals(origService.getLastUpdateDate(), newService.getLastUpdateDate());
-        assertEquals(origService.getLifecycleState(), newService.getLifecycleState());
-        assertEquals(origService.getTags(), newService.getTags());
-    }
 
 
     /* CREATE validations - start ***********************/
     // Service name - start
 
-
     @Test
     public void testFailedServiceValidations() {
 
@@ -229,7 +278,6 @@
         List<String> tgs = new ArrayList<>();
         tgs.add(serviceName);
         serviceExccedsNameLimit.setTags(tgs);
-        validateUserRoles(Role.ADMIN, Role.DESIGNER);
         try {
             bl.createService(serviceExccedsNameLimit, user);
         } catch (ComponentException exp) {
@@ -267,6 +315,7 @@
 
     // Service name - end
     // Service description - start
+
     private void testServiceDescriptionEmpty() {
         Service serviceExist = createServiceObject(false);
         serviceExist.setDescription("");
@@ -278,7 +327,6 @@
         }
         fail();
     }
-
     private void testServiceDescriptionMissing() {
         Service serviceExist = createServiceObject(false);
         serviceExist.setDescription(null);
@@ -329,6 +377,7 @@
 
     // Service description - stop
     // Service icon - start
+
     private void testServiceIconEmpty() {
         Service serviceExist = createServiceObject(false);
         serviceExist.setIcon("");
@@ -336,7 +385,6 @@
         assertThat(service.left().value().getIcon()).isEqualTo(DEFAULT_ICON);
 
     }
-
     private void testServiceIconMissing() {
         Service serviceExist = createServiceObject(false);
         serviceExist.setIcon(null);
@@ -401,6 +449,7 @@
 
     // Service tags - stop
     // Service contactId - start
+
     private void testContactIdTooLong() {
         Service serviceContactId = createServiceObject(false);
         // 59 chars instead of 50
@@ -414,7 +463,6 @@
         }
         fail();
     }
-
     private void testContactIdWrongFormatCreate() {
         Service serviceContactId = createServiceObject(false);
         // 3 letters and 3 digits and special characters
@@ -443,6 +491,7 @@
 
     // Service contactId - stop
     // Service category - start
+
     private void testServiceCategoryExist() {
         Service serviceExist = createServiceObject(false);
         serviceExist.setCategories(null);
@@ -454,7 +503,6 @@
         }
         fail();
     }
-
     @Test
     public void markDistributionAsDeployedTestAlreadyDeployed() {
         String notifyAction = "DNotify";
@@ -538,6 +586,7 @@
 
     // Service category - stop
     // Service projectCode - start
+
     private void testInvalidProjectCode() {
 
         Service serviceExist = createServiceObject(false);
@@ -552,7 +601,6 @@
         fail();
     }
 
-
     private void testProjectCodeTooLong() {
 
         Service serviceExist = createServiceObject(false);
@@ -695,7 +743,6 @@
     @Test
     public void testDerivedFromGeneric() {
         Service service = createServiceObject(true);
-        validateUserRoles(Role.ADMIN, Role.DESIGNER);
         when(toscaOperationFacade.createToscaComponent(service)).thenReturn(Either.left(service));
         when(genericTypeBusinessLogic.fetchDerivedFromGenericType(service)).thenReturn(Either.left(genericService));
         Either<Service, ResponseFormat> createResponse = bl.createService(service, user);
@@ -947,4 +994,29 @@
         Assert.assertEquals(HttpStatus.NOT_FOUND.value(), operationEither.right().value().getStatus().intValue());
     }
 
+    private Resource mockGenericTypeResource() {
+        final Resource genericTypeResource = new Resource();
+        genericTypeResource.setProperties(mockPropertyList());
+        return genericTypeResource;
+    }
+
+    private List<PropertyDefinition> mockPropertyList() {
+        final List<PropertyDefinition> propertyList = new ArrayList<>();
+        final PropertyDefinition propertyDefinition1 = new PropertyDefinition();
+        propertyDefinition1.setName("property1");
+        propertyDefinition1.setType("string");
+        propertyList.add(propertyDefinition1);
+
+        final PropertyDefinition propertyDefinition2 = new PropertyDefinition();
+        propertyDefinition2.setName("property2");
+        propertyDefinition2.setType("boolean");
+        propertyList.add(propertyDefinition2);
+
+        final PropertyDefinition propertyDefinition3 = new PropertyDefinition();
+        propertyDefinition3.setName("property3");
+        propertyDefinition3.setType("string");
+        propertyList.add(propertyDefinition3);
+        return propertyList;
+    }
+
 }