2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.property;
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;
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;
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;
56 @RunWith(MockitoJUnitRunner.class)
57 //note that testing for most of the common logic is under the ComponentInstancePropertyDeceleratorTest
58 public class PolicyPropertyDeceleratorTest extends PropertyDeceleratorTestBase{
60 private static final String POLICY_ID = "policyId";
61 private static final String RESOURCE_ID = "resourceId";
62 private static final String INPUT_ID = "inputId";
64 private PolicyPropertyDeclarator policyPropertyDeclarator;
66 private PolicyOperation policyOperation;
68 private PropertyOperation propertyOperation;
70 private ArgumentCaptor<List<PropertyDataDefinition>> updatedPropsCapture;
71 private Resource resource;
72 private InputDefinition input;
76 public void setUp() throws Exception {
78 resource = createResourceWithPolicy();
79 input = new InputDefinition();
80 input.setUniqueId(INPUT_ID);
81 input.setName(INPUT_ID);
82 input.setValue("value");
86 public void testDeclarePropertiesAsInputs_policyNotExist() {
87 Either<List<InputDefinition>, StorageOperationStatus> declareResult = policyPropertyDeclarator.declarePropertiesAsInputs(resource, "nonExistingPolicy", Collections.emptyList());
88 assertThat(declareResult.right().value()).isEqualTo(StorageOperationStatus.NOT_FOUND);
89 verifyZeroInteractions(policyOperation);
93 public void testDeclarePropertiesAsInputs_failedToUpdateProperties() {
94 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
95 Either<List<InputDefinition>, StorageOperationStatus> declareResult = policyPropertyDeclarator.declarePropertiesAsInputs(resource, POLICY_ID, Collections.emptyList());
96 assertThat(declareResult.right().value()).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
100 public void testDeclarePropertiesAsInputs() {
101 List<PropertyDataDefinition> properties = Arrays.asList(prop1, prop2);
102 List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
103 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
104 Either<List<InputDefinition>, StorageOperationStatus> createdInputs = policyPropertyDeclarator.declarePropertiesAsInputs(resource, POLICY_ID, propsToDeclare);
105 List<InputDefinition> inputs = createdInputs.left().value();
106 assertThat(inputs).hasSize(2);
107 verifyInputPropertiesList(inputs, updatedPropsCapture.getValue());
108 //creation of inputs values is part of the DefaultPropertyDecelerator and is tested in the ComponentInstancePropertyDeceleratorTest class
112 public void testUnDeclareProperties_whenComponentHasNoPolicies_returnOk() {
113 Resource resource = new Resource();
114 StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);
115 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
116 verifyZeroInteractions(policyOperation);
120 public void testUnDeclareProperties_whenNoPropertiesFromPolicyMatchInputId_returnOk() {
121 StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsInputs(createResourceWithPolicy(), input);
122 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
123 verifyZeroInteractions(policyOperation);
127 public void whenFailingToUpdateDeclaredProperties_returnErrorStatus() {
128 Resource resource = createResourceWithPolicies(POLICY_ID);
129 PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
130 PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
131 policyDefinition.setProperties(Collections.singletonList(getInputPropForInput));
132 when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
133 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.GENERAL_ERROR);
134 StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);
135 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.GENERAL_ERROR);
139 public void testUnDeclareProperties_propertiesUpdatedCorrectly() {
140 Resource resource = createResourceWithPolicies(POLICY_ID, "policyId2");
141 PolicyDefinition policyDefinition = resource.getPolicies().get(POLICY_ID);
142 PropertyDataDefinition getInputPropForInput = buildGetInputProperty(INPUT_ID);
143 PropertyDataDefinition someOtherProperty = new PropertyDataDefinitionBuilder().build();
144 policyDefinition.setProperties(Arrays.asList(getInputPropForInput, someOtherProperty));
146 when(propertyOperation.findDefaultValueFromSecondPosition(Collections.emptyList(), getInputPropForInput.getUniqueId(), getInputPropForInput.getDefaultValue())).thenReturn(Either.left(getInputPropForInput.getDefaultValue()));
147 when(policyOperation.updatePolicyProperties(eq(resource), eq(POLICY_ID), updatedPropsCapture.capture())).thenReturn(StorageOperationStatus.OK);
148 StorageOperationStatus storageOperationStatus = policyPropertyDeclarator.unDeclarePropertiesAsInputs(resource, input);
150 assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK);
151 List<PropertyDataDefinition> updatedProperties = updatedPropsCapture.getValue();
152 assertThat(updatedProperties).hasSize(1);
153 PropertyDataDefinition updatedProperty = updatedProperties.get(0);
154 assertThat(updatedProperty.isGetInputProperty()).isFalse();
155 assertThat(updatedProperty.getValue()).isEmpty();
156 assertThat(updatedProperty.getDefaultValue()).isEqualTo(getInputPropForInput.getDefaultValue());
157 assertThat(updatedProperty.getUniqueId()).isEqualTo(getInputPropForInput.getUniqueId());
160 private Resource createResourceWithPolicy() {
161 return createResourceWithPolicies(POLICY_ID);
164 private Resource createResourceWithPolicies(String ... policies) {
165 List<PolicyDefinition> policiesDef = Stream.of(policies)
166 .map(this::buildPolicy)
167 .collect(Collectors.toList());
169 return new ResourceBuilder()
170 .setUniqueId(RESOURCE_ID)
171 .setPolicies(policiesDef)
175 private PolicyDefinition buildPolicy(String policyId) {
176 return PolicyDefinitionBuilder.create()
177 .setUniqueId(policyId)
182 private PropertyDataDefinition buildGetInputProperty(String inputId) {
183 return new PropertyDataDefinitionBuilder()
184 .addGetInputValue(inputId)
185 .setUniqueId(POLICY_ID + "_" + inputId)
186 .setDefaultValue("defaultValue")
187 .setValue(generateGetInputValue(inputId))