1 package org.openecomp.sdc.be.components.merge.input;
4 import org.junit.Before;
6 import org.mockito.Mockito;
7 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
8 import org.openecomp.sdc.be.components.utils.ObjectGenerator;
9 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
10 import org.openecomp.sdc.be.dao.api.ActionStatus;
11 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
12 import org.openecomp.sdc.be.impl.ComponentsUtils;
13 import org.openecomp.sdc.be.model.InputDefinition;
14 import org.openecomp.sdc.be.model.Resource;
15 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
17 import java.util.ArrayList;
18 import java.util.List;
21 import static java.util.Collections.emptyList;
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.mockito.ArgumentMatchers.eq;
24 import static org.mockito.Mockito.doCallRealMethod;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verifyZeroInteractions;
27 import static org.mockito.Mockito.when;
28 import static org.openecomp.sdc.be.components.utils.Conditions.hasPropertiesWithNames;
29 import static org.openecomp.sdc.be.dao.utils.CollectionUtils.union;
31 public class ComponentInputsMergeBLTest extends BaseComponentInputsMerge {
33 private ComponentInputsMergeBL testInstance;
37 public void setUp() throws Exception {
39 testInstance = new ComponentInputsMergeBL(inputsValuesMergingBusinessLogic, declaredInputsResolver, toscaOperationFacade, new ComponentsUtils(mock(AuditingManager.class)));
43 public void whenOldComponentHasNoInputs_returnOk() {
44 ActionStatus actionStatus = testInstance.mergeComponents(new Resource(), new Resource());
45 assertThat(actionStatus).isEqualTo(ActionStatus.OK);
46 verifyZeroInteractions(toscaOperationFacade, inputsValuesMergingBusinessLogic, declaredInputsResolver);
50 public void whenCurrResourceHasNoProperties_noRedeclarationOFInputsRequired() {
51 Resource newResource = new ResourceBuilder().setUniqueId(RESOURCE_ID).build();
52 when(toscaOperationFacade.updateInputsToComponent(emptyList(), RESOURCE_ID)).thenReturn(Either.left(null));
53 doCallRealMethod().when(inputsValuesMergingBusinessLogic).mergeComponentInputs(Mockito.anyList(), Mockito.anyList());
54 ActionStatus actionStatus = testInstance.mergeComponents(prevResource, newResource);
55 assertThat(actionStatus).isEqualTo(ActionStatus.OK);
56 verifyCallToMergeComponentInputs(prevResource, emptyList());
60 public void whenCurrResourceHasNoInputs_noMergeRequired_updateResourceWithInputsDeclaredInPrevVersion() {
61 List<InputDefinition> prevDeclaredInputs = ObjectGenerator.buildInputs("declared1", "declared2");
62 currResource.setInputs(null);
63 when(declaredInputsResolver.getPreviouslyDeclaredInputsToMerge(eq(prevResource), eq(currResource), getInputPropertiesCaptor.capture())).thenReturn(prevDeclaredInputs);
64 when(toscaOperationFacade.updateInputsToComponent(prevDeclaredInputs, RESOURCE_ID)).thenReturn(Either.left(null));
65 doCallRealMethod().when(inputsValuesMergingBusinessLogic).mergeComponentInputs(Mockito.anyList(), Mockito.anyList());
66 ActionStatus actionStatus = testInstance.mergeComponents(prevResource, currResource);
67 assertThat(actionStatus).isEqualTo(ActionStatus.OK);
68 verifyCallToMergeComponentInputs(prevResource, emptyList());
69 verifyPropertiesPassedToDeclaredInputsResolver();
73 public void findInputsDeclaredFromPropertiesAndMergeThemIntoNewComponent() {
74 List<InputDefinition> prevDeclaredInputs = ObjectGenerator.buildInputs("declared1", "declared2");
75 List<InputDefinition> currInputsPreMerge = new ArrayList<>(currResource.getInputs());
76 when(declaredInputsResolver.getPreviouslyDeclaredInputsToMerge(eq(prevResource), eq(currResource), getInputPropertiesCaptor.capture())).thenReturn(prevDeclaredInputs);
77 List<InputDefinition> expectedInputsToUpdate = union(currInputsPreMerge, prevDeclaredInputs);
78 when(toscaOperationFacade.updateInputsToComponent(expectedInputsToUpdate, RESOURCE_ID)).thenReturn(Either.left(null));
79 doCallRealMethod().when(inputsValuesMergingBusinessLogic).mergeComponentInputs(Mockito.anyList(), Mockito.anyList());
80 ActionStatus actionStatus = testInstance.mergeComponents(prevResource, currResource);
81 assertThat(actionStatus).isEqualTo(ActionStatus.OK);
82 verifyCallToMergeComponentInputs(prevResource, currInputsPreMerge);
83 verifyPropertiesPassedToDeclaredInputsResolver();
87 public void whenFailingToUpdateInputs_propagateTheError() {
88 Resource newResource = new ResourceBuilder().setUniqueId(RESOURCE_ID).build();
89 when(toscaOperationFacade.updateInputsToComponent(emptyList(), RESOURCE_ID)).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
90 ActionStatus actionStatus = testInstance.mergeComponents(prevResource, newResource);
91 assertThat(actionStatus).isEqualTo(ActionStatus.GENERAL_ERROR);
94 private void verifyPropertiesPassedToDeclaredInputsResolver() {
95 Map<String, List<PropertyDataDefinition>> allResourceProps = getInputPropertiesCaptor.getValue();
96 assertThat(allResourceProps)
97 .hasEntrySatisfying("inst1", hasPropertiesWithNames("prop1", "prop2"))
98 .hasEntrySatisfying("inst2", hasPropertiesWithNames("prop3"))
99 .hasEntrySatisfying("group1", hasPropertiesWithNames("prop1"))
100 .hasEntrySatisfying("policy1", hasPropertiesWithNames("prop2"));