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