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