fix delete vfmodule

corrected the vnfc customization id that is passed down
add junit for getRelatedResourcesInVnfc method

Change-Id: I943952a260191a49c8fc1c0813b1e111448f637b
Issue-ID: SO-1759
Signed-off-by: Benjamin, Max (mb388a) <mb388a@us.att.com>
diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowAction.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowAction.java
index 867be2e..7562bd9 100644
--- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowAction.java
+++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowAction.java
@@ -372,6 +372,31 @@
 		return vnfcs;
 	}
 	
+	protected <T> List<T> getRelatedResourcesInVnfc(Vnfc vnfc, Class<T> resultClass, AAIObjectType type) {
+		
+		List<T> configurations = new ArrayList<>();
+		AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VNFC, vnfc.getVnfcName());
+		AAIResultWrapper vnfcResultsWrapper = bbInputSetupUtils.getAAIResourceDepthOne(uri);
+		Optional<Relationships> relationshipsOp = vnfcResultsWrapper.getRelationships();
+		if (!relationshipsOp.isPresent()) {
+			logger.debug("No relationships were found for VNFC in AAI");
+		} else {
+			Relationships relationships = relationshipsOp.get();
+			List<AAIResultWrapper> configurationResultWrappers = this.getResultWrappersFromRelationships(relationships, type);
+			for(AAIResultWrapper configurationResultWrapper : configurationResultWrappers) {
+				Optional<T> configurationOp = configurationResultWrapper.asBean(resultClass);
+				if(configurationOp.isPresent()) {
+					configurations.add(configurationOp.get());
+				}
+			}
+		}
+		return configurations;
+	}
+	
+	protected List<AAIResultWrapper> getResultWrappersFromRelationships(Relationships relationships, AAIObjectType type){
+		return relationships.getByType(type);
+	}
+	
 	protected boolean isConfiguration(List<OrchestrationFlow> orchFlows) {
 		for(OrchestrationFlow flow : orchFlows) {
 			if(flow.getFlowName().contains("Configuration")) {
@@ -384,6 +409,7 @@
 	protected List<ExecuteBuildingBlock> getConfigBuildingBlocks(ServiceInstancesRequest sIRequest, List<OrchestrationFlow> orchFlows, String requestId, Resource resourceKey,
 			String apiVersion, String resourceId, String requestAction, boolean aLaCarte, String vnfType,
 			WorkflowResourceIds workflowResourceIds, RequestDetails requestDetails, DelegateExecution execution) {
+		
 		List<ExecuteBuildingBlock> flowsToExecuteConfigs = new ArrayList<>();
 		List<OrchestrationFlow> result = new ArrayList<>(orchFlows);
 		result = orchFlows.stream().filter(item -> item.getFlowName().contains(FABRIC_CONFIGURATION)).collect(Collectors.toList());
@@ -392,22 +418,29 @@
 		
 		String vnfCustomizationUUID = bbInputSetupUtils.getAAIGenericVnf(vnfId).getModelCustomizationId();
 		String vfModuleCustomizationUUID = bbInputSetupUtils.getAAIVfModule(vnfId, vfModuleId).getModelCustomizationId();
-		List<org.onap.aai.domain.yang.Configuration> configurations = getRelatedResourcesInVfModule(vnfId, vfModuleId, org.onap.aai.domain.yang.Configuration.class, AAIObjectType.CONFIGURATION);
-		
-		for(org.onap.aai.domain.yang.Configuration configuration : configurations) {
-			workflowResourceIds.setConfigurationId(configuration.getConfigurationId());
-			for(OrchestrationFlow orchFlow : result) {
-				resourceKey.setVfModuleCustomizationId(vfModuleCustomizationUUID);
-				resourceKey.setCvnfModuleCustomizationId(configuration.getModelCustomizationId());
-				resourceKey.setVnfCustomizationId(vnfCustomizationUUID);
-				ExecuteBuildingBlock ebb = buildExecuteBuildingBlock(orchFlow, requestId, resourceKey, apiVersion, resourceId,
-						requestAction, aLaCarte, vnfType, workflowResourceIds, requestDetails, false, null, true);
-				String vnfcName = getVnfcNameForConfiguration(configuration);
-				if(vnfcName == null || vnfcName.isEmpty()) {
-					buildAndThrowException(execution, "Exception in create execution list " + ": VnfcName does not exist or is null while there is a configuration for the vfModule", new Exception("Vnfc and Configuration do not match"));
+
+		List<org.onap.aai.domain.yang.Vnfc> vnfcs = getRelatedResourcesInVfModule(vnfId, vfModuleId, org.onap.aai.domain.yang.Vnfc.class, AAIObjectType.VNFC);
+		for(org.onap.aai.domain.yang.Vnfc vnfc : vnfcs) {
+			List<org.onap.aai.domain.yang.Configuration> configurations = getRelatedResourcesInVnfc(vnfc, org.onap.aai.domain.yang.Configuration.class, AAIObjectType.CONFIGURATION);
+			if (configurations.size() > 1){
+				String multipleRelationshipsError = "Multiple relationships exist from VNFC "+vnfc.getVnfcName()+" to Configurations";
+				buildAndThrowException(execution, multipleRelationshipsError, new Exception(multipleRelationshipsError));
+			}
+			for(org.onap.aai.domain.yang.Configuration configuration : configurations) {
+				workflowResourceIds.setConfigurationId(configuration.getConfigurationId());
+				for(OrchestrationFlow orchFlow : result) {
+					resourceKey.setVfModuleCustomizationId(vfModuleCustomizationUUID);
+					resourceKey.setCvnfModuleCustomizationId(vnfc.getModelCustomizationId());
+					resourceKey.setVnfCustomizationId(vnfCustomizationUUID);
+					ExecuteBuildingBlock ebb = buildExecuteBuildingBlock(orchFlow, requestId, resourceKey, apiVersion, resourceId,
+							requestAction, aLaCarte, vnfType, workflowResourceIds, requestDetails, false, null, true);
+					String vnfcName = getVnfcNameForConfiguration(configuration);
+					if(vnfcName == null || vnfcName.isEmpty()) {
+						buildAndThrowException(execution, "Exception in create execution list " + ": VnfcName does not exist or is null while there is a configuration for the vfModule", new Exception("Vnfc and Configuration do not match"));
+					}
+					ebb.getConfigurationResourceKeys().setVnfcName(vnfcName);
+					flowsToExecuteConfigs.add(ebb);
 				}
-				ebb.getConfigurationResourceKeys().setVnfcName(vnfcName);
-				flowsToExecuteConfigs.add(ebb);
 			}
 		}
 		return flowsToExecuteConfigs;
diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowActionTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowActionTest.java
index 0bc20c1..87e6daa 100644
--- a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowActionTest.java
+++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowActionTest.java
@@ -53,6 +53,7 @@
 import org.junit.rules.ExpectedException;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
+import org.mockito.Spy;
 import org.onap.aai.domain.yang.GenericVnf;
 import org.onap.aai.domain.yang.L3Network;
 import org.onap.aai.domain.yang.Relationship;
@@ -60,6 +61,7 @@
 import org.onap.aai.domain.yang.ServiceInstance;
 import org.onap.aai.domain.yang.VfModule;
 import org.onap.aai.domain.yang.VfModules;
+import org.onap.aai.domain.yang.Vnfc;
 import org.onap.aai.domain.yang.VolumeGroup;
 import org.onap.so.bpmn.BaseTaskTest;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.Collection;
@@ -67,7 +69,11 @@
 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
 import org.onap.so.bpmn.servicedecomposition.entities.WorkflowResourceIds;
+import org.onap.so.client.aai.AAIObjectType;
+import org.onap.so.client.aai.entities.AAIResultWrapper;
 import org.onap.so.client.aai.entities.Relationships;
+import org.onap.so.client.aai.entities.uri.AAIResourceUri;
+import org.onap.so.client.aai.entities.uri.AAIUriFactory;
 import org.onap.so.db.catalog.beans.CollectionNetworkResourceCustomization;
 import org.onap.so.db.catalog.beans.CollectionResource;
 import org.onap.so.db.catalog.beans.CollectionResourceCustomization;
@@ -100,9 +106,15 @@
 	private DelegateExecution execution;
 
 	
+	@InjectMocks
+	@Spy
+	protected WorkflowAction SPY_workflowAction;
+	
 	@Rule
 	public ExpectedException thrown = ExpectedException.none();
 	
+	private String RESOURCE_PATH = "src/test/resources/__files/";
+	
 	@Before
 	public void before() throws Exception {
 		execution = new DelegateExecutionFake();
@@ -930,6 +942,123 @@
 		assertEqualsBulkFlowName(ebbs,"AssignVfModuleBB","CreateVfModuleBB","ActivateVfModuleBB","AssignFabricConfigurationBB","ActivateFabricConfigurationBB", "AssignFabricConfigurationBB","ActivateFabricConfigurationBB");
 	}
 	
+	@Test
+	public void selectExecutionListALaCarteVfModuleFabricDeleteTest() throws Exception{
+		String gAction = "deleteInstance";
+		String resource = "VfModule";
+		execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
+		execution.setVariable("requestAction", gAction);
+		String bpmnRequest = new String(Files.readAllBytes(Paths.get("src/test/resources/__files/VfModuleCreateWithFabric.json")));
+		execution.setVariable("bpmnRequest", bpmnRequest);		
+		execution.setVariable("aLaCarte", true);
+		execution.setVariable("apiVersion", "7");
+		execution.setVariable("requestUri", "v7/serviceInstances/f647e3ef-6d2e-4cd3-bff4-8df4634208de/vnfs/b80b16a5-f80d-4ffa-91c8-bd47c7438a3d/vfModules");
+		
+		NorthBoundRequest northBoundRequest = new NorthBoundRequest();
+		List<OrchestrationFlow> orchFlows = createFlowList("DeactivateVfModuleBB","DeleteVfModuleBB","UnassignVfModuleBB","DeactivateFabricConfigurationBB","UnassignFabricConfigurationBB");
+		northBoundRequest.setOrchestrationFlowList(orchFlows);
+		
+		when(catalogDbClient.getNorthBoundRequestByActionAndIsALaCarteAndRequestScopeAndCloudOwner(gAction,resource,true,"my-custom-cloud-owner")).thenReturn(northBoundRequest);
+		
+		org.onap.aai.domain.yang.GenericVnf vnf = new org.onap.aai.domain.yang.GenericVnf();
+		vnf.setVnfId("vnf0");
+		vnf.setModelCustomizationId("modelCustomizationId");
+		when(bbSetupUtils.getAAIGenericVnf(anyObject())).thenReturn(vnf);
+		
+		org.onap.aai.domain.yang.VfModule vfModule = new org.onap.aai.domain.yang.VfModule();
+		vfModule.setModelCustomizationId("modelCustomizationId");
+		when(bbSetupUtils.getAAIVfModule(anyObject(), anyObject())).thenReturn(vfModule);
+		
+		List<org.onap.aai.domain.yang.Vnfc> vnfcs = new ArrayList<org.onap.aai.domain.yang.Vnfc>();
+		org.onap.aai.domain.yang.Vnfc vnfc = new org.onap.aai.domain.yang.Vnfc();
+		vnfc.setModelInvariantId("modelInvariantId");
+		vnfc.setVnfcName("testVnfcName");
+		vnfcs.add(vnfc);
+		doReturn(vnfcs).when(SPY_workflowAction).getRelatedResourcesInVfModule(anyObject(), anyObject(), anyObject(), anyObject());
+		
+		List<org.onap.aai.domain.yang.Configuration> configurations = new ArrayList<org.onap.aai.domain.yang.Configuration>();
+		org.onap.aai.domain.yang.Configuration configuration = new org.onap.aai.domain.yang.Configuration();
+		configuration.setConfigurationId("configurationId");
+		configuration.setModelCustomizationId("modelCustimizationId");
+		configuration.setConfigurationName("testConfigurationName");
+		configurations.add(configuration);
+		doReturn(configurations).when(SPY_workflowAction).getRelatedResourcesInVnfc(anyObject(), anyObject(), anyObject());
+		
+		doReturn("testName").when(SPY_workflowAction).getVnfcNameForConfiguration(anyObject());
+		
+		SPY_workflowAction.selectExecutionList(execution);
+		List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
+		assertEqualsBulkFlowName(ebbs,"DeactivateFabricConfigurationBB","UnassignFabricConfigurationBB","DeactivateVfModuleBB","DeleteVfModuleBB","UnassignVfModuleBB");
+	}
+	
+	@Test
+	public void selectExecutionListALaCarteVfModuleNoFabricDeleteTest() throws Exception{
+		String gAction = "deleteInstance";
+		String resource = "VfModule";
+		execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
+		execution.setVariable("requestAction", gAction);
+		String bpmnRequest = new String(Files.readAllBytes(Paths.get("src/test/resources/__files/VfModuleCreateWithFabric.json")));
+		execution.setVariable("bpmnRequest", bpmnRequest);		
+		execution.setVariable("aLaCarte", true);
+		execution.setVariable("apiVersion", "7");
+		execution.setVariable("requestUri", "v7/serviceInstances/f647e3ef-6d2e-4cd3-bff4-8df4634208de/vnfs/b80b16a5-f80d-4ffa-91c8-bd47c7438a3d/vfModules");
+		
+		NorthBoundRequest northBoundRequest = new NorthBoundRequest();
+		List<OrchestrationFlow> orchFlows = createFlowList("DeactivateVfModuleBB","DeleteVfModuleBB","UnassignVfModuleBB","DeactivateFabricConfigurationBB","UnassignFabricConfigurationBB");
+		northBoundRequest.setOrchestrationFlowList(orchFlows);
+		
+		when(catalogDbClient.getNorthBoundRequestByActionAndIsALaCarteAndRequestScopeAndCloudOwner(gAction,resource,true,"my-custom-cloud-owner")).thenReturn(northBoundRequest);
+		
+		org.onap.aai.domain.yang.GenericVnf vnf = new org.onap.aai.domain.yang.GenericVnf();
+		vnf.setVnfId("vnf0");
+		vnf.setModelCustomizationId("modelCustomizationId");
+		when(bbSetupUtils.getAAIGenericVnf(anyObject())).thenReturn(vnf);
+		
+		org.onap.aai.domain.yang.VfModule vfModule = new org.onap.aai.domain.yang.VfModule();
+		vfModule.setModelCustomizationId("modelCustomizationId");
+		when(bbSetupUtils.getAAIVfModule(anyObject(), anyObject())).thenReturn(vfModule);
+		
+		List<org.onap.aai.domain.yang.Vnfc> vnfcs = new ArrayList<org.onap.aai.domain.yang.Vnfc>();
+		org.onap.aai.domain.yang.Vnfc vnfc = new org.onap.aai.domain.yang.Vnfc();
+		vnfc.setModelInvariantId("modelInvariantId");
+		vnfc.setVnfcName("testVnfcName");
+		vnfcs.add(vnfc);
+		doReturn(vnfcs).when(SPY_workflowAction).getRelatedResourcesInVfModule(anyObject(), anyObject(), anyObject(), anyObject());
+		
+		List<org.onap.aai.domain.yang.Configuration> configurations = new ArrayList<org.onap.aai.domain.yang.Configuration>();
+		org.onap.aai.domain.yang.Configuration configuration = new org.onap.aai.domain.yang.Configuration();
+		doReturn(configurations).when(SPY_workflowAction).getRelatedResourcesInVnfc(anyObject(), anyObject(), anyObject());
+		
+		doReturn("testName").when(SPY_workflowAction).getVnfcNameForConfiguration(anyObject());
+		
+		SPY_workflowAction.selectExecutionList(execution);
+		List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
+		assertEqualsBulkFlowName(ebbs,"DeactivateVfModuleBB","DeleteVfModuleBB","UnassignVfModuleBB");
+	}
+	
+	@Test
+	public void getRelatedResourcesInVfModuleTest() throws Exception{
+		org.onap.aai.domain.yang.Vnfc vnfc = new org.onap.aai.domain.yang.Vnfc();
+		vnfc.setModelInvariantId("modelInvariantId");
+		vnfc.setVnfcName("testVnfcName");
+		
+		String vfncPayload = new String(Files.readAllBytes(Paths.get(RESOURCE_PATH + "/BuildingBlocks/vnfcResponse.json")));
+		AAIResultWrapper vfncWrapper = new AAIResultWrapper(vfncPayload);
+		
+		String configurationPayload = new String(Files.readAllBytes(Paths.get(RESOURCE_PATH + "/BuildingBlocks/configurationResponse.json")));
+		AAIResultWrapper configurationWrapper = new AAIResultWrapper(configurationPayload);
+		List<AAIResultWrapper> configurationResultWrappers = new ArrayList<AAIResultWrapper>();
+		configurationResultWrappers.add(configurationWrapper);
+		
+		AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VNFC, vnfc.getVnfcName());
+		when(bbSetupUtils.getAAIResourceDepthOne(uri)).thenReturn(vfncWrapper);
+		
+		doReturn(configurationResultWrappers).when(SPY_workflowAction).getResultWrappersFromRelationships(anyObject(),anyObject());
+		List<org.onap.aai.domain.yang.Configuration> configurationsList = SPY_workflowAction.getRelatedResourcesInVnfc(vnfc, org.onap.aai.domain.yang.Configuration.class, AAIObjectType.CONFIGURATION);
+		assertEquals(1,configurationsList.size());
+		assertEquals("testConfigurationId",configurationsList.get(0).getConfigurationId());
+	}
+	
 	/**
 	 * WorkflowActionBB Tests
 	 */
diff --git a/bpmn/so-bpmn-tasks/src/test/resources/__files/BuildingBlocks/configurationResponse.json b/bpmn/so-bpmn-tasks/src/test/resources/__files/BuildingBlocks/configurationResponse.json
new file mode 100644
index 0000000..c8420a5
--- /dev/null
+++ b/bpmn/so-bpmn-tasks/src/test/resources/__files/BuildingBlocks/configurationResponse.json
@@ -0,0 +1,8 @@
+{
+	"configuration-id": "testConfigurationId",
+	"configuration-type": "...",
+	"orchestration-status": "...",
+	"tunnel-bandwidth": "100",
+	"vendor-allowed-max-bandwidth": "90",
+	"resource-version": "1234"
+}
\ No newline at end of file
diff --git a/bpmn/so-bpmn-tasks/src/test/resources/__files/BuildingBlocks/vnfcResponse.json b/bpmn/so-bpmn-tasks/src/test/resources/__files/BuildingBlocks/vnfcResponse.json
new file mode 100644
index 0000000..0b2d847
--- /dev/null
+++ b/bpmn/so-bpmn-tasks/src/test/resources/__files/BuildingBlocks/vnfcResponse.json
@@ -0,0 +1,29 @@
+{
+  "vnfc-name": "vnfcName",
+  "nfc-naming-code": "oamfw",
+  "nfc-function": "FIREWALL",
+  "prov-status": "PREPROV",
+  "orchestration-status": "Active",
+  "in-maint": false,
+  "is-closed-loop-disabled": false,
+  "model-invariant-id": "9dae8bb5-0709-4c0e-8f5b-6448232510ea",
+  "model-version-id": "b4e05ccb-01d0-4176-90ae-380308a35e5f",
+  "model-customization-id": "abf73d38-9da2-47af-bd53-26954318449f",
+  "resource-version": "1552319567126",
+   "relationship-list": {
+      "relationship": [
+        {
+          "related-to": "generic-vnf",
+          "related-link": "/aai/v11/network/generic-vnfs/generic-vnf/testGenericVnfId",
+          "relationship-data": {
+            "relationship-key": "generic-vnf",
+            "relationship-value": "4a9184ac-5fad-44a1-b224-289bb490eaa9"
+          },
+          "related-to-property": {
+            "property-key": "generic-vnf.vnf-name",
+            "property-value": "USBBBBmtwnjVVHP047"
+          }
+        }
+      ]
+    }
+}
\ No newline at end of file
diff --git a/mso-catalog-db/src/main/java/org/onap/so/db/catalog/client/CatalogDbClient.java b/mso-catalog-db/src/main/java/org/onap/so/db/catalog/client/CatalogDbClient.java
index df1c947..d010288 100644
--- a/mso-catalog-db/src/main/java/org/onap/so/db/catalog/client/CatalogDbClient.java
+++ b/mso-catalog-db/src/main/java/org/onap/so/db/catalog/client/CatalogDbClient.java
@@ -787,7 +787,7 @@
             logger.debug("Found CvnfcCustomization: {}", filtered.get(0));
             return filtered.get(0);
         }else
-            throw new EntityNotFoundException("Unable to find VfModuleCustomization ModelCustomizationUUID:" + cvnfcCustomizationUuid);
+            throw new EntityNotFoundException("Unable to find CvnfcCustomization ModelCustomizationUUID:" + cvnfcCustomizationUuid);
     }
     
     public CvnfcConfigurationCustomization getCvnfcCustomization(String serviceModelUUID, String vnfCustomizationUuid,