Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / instance / ComponentInstanceRelationMergeTest.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.instance;
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.ArgumentCaptor;
28 import org.mockito.Captor;
29 import org.mockito.Mock;
30 import org.mockito.Mockito;
31 import org.mockito.junit.MockitoJUnitRunner;
32 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
33 import org.openecomp.sdc.be.components.impl.utils.ExceptionUtils;
34 import org.openecomp.sdc.be.components.merge.utils.MergeInstanceUtils;
35 import org.openecomp.sdc.be.components.utils.GroupDefinitionBuilder;
36 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
37 import org.openecomp.sdc.be.dao.api.ActionStatus;
38 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
39 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
40 import org.openecomp.sdc.be.impl.ComponentsUtils;
41 import org.openecomp.sdc.be.model.CapabilityDefinition;
42 import org.openecomp.sdc.be.model.CapabilityRequirementRelationship;
43 import org.openecomp.sdc.be.model.Component;
44 import org.openecomp.sdc.be.model.ComponentInstance;
45 import org.openecomp.sdc.be.model.GroupDefinition;
46 import org.openecomp.sdc.be.model.RelationshipImpl;
47 import org.openecomp.sdc.be.model.RelationshipInfo;
48 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
49 import org.openecomp.sdc.be.model.RequirementDefinition;
50 import org.openecomp.sdc.be.model.Resource;
51 import org.openecomp.sdc.be.model.Service;
52 import org.openecomp.sdc.be.model.User;
53 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
54 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
55 import org.openecomp.sdc.exception.ResponseFormat;
56
57 import java.util.ArrayList;
58 import java.util.Collections;
59 import java.util.HashMap;
60 import java.util.LinkedList;
61 import java.util.List;
62 import java.util.Map;
63 import java.util.stream.IntStream;
64
65 import static org.assertj.core.api.Assertions.assertThat;
66 import static org.assertj.core.api.Assertions.tuple;
67 import static org.junit.Assert.assertEquals;
68 import static org.junit.Assert.assertNotNull;
69 import static org.junit.Assert.assertNull;
70 import static org.junit.Assert.assertTrue;
71 import static org.mockito.ArgumentMatchers.any;
72 import static org.mockito.Mockito.verify;
73 import static org.mockito.Mockito.when;
74
75 @RunWith(MockitoJUnitRunner.class)
76 public class ComponentInstanceRelationMergeTest {
77     private ComponentInstanceRelationMerge compInstanceRelationMerge;
78     @Mock
79     private DataForMergeHolder dataHolder;
80     private Component containerComponent;
81     private Component updatedContainerComponent;
82     private ComponentInstance currentResourceInstance;
83
84     private RequirementCapabilityRelDef requirementDef1;
85     private RequirementCapabilityRelDef requirementDef2;
86     private RequirementCapabilityRelDef capabilityDef1;
87     private RequirementCapabilityRelDef capabilityDef2;
88     private RequirementCapabilityRelDef capabilityDef3;
89     
90     @Mock
91     private ComponentsUtils componentsUtils;
92     
93     @Mock
94     private ToscaOperationFacade toscaOperationFacade;
95
96     @Mock
97     private User user;
98
99     @Mock
100     private ExceptionUtils exceptionUtils;
101     
102     @Captor
103     private ArgumentCaptor<ContainerRelationsMergeInfo> argumentCaptor;
104     
105     
106     @Before
107     public void startUp() {
108         MergeInstanceUtils mergeInstanceUtils = new MergeInstanceUtils(toscaOperationFacade, exceptionUtils);
109         compInstanceRelationMerge = new ComponentInstanceRelationMerge(componentsUtils, mergeInstanceUtils, toscaOperationFacade);
110         
111         containerComponent = new Service();
112         List<RequirementCapabilityRelDef> resourceInstancesRelations = new ArrayList<>();
113
114         requirementDef1 = createRequirementDef("SRV1.VF1.VFI_1", "SRV1.VF2.VFI_1", "SRV1.VF1.VFC_1.VFCI_1", "Requirement1");
115         resourceInstancesRelations.add(requirementDef1);
116         requirementDef2 = createRequirementDef("SRV1.VF1.VFI_1", "SRV1.VF3.VFI_1", "SRV1.VF1.VFC_2.VFCI_2", "Requirement2");
117         resourceInstancesRelations.add(requirementDef2);
118         
119         
120         capabilityDef1 = createCapabilityDef("SRV1.VF4.VFI_1", "SRV1.VF1.VFI_1", "SRV1.VF1.VFC_3.VFCI_3", "Capability3");
121         resourceInstancesRelations.add(capabilityDef1);
122         capabilityDef2 = createCapabilityDef("SRV1.VF5.VFI_1", "SRV1.VF1.VFI_1", "SRV1.VF1.VFC_4.VFCI_1", "Capability4");
123         resourceInstancesRelations.add(capabilityDef2);
124
125
126         capabilityDef3 = createCapabilityDef("SRV1.VF5.VFI_1", "SRV1.VF1.VFI_1", "grp1Id", "Capability5");
127         resourceInstancesRelations.add(capabilityDef3);
128
129         containerComponent.setComponentInstancesRelations(resourceInstancesRelations );
130         
131         currentResourceInstance = new ComponentInstance();
132         currentResourceInstance.setUniqueId("SRV1.VF1.VFI_1");
133         currentResourceInstance.setComponentUid("SRV1.VF1");
134         currentResourceInstance.setIsProxy(false);
135         
136         updatedContainerComponent = new Service();
137         updatedContainerComponent.setUniqueId("123123123123123123");
138     }
139     
140     @Test
141     public void testSaveDataBeforeMerge() {
142         Resource vf = new ResourceBuilder()
143                 .setResourceType(ResourceTypeEnum.VF)
144                 .setComponentType(ComponentTypeEnum.RESOURCE)
145                 .addComponentInstance(createVfci("vfc_A", "SRV1.VF1.VFC_1.VFCI_1", "SRV1.VF1.VFC_1", true))
146                 .addComponentInstance(createVfci("vfc_B", "SRV1.VF1.VFC_2.VFCI_2", "SRV1.VF1.VFC_2", true))
147                 .addComponentInstance(createVfci("vfc_C", "SRV1.VF1.VFC_3.VFCI_3", "SRV1.VF1.VFC_3", false))
148                 .addComponentInstance(createVfci("vfc_D", "SRV1.VF1.VFC_4.VFCI_1", "SRV1.VF1.VFC_4", true))
149                 .addGroup(createGroup("grp1", "grp1Id"))
150                 .build();
151
152         compInstanceRelationMerge.saveDataBeforeMerge(dataHolder, containerComponent, currentResourceInstance, vf);
153         
154         verify(dataHolder).setVfRelationsInfo(argumentCaptor.capture());
155         ContainerRelationsMergeInfo relationsMergeInfo = argumentCaptor.getValue();
156         List<RelationMergeInfo> fromRelationsMergeInfo = relationsMergeInfo.getFromRelationsInfo();
157         List<RelationMergeInfo> toRelationsMergeInfo = relationsMergeInfo.getToRelationsInfo();
158         
159         assertNotNull("Expected not null list of relations merge info", fromRelationsMergeInfo);
160         assertNotNull("Expected not null list of relations merge info", toRelationsMergeInfo);
161         
162         assertEquals("Expected 2 elements", 2, fromRelationsMergeInfo.size());
163         assertEquals("Expected 2 elements", 2, toRelationsMergeInfo.size());
164     }
165     
166     @Test
167     public void testSaveDataBeforeMerge_RelationsNull() {
168         Resource vf = new Resource();
169
170         List<ComponentInstance> vfcInstances = new ArrayList<>();
171         vfcInstances.add(createVfci("vfc_A", "SRV1.VF1.VFC_1.VFCI_1", "SRV1.VF1.VFC_1", true));
172         vfcInstances.add(createVfci("vfc_B", "SRV1.VF1.VFC_2.VFCI_2", "SRV1.VF1.VFC_2", true));
173         vfcInstances.add(createVfci("vfc_C", "SRV1.VF1.VFC_3.VFCI_3", "SRV1.VF1.VFC_3", false));
174         vfcInstances.add(createVfci("vfc_D", "SRV1.VF1.VFC_4.VFCI_1", "SRV1.VF1.VFC_4", true));
175         vf.setComponentInstances(vfcInstances);
176         vf.setComponentType(ComponentTypeEnum.RESOURCE);
177         vf.setResourceType(ResourceTypeEnum.VF);
178         
179         containerComponent.setComponentInstancesRelations(null);
180         compInstanceRelationMerge.saveDataBeforeMerge(dataHolder, containerComponent, currentResourceInstance, vf);
181         
182         verify(dataHolder, Mockito.never()).setVfRelationsInfo(Mockito.any());
183     }
184     
185     @Test
186     public void testSaveDataBeforeMerge_RelationsEmptyList() {
187         Resource vf = new Resource();
188
189         List<ComponentInstance> vfcInstances = new ArrayList<>();
190         vfcInstances.add(createVfci("vfc_A", "SRV1.VF1.VFC_1.VFCI_1", "SRV1.VF1.VFC_1", true));
191         vfcInstances.add(createVfci("vfc_B", "SRV1.VF1.VFC_2.VFCI_2", "SRV1.VF1.VFC_2", true));
192         vfcInstances.add(createVfci("vfc_C", "SRV1.VF1.VFC_3.VFCI_3", "SRV1.VF1.VFC_3", false));
193         vfcInstances.add(createVfci("vfc_D", "SRV1.VF1.VFC_4.VFCI_1", "SRV1.VF1.VFC_4", true));
194         vf.setComponentInstances(vfcInstances);
195         vf.setComponentType(ComponentTypeEnum.RESOURCE);
196         vf.setResourceType(ResourceTypeEnum.VF);
197         
198         containerComponent.setComponentInstancesRelations(Collections.emptyList());
199         compInstanceRelationMerge.saveDataBeforeMerge(dataHolder, containerComponent, currentResourceInstance, vf);
200         
201         verify(dataHolder, Mockito.never()).setVfRelationsInfo(Mockito.any());
202     }
203     
204     @Test
205     public void testMergeDataAfterCreate_NoSavedData() {
206         when(dataHolder.getContainerRelationsMergeInfo()).thenReturn(null);
207         compInstanceRelationMerge.mergeDataAfterCreate(user, dataHolder, updatedContainerComponent, "SRV1.VF1.VFI_2");
208         List<RequirementCapabilityRelDef> relations = updatedContainerComponent.getComponentInstancesRelations();
209         assertNull("Expected no relations", relations);
210     }
211
212     @Test
213     public void testMergeDataAfterCreate() {
214         Resource newInstanceOriginVf = new ResourceBuilder()
215                 .setResourceType(ResourceTypeEnum.VF)
216                 .setComponentType(ComponentTypeEnum.RESOURCE)
217                 .addComponentInstance(createVfci("vfc_A", "SRV1.VF1.VFC_1.VFCI_1", "SRV1.VF1.VFC_1", true))
218                 .addComponentInstance(createVfci("vfc_B", "SRV1.VF1.VFC_2.VFCI_2", "SRV1.VF1.VFC_2", true))
219                 .addComponentInstance(createVfci("vfc_C", "SRV1.VF1.VFC_3.VFCI_3", "SRV1.VF1.VFC_3", false))
220                 .addComponentInstance(createVfci("vfc_D", "SRV1.VF1.VFC_4.VFCI_1", "SRV1.VF1.VFC_4", true))
221                 .addGroup(createGroup("grp1", "grp1Id"))
222                 .build();
223
224
225         List<ComponentInstance> componentInstances = new ArrayList<>();
226         componentInstances.add(createVfi("SRV1.VF1", "SRV1.VF1.VFI_2"));
227         updatedContainerComponent.setComponentInstances(componentInstances);
228
229         List<RequirementCapabilityRelDef> resourceInstancesRelations = new ArrayList<>();
230         updatedContainerComponent.setComponentInstancesRelations(resourceInstancesRelations);
231
232         List<RelationMergeInfo> fromRelationsMergeInfo = new ArrayList<>();
233         List<RelationMergeInfo> toRelationsMergeInfo = new ArrayList<>();
234         
235         RelationMergeInfo relationMergeInfo1 = new RelationMergeInfo("CapabilityType1", "capabilityA", "vfc_A", requirementDef1);
236         fromRelationsMergeInfo.add(relationMergeInfo1);
237         RelationMergeInfo relationMergeInfo2 = new RelationMergeInfo("CapabilityType4", "capabilityD", "vfc_D", capabilityDef2);
238         toRelationsMergeInfo.add(relationMergeInfo2);
239
240         RelationMergeInfo relationMergeInfo3 = new RelationMergeInfo("CapabilityType5", "capabilityE", "grp1", capabilityDef3);
241         toRelationsMergeInfo.add(relationMergeInfo3);
242         
243         ContainerRelationsMergeInfo relationsMergeInfo = new ContainerRelationsMergeInfo(fromRelationsMergeInfo, toRelationsMergeInfo);
244
245         when(toscaOperationFacade.getToscaElement("SRV1.VF1")).thenReturn(Either.left(newInstanceOriginVf));
246         when(dataHolder.getContainerRelationsMergeInfo()).thenReturn(relationsMergeInfo);
247         when(toscaOperationFacade.associateResourceInstances(any(), Mockito.anyString(), Mockito.anyList())).thenReturn(Either.left(resourceInstancesRelations));
248         Component mergeResult = compInstanceRelationMerge.mergeDataAfterCreate(user, dataHolder, updatedContainerComponent, "SRV1.VF1.VFI_2");
249         assertTrue(mergeResult != null);
250         List<RequirementCapabilityRelDef> relations = updatedContainerComponent.getComponentInstancesRelations();
251         assertThat(relations)
252                 .containsExactlyInAnyOrder(requirementDef1, capabilityDef2, capabilityDef3)
253                 .extracting(relation -> relation.resolveSingleRelationship().getRelation().getCapabilityOwnerId(),
254                             relation -> relation.resolveSingleRelationship().getRelation().getRequirementOwnerId())
255                 .containsExactlyInAnyOrder(tuple("SRV1.VF1.VFC_4.VFCI_1", null),
256                                            tuple("grp1Id", null),
257                                            tuple(null, "SRV1.VF1.VFC_1.VFCI_1"));
258     }
259     
260     
261     @Test(expected = ComponentException.class)
262     public void testMergeDataAfterCreate_FailedToAssociateResourceInstances() {
263         Resource vf = new Resource();
264
265         List<ComponentInstance> vfcInstances = new ArrayList<>();
266         vfcInstances.add(createVfci("vfc_A", "SRV1.VF1.VFC_1.VFCI_1", "SRV1.VF1.VFC_1", true));
267         vfcInstances.add(createVfci("vfc_B", "SRV1.VF1.VFC_2.VFCI_2", "SRV1.VF1.VFC_2", true));
268         vfcInstances.add(createVfci("vfc_C", "SRV1.VF1.VFC_3.VFCI_3", "SRV1.VF1.VFC_3", false));
269         vfcInstances.add(createVfci("vfc_D", "SRV1.VF1.VFC_4.VFCI_1", "SRV1.VF1.VFC_4", true));
270         vf.setComponentInstances(vfcInstances);
271         vf.setComponentType(ComponentTypeEnum.RESOURCE);
272         vf.setResourceType(ResourceTypeEnum.VF);
273         
274         Either<Component, StorageOperationStatus> eitherVF = Either.left(vf);
275         when(toscaOperationFacade.getToscaElement("SRV1.VF1")).thenReturn(eitherVF);
276         
277         
278         List<RelationMergeInfo> fromRelationsMergeInfo = new ArrayList<>();
279         List<RelationMergeInfo> toRelationsMergeInfo = new ArrayList<>();
280         
281         RelationMergeInfo relationMergeInfo1 = new RelationMergeInfo("CapabilityType1", "capabilityA", "vfc_A", requirementDef1);
282         fromRelationsMergeInfo.add(relationMergeInfo1);
283         RelationMergeInfo relationMergeInfo2 = new RelationMergeInfo("CapabilityType4", "capabilityD", "vfc_D", capabilityDef2);
284         toRelationsMergeInfo.add(relationMergeInfo2);
285         
286         ContainerRelationsMergeInfo relationsMergeInfo = new ContainerRelationsMergeInfo(fromRelationsMergeInfo, toRelationsMergeInfo);
287         
288         when(dataHolder.getContainerRelationsMergeInfo()).thenReturn(relationsMergeInfo);
289         
290         List<ComponentInstance> componentInstances = new ArrayList<>();
291         componentInstances.add(createVfi("SRV1.VF1", "SRV1.VF1.VFI_2"));
292         componentInstances.add(createVfi("SRV1.VF2", "SRV1.VF2.VFI_1"));
293         componentInstances.add(createVfi("SRV1.VF3", "SRV1.VF3.VFI_1"));
294         componentInstances.add(createVfi("SRV1.VF4", "SRV1.VF4.VFI_1"));
295         componentInstances.add(createVfi("SRV1.VF5", "SRV1.VF5.VFI_1"));
296         updatedContainerComponent.setComponentInstances(componentInstances);
297         
298         List<RequirementCapabilityRelDef> resourceInstancesRelations = new ArrayList<>();
299         updatedContainerComponent.setComponentInstancesRelations(resourceInstancesRelations);
300         
301         when(toscaOperationFacade.associateResourceInstances(any(), Mockito.anyString(), Mockito.anyList())).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
302         when(componentsUtils.convertFromStorageResponse(Mockito.any())).thenReturn(ActionStatus.GENERAL_ERROR);
303         
304         ResponseFormat expectedResponseFormat = new ResponseFormat();
305         when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR, updatedContainerComponent.getUniqueId())).thenReturn(expectedResponseFormat );
306
307         compInstanceRelationMerge.mergeDataAfterCreate(user, dataHolder, updatedContainerComponent, "SRV1.VF1.VFI_2");
308     }
309     
310     @Test
311     public void testMergeDataAfterCreate_UpdatedContainerEmpty() {
312         List<RelationMergeInfo> fromRelationsMergeInfo = new ArrayList<>();
313         List<RelationMergeInfo> toRelationsMergeInfo = new ArrayList<>();
314         
315         RelationMergeInfo relationMergeInfo1 = new RelationMergeInfo("CapabilityType1", "capabilityA", "vfc_A", requirementDef1);
316         fromRelationsMergeInfo.add(relationMergeInfo1);
317         RelationMergeInfo relationMergeInfo2 = new RelationMergeInfo("CapabilityType4", "capabilityD", "vfc_D", capabilityDef2);
318         toRelationsMergeInfo.add(relationMergeInfo2);
319         
320         ContainerRelationsMergeInfo relationsMergeInfo = new ContainerRelationsMergeInfo(fromRelationsMergeInfo, toRelationsMergeInfo);
321         
322         when(dataHolder.getContainerRelationsMergeInfo()).thenReturn(relationsMergeInfo);
323         
324         Component result = compInstanceRelationMerge.mergeDataAfterCreate(user, dataHolder, updatedContainerComponent, "SRV1.VF1.VFI_2");
325         assertEquals(updatedContainerComponent, result);
326     }
327     
328     @Test
329     public void testMergeDataAfterCreate_OwnerChanged() {
330         Resource vf = new Resource();
331
332         List<ComponentInstance> vfcInstances = new ArrayList<>();
333         vfcInstances.add(createVfci("vfc_A", "SRV1.VF1.VFC_1.VFCI_2", "SRV1.VF1.VFC_1", true));
334         vfcInstances.add(createVfci("vfc_B", "SRV1.VF1.VFC_2.VFCI_2", "SRV1.VF1.VFC_2", true));
335         vfcInstances.add(createVfci("vfc_C", "SRV1.VF1.VFC_3.VFCI_3", "SRV1.VF1.VFC_3", false));
336         vfcInstances.add(createVfci("vfc_D", "SRV1.VF1.VFC_4.VFCI_2", "SRV1.VF1.VFC_4", true));
337         vf.setComponentInstances(vfcInstances);
338         vf.setComponentType(ComponentTypeEnum.RESOURCE);
339         vf.setResourceType(ResourceTypeEnum.VF);
340         
341         Either<Component, StorageOperationStatus> eitherVF = Either.left(vf);
342         when(toscaOperationFacade.getToscaElement("SRV1.VF1")).thenReturn(eitherVF);
343         
344         
345         List<RelationMergeInfo> fromRelationsMergeInfo = new ArrayList<>();
346         List<RelationMergeInfo> toRelationsMergeInfo = new ArrayList<>();
347         
348         RelationMergeInfo relationMergeInfo1 = new RelationMergeInfo("CapabilityType1", "capabilityA", "vfc_A", requirementDef1);
349         fromRelationsMergeInfo.add(relationMergeInfo1);
350         RelationMergeInfo relationMergeInfo2 = new RelationMergeInfo("CapabilityType4", "capabilityD", "vfc_D", capabilityDef2);
351         toRelationsMergeInfo.add(relationMergeInfo2);
352         
353         ContainerRelationsMergeInfo relationsMergeInfo = new ContainerRelationsMergeInfo(fromRelationsMergeInfo, toRelationsMergeInfo);
354         
355         when(dataHolder.getContainerRelationsMergeInfo()).thenReturn(relationsMergeInfo);
356         
357         List<ComponentInstance> componentInstances = new ArrayList<>();
358         componentInstances.add(createVfi("SRV1.VF1", "SRV1.VF1.VFI_2"));
359         componentInstances.add(createVfi("SRV1.VF2", "SRV1.VF2.VFI_1"));
360         componentInstances.add(createVfi("SRV1.VF3", "SRV1.VF3.VFI_1"));
361         componentInstances.add(createVfi("SRV1.VF4", "SRV1.VF4.VFI_1"));
362         componentInstances.add(createVfi("SRV1.VF5", "SRV1.VF5.VFI_1"));
363         updatedContainerComponent.setComponentInstances(componentInstances);
364         
365         List<RequirementCapabilityRelDef> resourceInstancesRelations = new ArrayList<>();
366         updatedContainerComponent.setComponentInstancesRelations(resourceInstancesRelations);
367         
368         when(toscaOperationFacade.associateResourceInstances(any(),Mockito.anyString(), Mockito.anyList())).thenReturn(Either.left(resourceInstancesRelations));
369         
370         compInstanceRelationMerge.mergeDataAfterCreate(user, dataHolder, updatedContainerComponent, "SRV1.VF1.VFI_2");
371         
372         verify(dataHolder).getContainerRelationsMergeInfo();
373         verify(toscaOperationFacade).associateResourceInstances(any(), Mockito.anyString(), Mockito.anyList());
374         
375         List<RequirementCapabilityRelDef> relations = updatedContainerComponent.getComponentInstancesRelations();
376         assertEquals("Expected 2 relations", 2, relations.size());
377         
378         
379         RequirementCapabilityRelDef capabilityRelDef = relations.get(0);
380         assertEquals("SRV1.VF1.VFC_4.VFCI_2", capabilityRelDef.resolveSingleRelationship().getRelation().getCapabilityOwnerId());
381         assertEquals("SRV1.VF5.VFI_1", capabilityRelDef.getFromNode());
382         assertEquals("SRV1.VF1.VFI_2", capabilityRelDef.getToNode());
383         
384         RequirementCapabilityRelDef requirementRelDef = relations.get(1);
385         assertEquals("SRV1.VF1.VFC_1.VFCI_2", requirementRelDef.resolveSingleRelationship().getRelation().getRequirementOwnerId());
386         assertEquals("SRV1.VF1.VFI_2", requirementRelDef.getFromNode());
387         assertEquals("SRV1.VF2.VFI_1", requirementRelDef.getToNode());
388     }
389
390
391     /**
392      * @param vfId
393      * @param vfiUniqueId
394      * @return
395      */
396     private ComponentInstance createVfi(String vfId, String vfiUniqueId) {
397         ComponentInstance vfi = new ComponentInstance();
398         vfi.setUniqueId(vfiUniqueId);
399         vfi.setComponentUid(vfId);
400         
401         Resource vf = new Resource();
402         vf.setUniqueId(vfId);
403         return vfi;
404     }
405     
406     private ComponentInstance createVfci(String name, String uniqueId, String componentUid, boolean foundVfc) {
407         ComponentInstance compInst = new ComponentInstance();
408         compInst.setName(name);
409         compInst.setUniqueId(uniqueId);
410         compInst.setComponentUid(componentUid);
411         
412         if(foundVfc) {
413             createVfc(componentUid);
414         }
415         else {
416             failLoadVfc(componentUid);
417         }
418         return compInst;
419     }
420
421     public GroupDefinition createGroup(String name, String id) {
422         return GroupDefinitionBuilder.create()
423                 .setUniqueId(id)
424                 .setInvariantName(name)
425                 .build();
426
427     }
428     
429     private void failLoadVfc(String uid) {
430         Either<Component, StorageOperationStatus> eitherVFC = Either.right(StorageOperationStatus.NOT_FOUND);
431         when(toscaOperationFacade.getToscaElement(uid)).thenReturn(eitherVFC);
432     }
433
434     private Component createVfc(String uid) {
435         Resource vfc = new Resource();
436         vfc.setComponentType(ComponentTypeEnum.RESOURCE);
437         vfc.setUniqueId(uid);
438         
439         Map<String, List<CapabilityDefinition>> capabilities = new HashMap<>();
440         Map<String, List<RequirementDefinition>> requirements = new HashMap<>();;
441         IntStream.range(0, 5).forEach(i -> {
442             
443             List<CapabilityDefinition> capList = new LinkedList<>();
444             capList.add(null);
445             CapabilityDefinition capDef = new CapabilityDefinition();
446             capDef.setName("CapabilityName" + i);
447             capDef.setUniqueId("Capability" + i);
448             capDef.setType("CapabilityType" + i);
449             capList.add(capDef);
450             capabilities.put("Key" + i, capList);
451             
452             List<RequirementDefinition> reqList = new LinkedList<>();
453             reqList.add(null);
454             RequirementDefinition reqDef = new RequirementDefinition();
455             reqDef.setName("RequirementName" + i);
456             reqDef.setUniqueId("Requirement" + i);
457             reqDef.setCapability("CapabilityType" + i);
458             reqList.add(reqDef);
459             requirements.put("Key" + i, reqList);
460             
461         });
462         vfc.setCapabilities(capabilities );
463         vfc.setRequirements(requirements);
464         
465         Either<Component, StorageOperationStatus> eitherVFC = Either.left(vfc);
466         when(toscaOperationFacade.getToscaElement(uid)).thenReturn(eitherVFC);
467         
468         return vfc;
469     }
470     
471     private RequirementCapabilityRelDef createRequirementCapabilityDef(String fromNode, String toNode) {
472         RequirementCapabilityRelDef reqCapDef = new RequirementCapabilityRelDef();
473         
474         reqCapDef.setFromNode(fromNode);
475         reqCapDef.setToNode(toNode);
476         
477         List<CapabilityRequirementRelationship> relationships = new ArrayList<>();
478         CapabilityRequirementRelationship capabilityRequirementRelationship = new CapabilityRequirementRelationship();
479         relationships.add(capabilityRequirementRelationship);
480         reqCapDef.setRelationships(relationships);
481         
482         return reqCapDef;
483     }
484
485     private RequirementCapabilityRelDef createRequirementDef(String fromNode, String toNode, String ownerId, String requirementUid) {
486         RequirementCapabilityRelDef reqCapDef = createRequirementCapabilityDef(fromNode, toNode);
487         CapabilityRequirementRelationship capabilityRequirementRelationship = reqCapDef.resolveSingleRelationship();
488         
489         RelationshipInfo relationshipInfo = new RelationshipInfo();
490         relationshipInfo.setRequirementOwnerId(ownerId);
491         relationshipInfo.setRequirementUid(requirementUid);
492         relationshipInfo.setRelationships(new RelationshipImpl());
493         capabilityRequirementRelationship.setRelation(relationshipInfo );
494         
495         
496         
497         return reqCapDef;
498     }
499     
500     private RequirementCapabilityRelDef createCapabilityDef(String fromNode, String toNode, String ownerId, String capabilityUid) {
501         RequirementCapabilityRelDef reqCapDef = createRequirementCapabilityDef(fromNode, toNode);
502         CapabilityRequirementRelationship capabilityRequirementRelationship = reqCapDef.resolveSingleRelationship();
503         
504         RelationshipInfo relationshipInfo = new RelationshipInfo();
505         relationshipInfo.setCapabilityOwnerId(ownerId);
506         relationshipInfo.setCapabilityUid(capabilityUid);
507         relationshipInfo.setRelationships(new RelationshipImpl());
508         capabilityRequirementRelationship.setRelation(relationshipInfo );
509         
510         return reqCapDef;
511     }
512
513     
514
515 }