Added oparent to sdc main
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / property / PolicyPropertyDeclaratorTest.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.property;
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.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.junit.MockitoJUnitRunner;
32 import org.openecomp.sdc.be.components.utils.PolicyDefinitionBuilder;
33 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
34 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
35 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
36 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
37 import org.openecomp.sdc.be.model.InputDefinition;
38 import org.openecomp.sdc.be.model.PolicyDefinition;
39 import org.openecomp.sdc.be.model.Resource;
40 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.PolicyOperation;
41 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
42 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
43
44 import java.util.Arrays;
45 import java.util.Collections;
46 import java.util.List;
47 import java.util.stream.Collectors;
48 import java.util.stream.Stream;
49
50 import static org.assertj.core.api.Java6Assertions.assertThat;
51 import static org.mockito.ArgumentMatchers.eq;
52 import static org.mockito.Mockito.verifyZeroInteractions;
53 import static org.mockito.Mockito.when;
54
55 @RunWith(MockitoJUnitRunner.class)
56 //note that testing for most of the common logic is under the ComponentInstancePropertyDeclaratorTest
57 public class PolicyPropertyDeclaratorTest extends PropertyDeclaratorTestBase {
58
59     private static final String POLICY_ID = "policyId";
60     @InjectMocks
61     private PolicyPropertyDeclarator policyPropertyDeclarator;
62     @Mock
63     private PolicyOperation policyOperation;
64     @Mock
65     private PropertyOperation propertyOperation;
66     @Captor
67     private ArgumentCaptor<List<PropertyDataDefinition>> updatedPropsCapture;
68     private Resource resource;
69     private InputDefinition input;
70
71     @Override
72     @Before
73     public void setUp() throws Exception {
74         super.setUp();
75         resource = createResourceWithPolicy();
76         input = new InputDefinition();
77         input.setUniqueId(INPUT_ID);
78         input.setName(INPUT_ID);
79         input.setValue("value");
80     }
81
82     @Test
83     public void testDeclarePropertiesAsInputs_policyNotExist() {
84         Either<List<InputDefinition>, StorageOperationStatus> declareResult = policyPropertyDeclarator.declarePropertiesAsInputs(resource, "nonExistingPolicy", Collections.emptyList());
85         assertThat(declareResult.right().value()).isEqualTo(StorageOperationStatus.NOT_FOUND);
86         verifyZeroInteractions(policyOperation);
87     }
88
89     @Test
90     public void testDeclarePropertiesAsInputs_failedToUpdateProperties() {
91         when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
92         Either<List<InputDefinition>, StorageOperationStatus> declareResult = policyPropertyDeclarator.declarePropertiesAsInputs(resource, POLICY_ID, Collections.emptyList());
93         assertThat(declareResult.right().value()).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
94     }
95
96     @Test
97     public void testDeclarePropertiesAsInputs() {
98         List<PropertyDataDefinition> properties = Arrays.asList(prop1, prop2);
99         List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
100         when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
101         Either<List<InputDefinition>, StorageOperationStatus> createdInputs = policyPropertyDeclarator.declarePropertiesAsInputs(resource, POLICY_ID, propsToDeclare);
102         List<InputDefinition> inputs = createdInputs.left().value();
103         assertThat(inputs).hasSize(2);
104         verifyInputPropertiesList(inputs, updatedPropsCapture.getValue());
105         //creation of inputs values is part of the DefaultPropertyDeclarator and is tested in the ComponentInstancePropertyDeclaratorTest class
106     }
107
108     @Test
109     public void testUnDeclareProperties_whenComponentHasNoPolicies_returnOk() {
110         Resource resource = new Resource();
111         StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);
112         assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
113         verifyZeroInteractions(policyOperation);
114     }
115
116     @Test
117     public void testUnDeclareProperties_whenNoPropertiesFromPolicyMatchInputId_returnOk() {
118         StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsInputs(createResourceWithPolicy(), input);
119         assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
120         verifyZeroInteractions(policyOperation);
121     }
122
123     @Test
124     public void whenFailingToUpdateDeclaredProperties_returnErrorStatus() {
125         Resource resource = createResourceWithPolicies(POLICY_ID);
126         PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
127         PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
128         policyDefinition.setProperties(Collections.singletonList(getInputPropForInput));
129         when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
130         when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
131         StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);
132         assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
133     }
134
135     @Test
136     public void testUnDeclareProperties_propertiesUpdatedCorrectly() {
137         Resource resource = createResourceWithPolicies(POLICY_ID, "policyId2");
138         PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
139         PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
140         PropertyDataDefinition someOtherProperty = new PropertyDataDefinitionBuilder().build();
141         policyDefinition.setProperties(Arrays.asList(getInputPropForInput, someOtherProperty));
142
143         when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
144         when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
145         StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);
146
147         assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
148         List<PropertyDataDefinition> updatedProperties = updatedPropsCapture.getValue();
149         assertThat(updatedProperties).hasSize(1);
150         PropertyDataDefinition updatedProperty = updatedProperties.get(0);
151         assertThat(updatedProperty.isGetInputProperty()).isFalse();
152         assertThat(updatedProperty.getValue()).isEmpty();
153         assertThat(updatedProperty.getDefaultValue()).isEqualTo(getInputPropForInput.getDefaultValue());
154         assertThat(updatedProperty.getUniqueId()).isEqualTo(getInputPropForInput.getUniqueId());
155     }
156
157     @Test
158     public void testUnDeclarePropertiesAsListInputs_whenComponentHasNoPolicies_returnOk() {
159         Resource resource = new Resource();
160         StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsListInputs(resource, input);
161         assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
162         verifyZeroInteractions(policyOperation);
163     }
164
165     @Test
166     public void testUnDeclarePropertiesAsListInputs_whenNoPropertiesFromPolicyMatchInputId_returnOk() {
167         StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsListInputs(createResourceWithPolicy(), input);
168         assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
169         verifyZeroInteractions(policyOperation);
170     }
171
172     @Test
173     public void whenFailingToUpdateDeclaredPropertiesAsListInputs_returnErrorStatus() {
174         Resource resource = createResourceWithPolicies(POLICY_ID);
175         PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
176         PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
177         policyDefinition.setProperties(Collections.singletonList(getInputPropForInput));
178         when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
179         when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
180         StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsListInputs(resource, input);
181         assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
182     }
183
184     @Test
185     public void testUnDeclarePropertiesAsListInputs_propertiesUpdatedCorrectly() {
186         Resource resource = createResourceWithPolicies(POLICY_ID, "policyId3");
187         PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
188         PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
189         PropertyDataDefinition someOtherProperty = new PropertyDataDefinitionBuilder().build();
190         policyDefinition.setProperties(Arrays.asList(getInputPropForInput, someOtherProperty));
191
192         when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
193         when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
194         StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsListInputs(resource, input);
195
196         assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
197         List<PropertyDataDefinition> updatedProperties = updatedPropsCapture.getValue();
198         assertThat(updatedProperties).hasSize(1);
199         PropertyDataDefinition updatedProperty = updatedProperties.get(0);
200         assertThat(updatedProperty.isGetInputProperty()).isFalse();
201         assertThat(updatedProperty.getValue()).isEmpty();
202         assertThat(updatedProperty.getDefaultValue()).isEqualTo(getInputPropForInput.getDefaultValue());
203         assertThat(updatedProperty.getUniqueId()).isEqualTo(getInputPropForInput.getUniqueId());
204     }
205
206     private Resource createResourceWithPolicy() {
207         return createResourceWithPolicies(POLICY_ID);
208     }
209
210     private Resource createResourceWithPolicies(String ... policies) {
211         List<PolicyDefinition> policiesDef = Stream.of(policies)
212                 .map(this::buildPolicy)
213                 .collect(Collectors.toList());
214
215         return new ResourceBuilder()
216                 .setUniqueId(RESOURCE_ID)
217                 .setPolicies(policiesDef)
218                 .build();
219     }
220
221     private PolicyDefinition buildPolicy(String policyId) {
222         return PolicyDefinitionBuilder.create()
223                 .setUniqueId(policyId)
224                 .setName(policyId)
225                 .build();
226     }
227
228     private PropertyDataDefinition buildGetInputProperty(String inputId) {
229         return new PropertyDataDefinitionBuilder()
230                 .addGetInputValue(inputId)
231                 .setUniqueId(POLICY_ID + "_" + inputId)
232                 .setDefaultValue("defaultValue")
233                 .setValue(generateGetInputValue(inputId))
234                 .build();
235     }
236
237
238 }
239