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;
36 @RunWith(MockitoJUnitRunner.class)
37 //note that testing for most of the common logic is under the ComponentInstancePropertyDeceleratorTest
38 public class PolicyPropertyDeceleratorTest extends PropertyDeceleratorTestBase{
40 private static final String POLICY_ID = "policyId";
41 private static final String RESOURCE_ID = "resourceId";
42 private static final String INPUT_ID = "inputId";
44 private PolicyPropertyDecelerator policyPropertyDecelerator;
46 private PolicyOperation policyOperation;
48 private PropertyOperation propertyOperation;
50 private ArgumentCaptor<List<PropertyDataDefinition>> updatedPropsCapture;
51 private Resource resource;
52 private InputDefinition input;
56 public void setUp() throws Exception {
58 resource = createResourceWithPolicy();
59 input = new InputDefinition();
60 input.setUniqueId(INPUT_ID);
61 input.setName(INPUT_ID);
62 input.setValue("value");
66 public void testDeclarePropertiesAsInputs_policyNotExist() {
67 Either<List<InputDefinition>, StorageOperationStatus> declareResult = policyPropertyDecelerator.declarePropertiesAsInputs(resource, "nonExistingPolicy", Collections.emptyList());
68 assertThat(declareResult.right().value()).isEqualTo(StorageOperationStatus.NOT_FOUND);
69 verifyZeroInteractions(policyOperation);
73 public void testDeclarePropertiesAsInputs_failedToUpdateProperties() {
74 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
75 Either<List<InputDefinition>, StorageOperationStatus> declareResult = policyPropertyDecelerator.declarePropertiesAsInputs(resource, POLICY_ID, Collections.emptyList());
76 assertThat(declareResult.right().value()).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
80 public void testDeclarePropertiesAsInputs() {
81 List<PropertyDataDefinition> properties = Arrays.asList(prop1, prop2);
82 List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
83 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
84 Either<List<InputDefinition>, StorageOperationStatus> createdInputs = policyPropertyDecelerator.declarePropertiesAsInputs(resource, POLICY_ID, propsToDeclare);
85 List<InputDefinition> inputs = createdInputs.left().value();
86 assertThat(inputs).hasSize(2);
87 verifyInputPropertiesList(inputs, updatedPropsCapture.getValue());
88 //creation of inputs values is part of the DefaultPropertyDecelerator and is tested in the ComponentInstancePropertyDeceleratorTest class
92 public void testUnDeclareProperties_whenComponentHasNoPolicies_returnOk() {
93 Resource resource = new Resource();
94 StorageOperationStatus storageOperationStatus = policyPropertyDecelerator.unDeclarePropertiesAsInputs(resource, input);
95 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
96 verifyZeroInteractions(policyOperation);
100 public void testUnDeclareProperties_whenNoPropertiesFromPolicyMatchInputId_returnOk() {
101 StorageOperationStatus storageOperationStatus = policyPropertyDecelerator.unDeclarePropertiesAsInputs(createResourceWithPolicy(), input);
102 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
103 verifyZeroInteractions(policyOperation);
107 public void whenFailingToUpdateDeclaredProperties_returnErrorStatus() {
108 Resource resource = createResourceWithPolicies(POLICY_ID);
109 PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
110 PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
111 policyDefinition.setProperties(Collections.singletonList(getInputPropForInput));
112 when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
113 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
114 StorageOperationStatus storageOperationStatus = policyPropertyDecelerator.unDeclarePropertiesAsInputs(resource, input);
115 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
119 public void testUnDeclareProperties_propertiesUpdatedCorrectly() {
120 Resource resource = createResourceWithPolicies(POLICY_ID, "policyId2");
121 PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
122 PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
123 PropertyDataDefinition someOtherProperty = new PropertyDataDefinitionBuilder().build();
124 policyDefinition.setProperties(Arrays.asList(getInputPropForInput, someOtherProperty));
126 when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
127 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
128 StorageOperationStatus storageOperationStatus = policyPropertyDecelerator.unDeclarePropertiesAsInputs(resource, input);
130 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
131 List<PropertyDataDefinition> updatedProperties = updatedPropsCapture.getValue();
132 assertThat(updatedProperties).hasSize(1);
133 PropertyDataDefinition updatedProperty = updatedProperties.get(0);
134 assertThat(updatedProperty.isGetInputProperty()).isFalse();
135 assertThat(updatedProperty.getValue()).isEmpty();
136 assertThat(updatedProperty.getDefaultValue()).isEqualTo(getInputPropForInput.getDefaultValue());
137 assertThat(updatedProperty.getUniqueId()).isEqualTo(getInputPropForInput.getUniqueId());
140 private Resource createResourceWithPolicy() {
141 return createResourceWithPolicies(POLICY_ID);
144 private Resource createResourceWithPolicies(String ... policies) {
145 List<PolicyDefinition> policiesDef = Stream.of(policies)
146 .map(this::buildPolicy)
147 .collect(Collectors.toList());
149 return new ResourceBuilder()
150 .setUniqueId(RESOURCE_ID)
151 .setPolicies(policiesDef)
155 private PolicyDefinition buildPolicy(String policyId) {
156 return PolicyDefinitionBuilder.create()
157 .setUniqueId(policyId)
162 private PropertyDataDefinition buildGetInputProperty(String inputId) {
163 return new PropertyDataDefinitionBuilder()
164 .addGetInputValue(inputId)
165 .setUniqueId(POLICY_ID + "_" + inputId)
166 .setDefaultValue("defaultValue")
167 .setValue(generateGetInputValue(inputId))