1 package org.openecomp.sdc.be.components.property;
3 import static org.assertj.core.api.Java6Assertions.assertThat;
4 import static org.mockito.ArgumentMatchers.eq;
5 import static org.mockito.Mockito.verifyZeroInteractions;
6 import static org.mockito.Mockito.when;
8 import java.util.Arrays;
9 import java.util.Collections;
10 import java.util.List;
11 import java.util.stream.Collectors;
12 import java.util.stream.Stream;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.ArgumentCaptor;
18 import org.mockito.Captor;
19 import org.mockito.InjectMocks;
20 import org.mockito.Mock;
21 import org.mockito.junit.MockitoJUnitRunner;
22 import org.openecomp.sdc.be.components.utils.PolicyDefinitionBuilder;
23 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
24 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
25 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
26 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
27 import org.openecomp.sdc.be.model.InputDefinition;
28 import org.openecomp.sdc.be.model.PolicyDefinition;
29 import org.openecomp.sdc.be.model.Resource;
30 import org.openecomp.sdc.be.model.jsontitan.operations.PolicyOperation;
31 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
32 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
34 import fj.data.Either;
37 @RunWith(MockitoJUnitRunner.class)
38 //note that testing for most of the common logic is under the ComponentInstancePropertyDeceleratorTest
39 public class PolicyPropertyDeceleratorTest extends PropertyDeceleratorTestBase{
41 private static final String POLICY_ID = "policyId";
42 private static final String RESOURCE_ID = "resourceId";
43 private static final String INPUT_ID = "inputId";
45 private PolicyPropertyDecelerator policyPropertyDecelerator;
47 private PolicyOperation policyOperation;
49 private PropertyOperation propertyOperation;
51 private ArgumentCaptor<List<PropertyDataDefinition>> updatedPropsCapture;
52 private Resource resource;
53 private InputDefinition input;
57 public void setUp() throws Exception {
59 resource = createResourceWithPolicy();
60 input = new InputDefinition();
61 input.setUniqueId(INPUT_ID);
62 input.setName(INPUT_ID);
63 input.setValue("value");
67 public void testDeclarePropertiesAsInputs_policyNotExist() {
68 Either<List<InputDefinition>, StorageOperationStatus> declareResult = policyPropertyDecelerator.declarePropertiesAsInputs(resource, "nonExistingPolicy", Collections.emptyList());
69 assertThat(declareResult.right().value()).isEqualTo(StorageOperationStatus.NOT_FOUND);
70 verifyZeroInteractions(policyOperation);
74 public void testDeclarePropertiesAsInputs_failedToUpdateProperties() {
75 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
76 Either<List<InputDefinition>, StorageOperationStatus> declareResult = policyPropertyDecelerator.declarePropertiesAsInputs(resource, POLICY_ID, Collections.emptyList());
77 assertThat(declareResult.right().value()).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
81 public void testDeclarePropertiesAsInputs() {
82 List<PropertyDataDefinition> properties = Arrays.asList(prop1, prop2);
83 List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
84 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
85 Either<List<InputDefinition>, StorageOperationStatus> createdInputs = policyPropertyDecelerator.declarePropertiesAsInputs(resource, POLICY_ID, propsToDeclare);
86 List<InputDefinition> inputs = createdInputs.left().value();
87 assertThat(inputs).hasSize(2);
88 verifyInputPropertiesList(inputs, updatedPropsCapture.getValue());
89 //creation of inputs values is part of the DefaultPropertyDecelerator and is tested in the ComponentInstancePropertyDeceleratorTest class
93 public void testUnDeclareProperties_whenComponentHasNoPolicies_returnOk() {
94 Resource resource = new Resource();
95 StorageOperationStatus storageOperationStatus = policyPropertyDecelerator.unDeclarePropertiesAsInputs(resource, input);
96 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
97 verifyZeroInteractions(policyOperation);
101 public void testUnDeclareProperties_whenNoPropertiesFromPolicyMatchInputId_returnOk() {
102 StorageOperationStatus storageOperationStatus = policyPropertyDecelerator.unDeclarePropertiesAsInputs(createResourceWithPolicy(), input);
103 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
104 verifyZeroInteractions(policyOperation);
108 public void whenFailingToUpdateDeclaredProperties_returnErrorStatus() {
109 Resource resource = createResourceWithPolicies(POLICY_ID);
110 PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
111 PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
112 policyDefinition.setProperties(Collections.singletonList(getInputPropForInput));
113 when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
114 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
115 StorageOperationStatus storageOperationStatus = policyPropertyDecelerator.unDeclarePropertiesAsInputs(resource, input);
116 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
120 public void testUnDeclareProperties_propertiesUpdatedCorrectly() {
121 Resource resource = createResourceWithPolicies(POLICY_ID, "policyId2");
122 PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
123 PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
124 PropertyDataDefinition someOtherProperty = new PropertyDataDefinitionBuilder().build();
125 policyDefinition.setProperties(Arrays.asList(getInputPropForInput, someOtherProperty));
127 when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
128 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
129 StorageOperationStatus storageOperationStatus = policyPropertyDecelerator.unDeclarePropertiesAsInputs(resource, input);
131 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
132 List<PropertyDataDefinition> updatedProperties = updatedPropsCapture.getValue();
133 assertThat(updatedProperties).hasSize(1);
134 PropertyDataDefinition updatedProperty = updatedProperties.get(0);
135 assertThat(updatedProperty.isGetInputProperty()).isFalse();
136 assertThat(updatedProperty.getValue()).isEmpty();
137 assertThat(updatedProperty.getDefaultValue()).isEqualTo(getInputPropForInput.getDefaultValue());
138 assertThat(updatedProperty.getUniqueId()).isEqualTo(getInputPropForInput.getUniqueId());
141 private Resource createResourceWithPolicy() {
142 return createResourceWithPolicies(POLICY_ID);
145 private Resource createResourceWithPolicies(String ... policies) {
146 List<PolicyDefinition> policiesDef = Stream.of(policies)
147 .map(this::buildPolicy)
148 .collect(Collectors.toList());
150 return new ResourceBuilder()
151 .setUniqueId(RESOURCE_ID)
152 .setPolicies(policiesDef)
156 private PolicyDefinition buildPolicy(String policyId) {
157 return PolicyDefinitionBuilder.create()
158 .setUniqueId(policyId)
163 private PropertyDataDefinition buildGetInputProperty(String inputId) {
164 return new PropertyDataDefinitionBuilder()
165 .addGetInputValue(inputId)
166 .setUniqueId(POLICY_ID + "_" + inputId)
167 .setDefaultValue("defaultValue")
168 .setValue(generateGetInputValue(inputId))