2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.merge.utils;
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;
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;
53 @RunWith(MockitoJUnitRunner.class)
54 public class MergeInstanceUtilsTest {
56 private MergeInstanceUtils mergeInstanceUtils;
59 private ToscaOperationFacade toscaOperationFacade;
62 private JanusGraphDao janusGraphDao;
65 public void startUp() {
66 ExceptionUtils exceptionUtils = new ExceptionUtils(janusGraphDao);
67 mergeInstanceUtils = new MergeInstanceUtils(toscaOperationFacade, exceptionUtils);
71 public void testMapOldToNewCapabilitiesOwnerIdsComponentComponent() {
72 Component container = new Service();
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));
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));
85 Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(container, vfOld, "SRV1.VF2.VFI_1", asList("SRV1.VF1.VFI_1.VFC1.VFCI_1", oldGrp.getUniqueId()));
87 .containsEntry("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF1.VFI_1.VFC1.VFCI_2")
88 .containsEntry(oldGrp.getUniqueId(), newGrp.getUniqueId());
92 public void testMapOldToNewCapabilitiesOwnerIdsComponentComponent_Proxy() {
93 Resource container = new Resource();
94 container.setComponentType(ComponentTypeEnum.RESOURCE);
95 container.setResourceType(ResourceTypeEnum.VF);
97 Component serviceOld = new Service();
98 serviceOld.setComponentType(ComponentTypeEnum.SERVICE);
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));
105 Component serviceNew = new Service();
106 serviceNew.setComponentType(ComponentTypeEnum.SERVICE);
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));
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);
119 Either<Component, StorageOperationStatus> eitherComponent4 = Either.left(serviceNew);
120 when(toscaOperationFacade.getToscaElement(proxyVfciNew.getSourceModelUid())).thenReturn(eitherComponent4);
122 container.setComponentInstances(singletonList(proxyVfciNew));
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());
131 public void whenFailingToGetInstanceOriginNodeType_throwExceptionAndRollBack() {
132 Resource oldVf = new ResourceBuilder()
133 .setResourceType(ResourceTypeEnum.VF)
134 .setComponentType(ComponentTypeEnum.RESOURCE)
137 ComponentInstance newVfInstance = createComponentInstance("inst1", "inst1Uid", false);
138 Resource container = new ResourceBuilder()
139 .addComponentInstance(newVfInstance)
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();
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"));
156 public void testMapOldToNewCapabilitiesOwnerIdsInstToInstWithGroups() {
157 ComponentInstance prevInstance = createVfcInstance("prevInst1", "prevInst1Uid");
158 GroupDefinition prevGroup = createGroup("grp1", "prevGrp1");
159 ComponentInstance prevInstanceRoot = createVfInstance("prevId", prevInstance, prevGroup);
161 ComponentInstance currInstance = createVfcInstance("newInst1", "newInst1Uid");
162 GroupDefinition currGroup = createGroup("grp1", "currGrp1");
163 ComponentInstance newInstanceRoot = createVfInstance("currId", currInstance, currGroup);
165 Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(prevInstanceRoot, newInstanceRoot);
166 assertThat(mapResult)
167 .containsEntry(prevInstance.getUniqueId(), currInstance.getUniqueId())
168 .containsEntry(prevGroup.getUniqueId(), currGroup.getUniqueId());
173 public void testMapOldToNewCapabilitiesOwnerIdsComponentInstComponentInst_Proxy() {
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);
179 Component serviceOld = new Service();
180 serviceOld.setComponentType(ComponentTypeEnum.SERVICE);
181 serviceOld.setComponentInstances(singletonList(vfiOld));
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);
189 Either<Component, StorageOperationStatus> eitherComponent2 = Either.left(serviceOld);
190 when(toscaOperationFacade.getToscaElement(proxyVfciOld.getSourceModelUid())).thenReturn(eitherComponent2);
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);
196 Component serviceNew = new Service();
197 serviceNew.setComponentType(ComponentTypeEnum.SERVICE);
198 serviceNew.setComponentInstances(singletonList(vfiNew));
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);
206 Either<Component, StorageOperationStatus> eitherComponent4 = Either.left(serviceNew);
207 when(toscaOperationFacade.getToscaElement(proxyVfciNew.getSourceModelUid())).thenReturn(eitherComponent4);
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());
217 public void testGetInstanceAtomicBuildingBlocks_NullComponentInstance() {
218 assertEmpty(mergeInstanceUtils.getInstanceAtomicBuildingBlocks(null));
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));
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));
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));
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());
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());
270 public void testGetAtomicBuildingBlocks_noDuplication() {
271 GroupDefinition group1FirstCopy = createGroup("grp1", "grp1Id");
272 GroupDefinition group1SecondCopy = createGroup("grp1", "grp1Id");
274 ComponentInstance cmtInst1FirstCopy = createVfcInstance("inst1", "inst1Uid");
275 ComponentInstance cmtInst1SecondCopy = createVfcInstance("inst1", "inst1Uid");
277 ComponentInstance vfi = createVfInstance("vfi", cmtInst1FirstCopy, group1FirstCopy);
278 ComponentInstance vfi2 = createVfInstance("vfi2", cmtInst1SecondCopy, group1SecondCopy);
280 Service service = new ServiceBuilder()
281 .addComponentInstance(vfi)
282 .addComponentInstance(vfi2)
283 .setUniqueId("service1")
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));
294 public void testGetAtomicBuildingBlocks_ComponentNull_InstanceComponent() {
295 assertEmpty(mergeInstanceUtils.getInstanceAtomicBuildingBlocks(null, new Resource()));
299 public void testGetInstanceAtomicBuildingBlocks_ComponentInstance_NullComponent() {
300 assertEmpty(mergeInstanceUtils.getInstanceAtomicBuildingBlocks(new ComponentInstance(), null));
304 public void testGetInstanceAtomicBuildingBlocks_NullComponentInstance_NullComponent() {
305 assertEmpty(mergeInstanceUtils.getInstanceAtomicBuildingBlocks(null, null));
308 private void assertEmpty(ComponentInstanceBuildingBlocks componentInstanceBuildingBlocks) {
309 assertThat(componentInstanceBuildingBlocks)
310 .extracting("vfcInstances", "groups")
311 .containsExactly(emptyList(), emptyList());
314 private ComponentInstance createVfcInstance(String instId, String instUid) {
315 ComponentInstance vfci = createComponentInstance(instId, instUid, false);
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));
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);
332 private Component createVf(String id, ComponentInstance instance, GroupDefinition group) {
333 return new ResourceBuilder()
334 .setResourceType(ResourceTypeEnum.VF)
335 .setComponentType(ComponentTypeEnum.RESOURCE)
337 .addComponentInstance(instance)
342 private ComponentInstance createComponentInstance(String uniqueId, String componentUid, boolean isProxy) {
343 ComponentInstance componentInstance = new ComponentInstance();
344 componentInstance.setUniqueId(uniqueId);
345 componentInstance.setIsProxy(isProxy);
347 componentInstance.setSourceModelUid(componentUid);
349 componentInstance.setComponentUid(componentUid);
351 return componentInstance;
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);
362 private GroupDefinition createGroup(String invariantName, String id) {
363 return GroupDefinitionBuilder.create()
364 .setInvariantName(invariantName)
366 .setName(id + "name")