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.Component;
38 import org.openecomp.sdc.be.model.ComponentInstance;
39 import org.openecomp.sdc.be.model.GroupDefinition;
40 import org.openecomp.sdc.be.model.Resource;
41 import org.openecomp.sdc.be.model.Service;
42 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
43 import org.openecomp.sdc.be.model.operations.StorageException;
44 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
48 import static java.util.Arrays.asList;
49 import static java.util.Collections.emptyList;
50 import static java.util.Collections.singletonList;
51 import static org.assertj.core.api.Assertions.assertThat;
52 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
53 import static org.junit.Assert.assertEquals;
54 import static org.mockito.Mockito.verify;
55 import static org.mockito.Mockito.when;
57 @RunWith(MockitoJUnitRunner.class)
58 public class MergeInstanceUtilsTest {
60 private MergeInstanceUtils mergeInstanceUtils;
63 private ToscaOperationFacade toscaOperationFacade;
66 private JanusGraphDao janusGraphDao;
69 public void startUp() {
70 ExceptionUtils exceptionUtils = new ExceptionUtils(janusGraphDao);
71 mergeInstanceUtils = new MergeInstanceUtils(toscaOperationFacade, exceptionUtils);
75 public void testMapOldToNewCapabilitiesOwnerIdsComponentComponent() {
76 Component container = new Service();
78 ComponentInstance vfciOld = createVfcInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF1.VFI_1.VFC1");
79 GroupDefinition oldGrp = createGroup("group1", "grp1Id");
80 Component vfOld = createVf("prevVfId", vfciOld, oldGrp);
81 ComponentInstance vfi1 = createComponentInstance("SRV1.VF2.VFI_1" ,"SRV1.VF2", false);
82 container.setComponentInstances(singletonList(vfi1));
84 ComponentInstance vfciNew = createComponentInstance("SRV1.VF1.VFI_1.VFC1.VFCI_2", "SRV1.VF1.VFI_1.VFC1", false);
85 GroupDefinition newGrp = createGroup("group1", "newGrp1Id");
86 Component vfNew = createVf("newVfId", vfciNew, newGrp);
87 when(toscaOperationFacade.getToscaElement(vfi1.getComponentUid())).thenReturn(Either.left(vfNew));
89 Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(container, vfOld, "SRV1.VF2.VFI_1", asList("SRV1.VF1.VFI_1.VFC1.VFCI_1", oldGrp.getUniqueId()));
91 .containsEntry("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF1.VFI_1.VFC1.VFCI_2")
92 .containsEntry(oldGrp.getUniqueId(), newGrp.getUniqueId());
96 public void testMapOldToNewCapabilitiesOwnerIdsComponentComponent_Proxy() {
97 Resource container = new Resource();
98 container.setComponentType(ComponentTypeEnum.RESOURCE);
99 container.setResourceType(ResourceTypeEnum.VF);
101 Component serviceOld = new Service();
102 serviceOld.setComponentType(ComponentTypeEnum.SERVICE);
104 ComponentInstance vfciOld = createVfcInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF1.VFI_1.VFC1");
105 GroupDefinition prevGroup = createGroup("grp1", "grp1Old");
106 ComponentInstance vfiOld = createVfInstance("SRV1.VF1.VFI_1", vfciOld, prevGroup);
107 serviceOld.setComponentInstances(singletonList(vfiOld));
109 Component serviceNew = new Service();
110 serviceNew.setComponentType(ComponentTypeEnum.SERVICE);
112 ComponentInstance vfciNew = createVfcInstance("SRV1.VF2.VFI_1.VFC2.VFCI_1", "SRV1.VF2.VFI_1.VFC2");
113 GroupDefinition grpNew = createGroup("grp1", "newGrp1");
114 ComponentInstance vfiNew = createVfInstance("SRV1.VF2.VFI_1" ,vfciNew, grpNew);
115 serviceNew.setComponentInstances(singletonList(vfiNew));
117 ComponentInstance proxyVfciNew = createComponentInstance("SRV1.PROXY_VFC_NEW.VFCI1", "SRV1.PROXY_VFC_NEW", true);
118 proxyVfciNew.setSourceModelUid("SRV_PROXY_NEW");
119 Resource proxyVfcNew = new Resource();
120 proxyVfcNew.setComponentType(ComponentTypeEnum.RESOURCE);
121 proxyVfcNew.setResourceType(ResourceTypeEnum.VFC);
123 Either<Component, StorageOperationStatus> eitherComponent4 = Either.left(serviceNew);
124 when(toscaOperationFacade.getToscaElement(proxyVfciNew.getSourceModelUid())).thenReturn(eitherComponent4);
126 container.setComponentInstances(singletonList(proxyVfciNew));
128 Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(container, serviceOld, "SRV1.PROXY_VFC_NEW.VFCI1", asList("SRV1.VF1.VFI_1.VFC1.VFCI_1", prevGroup.getUniqueId()));
129 assertThat(mapResult)
130 .containsEntry("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF2.VFI_1.VFC2.VFCI_1")
131 .containsEntry(prevGroup.getUniqueId(), grpNew.getUniqueId());
135 public void whenFailingToGetInstanceOriginNodeType_throwExceptionAndRollBack() {
136 Resource oldVf = new ResourceBuilder()
137 .setResourceType(ResourceTypeEnum.VF)
138 .setComponentType(ComponentTypeEnum.RESOURCE)
141 ComponentInstance newVfInstance = createComponentInstance("inst1", "inst1Uid", false);
142 Resource container = new ResourceBuilder()
143 .addComponentInstance(newVfInstance)
145 when(toscaOperationFacade.getToscaElement("inst1Uid")).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
146 assertThatExceptionOfType(StorageException.class)
147 .isThrownBy(() -> mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(container, oldVf, "inst1", emptyList()));
148 verify(janusGraphDao).rollback();
152 public void testMapOldToNewCapabilitiesOwnerIdsComponentInstanceComponentInstance() {
153 ComponentInstance oldInstance = createVfcInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF1.VFI_1.VFC1");
154 ComponentInstance newInstance = createVfcInstance("SRV1.VF1.VFI_1.VFC2.VFCI_1", "SRV1.VF1.VFI_1.VFC2");
155 Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(oldInstance, newInstance);
156 assertEquals("SRV1.VF1.VFI_1.VFC2.VFCI_1", mapResult.get("SRV1.VF1.VFI_1.VFC1.VFCI_1"));
160 public void testMapOldToNewCapabilitiesOwnerIdsInstToInstWithGroups() {
161 ComponentInstance prevInstance = createVfcInstance("prevInst1", "prevInst1Uid");
162 GroupDefinition prevGroup = createGroup("grp1", "prevGrp1");
163 ComponentInstance prevInstanceRoot = createVfInstance("prevId", prevInstance, prevGroup);
165 ComponentInstance currInstance = createVfcInstance("newInst1", "newInst1Uid");
166 GroupDefinition currGroup = createGroup("grp1", "currGrp1");
167 ComponentInstance newInstanceRoot = createVfInstance("currId", currInstance, currGroup);
169 Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(prevInstanceRoot, newInstanceRoot);
170 assertThat(mapResult)
171 .containsEntry(prevInstance.getUniqueId(), currInstance.getUniqueId())
172 .containsEntry(prevGroup.getUniqueId(), currGroup.getUniqueId());
177 public void testMapOldToNewCapabilitiesOwnerIdsComponentInstComponentInst_Proxy() {
179 ComponentInstance vfciOld = createVfcInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF1.VFI_1.VFC1");
180 GroupDefinition oldGrp = createGroup("grp1", "grp1Old");
181 ComponentInstance vfiOld = createVfInstance("SRV1.VF1.VFI_1", vfciOld, oldGrp);
183 Component serviceOld = new Service();
184 serviceOld.setComponentType(ComponentTypeEnum.SERVICE);
185 serviceOld.setComponentInstances(singletonList(vfiOld));
187 ComponentInstance proxyVfciOld = createComponentInstance("SRV1.PROXY_VFC.VFCI1", "SRV1.PROXY_VFC", true);
188 proxyVfciOld.setSourceModelUid("SRV_PROXY");
189 Resource proxyVfcOld = new Resource();
190 proxyVfcOld.setComponentType(ComponentTypeEnum.RESOURCE);
191 proxyVfcOld.setResourceType(ResourceTypeEnum.VFC);
193 Either<Component, StorageOperationStatus> eitherComponent2 = Either.left(serviceOld);
194 when(toscaOperationFacade.getToscaElement(proxyVfciOld.getSourceModelUid())).thenReturn(eitherComponent2);
196 ComponentInstance vfciNew = createVfcInstance("SRV1.VF2.VFI_1.VFC2.VFCI_1", "SRV1.VF2.VFI_1.VFC2");
197 GroupDefinition newGrp = createGroup("grp1", "grp1New");
198 ComponentInstance vfiNew = createVfInstance("SRV1.VF2.VFI_1" ,vfciNew, newGrp);
200 Component serviceNew = new Service();
201 serviceNew.setComponentType(ComponentTypeEnum.SERVICE);
202 serviceNew.setComponentInstances(singletonList(vfiNew));
204 ComponentInstance proxyVfciNew = createComponentInstance("SRV1.PROXY_VFC_NEW.VFCI1", "SRV1.PROXY_VFC_NEW", true);
205 proxyVfciNew.setSourceModelUid("SRV_PROXY_NEW");
206 Resource proxyVfcNew = new Resource();
207 proxyVfcNew.setComponentType(ComponentTypeEnum.RESOURCE);
208 proxyVfcNew.setResourceType(ResourceTypeEnum.VFC);
210 Either<Component, StorageOperationStatus> eitherComponent4 = Either.left(serviceNew);
211 when(toscaOperationFacade.getToscaElement(proxyVfciNew.getSourceModelUid())).thenReturn(eitherComponent4);
213 Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(proxyVfciOld, proxyVfciNew);
214 assertThat(mapResult)
215 .containsEntry("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF2.VFI_1.VFC2.VFCI_1")
216 .containsEntry(oldGrp.getUniqueId(), newGrp.getUniqueId());
221 public void testGetInstanceAtomicBuildingBlocks_NullComponentInstance() {
222 assertEmpty(mergeInstanceUtils.getInstanceAtomicBuildingBlocks(null));
226 public void testgetInstanceAtomicBuildingBlocks_ComponentInstanceFailedLoadComponent() {
227 ComponentInstance componentInstance = createComponentInstance("SRV1.VF1.VFI_1", "SRV1.VF1", false);
228 Either<Component, StorageOperationStatus> eitherComponent = Either.right(StorageOperationStatus.NOT_FOUND);
229 when(toscaOperationFacade.getToscaElement(componentInstance.getComponentUid())).thenReturn(eitherComponent);
230 assertThatExceptionOfType(StorageException.class).isThrownBy(() -> mergeInstanceUtils.getInstanceAtomicBuildingBlocks(componentInstance));
234 public void testGetInstanceAtomicBuildingBlocks_ComponentInstanceFailedLoadActualComponent() {
235 ComponentInstance componentInstance = createComponentInstance("SRV1.PROXY_VFC.VFCI1", "SRV1.PROXY_VFC", true);
236 componentInstance.setSourceModelUid("SRV_PROXY");
237 Either<Component, StorageOperationStatus> eitherComponent = Either.right(StorageOperationStatus.NOT_FOUND);
238 when(toscaOperationFacade.getToscaElement(componentInstance.getSourceModelUid())).thenReturn(eitherComponent);
239 assertThatExceptionOfType(StorageException.class).isThrownBy(() -> mergeInstanceUtils.getInstanceAtomicBuildingBlocks(componentInstance));
244 public void testGetAtomicBuildingBlocks() {
245 ComponentInstance componentInstance = createVfcInstance("inst1", "inst1Uid");
246 GroupDefinition group = createGroup("grp1", "grp1Id");
247 ComponentInstance vfi = createVfInstance("vfi", componentInstance, group);
248 ComponentInstanceBuildingBlocks instanceBuildingBlocks = mergeInstanceUtils.getInstanceAtomicBuildingBlocks(vfi);
249 assertThat(instanceBuildingBlocks)
250 .extracting("vfcInstances", "groups")
251 .containsExactlyInAnyOrder(singletonList(componentInstance), singletonList(group));
255 public void testGetAtomicBuildingBlocksComponentInstance_noGroups() {
256 ComponentInstance componentInstance = createVfcInstance("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF1.VFI_1.VFC1");
257 ComponentInstanceBuildingBlocks instanceBuildingBlocks = mergeInstanceUtils.getInstanceAtomicBuildingBlocks(componentInstance);
258 assertThat(instanceBuildingBlocks)
259 .extracting("vfcInstances", "groups")
260 .containsExactly(singletonList(componentInstance), emptyList());
264 public void testGetAtomicBuildingBlocks_noInstances() {
265 GroupDefinition group = createGroup("grp1", "grp1Id");
266 ComponentInstance vfi = createVfInstance("vfi", null, group);
267 ComponentInstanceBuildingBlocks instanceBuildingBlocks = mergeInstanceUtils.getInstanceAtomicBuildingBlocks(vfi);
268 assertThat(instanceBuildingBlocks)
269 .extracting("groups", "vfcInstances")
270 .containsExactlyInAnyOrder(singletonList(group), emptyList());
274 public void testGetAtomicBuildingBlocks_noDuplication() {
275 GroupDefinition group1FirstCopy = createGroup("grp1", "grp1Id");
276 GroupDefinition group1SecondCopy = createGroup("grp1", "grp1Id");
278 ComponentInstance cmtInst1FirstCopy = createVfcInstance("inst1", "inst1Uid");
279 ComponentInstance cmtInst1SecondCopy = createVfcInstance("inst1", "inst1Uid");
281 ComponentInstance vfi = createVfInstance("vfi", cmtInst1FirstCopy, group1FirstCopy);
282 ComponentInstance vfi2 = createVfInstance("vfi2", cmtInst1SecondCopy, group1SecondCopy);
284 Service service = new ServiceBuilder()
285 .addComponentInstance(vfi)
286 .addComponentInstance(vfi2)
287 .setUniqueId("service1")
290 ComponentInstance proxy = createServiceProxy("serviceProxy", service);
291 ComponentInstanceBuildingBlocks instanceAtomicBuildingBlocks = mergeInstanceUtils.getInstanceAtomicBuildingBlocks(proxy);
292 assertThat(instanceAtomicBuildingBlocks)
293 .extracting("groups", "vfcInstances")
294 .containsExactlyInAnyOrder(singletonList(group1FirstCopy), singletonList(cmtInst1FirstCopy));
298 public void testGetAtomicBuildingBlocks_ComponentNull_InstanceComponent() {
299 assertEmpty(mergeInstanceUtils.getInstanceAtomicBuildingBlocks(null, new Resource()));
303 public void testGetInstanceAtomicBuildingBlocks_ComponentInstance_NullComponent() {
304 assertEmpty(mergeInstanceUtils.getInstanceAtomicBuildingBlocks(new ComponentInstance(), null));
308 public void testGetInstanceAtomicBuildingBlocks_NullComponentInstance_NullComponent() {
309 assertEmpty(mergeInstanceUtils.getInstanceAtomicBuildingBlocks(null, null));
312 private void assertEmpty(ComponentInstanceBuildingBlocks componentInstanceBuildingBlocks) {
313 assertThat(componentInstanceBuildingBlocks)
314 .extracting("vfcInstances", "groups")
315 .containsExactly(emptyList(), emptyList());
318 private ComponentInstance createVfcInstance(String instId, String instUid) {
319 ComponentInstance vfci = createComponentInstance(instId, instUid, false);
324 private ComponentInstance createVfInstance(String id, ComponentInstance withInstance, GroupDefinition withGroup) {
325 Component vf = createVf(id, withInstance, withGroup);
326 ComponentInstance vfInstance = new ComponentInstanceBuilder().setComponentUid(vf.getUniqueId()).build();
327 when(toscaOperationFacade.getToscaElement(id)).thenReturn(Either.left(vf));
331 private ComponentInstance createServiceProxy(String id, Service fromService) {
332 when(toscaOperationFacade.getToscaElement(fromService.getUniqueId())).thenReturn(Either.left(fromService));
333 return createComponentInstance(id, fromService.getUniqueId(), true);
336 private Component createVf(String id, ComponentInstance instance, GroupDefinition group) {
337 return new ResourceBuilder()
338 .setResourceType(ResourceTypeEnum.VF)
339 .setComponentType(ComponentTypeEnum.RESOURCE)
341 .addComponentInstance(instance)
346 private ComponentInstance createComponentInstance(String uniqueId, String componentUid, boolean isProxy) {
347 ComponentInstance componentInstance = new ComponentInstance();
348 componentInstance.setUniqueId(uniqueId);
349 componentInstance.setIsProxy(isProxy);
351 componentInstance.setSourceModelUid(componentUid);
353 componentInstance.setComponentUid(componentUid);
355 return componentInstance;
358 private void createVfc(ComponentInstance componentInstance) {
359 Resource vfc = new Resource();
360 Either<Component, StorageOperationStatus> eitherComponent = Either.left(vfc);
361 vfc.setComponentType(ComponentTypeEnum.RESOURCE);
362 vfc.setResourceType(ResourceTypeEnum.VFC);
363 when(toscaOperationFacade.getToscaElement(componentInstance.getComponentUid())).thenReturn(eitherComponent);
366 private GroupDefinition createGroup(String invariantName, String id) {
367 return GroupDefinitionBuilder.create()
368 .setInvariantName(invariantName)
370 .setName(id + "name")