Prepare for Junit5
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / input / GlobalInputsMergeCommandTest.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.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
29 import org.openecomp.sdc.be.components.impl.utils.ExceptionUtils;
30 import org.openecomp.sdc.be.components.merge.GlobalInputsFilteringBusinessLogic;
31 import org.openecomp.sdc.be.components.utils.ObjectGenerator;
32 import org.openecomp.sdc.be.dao.api.ActionStatus;
33 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
34 import org.openecomp.sdc.be.impl.ComponentsUtils;
35 import org.openecomp.sdc.be.model.InputDefinition;
36
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Map;
40
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.mockito.ArgumentMatchers.eq;
43 import static org.mockito.Mockito.doCallRealMethod;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.when;
46 import static org.openecomp.sdc.be.components.utils.Conditions.hasPropertiesWithNames;
47 import static org.openecomp.sdc.be.dao.utils.CollectionUtils.union;
48
49 public class GlobalInputsMergeCommandTest extends BaseComponentInputsMerge {
50
51     private GlobalInputsMergeCommand testInstance;
52     @Mock
53     private GlobalInputsFilteringBusinessLogic globalInputsFilteringBusinessLogic;
54     @Mock
55     private ExceptionUtils exceptionUtils;
56
57     @Override
58     @BeforeEach
59     public void setUp() throws Exception {
60         super.setUp();
61         new ComponentInputsMergeBL(inputsValuesMergingBusinessLogic, declaredInputsResolver, toscaOperationFacade, new ComponentsUtils(mock(AuditingManager.class)));
62         testInstance = new GlobalInputsMergeCommand(inputsValuesMergingBusinessLogic, declaredInputsResolver, toscaOperationFacade, new ComponentsUtils(mock(AuditingManager.class)), globalInputsFilteringBusinessLogic, exceptionUtils);
63     }
64
65     @Test
66     public void mergeOnlyGlobalInputs_redeclareOnlyGroupAndPolicyProperties() {
67         List<InputDefinition> globalInputs = ObjectGenerator.buildInputs("input1");
68         List<InputDefinition> prevDeclaredInputs = ObjectGenerator.buildInputs("input2", "input3");
69         List<InputDefinition> expectedInputsToMerge = new ArrayList<>(globalInputs);
70         List<InputDefinition> expectedInputsToUpdate = union(globalInputs, prevDeclaredInputs);
71
72         when(globalInputsFilteringBusinessLogic.filterGlobalInputs(currResource)).thenReturn(Either.left(globalInputs));
73         when(declaredInputsResolver.getPreviouslyDeclaredInputsToMerge(eq(prevResource), eq(currResource), getInputPropertiesCaptor.capture())).thenReturn(prevDeclaredInputs);
74         when(toscaOperationFacade.updateInputsToComponent(expectedInputsToUpdate, RESOURCE_ID)).thenReturn(Either.left(null));
75         doCallRealMethod().when(inputsValuesMergingBusinessLogic).mergeComponentInputs(Mockito.anyList(), Mockito.anyList());
76         ActionStatus actionStatus = testInstance.mergeComponents(prevResource, currResource);
77         assertThat(actionStatus).isEqualTo(ActionStatus.OK);
78         verifyCallToMergeComponentInputs(prevResource, expectedInputsToMerge);
79         verifyGroupsAndPolicyPropertiesPassedToDeclaredInputsResolver();
80     }
81
82     private void verifyGroupsAndPolicyPropertiesPassedToDeclaredInputsResolver() {
83         Map<String, List<PropertyDataDefinition>> allResourceProps = getInputPropertiesCaptor.getValue();
84         assertThat(allResourceProps)
85                 .hasEntrySatisfying("group1", hasPropertiesWithNames("prop1"))
86                 .hasEntrySatisfying("policy1", hasPropertiesWithNames("prop2"));
87     }
88 }