Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / instance / ComponentInstanceInterfacesMergeTest.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 org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.impl.ComponentsUtils;
30 import org.openecomp.sdc.be.model.Component;
31 import org.openecomp.sdc.be.model.ComponentInstance;
32 import org.openecomp.sdc.be.model.ComponentInstanceInterface;
33 import org.openecomp.sdc.be.model.InterfaceDefinition;
34 import org.openecomp.sdc.be.model.Service;
35 import org.openecomp.sdc.be.model.User;
36 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
37 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
38 import org.openecomp.sdc.common.api.UserRoleEnum;
39 import org.openecomp.sdc.test.utils.InterfaceOperationTestUtils;
40
41 import java.util.Collections;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.mockito.ArgumentMatchers.any;
48 import static org.mockito.ArgumentMatchers.anyString;
49 import static org.mockito.Mockito.doReturn;
50 import static org.mockito.Mockito.when;
51
52 public class ComponentInstanceInterfacesMergeTest {
53
54   @InjectMocks
55   private ComponentInstanceInterfacesMerge componentInstanceInterfacesMerge;
56
57   @Mock
58   private DataForMergeHolder dataHolder;
59
60   @Mock
61   private ComponentsUtils componentsUtils;
62
63   @Mock
64   private Component containerComponent;
65
66   @Mock
67   private ToscaOperationFacade toscaOperationFacade;
68
69   private Component updatedContainer;
70   private Component origContainer;
71   private Component origComponent;
72   private ComponentInstance currentResourceInstance;
73   private ComponentInstanceInterface origComponentInstanceInterface;
74   private ComponentInstanceInterface newComponentInstanceInterface;
75   private User user;
76
77   @Before
78   public void setUpData() {
79     MockitoAnnotations.initMocks(this);
80     user = new User();
81     user.setUserId("44");
82     user.setRole(UserRoleEnum.ADMIN.getName());
83
84     currentResourceInstance = new ComponentInstance();
85     currentResourceInstance.setUniqueId("TestUniqueID1");
86     currentResourceInstance.setComponentUid("TestUID1");
87
88     origComponent = new Service();
89     origComponent.setUniqueId("TestUniqueID12");
90
91     dataHolder = new DataForMergeHolder();
92     dataHolder.setOrigInstanceNode(origComponent);
93
94     Map<String, InterfaceDefinition> origInterfaceDefinition =
95             InterfaceOperationTestUtils.createMockInterfaceDefinitionMap("Interface1", "Operation1", "Operation1");
96     origComponentInstanceInterface = new ComponentInstanceInterface("TestService1", origInterfaceDefinition.get("Interface1"));
97
98     Map<String, InterfaceDefinition> newInterfaceDefinition =
99             InterfaceOperationTestUtils.createMockInterfaceDefinitionMap("Interface2", "Operation2", "Operation2");
100     newComponentInstanceInterface = new ComponentInstanceInterface("TestService2", newInterfaceDefinition.get("Interface2"));
101
102     when(toscaOperationFacade.updateComponentInstanceInterfaces(any(), anyString())).thenReturn(StorageOperationStatus.OK);
103     when(componentsUtils.convertFromStorageResponse(any())).thenReturn(ActionStatus.OK);
104
105     ComponentInstance componentInstance = new ComponentInstance();
106     componentInstance.setUniqueId("CI_1");
107     componentInstance.setInterfaces((Map) newInterfaceDefinition);
108
109     Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces = new HashMap<>();
110     componentInstanceInterfaces.put(componentInstance.getUniqueId(), Collections.singletonList(newComponentInstanceInterface));
111
112     updatedContainer = new Service();
113     updatedContainer.setComponentInstances(Collections.singletonList(componentInstance));
114     updatedContainer.setComponentInstancesInterfaces(componentInstanceInterfaces);
115
116     origContainer = new Service();
117     origContainer.setComponentInstances(Collections.singletonList(componentInstance));
118     origContainer.setComponentInstancesInterfaces(componentInstanceInterfaces);
119   }
120
121   @Test
122   public void saveDataBeforeMerge() {
123     doReturn(Collections.singletonList(origComponentInstanceInterface)).when(containerComponent).safeGetComponentInstanceInterfaces(any());
124     componentInstanceInterfacesMerge.saveDataBeforeMerge(dataHolder, containerComponent, currentResourceInstance, origComponent);
125     assertEquals(origComponent, dataHolder.getOrigInstanceNode());
126     assertEquals(origComponentInstanceInterface, dataHolder.getOrigComponentInstanceInterfaces().get(0));
127   }
128
129   @Test
130   public void mergeDataAfterCreate() {
131     doReturn(Collections.singletonList(origComponentInstanceInterface)).when(containerComponent).safeGetComponentInstanceInterfaces(any());
132     componentInstanceInterfacesMerge.saveDataBeforeMerge(dataHolder, containerComponent, currentResourceInstance, origComponent);
133     componentInstanceInterfacesMerge.mergeDataAfterCreate(user, dataHolder, updatedContainer, "CI_1");
134     assertEquals(updatedContainer.getComponentInstancesInterfaces().get("CI_1"), origContainer.getComponentInstancesInterfaces().get("CI_1"));
135   }
136 }