Added oparent to sdc main
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / instance / ComponentInstanceInputsRedeclareHandlerTest.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.instance;
22
23 import fj.data.Either;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.Captor;
29 import org.mockito.Mock;
30 import org.mockito.junit.MockitoJUnitRunner;
31 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
32 import org.openecomp.sdc.be.components.merge.input.DeclaredInputsResolver;
33 import org.openecomp.sdc.be.components.merge.input.InputsValuesMergingBusinessLogic;
34 import org.openecomp.sdc.be.components.utils.AnnotationBuilder;
35 import org.openecomp.sdc.be.components.utils.InputsBuilder;
36 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
37 import org.openecomp.sdc.be.dao.api.ActionStatus;
38 import org.openecomp.sdc.be.datatypes.elements.Annotation;
39 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
40 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
41 import org.openecomp.sdc.be.impl.ComponentsUtils;
42 import org.openecomp.sdc.be.model.InputDefinition;
43 import org.openecomp.sdc.be.model.Resource;
44 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
45
46 import java.util.Collections;
47 import java.util.List;
48 import java.util.Map;
49
50 import static java.util.Arrays.asList;
51 import static org.assertj.core.api.Assertions.assertThat;
52 import static org.mockito.ArgumentMatchers.anyList;
53 import static org.mockito.ArgumentMatchers.eq;
54 import static org.mockito.Mockito.mock;
55 import static org.mockito.Mockito.when;
56 import static org.openecomp.sdc.be.components.utils.Conditions.hasPropertiesWithNames;
57
58 @RunWith(MockitoJUnitRunner.class)
59 public class ComponentInstanceInputsRedeclareHandlerTest {
60
61     private static final String RESOURCE_ID = "resourceID";
62     private ComponentInstanceInputsRedeclareHandler testInstance;
63     @Mock
64     private ToscaOperationFacade toscaOperationFacade;
65     @Mock
66     private DeclaredInputsResolver declaredInputsResolver;
67     @Mock
68     private InputsValuesMergingBusinessLogic inputsValuesMergingBusinessLogic;
69     @Captor
70     private ArgumentCaptor<Map<String, List<PropertyDataDefinition>>> getInputPropertiesCaptor;
71     private Resource  currContainer;
72     private List<InputDefinition> prevDeclaredInputs;
73     private Annotation annotation1, annotation2, annotation3;
74
75     @Before
76     public void setUp() throws Exception {
77         testInstance = new ComponentInstanceInputsRedeclareHandler(declaredInputsResolver, toscaOperationFacade, new ComponentsUtils(mock(AuditingManager.class)), inputsValuesMergingBusinessLogic);
78         currContainer = new ResourceBuilder()
79                 .addInstanceProperty("inst1", "prop1")
80                 .addInstanceProperty("inst1", "prop2")
81                 .addInstanceInput("inst1", "prop1", Collections.singletonList(new GetInputValueDataDefinition()))
82                 .addInstanceInput("inst1", "prop2", Collections.singletonList(new GetInputValueDataDefinition()))
83                 .setUniqueId(RESOURCE_ID)
84                 .build();
85
86         annotation1 = AnnotationBuilder.create()
87                 .setName("annotation1")
88                 .build();
89
90         annotation2 = AnnotationBuilder.create()
91                 .setName("annotation2")
92                 .build();
93
94         annotation3 = AnnotationBuilder.create()
95                 .setName("annotation3")
96                 .build();
97
98         InputDefinition declaredInput1 = InputsBuilder.create()
99                 .setPropertyId("prop1")
100                 .setName("input1")
101                 .addAnnotation(annotation1)
102                 .addAnnotation(annotation2)
103                 .build();
104
105         InputDefinition declaredInput2 = InputsBuilder.create()
106                 .setPropertyId("prop2")
107                 .setName("input2")
108                 .addAnnotation(annotation3)
109                 .build();
110
111         prevDeclaredInputs = asList(declaredInput1, declaredInput2);
112     }
113
114     @Test
115     public void redeclareOnlyPropertiesForGivenInstance() {
116         Resource originInstanceType = new Resource();
117         when(declaredInputsResolver.getPreviouslyDeclaredInputsToMerge(anyList(), eq(currContainer), getInputPropertiesCaptor.capture())).thenReturn(prevDeclaredInputs);
118         when(toscaOperationFacade.updateInputsToComponent(prevDeclaredInputs, RESOURCE_ID)).thenReturn(Either.left(null));
119         ActionStatus actionStatus = testInstance.redeclareComponentInputsForInstance(currContainer, "inst1", originInstanceType, Collections.emptyList());
120         assertThat(actionStatus).isEqualTo(ActionStatus.OK);
121         verifyInstanceSpecificPropertiesPassedToDeclaredInputsResolver();
122     }
123
124     @Test
125     public void updateInputsWithAnnotationsFromOriginInstanceType() {
126         InputDefinition input1 = InputsBuilder.create()
127                 .addAnnotation(annotation2)
128                 .addAnnotation(annotation3)
129                 .setName("prop1")
130                 .build();
131
132         InputDefinition input2 = InputsBuilder.create()
133                 .setName("prop2")
134                 .build();
135         Resource originInstanceType = new ResourceBuilder()
136                 .addInput(input1)
137                 .addInput(input2)
138                 .build();
139
140         when(declaredInputsResolver.getPreviouslyDeclaredInputsToMerge(anyList(), eq(currContainer), getInputPropertiesCaptor.capture())).thenReturn(prevDeclaredInputs);
141         when(toscaOperationFacade.updateInputsToComponent(prevDeclaredInputs, RESOURCE_ID)).thenReturn(Either.left(null));
142         ActionStatus actionStatus = testInstance.redeclareComponentInputsForInstance(currContainer, "inst1", originInstanceType, Collections.emptyList());
143         assertThat(actionStatus).isEqualTo(ActionStatus.OK);
144         assertThat(prevDeclaredInputs)
145                 .extracting("annotations")
146                 .containsExactlyInAnyOrder(asList(annotation1, annotation3, annotation2), asList(annotation3));
147     }
148
149     private void verifyInstanceSpecificPropertiesPassedToDeclaredInputsResolver() {
150         Map<String, List<PropertyDataDefinition>> allResourceProps = getInputPropertiesCaptor.getValue();
151         assertThat(allResourceProps)
152                 .hasEntrySatisfying("inst1", hasPropertiesWithNames("prop1", "prop2"));
153     }
154 }