1 package org.openecomp.sdc.be.components.merge.utils;
4 import org.junit.Before;
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;
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;
33 @RunWith(MockitoJUnitRunner.class)
34 public class MergeInstanceUtilsTest {
36 private MergeInstanceUtils mergeInstanceUtils;
39 private ToscaOperationFacade toscaOperationFacade;
42 private TitanDao titanDao;
45 public void startUp() {
46 ExceptionUtils exceptionUtils = new ExceptionUtils(titanDao);
47 mergeInstanceUtils = new MergeInstanceUtils(toscaOperationFacade, exceptionUtils);
51 public void testMapOldToNewCapabilitiesOwnerIdsComponentComponent() {
52 Component container = new Service();
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));
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));
65 Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(container, vfOld, "SRV1.VF2.VFI_1", asList("SRV1.VF1.VFI_1.VFC1.VFCI_1", oldGrp.getUniqueId()));
67 .containsEntry("SRV1.VF1.VFI_1.VFC1.VFCI_1", "SRV1.VF1.VFI_1.VFC1.VFCI_2")
68 .containsEntry(oldGrp.getUniqueId(), newGrp.getUniqueId());
72 public void testMapOldToNewCapabilitiesOwnerIdsComponentComponent_Proxy() {
73 Resource container = new Resource();
74 container.setComponentType(ComponentTypeEnum.RESOURCE);
75 container.setResourceType(ResourceTypeEnum.VF);
77 Component serviceOld = new Service();
78 serviceOld.setComponentType(ComponentTypeEnum.SERVICE);
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));
85 Component serviceNew = new Service();
86 serviceNew.setComponentType(ComponentTypeEnum.SERVICE);
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));
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);
99 Either<Component, StorageOperationStatus> eitherComponent4 = Either.left(serviceNew);
100 when(toscaOperationFacade.getToscaElement(proxyVfciNew.getSourceModelUid())).thenReturn(eitherComponent4);
102 container.setComponentInstances(singletonList(proxyVfciNew));
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());
111 public void whenFailingToGetInstanceOriginNodeType_throwExceptionAndRollBack() {
112 Resource oldVf = new ResourceBuilder()
113 .setResourceType(ResourceTypeEnum.VF)
114 .setComponentType(ComponentTypeEnum.RESOURCE)
117 ComponentInstance newVfInstance = createComponentInstance("inst1", "inst1Uid", false);
118 Resource container = new ResourceBuilder()
119 .addComponentInstance(newVfInstance)
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();
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"));
136 public void testMapOldToNewCapabilitiesOwnerIdsInstToInstWithGroups() {
137 ComponentInstance prevInstance = createVfcInstance("prevInst1", "prevInst1Uid");
138 GroupDefinition prevGroup = createGroup("grp1", "prevGrp1");
139 ComponentInstance prevInstanceRoot = createVfInstance("prevId", prevInstance, prevGroup);
141 ComponentInstance currInstance = createVfcInstance("newInst1", "newInst1Uid");
142 GroupDefinition currGroup = createGroup("grp1", "currGrp1");
143 ComponentInstance newInstanceRoot = createVfInstance("currId", currInstance, currGroup);
145 Map<String, String> mapResult = mergeInstanceUtils.mapOldToNewCapabilitiesOwnerIds(prevInstanceRoot, newInstanceRoot);
146 assertThat(mapResult)
147 .containsEntry(prevInstance.getUniqueId(), currInstance.getUniqueId())
148 .containsEntry(prevGroup.getUniqueId(), currGroup.getUniqueId());
153 public void testMapOldToNewCapabilitiesOwnerIdsComponentInstComponentInst_Proxy() {
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);
159 Component serviceOld = new Service();
160 serviceOld.setComponentType(ComponentTypeEnum.SERVICE);
161 serviceOld.setComponentInstances(singletonList(vfiOld));
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);
169 Either<Component, StorageOperationStatus> eitherComponent2 = Either.left(serviceOld);
170 when(toscaOperationFacade.getToscaElement(proxyVfciOld.getSourceModelUid())).thenReturn(eitherComponent2);
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);
176 Component serviceNew = new Service();
177 serviceNew.setComponentType(ComponentTypeEnum.SERVICE);
178 serviceNew.setComponentInstances(singletonList(vfiNew));
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);
186 Either<Component, StorageOperationStatus> eitherComponent4 = Either.left(serviceNew);
187 when(toscaOperationFacade.getToscaElement(proxyVfciNew.getSourceModelUid())).thenReturn(eitherComponent4);
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());
197 public void testGetInstanceAtomicBuildingBlocks_NullComponentInstance() {
198 assertEmpty(mergeInstanceUtils.getInstanceAtomicBuildingBlocks(null));
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));
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));
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));
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());
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());
250 public void testGetAtomicBuildingBlocks_noDuplication() {
251 GroupDefinition group1FirstCopy = createGroup("grp1", "grp1Id");
252 GroupDefinition group1SecondCopy = createGroup("grp1", "grp1Id");
254 ComponentInstance cmtInst1FirstCopy = createVfcInstance("inst1", "inst1Uid");
255 ComponentInstance cmtInst1SecondCopy = createVfcInstance("inst1", "inst1Uid");
257 ComponentInstance vfi = createVfInstance("vfi", cmtInst1FirstCopy, group1FirstCopy);
258 ComponentInstance vfi2 = createVfInstance("vfi2", cmtInst1SecondCopy, group1SecondCopy);
260 Service service = new ServiceBuilder()
261 .addComponentInstance(vfi)
262 .addComponentInstance(vfi2)
263 .setUniqueId("service1")
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));
274 public void testGetAtomicBuildingBlocks_ComponentNull_InstanceComponent() {
275 assertEmpty(mergeInstanceUtils.getInstanceAtomicBuildingBlocks(null, new Resource()));
279 public void testGetInstanceAtomicBuildingBlocks_ComponentInstance_NullComponent() {
280 assertEmpty(mergeInstanceUtils.getInstanceAtomicBuildingBlocks(new ComponentInstance(), null));
284 public void testGetInstanceAtomicBuildingBlocks_NullComponentInstance_NullComponent() {
285 assertEmpty(mergeInstanceUtils.getInstanceAtomicBuildingBlocks(null, null));
288 private void assertEmpty(ComponentInstanceBuildingBlocks componentInstanceBuildingBlocks) {
289 assertThat(componentInstanceBuildingBlocks)
290 .extracting("vfcInstances", "groups")
291 .containsExactly(emptyList(), emptyList());
294 private ComponentInstance createVfcInstance(String instId, String instUid) {
295 ComponentInstance vfci = createComponentInstance(instId, instUid, false);
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));
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);
312 private Component createVf(String id, ComponentInstance instance, GroupDefinition group) {
313 return new ResourceBuilder()
314 .setResourceType(ResourceTypeEnum.VF)
315 .setComponentType(ComponentTypeEnum.RESOURCE)
317 .addComponentInstance(instance)
322 private ComponentInstance createComponentInstance(String uniqueId, String componentUid, boolean isProxy) {
323 ComponentInstance componentInstance = new ComponentInstance();
324 componentInstance.setUniqueId(uniqueId);
325 componentInstance.setIsProxy(isProxy);
327 componentInstance.setSourceModelUid(componentUid);
329 componentInstance.setComponentUid(componentUid);
331 return componentInstance;
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);
342 private GroupDefinition createGroup(String invariantName, String id) {
343 return GroupDefinitionBuilder.create()
344 .setInvariantName(invariantName)
346 .setName(id + "name")