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 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;
37 import java.util.ArrayList;
38 import java.util.List;
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;
51 public class ComponentInputsMergeBLTest extends BaseComponentInputsMerge {
53 private ComponentInputsMergeBL testInstance;
57 public void setUp() throws Exception {
59 testInstance = new ComponentInputsMergeBL(inputsValuesMergingBusinessLogic, declaredInputsResolver, toscaOperationFacade, new ComponentsUtils(mock(AuditingManager.class)));
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);
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());
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();
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();
107 public void identifyAlreadyExistingInputsAndDontMergeThemIntoNewComponent() {
108 List<InputDefinition> prevDeclaredInputs = ObjectGenerator.buildInputs("declared1", "declared2", "input1");
109 List<InputDefinition> prevDeclaredInputsNotPresentInCurrent = ObjectGenerator.buildInputs("declared1", "declared2");
110 List<InputDefinition> currInputsPreMerge = new ArrayList<>(currResource.getInputs());
111 when(declaredInputsResolver.getPreviouslyDeclaredInputsToMerge(eq(prevResource), eq(currResource), getInputPropertiesCaptor.capture())).thenReturn(prevDeclaredInputs);
112 List<InputDefinition> expectedInputsToUpdate = union(currInputsPreMerge, prevDeclaredInputsNotPresentInCurrent);
113 when(toscaOperationFacade.updateInputsToComponent(expectedInputsToUpdate, RESOURCE_ID)).thenReturn(Either.left(null));
114 doCallRealMethod().when(inputsValuesMergingBusinessLogic).mergeComponentInputs(Mockito.anyList(), Mockito.anyList());
115 ActionStatus actionStatus = testInstance.mergeComponents(prevResource, currResource);
116 assertThat(actionStatus).isEqualTo(ActionStatus.OK);
117 assertThat(currResource.getInputs()).containsExactlyInAnyOrderElementsOf(expectedInputsToUpdate);
118 verifyCallToMergeComponentInputs(prevResource, currInputsPreMerge);
119 verifyPropertiesPassedToDeclaredInputsResolver();
124 public void whenFailingToUpdateInputs_propagateTheError() {
125 Resource newResource = new ResourceBuilder().setUniqueId(RESOURCE_ID).build();
126 when(toscaOperationFacade.updateInputsToComponent(emptyList(), RESOURCE_ID)).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
127 ActionStatus actionStatus = testInstance.mergeComponents(prevResource, newResource);
128 assertThat(actionStatus).isEqualTo(ActionStatus.GENERAL_ERROR);
131 private void verifyPropertiesPassedToDeclaredInputsResolver() {
132 Map<String, List<PropertyDataDefinition>> allResourceProps = getInputPropertiesCaptor.getValue();
133 assertThat(allResourceProps)
134 .hasEntrySatisfying("inst1", hasPropertiesWithNames("prop1", "prop2"))
135 .hasEntrySatisfying("inst2", hasPropertiesWithNames("prop3"))
136 .hasEntrySatisfying("group1", hasPropertiesWithNames("prop1"))
137 .hasEntrySatisfying("policy1", hasPropertiesWithNames("prop2"));