1 package org.openecomp.sdc.be.components.merge.instance;
3 import static org.junit.Assert.assertEquals;
4 import static org.mockito.ArgumentMatchers.any;
5 import static org.mockito.ArgumentMatchers.anyString;
6 import static org.mockito.Mockito.doReturn;
7 import static org.mockito.Mockito.when;
9 import java.util.Collections;
10 import java.util.HashMap;
11 import java.util.List;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.mockito.InjectMocks;
17 import org.mockito.Mock;
18 import org.mockito.MockitoAnnotations;
19 import org.openecomp.sdc.be.dao.api.ActionStatus;
20 import org.openecomp.sdc.be.impl.ComponentsUtils;
21 import org.openecomp.sdc.be.model.Component;
22 import org.openecomp.sdc.be.model.ComponentInstance;
23 import org.openecomp.sdc.be.model.ComponentInstanceInterface;
24 import org.openecomp.sdc.be.model.InterfaceDefinition;
25 import org.openecomp.sdc.be.model.Service;
26 import org.openecomp.sdc.be.model.User;
27 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
28 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
29 import org.openecomp.sdc.common.api.UserRoleEnum;
30 import org.openecomp.sdc.test.utils.InterfaceOperationTestUtils;
32 public class ComponentInstanceInterfacesMergeTest {
35 private ComponentInstanceInterfacesMerge componentInstanceInterfacesMerge;
38 private DataForMergeHolder dataHolder;
41 private ComponentsUtils componentsUtils;
44 private Component containerComponent;
47 private ToscaOperationFacade toscaOperationFacade;
49 private Component updatedContainer;
50 private Component origContainer;
51 private Component origComponent;
52 private ComponentInstance currentResourceInstance;
53 private ComponentInstanceInterface origComponentInstanceInterface;
54 private ComponentInstanceInterface newComponentInstanceInterface;
58 public void setUpData() {
59 MockitoAnnotations.initMocks(this);
62 user.setRole(UserRoleEnum.ADMIN.getName());
64 currentResourceInstance = new ComponentInstance();
65 currentResourceInstance.setUniqueId("TestUniqueID1");
66 currentResourceInstance.setComponentUid("TestUID1");
68 origComponent = new Service();
69 origComponent.setUniqueId("TestUniqueID12");
71 dataHolder = new DataForMergeHolder();
72 dataHolder.setOrigInstanceNode(origComponent);
74 Map<String, InterfaceDefinition> origInterfaceDefinition =
75 InterfaceOperationTestUtils.createMockInterfaceDefinitionMap("Interface1", "Operation1", "Operation1");
76 origComponentInstanceInterface = new ComponentInstanceInterface("TestService1", origInterfaceDefinition.get("Interface1"));
78 Map<String, InterfaceDefinition> newInterfaceDefinition =
79 InterfaceOperationTestUtils.createMockInterfaceDefinitionMap("Interface2", "Operation2", "Operation2");
80 newComponentInstanceInterface = new ComponentInstanceInterface("TestService2", newInterfaceDefinition.get("Interface2"));
82 when(toscaOperationFacade.updateComponentInstanceInterfaces(any(), anyString())).thenReturn(StorageOperationStatus.OK);
83 when(componentsUtils.convertFromStorageResponse(any())).thenReturn(ActionStatus.OK);
85 ComponentInstance componentInstance = new ComponentInstance();
86 componentInstance.setUniqueId("CI_1");
87 componentInstance.setInterfaces((Map) newInterfaceDefinition);
89 Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces = new HashMap<>();
90 componentInstanceInterfaces.put(componentInstance.getUniqueId(), Collections.singletonList(newComponentInstanceInterface));
92 updatedContainer = new Service();
93 updatedContainer.setComponentInstances(Collections.singletonList(componentInstance));
94 updatedContainer.setComponentInstancesInterfaces(componentInstanceInterfaces);
96 origContainer = new Service();
97 origContainer.setComponentInstances(Collections.singletonList(componentInstance));
98 origContainer.setComponentInstancesInterfaces(componentInstanceInterfaces);
102 public void saveDataBeforeMerge() {
103 doReturn(Collections.singletonList(origComponentInstanceInterface)).when(containerComponent).safeGetComponentInstanceInterfaces(any());
104 componentInstanceInterfacesMerge.saveDataBeforeMerge(dataHolder, containerComponent, currentResourceInstance, origComponent);
105 assertEquals(origComponent, dataHolder.getOrigInstanceNode());
106 assertEquals(origComponentInstanceInterface, dataHolder.getOrigComponentInstanceInterfaces().get(0));
110 public void mergeDataAfterCreate() {
111 doReturn(Collections.singletonList(origComponentInstanceInterface)).when(containerComponent).safeGetComponentInstanceInterfaces(any());
112 componentInstanceInterfacesMerge.saveDataBeforeMerge(dataHolder, containerComponent, currentResourceInstance, origComponent);
113 componentInstanceInterfacesMerge.mergeDataAfterCreate(user, dataHolder, updatedContainer, "CI_1");
114 assertEquals(updatedContainer.getComponentInstancesInterfaces().get("CI_1"), origContainer.getComponentInstancesInterfaces().get("CI_1"));