Added oparent to sdc main
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / instance / ComponentInstancePropsAndInputsMergeTest.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.mockito.*;
27 import org.openecomp.sdc.be.components.utils.ObjectGenerator;
28 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
29 import org.openecomp.sdc.be.dao.api.ActionStatus;
30 import org.openecomp.sdc.be.impl.ComponentsUtils;
31 import org.openecomp.sdc.be.model.*;
32 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
33 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
34 import org.openecomp.sdc.exception.ResponseFormat;
35
36 import java.util.List;
37
38 import static org.junit.Assert.assertEquals;
39 import static org.junit.Assert.assertFalse;
40 import static org.mockito.ArgumentMatchers.*;
41 import static org.mockito.Mockito.verifyZeroInteractions;
42 import static org.mockito.Mockito.when;
43 public class ComponentInstancePropsAndInputsMergeTest {
44
45     private static final String INSTANCE_ID1 = "inst1";
46     private static final User USER = new User();
47
48     @InjectMocks
49     private ComponentInstancePropsAndInputsMerge testInstance;
50
51     @Mock
52     private ToscaOperationFacade toscaOperationFacade;
53
54     @Mock
55     private ComponentsUtils componentsUtils;
56
57     @Mock
58     private ComponentInstancePropertiesMergeBL componentInstancePropertiesMergeBL;
59
60     @Mock
61     private ComponentInstanceInputsMergeBL componentInstanceInputsMergeBL;
62
63     @Mock
64     private ComponentInstanceInputsRedeclareHandler componentInstanceInputsRedeclareHandler;
65
66     private Resource resourceToUpdate;
67
68     private DataForMergeHolder oldDataHolder;
69
70     @Before
71     public void setUp() throws Exception {
72         MockitoAnnotations.initMocks(this);
73         resourceToUpdate =  new ResourceBuilder().addInstanceInput(INSTANCE_ID1, "instInput1")
74                 .addInstanceInput(INSTANCE_ID1, "instInput2")
75                 .addInstanceProperty(INSTANCE_ID1, "instProp1")
76                 .addInstanceProperty(INSTANCE_ID1, "instProp2")
77                 .addInput("input1")
78                 .addInput("input2")
79                 .setUniqueId("resourceId").build();
80
81         List<InputDefinition> oldInputs = ObjectGenerator.buildInputs("input1");
82         List<ComponentInstanceProperty> oldInstProps = ObjectGenerator.buildInstanceProperties("instProp1", "instProp3");
83         List<ComponentInstanceInput> oldInstInputs = ObjectGenerator.buildInstanceInputs("instInput1", "instInput3");
84
85         oldDataHolder = new DataForMergeHolder();
86         oldDataHolder.setOrigComponentInputs(oldInputs);
87         oldDataHolder.setOrigComponentInstanceProperties(oldInstProps);
88         oldDataHolder.setOrigComponentInstanceInputs(oldInstInputs);
89     }
90
91     @Test
92     public void mergeDataAfterCreate() throws Exception {
93         List<InputDefinition> oldInputs = ObjectGenerator.buildInputs("input1");
94         List<ComponentInstanceProperty> oldInstProps = ObjectGenerator.buildInstanceProperties("instProp1", "instProp3");
95         List<ComponentInstanceInput> oldInstInputs = ObjectGenerator.buildInstanceInputs("instInput1", "instInput3");
96
97         DataForMergeHolder dataForMergeHolder = new DataForMergeHolder();
98         dataForMergeHolder.setOrigComponentInputs(oldInputs);
99         dataForMergeHolder.setOrigComponentInstanceProperties(oldInstProps);
100         dataForMergeHolder.setOrigComponentInstanceInputs(oldInstInputs);
101         Resource currInstanceOriginType = new Resource();
102         dataForMergeHolder.setCurrInstanceNode(currInstanceOriginType);
103
104         ArgumentCaptor<ComponentParametersView> parametersViewCaptor = ArgumentCaptor.forClass(ComponentParametersView.class);
105
106         when(toscaOperationFacade.getToscaElement(Mockito.eq("resourceId"), parametersViewCaptor.capture())).thenReturn(Either.left(resourceToUpdate));
107         when(componentInstanceInputsMergeBL.mergeComponentInstanceInputs(oldInstInputs, oldInputs, resourceToUpdate, INSTANCE_ID1)).thenReturn(ActionStatus.OK);
108         when(componentInstancePropertiesMergeBL.mergeComponentInstanceProperties(oldInstProps, oldInputs, resourceToUpdate, INSTANCE_ID1)).thenReturn(ActionStatus.OK);
109         when(componentInstanceInputsRedeclareHandler.redeclareComponentInputsForInstance(resourceToUpdate, INSTANCE_ID1, currInstanceOriginType, oldInputs)).thenReturn(ActionStatus.OK);
110         Either<Component, ResponseFormat> mergeResult = testInstance.mergeDataAfterCreate(USER, dataForMergeHolder, resourceToUpdate, INSTANCE_ID1);
111         assertEquals(mergeResult.left().value(), resourceToUpdate);
112         assertComponentFilter(parametersViewCaptor.getValue());
113     }
114
115     @Test
116     public void mergeDataAfterCreate_failedToMergeComponentInstanceInputs() throws Exception {
117         ResponseFormat errorResponse = new ResponseFormat();
118         when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(errorResponse);
119         when(componentInstanceInputsMergeBL.mergeComponentInstanceInputs(anyList(), anyList(), any(Component.class), anyString())).thenReturn(ActionStatus.GENERAL_ERROR);
120         Either<Component, ResponseFormat> mergeResult = testInstance.mergeDataAfterCreate(USER, new DataForMergeHolder(), new Service(), "inst1");
121         assertEquals(errorResponse, mergeResult.right().value());
122         verifyZeroInteractions(componentInstanceInputsRedeclareHandler, componentInstancePropertiesMergeBL, toscaOperationFacade);
123     }
124
125     @Test
126     public void mergeDataAfterCreate_failedToMergeComponentInstProps() throws Exception {
127         ResponseFormat errorResponse = new ResponseFormat();
128         when(componentInstanceInputsMergeBL.mergeComponentInstanceInputs(anyList(), anyList(), any(Component.class), anyString())).thenReturn(ActionStatus.OK);
129         when(componentInstancePropertiesMergeBL.mergeComponentInstanceProperties(anyList(), anyList(), any(Component.class), anyString())).thenReturn(ActionStatus.GENERAL_ERROR);
130         when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(errorResponse);
131         Either<Component, ResponseFormat> mergeResult = testInstance.mergeDataAfterCreate(USER, new DataForMergeHolder(), new Service(), "inst1");
132         assertEquals(errorResponse, mergeResult.right().value());
133         verifyZeroInteractions(componentInstanceInputsRedeclareHandler, toscaOperationFacade);
134     }
135
136     @Test
137     public void mergeDataAfterCreate_mergeInputs_FailedToFetchResource() throws Exception {
138         ResponseFormat errorResponse = new ResponseFormat();
139         when(componentInstanceInputsMergeBL.mergeComponentInstanceInputs(anyList(), anyList(), any(Component.class), anyString())).thenReturn(ActionStatus.OK);
140         when(componentInstancePropertiesMergeBL.mergeComponentInstanceProperties(anyList(), anyList(), any(Component.class), anyString())).thenReturn(ActionStatus.OK);
141         when(toscaOperationFacade.getToscaElement(any(), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
142         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
143         when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(errorResponse);
144         DataForMergeHolder dataHolder = new DataForMergeHolder();
145         dataHolder.setOrigComponentInputs(ObjectGenerator.buildInputs("input1", "input2"));
146         Either<Component, ResponseFormat> mergeResult = testInstance.mergeDataAfterCreate(USER, dataHolder, new Service(), "inst1");
147         assertEquals(errorResponse, mergeResult.right().value());
148         verifyZeroInteractions(componentInstanceInputsRedeclareHandler);
149     }
150
151     private void assertComponentFilter(ComponentParametersView value) {
152         assertFalse(value.isIgnoreComponentInstances());
153         assertFalse(value.isIgnoreComponentInstancesProperties());
154         assertFalse(value.isIgnoreComponentInstancesInputs());
155         assertFalse(value.isIgnoreArtifacts());
156     }
157 }