9868e66ff701aeeb82e5a2e9e237a4a4310828b3
[sdc.git] /
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.junit.jupiter.api.DisplayName;
27 import org.mockito.Mockito;
28 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
29 import org.openecomp.sdc.be.components.utils.ObjectGenerator;
30 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
31 import org.openecomp.sdc.be.dao.api.ActionStatus;
32 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
33 import org.openecomp.sdc.be.impl.ComponentsUtils;
34 import org.openecomp.sdc.be.model.InputDefinition;
35 import org.openecomp.sdc.be.model.Resource;
36 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
37
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.Map;
41
42 import static java.util.Collections.emptyList;
43 import static org.assertj.core.api.Assertions.assertThat;
44 import static org.mockito.ArgumentMatchers.eq;
45 import static org.mockito.Mockito.doCallRealMethod;
46 import static org.mockito.Mockito.mock;
47 import static org.mockito.Mockito.verifyZeroInteractions;
48 import static org.mockito.Mockito.when;
49 import static org.openecomp.sdc.be.components.utils.Conditions.hasPropertiesWithNames;
50 import static org.openecomp.sdc.be.dao.utils.CollectionUtils.union;
51
52 public class ComponentInputsMergeBLTest extends BaseComponentInputsMerge {
53
54     private ComponentInputsMergeBL testInstance;
55
56     @Before
57     @Override
58     public void setUp() throws Exception {
59         super.setUp();
60         testInstance = new ComponentInputsMergeBL(inputsValuesMergingBusinessLogic, declaredInputsResolver, toscaOperationFacade, new ComponentsUtils(mock(AuditingManager.class)));
61     }
62
63     @Test
64     @DisplayName("When old component has no inputs then return ok")
65     public void whenOldComponentHasNoInputs_returnOk() {
66         ActionStatus actionStatus = testInstance.mergeComponents(new Resource(), new Resource());
67         assertThat(actionStatus).isEqualTo(ActionStatus.OK);
68         verifyZeroInteractions(toscaOperationFacade, inputsValuesMergingBusinessLogic, declaredInputsResolver);
69     }
70
71     @Test
72     @DisplayName("When current resource has no properties no redeclaration of inputs required")
73     public void whenCurrResourceHasNoProperties_noRedeclarationOFInputsRequired() {
74         Resource newResource = new ResourceBuilder().setUniqueId(RESOURCE_ID).build();
75         when(toscaOperationFacade.updateInputsToComponent(emptyList(), RESOURCE_ID)).thenReturn(Either.left(null));
76         doCallRealMethod().when(inputsValuesMergingBusinessLogic).mergeComponentInputs(Mockito.anyList(), Mockito.anyList());
77         ActionStatus actionStatus = testInstance.mergeComponents(prevResource, newResource);
78         assertThat(actionStatus).isEqualTo(ActionStatus.OK);
79         verifyCallToMergeComponentInputs(prevResource, emptyList());
80     }
81
82     @Test
83     @DisplayName("When current resource has no inputs no merge required, update resource with inputs declared in previous version")
84     public void whenCurrResourceHasNoInputs_noMergeRequired_updateResourceWithInputsDeclaredInPrevVersion() {
85         List<InputDefinition> prevDeclaredInputs = ObjectGenerator.buildInputs("declared1", "declared2");
86         currResource.setInputs(null);
87         when(declaredInputsResolver.getPreviouslyDeclaredInputsToMerge(eq(prevResource), eq(currResource), getInputPropertiesCaptor.capture())).thenReturn(prevDeclaredInputs);
88         when(toscaOperationFacade.updateInputsToComponent(prevDeclaredInputs, RESOURCE_ID)).thenReturn(Either.left(null));
89         doCallRealMethod().when(inputsValuesMergingBusinessLogic).mergeComponentInputs(Mockito.anyList(), Mockito.anyList());
90         ActionStatus actionStatus = testInstance.mergeComponents(prevResource, currResource);
91         assertThat(actionStatus).isEqualTo(ActionStatus.OK);
92         verifyCallToMergeComponentInputs(prevResource, emptyList());
93         verifyPropertiesPassedToDeclaredInputsResolver();
94     }
95
96     @Test
97     @DisplayName("Find inputs declared from properties and merge them into new component")
98     public void findInputsDeclaredFromPropertiesAndMergeThemIntoNewComponent() {
99         List<InputDefinition> prevDeclaredInputs = ObjectGenerator.buildInputs("declared1", "declared2");
100         List<InputDefinition> currInputsPreMerge = new ArrayList<>(currResource.getInputs());
101         when(declaredInputsResolver.getPreviouslyDeclaredInputsToMerge(eq(prevResource), eq(currResource), getInputPropertiesCaptor.capture())).thenReturn(prevDeclaredInputs);
102         List<InputDefinition> expectedInputsToUpdate = union(currInputsPreMerge, prevDeclaredInputs);
103         when(toscaOperationFacade.updateInputsToComponent(expectedInputsToUpdate, RESOURCE_ID)).thenReturn(Either.left(null));
104         doCallRealMethod().when(inputsValuesMergingBusinessLogic).mergeComponentInputs(Mockito.anyList(), Mockito.anyList());
105         ActionStatus actionStatus = testInstance.mergeComponents(prevResource, currResource);
106         assertThat(actionStatus).isEqualTo(ActionStatus.OK);
107         verifyCallToMergeComponentInputs(prevResource, currInputsPreMerge);
108         verifyPropertiesPassedToDeclaredInputsResolver();
109     }
110
111     @Test
112     @DisplayName("Identify already existing inputs and don't merge them into new component")
113     public void identifyAlreadyExistingInputsAndDontMergeThemIntoNewComponent() {
114         List<InputDefinition> prevDeclaredInputs = ObjectGenerator.buildInputs("declared1", "declared2", "input1");
115         List<InputDefinition> prevDeclaredInputsNotPresentInCurrent = ObjectGenerator.buildInputs("declared1", "declared2");
116         List<InputDefinition> currInputsPreMerge = new ArrayList<>(currResource.getInputs());
117         when(declaredInputsResolver.getPreviouslyDeclaredInputsToMerge(eq(prevResource), eq(currResource), getInputPropertiesCaptor.capture())).thenReturn(prevDeclaredInputs);
118         List<InputDefinition> expectedInputsToUpdate = union(currInputsPreMerge, prevDeclaredInputsNotPresentInCurrent);
119         when(toscaOperationFacade.updateInputsToComponent(expectedInputsToUpdate, RESOURCE_ID)).thenReturn(Either.left(null));
120         doCallRealMethod().when(inputsValuesMergingBusinessLogic).mergeComponentInputs(Mockito.anyList(), Mockito.anyList());
121         ActionStatus actionStatus = testInstance.mergeComponents(prevResource, currResource);
122         assertThat(actionStatus).isEqualTo(ActionStatus.OK);
123         assertThat(currResource.getInputs()).containsExactlyInAnyOrderElementsOf(expectedInputsToUpdate);
124         verifyCallToMergeComponentInputs(prevResource, currInputsPreMerge);
125         verifyPropertiesPassedToDeclaredInputsResolver();
126     }
127
128
129     @Test
130     @DisplayName("When failing to update inputs propagate the error")
131     public void whenFailingToUpdateInputs_propagateTheError() {
132         Resource newResource = new ResourceBuilder().setUniqueId(RESOURCE_ID).build();
133         when(toscaOperationFacade.updateInputsToComponent(emptyList(), RESOURCE_ID)).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
134         ActionStatus actionStatus = testInstance.mergeComponents(prevResource, newResource);
135         assertThat(actionStatus).isEqualTo(ActionStatus.GENERAL_ERROR);
136     }
137
138     private void verifyPropertiesPassedToDeclaredInputsResolver() {
139         Map<String, List<PropertyDataDefinition>> allResourceProps = getInputPropertiesCaptor.getValue();
140         assertThat(allResourceProps)
141                 .hasEntrySatisfying("inst1", hasPropertiesWithNames("prop1", "prop2"))
142                 .hasEntrySatisfying("inst2", hasPropertiesWithNames("prop3"))
143                 .hasEntrySatisfying("group1", hasPropertiesWithNames("prop1"))
144                 .hasEntrySatisfying("policy1", hasPropertiesWithNames("prop2"));
145     }
146 }