2 * ============LICENSE_START=============================================================================================================
3 * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
4 * ===================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
10 * OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
11 * ============LICENSE_END===============================================================================================================
14 package org.openecomp.sdc.be.components.property;
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertTrue;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.ArgumentMatchers.anyList;
20 import static org.mockito.ArgumentMatchers.anyString;
21 import static org.mockito.ArgumentMatchers.eq;
22 import static org.mockito.Mockito.when;
24 import fj.data.Either;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.LinkedList;
29 import java.util.List;
31 import mockit.Deencapsulation;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Answers;
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.mockito.MockitoAnnotations;
39 import org.openecomp.sdc.be.components.property.propertytopolicydeclarators.ComponentInstancePropertyToPolicyDeclarator;
40 import org.openecomp.sdc.be.components.property.propertytopolicydeclarators.ComponentPropertyToPolicyDeclarator;
41 import org.openecomp.sdc.be.components.utils.ServiceBuilder;
42 import org.openecomp.sdc.be.model.Component;
43 import org.openecomp.sdc.be.model.ComponentInstInputsMap;
44 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
45 import org.openecomp.sdc.be.model.InputDefinition;
46 import org.openecomp.sdc.be.model.PolicyDefinition;
47 import org.openecomp.sdc.be.model.Resource;
48 import org.openecomp.sdc.be.model.Service;
49 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
51 public class PropertyDeclarationOrchestratorTest {
54 PropertyDeclarationOrchestrator testSubject;
56 @Mock(answer = Answers.CALLS_REAL_METHODS)
57 List<PropertyDeclarator> propertyDeceleratorsMock;
60 private ComponentInstancePropertyToPolicyDeclarator componentInstancePropertyToPolicyDeclarator;
63 private ComponentPropertyToPolicyDeclarator componentPropertyToPolicyDeclarator;
66 private ComponentInstanceInputPropertyDeclarator componentInstanceInputPropertyDecelerator;
69 private ComponentInstancePropertyDeclarator componentInstancePropertyDecelerator;
72 private ComponentPropertyDeclarator servicePropertyDeclarator;
75 private PolicyPropertyDeclarator policyPropertyDecelerator;
78 private GroupPropertyDeclarator groupPropertyDeclarator;;
80 private static final String PROP_UID = "propertyUid";
81 private static final String SERVICE_UID = "serviceUid";
83 private Service service;
86 public void setUp() throws Exception {
88 MockitoAnnotations.initMocks(this);
90 service = new ServiceBuilder().setUniqueId(SERVICE_UID).build();
93 @Test(expected = IllegalStateException.class)
94 public void testDeclarePropertiesToInputs() throws Exception {
95 Component component = new Resource();
96 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
97 Either<List<InputDefinition>, StorageOperationStatus> result;
100 result = testSubject.declarePropertiesToInputs(component, componentInstInputsMap);
104 public void testDeclarePropertiesToListInputs() throws Exception {
105 Component component = new Resource();
106 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
107 Map<String, List<ComponentInstancePropInput>> componentInstanceInputsMap = getPropertiesMapToDeclare();
108 componentInstInputsMap.setComponentInstanceInputsMap(componentInstanceInputsMap);
109 InputDefinition input = new InputDefinition();
110 input.setUniqueId(PROP_UID);
111 Either<InputDefinition, StorageOperationStatus> result;
113 when(componentInstanceInputPropertyDecelerator.declarePropertiesAsListInput(eq(component), anyString(), anyList(), eq(input))).thenReturn(Either.left(input));
115 result = testSubject.declarePropertiesToListInput(component, componentInstInputsMap, input);
117 assertTrue(result.isLeft());
118 assertEquals(PROP_UID, result.left().value().getUniqueId());
122 public void testUnDeclarePropertiesAsInputs() throws Exception {
123 Component component = new Resource();
124 InputDefinition inputToDelete = new InputDefinition();
125 StorageOperationStatus result;
127 Iterator<PropertyDeclarator> mockIter = Mockito.mock(Iterator.class);
128 when(propertyDeceleratorsMock.iterator()).thenReturn(mockIter);
129 when(mockIter.hasNext()).thenReturn(false);
131 setInputUndeclarationStubbings(component, inputToDelete);
134 result = testSubject.unDeclarePropertiesAsInputs(component, inputToDelete);
136 assertEquals(StorageOperationStatus.OK, result);
140 public void testUnDeclarePropertiesAsListInputs() throws Exception {
141 Component component = new Resource();
142 InputDefinition inputToDelete = new InputDefinition();
143 StorageOperationStatus result;
145 Iterator<PropertyDeclarator> mockIter = Mockito.mock(Iterator.class);
146 Mockito.when(propertyDeceleratorsMock.iterator()).thenReturn(mockIter);
147 Mockito.when(mockIter.hasNext()).thenReturn(false);
150 result = testSubject.unDeclarePropertiesAsListInputs(component, inputToDelete);
154 public void testGetPropOwnerId() throws Exception {
155 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
156 Map<String, List<ComponentInstancePropInput>> componentInstanceInputsMap = new HashMap<>();
157 List<ComponentInstancePropInput> value = new LinkedList<>();
158 componentInstanceInputsMap.put("mock", value);
159 componentInstInputsMap.setComponentInstanceInputsMap(componentInstanceInputsMap);
163 result = Deencapsulation.invoke(testSubject, "getPropOwnerId", componentInstInputsMap);
166 @Test(expected = IllegalStateException.class)
167 public void testGetPropertyDecelerator() throws Exception {
168 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
169 PropertyDeclarator result;
172 result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap);
176 public void testGetPropertyDeceleratorWithInputsMap() throws Exception {
177 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
178 Map<String, List<ComponentInstancePropInput>> componentInstanceInputsMap = getPropertiesMapToDeclare();
179 componentInstInputsMap.setComponentInstanceInputsMap(componentInstanceInputsMap);
180 PropertyDeclarator result;
183 result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap);
185 assertTrue(result instanceof ComponentInstanceInputPropertyDeclarator);
189 public void testGetPropertyDeceleratorWithCIProperties() throws Exception {
190 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
191 Map<String, List<ComponentInstancePropInput>> componentInstanceProperties = new HashMap<>();
192 List<ComponentInstancePropInput> value = new LinkedList<>();
193 componentInstanceProperties.put("mock", value);
194 componentInstInputsMap.setComponentInstanceProperties(componentInstanceProperties);
195 PropertyDeclarator result;
198 result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap);
200 assertTrue(result instanceof ComponentInstancePropertyDeclarator);
204 public void testGetPropertyDeceleratorWithCIPolicy() throws Exception {
205 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
206 Map<String, List<ComponentInstancePropInput>> policyProperties = getPropertiesMapToDeclare();
207 componentInstInputsMap.setPolicyProperties(policyProperties);
208 PropertyDeclarator result;
211 result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap);
213 assertTrue(result instanceof PolicyPropertyDeclarator);
217 public void testDeclarePropertiesToPolicies() {
218 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
219 Map<String, List<ComponentInstancePropInput>> policyProperties = getPropertiesMapToDeclare();
220 componentInstInputsMap.setComponentInstancePropertiesToPolicies(policyProperties);
222 PolicyDefinition declaredPolicy = getDeclaredPolicy();
224 when(componentInstancePropertyToPolicyDeclarator.declarePropertiesAsPolicies(any(), anyString(), anyList())).thenReturn(
225 Either.left(Collections.singletonList(declaredPolicy)));
227 Either<List<PolicyDefinition>, StorageOperationStatus> declareEither =
228 testSubject.declarePropertiesToPolicies(service, componentInstInputsMap);
230 validatePolicyDeclaration(declaredPolicy, declareEither);
234 public void testUndeclarePropertiesAsPolicies() {
235 when(componentInstancePropertyToPolicyDeclarator.unDeclarePropertiesAsPolicies(any(), any(PolicyDefinition.class))).thenReturn(StorageOperationStatus.OK);
236 when(componentPropertyToPolicyDeclarator.unDeclarePropertiesAsPolicies(any(), any(PolicyDefinition.class))).thenReturn(StorageOperationStatus.OK);
238 StorageOperationStatus undeclareStatus =
239 testSubject.unDeclarePropertiesAsPolicies(service, getDeclaredPolicy());
241 assertEquals(StorageOperationStatus.OK, undeclareStatus);
245 public void testDeclareServiceProperties() {
246 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
247 Map<String, List<ComponentInstancePropInput>> serviceProperties = getPropertiesMapToDeclare();
248 componentInstInputsMap.setServiceProperties(serviceProperties);
250 PolicyDefinition declaredPolicy = getDeclaredPolicy();
252 when(servicePropertyDeclarator.declarePropertiesAsPolicies(any(), anyString(), anyList())).thenReturn(
253 Either.left(Collections.singletonList(declaredPolicy)));
255 Either<List<PolicyDefinition>, StorageOperationStatus> declareEither =
256 testSubject.declarePropertiesToPolicies(service, componentInstInputsMap);
258 validatePolicyDeclaration(declaredPolicy, declareEither);
261 private PolicyDefinition getDeclaredPolicy() {
262 PolicyDefinition declaredPolicy = new PolicyDefinition();
263 declaredPolicy.setUniqueId(PROP_UID);
264 return declaredPolicy;
267 private Map<String, List<ComponentInstancePropInput>> getPropertiesMapToDeclare() {
268 Map<String, List<ComponentInstancePropInput>> policyProperties = new HashMap<>();
270 ComponentInstancePropInput propertyToDeclare = new ComponentInstancePropInput();
271 propertyToDeclare.setUniqueId(PROP_UID);
272 propertyToDeclare.setPropertiesName(PROP_UID);
274 policyProperties.put("mock", Collections.singletonList(propertyToDeclare));
275 return policyProperties;
278 private void validatePolicyDeclaration(PolicyDefinition declaredPolicy,
279 Either<List<PolicyDefinition>, StorageOperationStatus> declareEither) {
280 assertTrue(declareEither.isLeft());
282 List<PolicyDefinition> declaredPolicies = declareEither.left().value();
283 assertEquals(1, declaredPolicies.size());
284 assertEquals(declaredPolicy.getUniqueId(), declaredPolicies.get(0).getUniqueId());
287 private void setInputUndeclarationStubbings(Component component, InputDefinition inputToDelete) {
288 when(policyPropertyDecelerator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(
289 StorageOperationStatus.OK);
290 when(servicePropertyDeclarator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(StorageOperationStatus.OK);
291 when(componentInstancePropertyDecelerator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(StorageOperationStatus.OK);
292 when(componentInstanceInputPropertyDecelerator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(StorageOperationStatus.OK);
293 when(groupPropertyDeclarator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(StorageOperationStatus.OK);