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.input;
23 import static org.apache.commons.collections4.ListUtils.union;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.doCallRealMethod;
27 import static org.mockito.Mockito.verifyNoInteractions;
28 import static org.mockito.Mockito.when;
29 import static org.openecomp.sdc.be.components.utils.Conditions.hasPropertiesWithNames;
31 import fj.data.Either;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.DisplayName;
38 import org.junit.jupiter.api.Test;
39 import org.junit.jupiter.api.extension.ExtendWith;
40 import org.mockito.ArgumentMatchers;
41 import org.mockito.InjectMocks;
42 import org.mockito.Mock;
43 import org.mockito.Mockito;
44 import org.mockito.junit.jupiter.MockitoExtension;
45 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
46 import org.openecomp.sdc.be.components.utils.ObjectGenerator;
47 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
48 import org.openecomp.sdc.be.dao.api.ActionStatus;
49 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
50 import org.openecomp.sdc.be.impl.ComponentsUtils;
51 import org.openecomp.sdc.be.model.InputDefinition;
52 import org.openecomp.sdc.be.model.Resource;
53 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
55 @ExtendWith(MockitoExtension.class)
56 public class ComponentInputsMergeBLTest extends BaseComponentInputsMerge {
59 private ComponentInputsMergeBL testInstance;
62 private AuditingManager auditingManager;
65 private ComponentsUtils componentsUtils;
69 public void setUp() throws Exception {
74 @DisplayName("When old component has no inputs then return ok")
75 public void whenOldComponentHasNoInputs_returnOk() {
76 ActionStatus actionStatus = testInstance.mergeComponents(new Resource(), new Resource());
77 assertThat(actionStatus).isEqualTo(ActionStatus.OK);
78 verifyNoInteractions(toscaOperationFacade, inputsValuesMergingBusinessLogic, declaredInputsResolver);
82 @DisplayName("When current resource has no properties no redeclaration of inputs required")
83 public void whenCurrResourceHasNoProperties_noRedeclarationOFInputsRequired() {
84 Resource newResource = new ResourceBuilder().setUniqueId(RESOURCE_ID).build();
85 when(toscaOperationFacade.updateInputsToComponent(Collections.emptyList(), RESOURCE_ID))
86 .thenReturn(Either.left(null));
87 doCallRealMethod().when(inputsValuesMergingBusinessLogic)
88 .mergeComponentInputs(Mockito.anyList(), Mockito.anyList());
89 ActionStatus actionStatus = testInstance.mergeComponents(prevResource, newResource);
90 assertThat(actionStatus).isEqualTo(ActionStatus.OK);
91 verifyCallToMergeComponentInputs(prevResource, Collections.emptyList());
95 @DisplayName("When current resource has no inputs no merge required, update resource with inputs declared in previous version")
96 public void whenCurrResourceHasNoInputs_noMergeRequired_updateResourceWithInputsDeclaredInPrevVersion() {
97 List<InputDefinition> prevDeclaredInputs = ObjectGenerator.buildInputs("declared1", "declared2");
98 currResource.setInputs(null);
99 when(declaredInputsResolver
100 .getPreviouslyDeclaredInputsToMerge(ArgumentMatchers.eq(prevResource), ArgumentMatchers.eq(currResource),
101 getInputPropertiesCaptor.capture()))
102 .thenReturn(prevDeclaredInputs);
103 when(toscaOperationFacade.updateInputsToComponent(prevDeclaredInputs, RESOURCE_ID))
104 .thenReturn(Either.left(null));
105 doCallRealMethod().when(inputsValuesMergingBusinessLogic)
106 .mergeComponentInputs(Mockito.anyList(), Mockito.anyList());
107 ActionStatus actionStatus = testInstance.mergeComponents(prevResource, currResource);
108 assertThat(actionStatus).isEqualTo(ActionStatus.OK);
109 verifyCallToMergeComponentInputs(prevResource, Collections.emptyList());
110 verifyPropertiesPassedToDeclaredInputsResolver();
114 @DisplayName("Find inputs declared from properties and merge them into new component")
115 public void findInputsDeclaredFromPropertiesAndMergeThemIntoNewComponent() {
116 List<InputDefinition> prevDeclaredInputs = ObjectGenerator.buildInputs("declared1", "declared2");
117 List<InputDefinition> currInputsPreMerge = new ArrayList<>(currResource.getInputs());
118 when(declaredInputsResolver
119 .getPreviouslyDeclaredInputsToMerge(ArgumentMatchers.eq(prevResource), ArgumentMatchers.eq(currResource),
120 getInputPropertiesCaptor.capture()))
121 .thenReturn(prevDeclaredInputs);
122 List<InputDefinition> expectedInputsToUpdate = union(currInputsPreMerge, prevDeclaredInputs);
123 when(toscaOperationFacade.updateInputsToComponent(expectedInputsToUpdate, RESOURCE_ID))
124 .thenReturn(Either.left(null));
125 doCallRealMethod().when(inputsValuesMergingBusinessLogic)
126 .mergeComponentInputs(Mockito.anyList(), Mockito.anyList());
127 ActionStatus actionStatus = testInstance.mergeComponents(prevResource, currResource);
128 assertThat(actionStatus).isEqualTo(ActionStatus.OK);
129 verifyCallToMergeComponentInputs(prevResource, currInputsPreMerge);
130 verifyPropertiesPassedToDeclaredInputsResolver();
134 @DisplayName("Identify already existing inputs and don't merge them into new component")
135 public void identifyAlreadyExistingInputsAndDontMergeThemIntoNewComponent() {
136 List<InputDefinition> prevDeclaredInputs = ObjectGenerator.buildInputs("declared1", "declared2", "input1");
137 List<InputDefinition> prevDeclaredInputsNotPresentInCurrent = ObjectGenerator
138 .buildInputs("declared1", "declared2");
139 List<InputDefinition> currInputsPreMerge = new ArrayList<>(currResource.getInputs());
140 when(declaredInputsResolver
141 .getPreviouslyDeclaredInputsToMerge(ArgumentMatchers.eq(prevResource), ArgumentMatchers.eq(currResource),
142 getInputPropertiesCaptor.capture()))
143 .thenReturn(prevDeclaredInputs);
144 List<InputDefinition> expectedInputsToUpdate = union(currInputsPreMerge, prevDeclaredInputsNotPresentInCurrent);
145 when(toscaOperationFacade.updateInputsToComponent(expectedInputsToUpdate, RESOURCE_ID))
146 .thenReturn(Either.left(null));
147 doCallRealMethod().when(inputsValuesMergingBusinessLogic)
148 .mergeComponentInputs(Mockito.anyList(), Mockito.anyList());
149 ActionStatus actionStatus = testInstance.mergeComponents(prevResource, currResource);
150 assertThat(actionStatus).isEqualTo(ActionStatus.OK);
151 assertThat(currResource.getInputs()).containsExactlyInAnyOrderElementsOf(expectedInputsToUpdate);
152 verifyCallToMergeComponentInputs(prevResource, currInputsPreMerge);
153 verifyPropertiesPassedToDeclaredInputsResolver();
157 @DisplayName("When failing to update inputs propagate the error")
158 public void whenFailingToUpdateInputs_propagateTheError() {
159 Resource newResource = new ResourceBuilder().setUniqueId(RESOURCE_ID).build();
160 when(toscaOperationFacade.updateInputsToComponent(any(), any()))
161 .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
162 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR))
163 .thenReturn(ActionStatus.GENERAL_ERROR);
164 ActionStatus actionStatus = testInstance.mergeComponents(prevResource, newResource);
165 assertThat(actionStatus).isEqualTo(ActionStatus.GENERAL_ERROR);
168 private void verifyPropertiesPassedToDeclaredInputsResolver() {
169 Map<String, List<PropertyDataDefinition>> allResourceProps = getInputPropertiesCaptor.getValue();
170 assertThat(allResourceProps)
171 .hasEntrySatisfying("inst1", hasPropertiesWithNames("prop1", "prop2"))
172 .hasEntrySatisfying("inst2", hasPropertiesWithNames("prop3"))
173 .hasEntrySatisfying("group1", hasPropertiesWithNames("prop1"))
174 .hasEntrySatisfying("policy1", hasPropertiesWithNames("prop2"));