re base code
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / utils / MergeInstanceUtilsTest.java
1 package org.openecomp.sdc.be.components.merge.utils;
2
3 import fj.data.Either;
4 import org.junit.Before;
5 import org.junit.Test;
6 import org.junit.runner.RunWith;
7 import org.mockito.Mock;
8 import org.mockito.junit.MockitoJUnitRunner;
9 import org.openecomp.sdc.be.components.impl.utils.ExceptionUtils;
10 import org.openecomp.sdc.be.components.utils.ComponentInstanceBuilder;
11 import org.openecomp.sdc.be.components.utils.GroupDefinitionBuilder;
12 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
13 import org.openecomp.sdc.be.components.utils.ServiceBuilder;
14 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
15 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
16 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
17 import org.openecomp.sdc.be.model.*;
18 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
19 import org.openecomp.sdc.be.model.operations.StorageException;
20 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
21
22 import java.util.Map;
23
24 import static java.util.Arrays.asList;
25 import static java.util.Collections.emptyList;
26 import static java.util.Collections.singletonList;
27 import static org.assertj.core.api.Assertions.assertThat;
28 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
29 import static org.junit.Assert.assertEquals;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32
33 @RunWith(MockitoJUnitRunner.class)
34 public class MergeInstanceUtilsTest {
35
36     private MergeInstanceUtils mergeInstanceUtils;
37
38     @Mock
39     private ToscaOperationFacade toscaOperationFacade;
40
41     @Mock
42     private TitanDao titanDao;
43
44     @Before
45     public void startUp() {
46         ExceptionUtils exceptionUtils = new ExceptionUtils(titanDao);
47         mergeInstanceUtils = new MergeInstanceUtils(toscaOperationFacade, exceptionUtils);
48     }
49
50     @Test
51     public void testMapOldToNewCapabilitiesOwnerIdsComponentComponent() {
52         Component container = new Service();
53
54         ComponentInstance vfciOld = createVfcInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF1.VFI_1.VFC1");
55         GroupDefinition oldGrp = createGroup("group1", "grp1Id");
56         Component vfOld = createVf("prevVfId", vfciOld, oldGrp);
57         ComponentInstance vfi1 = createComponentInstance("SRV1.VF2.VFI_1" ,"SRV1.VF2", false);
58         container.setComponentInstances(singletonList(vfi1));
59
60         ComponentInstance vfciNew = createComponentInstance("SRV1.VF1.VFI_1.VFC1.VFCI_2", "SRV1.VF1.VFI_1.VFC1", false);
61         GroupDefinition newGrp = createGroup("group1", "newGrp1Id");
62         Component vfNew = createVf("newVfId", vfciNew, newGrp);
63         when(toscaOperationFacade.getToscaElement(vfi1.getComponentUid())).thenReturn(Either.left(vfNew));
64
65         Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(container, vfOld, "SRV1.VF2.VFI_1", asList("SRV1.VF1.VFI_1.VFC1.VFCI_1", oldGrp.getUniqueId()));
66         assertThat(mapResult)
67                 .containsEntry("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF1.VFI_1.VFC1.VFCI_2")
68                 .containsEntry(oldGrp.getUniqueId(), newGrp.getUniqueId());
69     }
70
71     @Test
72     public void testMapOldToNewCapabilitiesOwnerIdsComponentComponent_Proxy() {
73         Resource container = new Resource();
74         container.setComponentType(ComponentTypeEnum.RESOURCE);
75         container.setResourceType(ResourceTypeEnum.VF);
76
77         Component serviceOld = new Service();
78         serviceOld.setComponentType(ComponentTypeEnum.SERVICE);
79
80         ComponentInstance vfciOld = createVfcInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF1.VFI_1.VFC1");
81         GroupDefinition prevGroup = createGroup("grp1", "grp1Old");
82         ComponentInstance vfiOld = createVfInstance("SRV1.VF1.VFI_1", vfciOld, prevGroup);
83         serviceOld.setComponentInstances(singletonList(vfiOld));
84
85         Component serviceNew = new Service();
86         serviceNew.setComponentType(ComponentTypeEnum.SERVICE);
87
88         ComponentInstance vfciNew = createVfcInstance("SRV1.VF2.VFI_1.VFC2.VFCI_1", "SRV1.VF2.VFI_1.VFC2");
89         GroupDefinition grpNew = createGroup("grp1", "newGrp1");
90         ComponentInstance vfiNew = createVfInstance("SRV1.VF2.VFI_1" ,vfciNew, grpNew);
91         serviceNew.setComponentInstances(singletonList(vfiNew));
92
93         ComponentInstance proxyVfciNew = createComponentInstance("SRV1.PROXY_VFC_NEW.VFCI1", "SRV1.PROXY_VFC_NEW", true);
94         proxyVfciNew.setSourceModelUid("SRV_PROXY_NEW");
95         Resource proxyVfcNew = new Resource();
96         proxyVfcNew.setComponentType(ComponentTypeEnum.RESOURCE);
97         proxyVfcNew.setResourceType(ResourceTypeEnum.VFC);
98
99         Either<Component, StorageOperationStatus> eitherComponent4 = Either.left(serviceNew);
100         when(toscaOperationFacade.getToscaElement(proxyVfciNew.getSourceModelUid())).thenReturn(eitherComponent4);
101
102         container.setComponentInstances(singletonList(proxyVfciNew));
103
104         Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(container, serviceOld, "SRV1.PROXY_VFC_NEW.VFCI1", asList("SRV1.VF1.VFI_1.VFC1.VFCI_1", prevGroup.getUniqueId()));
105         assertThat(mapResult)
106                 .containsEntry("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF2.VFI_1.VFC2.VFCI_1")
107                 .containsEntry(prevGroup.getUniqueId(), grpNew.getUniqueId());
108     }
109
110     @Test
111     public void whenFailingToGetInstanceOriginNodeType_throwExceptionAndRollBack() {
112         Resource oldVf = new ResourceBuilder()
113                 .setResourceType(ResourceTypeEnum.VF)
114                 .setComponentType(ComponentTypeEnum.RESOURCE)
115                 .build();
116
117         ComponentInstance newVfInstance = createComponentInstance("inst1", "inst1Uid", false);
118         Resource container = new ResourceBuilder()
119                 .addComponentInstance(newVfInstance)
120                 .build();
121         when(toscaOperationFacade.getToscaElement("inst1Uid")).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
122         assertThatExceptionOfType(StorageException.class)
123                 .isThrownBy(() -> mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(container, oldVf, "inst1", emptyList()));
124         verify(titanDao).rollback();
125     }
126
127     @Test
128     public void testMapOldToNewCapabilitiesOwnerIdsComponentInstanceComponentInstance() {
129         ComponentInstance oldInstance = createVfcInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF1.VFI_1.VFC1");
130         ComponentInstance newInstance = createVfcInstance("SRV1.VF1.VFI_1.VFC2.VFCI_1", "SRV1.VF1.VFI_1.VFC2");
131         Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(oldInstance, newInstance);
132         assertEquals("SRV1.VF1.VFI_1.VFC2.VFCI_1", mapResult.get("SRV1.VF1.VFI_1.VFC1.VFCI_1"));
133     }
134
135     @Test
136     public void testMapOldToNewCapabilitiesOwnerIdsInstToInstWithGroups() {
137         ComponentInstance prevInstance = createVfcInstance("prevInst1", "prevInst1Uid");
138         GroupDefinition prevGroup = createGroup("grp1", "prevGrp1");
139         ComponentInstance prevInstanceRoot = createVfInstance("prevId", prevInstance, prevGroup);
140
141         ComponentInstance currInstance = createVfcInstance("newInst1", "newInst1Uid");
142         GroupDefinition currGroup = createGroup("grp1", "currGrp1");
143         ComponentInstance newInstanceRoot = createVfInstance("currId", currInstance, currGroup);
144
145         Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(prevInstanceRoot, newInstanceRoot);
146         assertThat(mapResult)
147                 .containsEntry(prevInstance.getUniqueId(), currInstance.getUniqueId())
148                 .containsEntry(prevGroup.getUniqueId(), currGroup.getUniqueId());
149     }
150
151
152     @Test
153     public void testMapOldToNewCapabilitiesOwnerIdsComponentInstComponentInst_Proxy() {
154
155         ComponentInstance vfciOld = createVfcInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF1.VFI_1.VFC1");
156         GroupDefinition oldGrp = createGroup("grp1", "grp1Old");
157         ComponentInstance vfiOld = createVfInstance("SRV1.VF1.VFI_1", vfciOld, oldGrp);
158
159         Component serviceOld = new Service();
160         serviceOld.setComponentType(ComponentTypeEnum.SERVICE);
161         serviceOld.setComponentInstances(singletonList(vfiOld));
162
163         ComponentInstance proxyVfciOld = createComponentInstance("SRV1.PROXY_VFC.VFCI1", "SRV1.PROXY_VFC", true);
164         proxyVfciOld.setSourceModelUid("SRV_PROXY");
165         Resource proxyVfcOld = new Resource();
166         proxyVfcOld.setComponentType(ComponentTypeEnum.RESOURCE);
167         proxyVfcOld.setResourceType(ResourceTypeEnum.VFC);
168
169         Either<Component, StorageOperationStatus> eitherComponent2 = Either.left(serviceOld);
170         when(toscaOperationFacade.getToscaElement(proxyVfciOld.getSourceModelUid())).thenReturn(eitherComponent2);
171
172         ComponentInstance vfciNew = createVfcInstance("SRV1.VF2.VFI_1.VFC2.VFCI_1", "SRV1.VF2.VFI_1.VFC2");
173         GroupDefinition newGrp = createGroup("grp1", "grp1New");
174         ComponentInstance vfiNew = createVfInstance("SRV1.VF2.VFI_1" ,vfciNew, newGrp);
175
176         Component serviceNew = new Service();
177         serviceNew.setComponentType(ComponentTypeEnum.SERVICE);
178         serviceNew.setComponentInstances(singletonList(vfiNew));
179
180         ComponentInstance proxyVfciNew = createComponentInstance("SRV1.PROXY_VFC_NEW.VFCI1", "SRV1.PROXY_VFC_NEW", true);
181         proxyVfciNew.setSourceModelUid("SRV_PROXY_NEW");
182         Resource proxyVfcNew = new Resource();
183         proxyVfcNew.setComponentType(ComponentTypeEnum.RESOURCE);
184         proxyVfcNew.setResourceType(ResourceTypeEnum.VFC);
185
186         Either<Component, StorageOperationStatus> eitherComponent4 = Either.left(serviceNew);
187         when(toscaOperationFacade.getToscaElement(proxyVfciNew.getSourceModelUid())).thenReturn(eitherComponent4);
188
189         Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(proxyVfciOld, proxyVfciNew);
190         assertThat(mapResult)
191                 .containsEntry("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF2.VFI_1.VFC2.VFCI_1")
192                 .containsEntry(oldGrp.getUniqueId(), newGrp.getUniqueId());
193     }
194
195
196     @Test
197     public void testGetInstanceAtomicBuildingBlocks_NullComponentInstance() {
198         assertEmpty(mergeInstanceUtils.getInstanceAtomicBuildingBlocks(null));
199     }
200
201     @Test
202     public void testgetInstanceAtomicBuildingBlocks_ComponentInstanceFailedLoadComponent() {
203         ComponentInstance componentInstance = createComponentInstance("SRV1.VF1.VFI_1", "SRV1.VF1", false);
204         Either<Component, StorageOperationStatus> eitherComponent = Either.right(StorageOperationStatus.NOT_FOUND);
205         when(toscaOperationFacade.getToscaElement(componentInstance.getComponentUid())).thenReturn(eitherComponent);
206         assertThatExceptionOfType(StorageException.class).isThrownBy(() -> mergeInstanceUtils.getInstanceAtomicBuildingBlocks(componentInstance));
207     }
208
209     @Test
210     public void testGetInstanceAtomicBuildingBlocks_ComponentInstanceFailedLoadActualComponent() {
211         ComponentInstance componentInstance = createComponentInstance("SRV1.PROXY_VFC.VFCI1", "SRV1.PROXY_VFC", true);
212         componentInstance.setSourceModelUid("SRV_PROXY");
213         Either<Component, StorageOperationStatus> eitherComponent = Either.right(StorageOperationStatus.NOT_FOUND);
214         when(toscaOperationFacade.getToscaElement(componentInstance.getSourceModelUid())).thenReturn(eitherComponent);
215         assertThatExceptionOfType(StorageException.class).isThrownBy(() -> mergeInstanceUtils.getInstanceAtomicBuildingBlocks(componentInstance));
216     }
217
218
219     @Test
220     public void testGetAtomicBuildingBlocks() {
221         ComponentInstance componentInstance = createVfcInstance("inst1", "inst1Uid");
222         GroupDefinition group = createGroup("grp1", "grp1Id");
223         ComponentInstance vfi = createVfInstance("vfi", componentInstance, group);
224         ComponentInstanceBuildingBlocks instanceBuildingBlocks = mergeInstanceUtils.getInstanceAtomicBuildingBlocks(vfi);
225         assertThat(instanceBuildingBlocks)
226                 .extracting("vfcInstances", "groups")
227                 .containsExactlyInAnyOrder(singletonList(componentInstance), singletonList(group));
228     }
229
230     @Test
231     public void testGetAtomicBuildingBlocksComponentInstance_noGroups() {
232         ComponentInstance componentInstance = createVfcInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF1.VFI_1.VFC1");
233         ComponentInstanceBuildingBlocks instanceBuildingBlocks = mergeInstanceUtils.getInstanceAtomicBuildingBlocks(componentInstance);
234         assertThat(instanceBuildingBlocks)
235                 .extracting("vfcInstances", "groups")
236                 .containsExactly(singletonList(componentInstance), emptyList());
237     }
238
239     @Test
240     public void testGetAtomicBuildingBlocks_noInstances() {
241         GroupDefinition group = createGroup("grp1", "grp1Id");
242         ComponentInstance vfi = createVfInstance("vfi", null, group);
243         ComponentInstanceBuildingBlocks instanceBuildingBlocks = mergeInstanceUtils.getInstanceAtomicBuildingBlocks(vfi);
244         assertThat(instanceBuildingBlocks)
245                 .extracting("groups", "vfcInstances")
246                 .containsExactlyInAnyOrder(singletonList(group), emptyList());
247     }
248
249     @Test
250     public void testGetAtomicBuildingBlocks_noDuplication() {
251         GroupDefinition group1FirstCopy = createGroup("grp1", "grp1Id");
252         GroupDefinition group1SecondCopy = createGroup("grp1", "grp1Id");
253
254         ComponentInstance cmtInst1FirstCopy = createVfcInstance("inst1", "inst1Uid");
255         ComponentInstance cmtInst1SecondCopy = createVfcInstance("inst1", "inst1Uid");
256
257         ComponentInstance vfi = createVfInstance("vfi", cmtInst1FirstCopy, group1FirstCopy);
258         ComponentInstance vfi2 = createVfInstance("vfi2", cmtInst1SecondCopy, group1SecondCopy);
259
260         Service service = new ServiceBuilder()
261                 .addComponentInstance(vfi)
262                 .addComponentInstance(vfi2)
263                 .setUniqueId("service1")
264                 .build();
265
266         ComponentInstance proxy = createServiceProxy("serviceProxy", service);
267         ComponentInstanceBuildingBlocks instanceAtomicBuildingBlocks = mergeInstanceUtils.getInstanceAtomicBuildingBlocks(proxy);
268         assertThat(instanceAtomicBuildingBlocks)
269                 .extracting("groups", "vfcInstances")
270                 .containsExactlyInAnyOrder(singletonList(group1FirstCopy), singletonList(cmtInst1FirstCopy));
271     }
272
273     @Test
274     public void testGetAtomicBuildingBlocks_ComponentNull_InstanceComponent() {
275         assertEmpty(mergeInstanceUtils.getInstanceAtomicBuildingBlocks(null, new Resource()));
276     }
277
278     @Test
279     public void testGetInstanceAtomicBuildingBlocks_ComponentInstance_NullComponent() {
280         assertEmpty(mergeInstanceUtils.getInstanceAtomicBuildingBlocks(new ComponentInstance(), null));
281     }
282
283     @Test
284     public void testGetInstanceAtomicBuildingBlocks_NullComponentInstance_NullComponent() {
285         assertEmpty(mergeInstanceUtils.getInstanceAtomicBuildingBlocks(null, null));
286     }
287
288     private void assertEmpty(ComponentInstanceBuildingBlocks componentInstanceBuildingBlocks) {
289         assertThat(componentInstanceBuildingBlocks)
290                 .extracting("vfcInstances", "groups")
291                 .containsExactly(emptyList(), emptyList());
292     }
293
294     private ComponentInstance createVfcInstance(String instId, String instUid) {
295         ComponentInstance vfci = createComponentInstance(instId, instUid, false);
296         createVfc(vfci);
297         return vfci;
298     }
299
300     private ComponentInstance createVfInstance(String id, ComponentInstance withInstance, GroupDefinition withGroup) {
301         Component vf = createVf(id, withInstance, withGroup);
302         ComponentInstance vfInstance = new ComponentInstanceBuilder().setComponentUid(vf.getUniqueId()).build();
303         when(toscaOperationFacade.getToscaElement(id)).thenReturn(Either.left(vf));
304         return vfInstance;
305     }
306
307     private ComponentInstance createServiceProxy(String id, Service fromService) {
308         when(toscaOperationFacade.getToscaElement(fromService.getUniqueId())).thenReturn(Either.left(fromService));
309         return createComponentInstance(id, fromService.getUniqueId(), true);
310     }
311
312     private Component createVf(String id, ComponentInstance instance, GroupDefinition group) {
313         return new ResourceBuilder()
314                 .setResourceType(ResourceTypeEnum.VF)
315                 .setComponentType(ComponentTypeEnum.RESOURCE)
316                 .addGroup(group)
317                 .addComponentInstance(instance)
318                 .setUniqueId(id)
319                 .build();
320     }
321
322     private ComponentInstance createComponentInstance(String uniqueId, String componentUid, boolean isProxy) {
323         ComponentInstance componentInstance = new ComponentInstance();
324         componentInstance.setUniqueId(uniqueId);
325         componentInstance.setIsProxy(isProxy);
326         if (isProxy) {
327             componentInstance.setSourceModelUid(componentUid);
328         } else {
329             componentInstance.setComponentUid(componentUid);
330         }
331         return componentInstance;
332     }
333
334     private void createVfc(ComponentInstance componentInstance) {
335         Resource vfc = new Resource();
336         Either<Component, StorageOperationStatus> eitherComponent = Either.left(vfc);
337         vfc.setComponentType(ComponentTypeEnum.RESOURCE);
338         vfc.setResourceType(ResourceTypeEnum.VFC);
339         when(toscaOperationFacade.getToscaElement(componentInstance.getComponentUid())).thenReturn(eitherComponent);
340     }
341
342     private GroupDefinition createGroup(String invariantName, String id) {
343         return GroupDefinitionBuilder.create()
344                 .setInvariantName(invariantName)
345                 .setUniqueId(id)
346                 .setName(id + "name")
347                 .build();
348
349     }
350 }