2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.merge.instance;
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;
41 import java.util.Collections;
42 import java.util.HashMap;
43 import java.util.List;
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;
52 public class ComponentInstanceInterfacesMergeTest {
55 private ComponentInstanceInterfacesMerge componentInstanceInterfacesMerge;
58 private DataForMergeHolder dataHolder;
61 private ComponentsUtils componentsUtils;
64 private Component containerComponent;
67 private ToscaOperationFacade toscaOperationFacade;
69 private Component updatedContainer;
70 private Component origContainer;
71 private Component origComponent;
72 private ComponentInstance currentResourceInstance;
73 private ComponentInstanceInterface origComponentInstanceInterface;
74 private ComponentInstanceInterface newComponentInstanceInterface;
78 public void setUpData() {
79 MockitoAnnotations.initMocks(this);
82 user.setRole(UserRoleEnum.ADMIN.getName());
84 currentResourceInstance = new ComponentInstance();
85 currentResourceInstance.setUniqueId("TestUniqueID1");
86 currentResourceInstance.setComponentUid("TestUID1");
88 origComponent = new Service();
89 origComponent.setUniqueId("TestUniqueID12");
91 dataHolder = new DataForMergeHolder();
92 dataHolder.setOrigInstanceNode(origComponent);
94 Map<String, InterfaceDefinition> origInterfaceDefinition =
95 InterfaceOperationTestUtils.createMockInterfaceDefinitionMap("Interface1", "Operation1", "Operation1");
96 origComponentInstanceInterface = new ComponentInstanceInterface("TestService1", origInterfaceDefinition.get("Interface1"));
98 Map<String, InterfaceDefinition> newInterfaceDefinition =
99 InterfaceOperationTestUtils.createMockInterfaceDefinitionMap("Interface2", "Operation2", "Operation2");
100 newComponentInstanceInterface = new ComponentInstanceInterface("TestService2", newInterfaceDefinition.get("Interface2"));
102 when(toscaOperationFacade.updateComponentInstanceInterfaces(any(), anyString())).thenReturn(StorageOperationStatus.OK);
103 when(componentsUtils.convertFromStorageResponse(any())).thenReturn(ActionStatus.OK);
105 ComponentInstance componentInstance = new ComponentInstance();
106 componentInstance.setUniqueId("CI_1");
107 componentInstance.setInterfaces((Map) newInterfaceDefinition);
109 Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces = new HashMap<>();
110 componentInstanceInterfaces.put(componentInstance.getUniqueId(), Collections.singletonList(newComponentInstanceInterface));
112 updatedContainer = new Service();
113 updatedContainer.setComponentInstances(Collections.singletonList(componentInstance));
114 updatedContainer.setComponentInstancesInterfaces(componentInstanceInterfaces);
116 origContainer = new Service();
117 origContainer.setComponentInstances(Collections.singletonList(componentInstance));
118 origContainer.setComponentInstancesInterfaces(componentInstanceInterfaces);
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));
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"));