Added oparent to sdc main
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / input / ComponentInputsMergeBLTest.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.input;
22
23 import fj.data.Either;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mockito;
27 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
28 import org.openecomp.sdc.be.components.utils.ObjectGenerator;
29 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
30 import org.openecomp.sdc.be.dao.api.ActionStatus;
31 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
32 import org.openecomp.sdc.be.impl.ComponentsUtils;
33 import org.openecomp.sdc.be.model.InputDefinition;
34 import org.openecomp.sdc.be.model.Resource;
35 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
36
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Map;
40
41 import static java.util.Collections.emptyList;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.mockito.ArgumentMatchers.eq;
44 import static org.mockito.Mockito.doCallRealMethod;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.verifyZeroInteractions;
47 import static org.mockito.Mockito.when;
48 import static org.openecomp.sdc.be.components.utils.Conditions.hasPropertiesWithNames;
49 import static org.openecomp.sdc.be.dao.utils.CollectionUtils.union;
50
51 public class ComponentInputsMergeBLTest extends BaseComponentInputsMerge {
52
53     private ComponentInputsMergeBL testInstance;
54
55     @Before
56     @Override
57     public void setUp() throws Exception {
58         super.setUp();
59         testInstance = new ComponentInputsMergeBL(inputsValuesMergingBusinessLogic, declaredInputsResolver, toscaOperationFacade, new ComponentsUtils(mock(AuditingManager.class)));
60     }
61
62     @Test
63     public void whenOldComponentHasNoInputs_returnOk() {
64         ActionStatus actionStatus = testInstance.mergeComponents(new Resource(), new Resource());
65         assertThat(actionStatus).isEqualTo(ActionStatus.OK);
66         verifyZeroInteractions(toscaOperationFacade, inputsValuesMergingBusinessLogic, declaredInputsResolver);
67     }
68
69     @Test
70     public void whenCurrResourceHasNoProperties_noRedeclarationOFInputsRequired() {
71         Resource newResource = new ResourceBuilder().setUniqueId(RESOURCE_ID).build();
72         when(toscaOperationFacade.updateInputsToComponent(emptyList(), RESOURCE_ID)).thenReturn(Either.left(null));
73         doCallRealMethod().when(inputsValuesMergingBusinessLogic).mergeComponentInputs(Mockito.anyList(), Mockito.anyList());
74         ActionStatus actionStatus = testInstance.mergeComponents(prevResource, newResource);
75         assertThat(actionStatus).isEqualTo(ActionStatus.OK);
76         verifyCallToMergeComponentInputs(prevResource, emptyList());
77     }
78
79     @Test
80     public void whenCurrResourceHasNoInputs_noMergeRequired_updateResourceWithInputsDeclaredInPrevVersion() {
81         List<InputDefinition> prevDeclaredInputs = ObjectGenerator.buildInputs("declared1", "declared2");
82         currResource.setInputs(null);
83         when(declaredInputsResolver.getPreviouslyDeclaredInputsToMerge(eq(prevResource), eq(currResource), getInputPropertiesCaptor.capture())).thenReturn(prevDeclaredInputs);
84         when(toscaOperationFacade.updateInputsToComponent(prevDeclaredInputs, RESOURCE_ID)).thenReturn(Either.left(null));
85         doCallRealMethod().when(inputsValuesMergingBusinessLogic).mergeComponentInputs(Mockito.anyList(), Mockito.anyList());
86         ActionStatus actionStatus = testInstance.mergeComponents(prevResource, currResource);
87         assertThat(actionStatus).isEqualTo(ActionStatus.OK);
88         verifyCallToMergeComponentInputs(prevResource, emptyList());
89         verifyPropertiesPassedToDeclaredInputsResolver();
90     }
91
92     @Test
93     public void findInputsDeclaredFromPropertiesAndMergeThemIntoNewComponent() {
94         List<InputDefinition> prevDeclaredInputs = ObjectGenerator.buildInputs("declared1", "declared2");
95         List<InputDefinition> currInputsPreMerge = new ArrayList<>(currResource.getInputs());
96         when(declaredInputsResolver.getPreviouslyDeclaredInputsToMerge(eq(prevResource), eq(currResource), getInputPropertiesCaptor.capture())).thenReturn(prevDeclaredInputs);
97         List<InputDefinition> expectedInputsToUpdate = union(currInputsPreMerge, prevDeclaredInputs);
98         when(toscaOperationFacade.updateInputsToComponent(expectedInputsToUpdate, RESOURCE_ID)).thenReturn(Either.left(null));
99         doCallRealMethod().when(inputsValuesMergingBusinessLogic).mergeComponentInputs(Mockito.anyList(), Mockito.anyList());
100         ActionStatus actionStatus = testInstance.mergeComponents(prevResource, currResource);
101         assertThat(actionStatus).isEqualTo(ActionStatus.OK);
102         verifyCallToMergeComponentInputs(prevResource, currInputsPreMerge);
103         verifyPropertiesPassedToDeclaredInputsResolver();
104     }
105
106     @Test
107     public void whenFailingToUpdateInputs_propagateTheError() {
108         Resource newResource = new ResourceBuilder().setUniqueId(RESOURCE_ID).build();
109         when(toscaOperationFacade.updateInputsToComponent(emptyList(), RESOURCE_ID)).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
110         ActionStatus actionStatus = testInstance.mergeComponents(prevResource, newResource);
111         assertThat(actionStatus).isEqualTo(ActionStatus.GENERAL_ERROR);
112     }
113
114     private void verifyPropertiesPassedToDeclaredInputsResolver() {
115         Map<String, List<PropertyDataDefinition>> allResourceProps = getInputPropertiesCaptor.getValue();
116         assertThat(allResourceProps)
117                 .hasEntrySatisfying("inst1", hasPropertiesWithNames("prop1", "prop2"))
118                 .hasEntrySatisfying("inst2", hasPropertiesWithNames("prop3"))
119                 .hasEntrySatisfying("group1", hasPropertiesWithNames("prop1"))
120                 .hasEntrySatisfying("policy1", hasPropertiesWithNames("prop2"));
121     }
122 }