1 package org.openecomp.sdc.be.components.property;
4 import org.junit.Before;
6 import org.junit.runner.RunWith;
7 import org.mockito.ArgumentCaptor;
8 import org.mockito.Captor;
9 import org.mockito.InjectMocks;
10 import org.mockito.Mock;
11 import org.mockito.junit.MockitoJUnitRunner;
12 import org.openecomp.sdc.be.components.utils.PolicyDefinitionBuilder;
13 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
14 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
15 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
16 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
17 import org.openecomp.sdc.be.model.InputDefinition;
18 import org.openecomp.sdc.be.model.PolicyDefinition;
19 import org.openecomp.sdc.be.model.Resource;
20 import org.openecomp.sdc.be.model.jsontitan.operations.PolicyOperation;
21 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
22 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.stream.Collectors;
28 import java.util.stream.Stream;
30 import static org.assertj.core.api.Java6Assertions.assertThat;
31 import static org.mockito.ArgumentMatchers.eq;
32 import static org.mockito.Mockito.verifyZeroInteractions;
33 import static org.mockito.Mockito.when;
35 @RunWith(MockitoJUnitRunner.class)
36 //note that testing for most of the common logic is under the ComponentInstancePropertyDeclaratorTest
37 public class PolicyPropertyDeclaratorTest extends PropertyDeclaratorTestBase {
39 private static final String POLICY_ID = "policyId";
41 private PolicyPropertyDeclarator policyPropertyDeclarator;
43 private PolicyOperation policyOperation;
45 private PropertyOperation propertyOperation;
47 private ArgumentCaptor<List<PropertyDataDefinition>> updatedPropsCapture;
48 private Resource resource;
49 private InputDefinition input;
53 public void setUp() throws Exception {
55 resource = createResourceWithPolicy();
56 input = new InputDefinition();
57 input.setUniqueId(INPUT_ID);
58 input.setName(INPUT_ID);
59 input.setValue("value");
63 public void testDeclarePropertiesAsInputs_policyNotExist() {
64 Either<List<InputDefinition>, StorageOperationStatus> declareResult = policyPropertyDeclarator.declarePropertiesAsInputs(resource, "nonExistingPolicy", Collections.emptyList());
65 assertThat(declareResult.right().value()).isEqualTo(StorageOperationStatus.NOT_FOUND);
66 verifyZeroInteractions(policyOperation);
70 public void testDeclarePropertiesAsInputs_failedToUpdateProperties() {
71 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
72 Either<List<InputDefinition>, StorageOperationStatus> declareResult = policyPropertyDeclarator.declarePropertiesAsInputs(resource, POLICY_ID, Collections.emptyList());
73 assertThat(declareResult.right().value()).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
77 public void testDeclarePropertiesAsInputs() {
78 List<PropertyDataDefinition> properties = Arrays.asList(prop1, prop2);
79 List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
80 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
81 Either<List<InputDefinition>, StorageOperationStatus> createdInputs = policyPropertyDeclarator.declarePropertiesAsInputs(resource, POLICY_ID, propsToDeclare);
82 List<InputDefinition> inputs = createdInputs.left().value();
83 assertThat(inputs).hasSize(2);
84 verifyInputPropertiesList(inputs, updatedPropsCapture.getValue());
85 //creation of inputs values is part of the DefaultPropertyDeclarator and is tested in the ComponentInstancePropertyDeclaratorTest class
89 public void testUnDeclareProperties_whenComponentHasNoPolicies_returnOk() {
90 Resource resource = new Resource();
91 StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);
92 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
93 verifyZeroInteractions(policyOperation);
97 public void testUnDeclareProperties_whenNoPropertiesFromPolicyMatchInputId_returnOk() {
98 StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsInputs(createResourceWithPolicy(), input);
99 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
100 verifyZeroInteractions(policyOperation);
104 public void whenFailingToUpdateDeclaredProperties_returnErrorStatus() {
105 Resource resource = createResourceWithPolicies(POLICY_ID);
106 PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
107 PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
108 policyDefinition.setProperties(Collections.singletonList(getInputPropForInput));
109 when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
110 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
111 StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);
112 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
116 public void testUnDeclareProperties_propertiesUpdatedCorrectly() {
117 Resource resource = createResourceWithPolicies(POLICY_ID, "policyId2");
118 PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
119 PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
120 PropertyDataDefinition someOtherProperty = new PropertyDataDefinitionBuilder().build();
121 policyDefinition.setProperties(Arrays.asList(getInputPropForInput, someOtherProperty));
123 when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
124 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
125 StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);
127 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
128 List<PropertyDataDefinition> updatedProperties = updatedPropsCapture.getValue();
129 assertThat(updatedProperties).hasSize(1);
130 PropertyDataDefinition updatedProperty = updatedProperties.get(0);
131 assertThat(updatedProperty.isGetInputProperty()).isFalse();
132 assertThat(updatedProperty.getValue()).isEmpty();
133 assertThat(updatedProperty.getDefaultValue()).isEqualTo(getInputPropForInput.getDefaultValue());
134 assertThat(updatedProperty.getUniqueId()).isEqualTo(getInputPropForInput.getUniqueId());
138 public void testUnDeclarePropertiesAsListInputs_whenComponentHasNoPolicies_returnOk() {
139 Resource resource = new Resource();
140 StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsListInputs(resource, input);
141 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
142 verifyZeroInteractions(policyOperation);
146 public void testUnDeclarePropertiesAsListInputs_whenNoPropertiesFromPolicyMatchInputId_returnOk() {
147 StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsListInputs(createResourceWithPolicy(), input);
148 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
149 verifyZeroInteractions(policyOperation);
153 public void whenFailingToUpdateDeclaredPropertiesAsListInputs_returnErrorStatus() {
154 Resource resource = createResourceWithPolicies(POLICY_ID);
155 PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
156 PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
157 policyDefinition.setProperties(Collections.singletonList(getInputPropForInput));
158 when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
159 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
160 StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsListInputs(resource, input);
161 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
165 public void testUnDeclarePropertiesAsListInputs_propertiesUpdatedCorrectly() {
166 Resource resource = createResourceWithPolicies(POLICY_ID, "policyId3");
167 PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
168 PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
169 PropertyDataDefinition someOtherProperty = new PropertyDataDefinitionBuilder().build();
170 policyDefinition.setProperties(Arrays.asList(getInputPropForInput, someOtherProperty));
172 when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
173 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
174 StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsListInputs(resource, input);
176 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
177 List<PropertyDataDefinition> updatedProperties = updatedPropsCapture.getValue();
178 assertThat(updatedProperties).hasSize(1);
179 PropertyDataDefinition updatedProperty = updatedProperties.get(0);
180 assertThat(updatedProperty.isGetInputProperty()).isFalse();
181 assertThat(updatedProperty.getValue()).isEmpty();
182 assertThat(updatedProperty.getDefaultValue()).isEqualTo(getInputPropForInput.getDefaultValue());
183 assertThat(updatedProperty.getUniqueId()).isEqualTo(getInputPropForInput.getUniqueId());
186 private Resource createResourceWithPolicy() {
187 return createResourceWithPolicies(POLICY_ID);
190 private Resource createResourceWithPolicies(String ... policies) {
191 List<PolicyDefinition> policiesDef = Stream.of(policies)
192 .map(this::buildPolicy)
193 .collect(Collectors.toList());
195 return new ResourceBuilder()
196 .setUniqueId(RESOURCE_ID)
197 .setPolicies(policiesDef)
201 private PolicyDefinition buildPolicy(String policyId) {
202 return PolicyDefinitionBuilder.create()
203 .setUniqueId(policyId)
208 private PropertyDataDefinition buildGetInputProperty(String inputId) {
209 return new PropertyDataDefinitionBuilder()
210 .addGetInputValue(inputId)
211 .setUniqueId(POLICY_ID + "_" + inputId)
212 .setDefaultValue("defaultValue")
213 .setValue(generateGetInputValue(inputId))