Sync Integ to Master
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / property / PolicyPropertyDeceleratorTest.java
1 package org.openecomp.sdc.be.components.property;
2
3 import fj.data.Either;
4 import org.junit.Before;
5 import org.junit.Test;
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;
23
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;
29
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;
34
35
36 @RunWith(MockitoJUnitRunner.class)
37 //note that testing for most of the common logic is under the ComponentInstancePropertyDeceleratorTest
38 public class PolicyPropertyDeceleratorTest extends PropertyDeceleratorTestBase{
39
40     private static final String POLICY_ID = "policyId";
41     private static final String RESOURCE_ID = "resourceId";
42     private static final String INPUT_ID = "inputId";
43     @InjectMocks
44     private PolicyPropertyDecelerator policyPropertyDecelerator;
45     @Mock
46     private PolicyOperation policyOperation;
47     @Mock
48     private PropertyOperation propertyOperation;
49     @Captor
50     private ArgumentCaptor<List<PropertyDataDefinition>> updatedPropsCapture;
51     private Resource resource;
52     private InputDefinition input;
53
54     @Override
55     @Before
56     public void setUp() throws Exception {
57         super.setUp();
58         resource = createResourceWithPolicy();
59         input = new InputDefinition();
60         input.setUniqueId(INPUT_ID);
61         input.setName(INPUT_ID);
62         input.setValue("value");
63     }
64
65     @Test
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);
70     }
71
72     @Test
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);
77     }
78
79     @Test
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
89     }
90
91     @Test
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);
97     }
98
99     @Test
100     public void testUnDeclareProperties_whenNoPropertiesFromPolicyMatchInputId_returnOk() {
101         StorageOperationStatus storageOperationStatus = policyPropertyDecelerator.unDeclarePropertiesAsInputs(createResourceWithPolicy(), input);
102         assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
103         verifyZeroInteractions(policyOperation);
104     }
105
106     @Test
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);
116     }
117
118     @Test
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));
125
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);
129
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());
138     }
139
140     private Resource createResourceWithPolicy() {
141         return createResourceWithPolicies(POLICY_ID);
142     }
143
144     private Resource createResourceWithPolicies(String ... policies) {
145         List<PolicyDefinition> policiesDef = Stream.of(policies)
146                 .map(this::buildPolicy)
147                 .collect(Collectors.toList());
148
149         return new ResourceBuilder()
150                 .setUniqueId(RESOURCE_ID)
151                 .setPolicies(policiesDef)
152                 .build();
153     }
154
155     private PolicyDefinition buildPolicy(String policyId) {
156         return PolicyDefinitionBuilder.create()
157                 .setUniqueId(policyId)
158                 .setName(policyId)
159                 .build();
160     }
161
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))
168                 .build();
169     }
170
171
172 }