re base code
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / utils / MergeInstanceUtilsTest.java
index 9d07be2..ef87dba 100644 (file)
@@ -1,70 +1,71 @@
 package org.openecomp.sdc.be.components.merge.utils;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.when;
-
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-
+import fj.data.Either;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
+import org.openecomp.sdc.be.components.impl.utils.ExceptionUtils;
+import org.openecomp.sdc.be.components.utils.ComponentInstanceBuilder;
+import org.openecomp.sdc.be.components.utils.GroupDefinitionBuilder;
+import org.openecomp.sdc.be.components.utils.ResourceBuilder;
+import org.openecomp.sdc.be.components.utils.ServiceBuilder;
+import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
-import org.openecomp.sdc.be.model.Component;
-import org.openecomp.sdc.be.model.ComponentInstance;
-import org.openecomp.sdc.be.model.Resource;
-import org.openecomp.sdc.be.model.Service;
+import org.openecomp.sdc.be.model.*;
 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
+import org.openecomp.sdc.be.model.operations.StorageException;
 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
 
-import fj.data.Either;
+import java.util.Map;
+
+import static java.util.Arrays.asList;
+import static java.util.Collections.emptyList;
+import static java.util.Collections.singletonList;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 @RunWith(MockitoJUnitRunner.class)
 public class MergeInstanceUtilsTest {
-    private MergeInstanceUtils mergeInstanceUtils = new MergeInstanceUtils();
+
+    private MergeInstanceUtils mergeInstanceUtils;
 
     @Mock
     private ToscaOperationFacade toscaOperationFacade;
 
+    @Mock
+    private TitanDao titanDao;
+
     @Before
     public void startUp() {
-        mergeInstanceUtils.setToscaOperationFacade(toscaOperationFacade);
+        ExceptionUtils exceptionUtils = new ExceptionUtils(titanDao);
+        mergeInstanceUtils = new MergeInstanceUtils(toscaOperationFacade, exceptionUtils);
     }
 
     @Test
     public void testMapOldToNewCapabilitiesOwnerIdsComponentComponent() {
         Component container = new Service();
 
-        Resource vfOld = new Resource();
-        vfOld.setComponentType(ComponentTypeEnum.RESOURCE);
-        vfOld.setResourceType(ResourceTypeEnum.VF);
-
-        ComponentInstance vfci1 = createComponentInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1" ,"SRV1.VF1.VFI_1.VFC1", false);
-        createVfc(vfci1);
-        List<ComponentInstance> vfciList = Arrays.asList(vfci1);
-        vfOld.setComponentInstances(vfciList);
-
+        ComponentInstance vfciOld = createVfcInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF1.VFI_1.VFC1");
+        GroupDefinition oldGrp = createGroup("group1", "grp1Id");
+        Component vfOld = createVf("prevVfId", vfciOld, oldGrp);
         ComponentInstance vfi1 = createComponentInstance("SRV1.VF2.VFI_1" ,"SRV1.VF2", false);
-        container.setComponentInstances(Arrays.asList(vfi1));
-
-        Resource vfNew = new Resource();
-        vfNew.setComponentType(ComponentTypeEnum.RESOURCE);
-        vfNew.setResourceType(ResourceTypeEnum.VF);
-
-        List<ComponentInstance> vfciList2 = Arrays.asList(vfci1);
-        vfNew.setComponentInstances(vfciList2);
+        container.setComponentInstances(singletonList(vfi1));
 
-        Either<Component, StorageOperationStatus> eitherComponent = Either.left(vfNew);
-        when(toscaOperationFacade.getToscaElement(vfi1.getComponentUid())).thenReturn(eitherComponent);
+        ComponentInstance vfciNew = createComponentInstance("SRV1.VF1.VFI_1.VFC1.VFCI_2", "SRV1.VF1.VFI_1.VFC1", false);
+        GroupDefinition newGrp = createGroup("group1", "newGrp1Id");
+        Component vfNew = createVf("newVfId", vfciNew, newGrp);
+        when(toscaOperationFacade.getToscaElement(vfi1.getComponentUid())).thenReturn(Either.left(vfNew));
 
-
-        Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(container, vfOld, "SRV1.VF2.VFI_1", Arrays.asList("SRV1.VF1.VFI_1.VFC1.VFCI_1"));
-        assertEquals("SRV1.VF1.VFI_1.VFC1.VFCI_1", mapResult.get("SRV1.VF1.VFI_1.VFC1.VFCI_1"));
+        Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(container, vfOld, "SRV1.VF2.VFI_1", asList("SRV1.VF1.VFI_1.VFC1.VFCI_1", oldGrp.getUniqueId()));
+        assertThat(mapResult)
+                .containsEntry("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF1.VFI_1.VFC1.VFCI_2")
+                .containsEntry(oldGrp.getUniqueId(), newGrp.getUniqueId());
     }
 
     @Test
@@ -73,40 +74,21 @@ public class MergeInstanceUtilsTest {
         container.setComponentType(ComponentTypeEnum.RESOURCE);
         container.setResourceType(ResourceTypeEnum.VF);
 
-
         Component serviceOld = new Service();
         serviceOld.setComponentType(ComponentTypeEnum.SERVICE);
 
-        Resource vfOld = new Resource();
-        vfOld.setComponentType(ComponentTypeEnum.RESOURCE);
-        vfOld.setResourceType(ResourceTypeEnum.VF);
-
-        ComponentInstance vfciOld = createComponentInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1" ,"SRV1.VF1.VFI_1.VFC1", false);
-        createVfc(vfciOld);
-        List<ComponentInstance> vfciList = Arrays.asList(vfciOld);
-        vfOld.setComponentInstances(vfciList);
-
-        ComponentInstance vfiOld = createComponentInstance("SRV1.VF1.VFI_1" ,"SRV1.VF1", false);
-        serviceOld.setComponentInstances(Arrays.asList(vfiOld));
-        Either<Component, StorageOperationStatus> eitherComponent = Either.left(vfOld);
-        when(toscaOperationFacade.getToscaElement(vfiOld.getComponentUid())).thenReturn(eitherComponent);
+        ComponentInstance vfciOld = createVfcInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF1.VFI_1.VFC1");
+        GroupDefinition prevGroup = createGroup("grp1", "grp1Old");
+        ComponentInstance vfiOld = createVfInstance("SRV1.VF1.VFI_1", vfciOld, prevGroup);
+        serviceOld.setComponentInstances(singletonList(vfiOld));
 
         Component serviceNew = new Service();
         serviceNew.setComponentType(ComponentTypeEnum.SERVICE);
 
-        Resource vfNew = new Resource();
-        vfNew.setComponentType(ComponentTypeEnum.RESOURCE);
-        vfNew.setResourceType(ResourceTypeEnum.VF);
-
-        ComponentInstance vfciNew = createComponentInstance("SRV1.VF2.VFI_1.VFC2.VFCI_1" ,"SRV1.VF2.VFI_1.VFC2", false);
-        createVfc(vfciNew);
-        List<ComponentInstance> vfciList2 = Arrays.asList(vfciNew);
-        vfNew.setComponentInstances(vfciList2);
-
-        ComponentInstance vfiNew = createComponentInstance("SRV1.VF2.VFI_1" ,"SRV1.VF2", false);
-        serviceNew.setComponentInstances(Arrays.asList(vfiNew));
-        Either<Component, StorageOperationStatus> eitherComponent3 = Either.left(vfNew);
-        when(toscaOperationFacade.getToscaElement(vfiNew.getComponentUid())).thenReturn(eitherComponent3);
+        ComponentInstance vfciNew = createVfcInstance("SRV1.VF2.VFI_1.VFC2.VFCI_1", "SRV1.VF2.VFI_1.VFC2");
+        GroupDefinition grpNew = createGroup("grp1", "newGrp1");
+        ComponentInstance vfiNew = createVfInstance("SRV1.VF2.VFI_1" ,vfciNew, grpNew);
+        serviceNew.setComponentInstances(singletonList(vfiNew));
 
         ComponentInstance proxyVfciNew = createComponentInstance("SRV1.PROXY_VFC_NEW.VFCI1", "SRV1.PROXY_VFC_NEW", true);
         proxyVfciNew.setSourceModelUid("SRV_PROXY_NEW");
@@ -117,44 +99,66 @@ public class MergeInstanceUtilsTest {
         Either<Component, StorageOperationStatus> eitherComponent4 = Either.left(serviceNew);
         when(toscaOperationFacade.getToscaElement(proxyVfciNew.getSourceModelUid())).thenReturn(eitherComponent4);
 
-        container.setComponentInstances(Arrays.asList(proxyVfciNew));
+        container.setComponentInstances(singletonList(proxyVfciNew));
 
-        Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(container, serviceOld, "SRV1.PROXY_VFC_NEW.VFCI1", Arrays.asList("SRV1.VF1.VFI_1.VFC1.VFCI_1"));
+        Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(container, serviceOld, "SRV1.PROXY_VFC_NEW.VFCI1", asList("SRV1.VF1.VFI_1.VFC1.VFCI_1", prevGroup.getUniqueId()));
+        assertThat(mapResult)
+                .containsEntry("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF2.VFI_1.VFC2.VFCI_1")
+                .containsEntry(prevGroup.getUniqueId(), grpNew.getUniqueId());
+    }
 
-        assertEquals("SRV1.VF2.VFI_1.VFC2.VFCI_1", mapResult.get("SRV1.VF1.VFI_1.VFC1.VFCI_1"));
+    @Test
+    public void whenFailingToGetInstanceOriginNodeType_throwExceptionAndRollBack() {
+        Resource oldVf = new ResourceBuilder()
+                .setResourceType(ResourceTypeEnum.VF)
+                .setComponentType(ComponentTypeEnum.RESOURCE)
+                .build();
+
+        ComponentInstance newVfInstance = createComponentInstance("inst1", "inst1Uid", false);
+        Resource container = new ResourceBuilder()
+                .addComponentInstance(newVfInstance)
+                .build();
+        when(toscaOperationFacade.getToscaElement("inst1Uid")).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
+        assertThatExceptionOfType(StorageException.class)
+                .isThrownBy(() -> mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(container, oldVf, "inst1", emptyList()));
+        verify(titanDao).rollback();
     }
 
     @Test
     public void testMapOldToNewCapabilitiesOwnerIdsComponentInstanceComponentInstance() {
-        ComponentInstance oldInstance = createComponentInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1" ,"SRV1.VF1.VFI_1.VFC1", false);
-        createVfc(oldInstance);
-
-        ComponentInstance newInstance = createComponentInstance("SRV1.VF1.VFI_1.VFC2.VFCI_1" ,"SRV1.VF1.VFI_1.VFC2", false);
-        createVfc(newInstance);
-
+        ComponentInstance oldInstance = createVfcInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF1.VFI_1.VFC1");
+        ComponentInstance newInstance = createVfcInstance("SRV1.VF1.VFI_1.VFC2.VFCI_1", "SRV1.VF1.VFI_1.VFC2");
         Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(oldInstance, newInstance);
         assertEquals("SRV1.VF1.VFI_1.VFC2.VFCI_1", mapResult.get("SRV1.VF1.VFI_1.VFC1.VFCI_1"));
     }
 
     @Test
-    public void testMapOldToNewCapabilitiesOwnerIdsComponentInstComponentInst_Proxy() {
+    public void testMapOldToNewCapabilitiesOwnerIdsInstToInstWithGroups() {
+        ComponentInstance prevInstance = createVfcInstance("prevInst1", "prevInst1Uid");
+        GroupDefinition prevGroup = createGroup("grp1", "prevGrp1");
+        ComponentInstance prevInstanceRoot = createVfInstance("prevId", prevInstance, prevGroup);
+
+        ComponentInstance currInstance = createVfcInstance("newInst1", "newInst1Uid");
+        GroupDefinition currGroup = createGroup("grp1", "currGrp1");
+        ComponentInstance newInstanceRoot = createVfInstance("currId", currInstance, currGroup);
+
+        Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(prevInstanceRoot, newInstanceRoot);
+        assertThat(mapResult)
+                .containsEntry(prevInstance.getUniqueId(), currInstance.getUniqueId())
+                .containsEntry(prevGroup.getUniqueId(), currGroup.getUniqueId());
+    }
 
-        Component serviceOld = new Service();
-        serviceOld.setComponentType(ComponentTypeEnum.SERVICE);
 
-        Resource vfOld = new Resource();
-        vfOld.setComponentType(ComponentTypeEnum.RESOURCE);
-        vfOld.setResourceType(ResourceTypeEnum.VF);
+    @Test
+    public void testMapOldToNewCapabilitiesOwnerIdsComponentInstComponentInst_Proxy() {
 
-        ComponentInstance vfciOld = createComponentInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1" ,"SRV1.VF1.VFI_1.VFC1", false);
-        createVfc(vfciOld);
-        List<ComponentInstance> vfciList = Arrays.asList(vfciOld);
-        vfOld.setComponentInstances(vfciList);
+        ComponentInstance vfciOld = createVfcInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF1.VFI_1.VFC1");
+        GroupDefinition oldGrp = createGroup("grp1", "grp1Old");
+        ComponentInstance vfiOld = createVfInstance("SRV1.VF1.VFI_1", vfciOld, oldGrp);
 
-        ComponentInstance vfiOld = createComponentInstance("SRV1.VF1.VFI_1" ,"SRV1.VF1", false);
-        serviceOld.setComponentInstances(Arrays.asList(vfiOld));
-        Either<Component, StorageOperationStatus> eitherComponent = Either.left(vfOld);
-        when(toscaOperationFacade.getToscaElement(vfiOld.getComponentUid())).thenReturn(eitherComponent);
+        Component serviceOld = new Service();
+        serviceOld.setComponentType(ComponentTypeEnum.SERVICE);
+        serviceOld.setComponentInstances(singletonList(vfiOld));
 
         ComponentInstance proxyVfciOld = createComponentInstance("SRV1.PROXY_VFC.VFCI1", "SRV1.PROXY_VFC", true);
         proxyVfciOld.setSourceModelUid("SRV_PROXY");
@@ -165,23 +169,13 @@ public class MergeInstanceUtilsTest {
         Either<Component, StorageOperationStatus> eitherComponent2 = Either.left(serviceOld);
         when(toscaOperationFacade.getToscaElement(proxyVfciOld.getSourceModelUid())).thenReturn(eitherComponent2);
 
+        ComponentInstance vfciNew = createVfcInstance("SRV1.VF2.VFI_1.VFC2.VFCI_1", "SRV1.VF2.VFI_1.VFC2");
+        GroupDefinition newGrp = createGroup("grp1", "grp1New");
+        ComponentInstance vfiNew = createVfInstance("SRV1.VF2.VFI_1" ,vfciNew, newGrp);
 
         Component serviceNew = new Service();
         serviceNew.setComponentType(ComponentTypeEnum.SERVICE);
-
-        Resource vfNew = new Resource();
-        vfNew.setComponentType(ComponentTypeEnum.RESOURCE);
-        vfNew.setResourceType(ResourceTypeEnum.VF);
-
-        ComponentInstance vfciNew = createComponentInstance("SRV1.VF2.VFI_1.VFC2.VFCI_1" ,"SRV1.VF2.VFI_1.VFC2", false);
-        createVfc(vfciNew);
-        List<ComponentInstance> vfciList2 = Arrays.asList(vfciNew);
-        vfNew.setComponentInstances(vfciList2);
-
-        ComponentInstance vfiNew = createComponentInstance("SRV1.VF2.VFI_1" ,"SRV1.VF2", false);
-        serviceNew.setComponentInstances(Arrays.asList(vfiNew));
-        Either<Component, StorageOperationStatus> eitherComponent3 = Either.left(vfNew);
-        when(toscaOperationFacade.getToscaElement(vfiNew.getComponentUid())).thenReturn(eitherComponent3);
+        serviceNew.setComponentInstances(singletonList(vfiNew));
 
         ComponentInstance proxyVfciNew = createComponentInstance("SRV1.PROXY_VFC_NEW.VFCI1", "SRV1.PROXY_VFC_NEW", true);
         proxyVfciNew.setSourceModelUid("SRV_PROXY_NEW");
@@ -193,132 +187,150 @@ public class MergeInstanceUtilsTest {
         when(toscaOperationFacade.getToscaElement(proxyVfciNew.getSourceModelUid())).thenReturn(eitherComponent4);
 
         Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(proxyVfciOld, proxyVfciNew);
-        assertEquals("SRV1.VF2.VFI_1.VFC2.VFCI_1", mapResult.get("SRV1.VF1.VFI_1.VFC1.VFCI_1"));
+        assertThat(mapResult)
+                .containsEntry("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF2.VFI_1.VFC2.VFCI_1")
+                .containsEntry(oldGrp.getUniqueId(), newGrp.getUniqueId());
     }
 
 
     @Test
-    public void testConvertToVfciNameMap() {
-        ComponentInstance componentInstance1 = new ComponentInstance();
-        componentInstance1.setName("ComponentInstance1");
-
-        ComponentInstance componentInstance2 = new ComponentInstance();
-        componentInstance2.setName("ComponentInstance2");
-
-        List<ComponentInstance> componentInstances = Arrays.asList(componentInstance1, componentInstance2);
-        Map<String, ComponentInstance> mapResult = mergeInstanceUtils.convertToVfciNameMap(componentInstances);
-
-        assertEquals(2, mapResult.size());
-        assertEquals(componentInstance1, mapResult.get("ComponentInstance1"));
-        assertEquals(componentInstance2, mapResult.get("ComponentInstance2"));
-    }
-
-    @Test
-    public void testGetVfcInstances_NullComponentInstance() {
-        List<ComponentInstance> vfcInstances = mergeInstanceUtils.getVfcInstances(null);
-
-        assertTrue(vfcInstances.isEmpty());
+    public void testGetInstanceAtomicBuildingBlocks_NullComponentInstance() {
+        assertEmpty(mergeInstanceUtils.getInstanceAtomicBuildingBlocks(null));
     }
 
     @Test
-    public void testGetVfcInstances_ComponentInstanceFailedLoadComponent() {
+    public void testgetInstanceAtomicBuildingBlocks_ComponentInstanceFailedLoadComponent() {
         ComponentInstance componentInstance = createComponentInstance("SRV1.VF1.VFI_1", "SRV1.VF1", false);
-
         Either<Component, StorageOperationStatus> eitherComponent = Either.right(StorageOperationStatus.NOT_FOUND);
         when(toscaOperationFacade.getToscaElement(componentInstance.getComponentUid())).thenReturn(eitherComponent);
-
-        List<ComponentInstance> vfcInstances = mergeInstanceUtils.getVfcInstances(componentInstance);
-
-        assertTrue(vfcInstances.isEmpty());
+        assertThatExceptionOfType(StorageException.class).isThrownBy(() -> mergeInstanceUtils.getInstanceAtomicBuildingBlocks(componentInstance));
     }
 
     @Test
-    public void testGetVfcInstances_ComponentInstanceFailedLoadActualComponent() {
+    public void testGetInstanceAtomicBuildingBlocks_ComponentInstanceFailedLoadActualComponent() {
         ComponentInstance componentInstance = createComponentInstance("SRV1.PROXY_VFC.VFCI1", "SRV1.PROXY_VFC", true);
         componentInstance.setSourceModelUid("SRV_PROXY");
-
         Either<Component, StorageOperationStatus> eitherComponent = Either.right(StorageOperationStatus.NOT_FOUND);
         when(toscaOperationFacade.getToscaElement(componentInstance.getSourceModelUid())).thenReturn(eitherComponent);
-
-        List<ComponentInstance> vfcInstances = mergeInstanceUtils.getVfcInstances(componentInstance);
-
-        assertTrue(vfcInstances.isEmpty());
+        assertThatExceptionOfType(StorageException.class).isThrownBy(() -> mergeInstanceUtils.getInstanceAtomicBuildingBlocks(componentInstance));
     }
 
 
     @Test
-    public void testGetVfcInstancesAtomicComponentInstance() {
-        ComponentInstance componentInstance = createComponentInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1" ,"SRV1.VF1.VFI_1.VFC1", false);
-
-        createVfc(componentInstance);
-
-        List<ComponentInstance> vfcInstances = mergeInstanceUtils.getVfcInstances(componentInstance);
-
-        assertEquals(1, vfcInstances.size());
-        assertEquals(componentInstance, vfcInstances.get(0));
+    public void testGetAtomicBuildingBlocks() {
+        ComponentInstance componentInstance = createVfcInstance("inst1", "inst1Uid");
+        GroupDefinition group = createGroup("grp1", "grp1Id");
+        ComponentInstance vfi = createVfInstance("vfi", componentInstance, group);
+        ComponentInstanceBuildingBlocks instanceBuildingBlocks = mergeInstanceUtils.getInstanceAtomicBuildingBlocks(vfi);
+        assertThat(instanceBuildingBlocks)
+                .extracting("vfcInstances", "groups")
+                .containsExactlyInAnyOrder(singletonList(componentInstance), singletonList(group));
     }
 
-
     @Test
-    public void testGetVfcInstancesNonAtomicComponentInstance() {
-        ComponentInstance componentInstance = createComponentInstance("SRV1.VF1.VFI_1", "SRV1.VF1", false);
-
-        Resource vf = new Resource();
-        Either<Component, StorageOperationStatus> eitherComponent = Either.left(vf);
-        vf.setComponentType(ComponentTypeEnum.RESOURCE);
-        vf.setResourceType(ResourceTypeEnum.VF);
-
-        ComponentInstance vfci = createComponentInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1" ,"SRV1.VF1.VFI_1.VFC1", false);
-        createVfc(vfci);
-        List<ComponentInstance> vfciList = Arrays.asList(vfci);
-
-        vf.setComponentInstances(vfciList);
-        when(toscaOperationFacade.getToscaElement(componentInstance.getComponentUid())).thenReturn(eitherComponent);
+    public void testGetAtomicBuildingBlocksComponentInstance_noGroups() {
+        ComponentInstance componentInstance = createVfcInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF1.VFI_1.VFC1");
+        ComponentInstanceBuildingBlocks instanceBuildingBlocks = mergeInstanceUtils.getInstanceAtomicBuildingBlocks(componentInstance);
+        assertThat(instanceBuildingBlocks)
+                .extracting("vfcInstances", "groups")
+                .containsExactly(singletonList(componentInstance), emptyList());
+    }
 
-        List<ComponentInstance> vfcInstances = mergeInstanceUtils.getVfcInstances(componentInstance);
+    @Test
+    public void testGetAtomicBuildingBlocks_noInstances() {
+        GroupDefinition group = createGroup("grp1", "grp1Id");
+        ComponentInstance vfi = createVfInstance("vfi", null, group);
+        ComponentInstanceBuildingBlocks instanceBuildingBlocks = mergeInstanceUtils.getInstanceAtomicBuildingBlocks(vfi);
+        assertThat(instanceBuildingBlocks)
+                .extracting("groups", "vfcInstances")
+                .containsExactlyInAnyOrder(singletonList(group), emptyList());
+    }
 
-        assertEquals(vfciList, vfcInstances);
+    @Test
+    public void testGetAtomicBuildingBlocks_noDuplication() {
+        GroupDefinition group1FirstCopy = createGroup("grp1", "grp1Id");
+        GroupDefinition group1SecondCopy = createGroup("grp1", "grp1Id");
+
+        ComponentInstance cmtInst1FirstCopy = createVfcInstance("inst1", "inst1Uid");
+        ComponentInstance cmtInst1SecondCopy = createVfcInstance("inst1", "inst1Uid");
+
+        ComponentInstance vfi = createVfInstance("vfi", cmtInst1FirstCopy, group1FirstCopy);
+        ComponentInstance vfi2 = createVfInstance("vfi2", cmtInst1SecondCopy, group1SecondCopy);
+
+        Service service = new ServiceBuilder()
+                .addComponentInstance(vfi)
+                .addComponentInstance(vfi2)
+                .setUniqueId("service1")
+                .build();
+
+        ComponentInstance proxy = createServiceProxy("serviceProxy", service);
+        ComponentInstanceBuildingBlocks instanceAtomicBuildingBlocks = mergeInstanceUtils.getInstanceAtomicBuildingBlocks(proxy);
+        assertThat(instanceAtomicBuildingBlocks)
+                .extracting("groups", "vfcInstances")
+                .containsExactlyInAnyOrder(singletonList(group1FirstCopy), singletonList(cmtInst1FirstCopy));
     }
 
     @Test
-    public void testGetVfcInstances_ComponentNullI_nstanceComponent() {
-        List<ComponentInstance> vfcInstances = mergeInstanceUtils.getVfcInstances(null, new Resource());
+    public void testGetAtomicBuildingBlocks_ComponentNull_InstanceComponent() {
+        assertEmpty(mergeInstanceUtils.getInstanceAtomicBuildingBlocks(null, new Resource()));
+    }
 
-        assertTrue(vfcInstances.isEmpty());
+    @Test
+    public void testGetInstanceAtomicBuildingBlocks_ComponentInstance_NullComponent() {
+        assertEmpty(mergeInstanceUtils.getInstanceAtomicBuildingBlocks(new ComponentInstance(), null));
     }
 
     @Test
-    public void testGetVfcInstances_ComponentInstance_NullComponent() {
-        List<ComponentInstance> vfcInstances = mergeInstanceUtils.getVfcInstances(new ComponentInstance(), null);
+    public void testGetInstanceAtomicBuildingBlocks_NullComponentInstance_NullComponent() {
+        assertEmpty(mergeInstanceUtils.getInstanceAtomicBuildingBlocks(null, null));
+    }
 
-        assertTrue(vfcInstances.isEmpty());
+    private void assertEmpty(ComponentInstanceBuildingBlocks componentInstanceBuildingBlocks) {
+        assertThat(componentInstanceBuildingBlocks)
+                .extracting("vfcInstances", "groups")
+                .containsExactly(emptyList(), emptyList());
     }
 
-    @Test
-    public void testGetVfcInstances_NullComponentInstance_NullComponent() {
-        List<ComponentInstance> vfcInstances = mergeInstanceUtils.getVfcInstances(null, null);
+    private ComponentInstance createVfcInstance(String instId, String instUid) {
+        ComponentInstance vfci = createComponentInstance(instId, instUid, false);
+        createVfc(vfci);
+        return vfci;
+    }
 
-        assertTrue(vfcInstances.isEmpty());
+    private ComponentInstance createVfInstance(String id, ComponentInstance withInstance, GroupDefinition withGroup) {
+        Component vf = createVf(id, withInstance, withGroup);
+        ComponentInstance vfInstance = new ComponentInstanceBuilder().setComponentUid(vf.getUniqueId()).build();
+        when(toscaOperationFacade.getToscaElement(id)).thenReturn(Either.left(vf));
+        return vfInstance;
     }
 
+    private ComponentInstance createServiceProxy(String id, Service fromService) {
+        when(toscaOperationFacade.getToscaElement(fromService.getUniqueId())).thenReturn(Either.left(fromService));
+        return createComponentInstance(id, fromService.getUniqueId(), true);
+    }
+
+    private Component createVf(String id, ComponentInstance instance, GroupDefinition group) {
+        return new ResourceBuilder()
+                .setResourceType(ResourceTypeEnum.VF)
+                .setComponentType(ComponentTypeEnum.RESOURCE)
+                .addGroup(group)
+                .addComponentInstance(instance)
+                .setUniqueId(id)
+                .build();
+    }
 
-    /**
-     * @param uniqueId
-     * @param componentUid
-     * @param isProxy
-     * @return
-     */
     private ComponentInstance createComponentInstance(String uniqueId, String componentUid, boolean isProxy) {
         ComponentInstance componentInstance = new ComponentInstance();
         componentInstance.setUniqueId(uniqueId);
-        componentInstance.setComponentUid(componentUid);
         componentInstance.setIsProxy(isProxy);
+        if (isProxy) {
+            componentInstance.setSourceModelUid(componentUid);
+        } else {
+            componentInstance.setComponentUid(componentUid);
+        }
         return componentInstance;
     }
 
-    /**
-     * @param componentInstance
-     */
     private void createVfc(ComponentInstance componentInstance) {
         Resource vfc = new Resource();
         Either<Component, StorageOperationStatus> eitherComponent = Either.left(vfc);
@@ -326,4 +338,13 @@ public class MergeInstanceUtilsTest {
         vfc.setResourceType(ResourceTypeEnum.VFC);
         when(toscaOperationFacade.getToscaElement(componentInstance.getComponentUid())).thenReturn(eitherComponent);
     }
+
+    private GroupDefinition createGroup(String invariantName, String id) {
+        return GroupDefinitionBuilder.create()
+                .setInvariantName(invariantName)
+                .setUniqueId(id)
+                .setName(id + "name")
+                .build();
+
+    }
 }