Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / property / PropertyDeclarationOrchestratorTest.java
1 /*
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
6  *
7  *        http://www.apache.org/licenses/LICENSE-2.0
8  *
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===============================================================================================================
12  *
13  */
14 package org.openecomp.sdc.be.components.property;
15
16 import fj.data.Either;
17 import mockit.Deencapsulation;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Answers;
21 import org.mockito.InjectMocks;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.MockitoAnnotations;
25 import org.openecomp.sdc.be.components.property.propertytopolicydeclarators.ComponentInstancePropertyToPolicyDeclarator;
26 import org.openecomp.sdc.be.components.property.propertytopolicydeclarators.ComponentPropertyToPolicyDeclarator;
27 import org.openecomp.sdc.be.components.utils.ServiceBuilder;
28 import org.openecomp.sdc.be.model.Component;
29 import org.openecomp.sdc.be.model.ComponentInstInputsMap;
30 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
31 import org.openecomp.sdc.be.model.InputDefinition;
32 import org.openecomp.sdc.be.model.PolicyDefinition;
33 import org.openecomp.sdc.be.model.Resource;
34 import org.openecomp.sdc.be.model.Service;
35 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
36
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.Iterator;
40 import java.util.LinkedList;
41 import java.util.List;
42 import java.util.Map;
43
44 import static org.junit.Assert.assertEquals;
45 import static org.junit.Assert.assertTrue;
46 import static org.mockito.ArgumentMatchers.any;
47 import static org.mockito.ArgumentMatchers.anyList;
48 import static org.mockito.ArgumentMatchers.anyString;
49 import static org.mockito.ArgumentMatchers.eq;
50 import static org.mockito.Mockito.when;
51
52 public class PropertyDeclarationOrchestratorTest {
53
54         @InjectMocks
55         PropertyDeclarationOrchestrator testSubject;
56
57         @Mock(answer = Answers.CALLS_REAL_METHODS)
58         List<PropertyDeclarator> propertyDeceleratorsMock;
59
60         @Mock
61         private ComponentInstancePropertyToPolicyDeclarator componentInstancePropertyToPolicyDeclarator;
62
63         @Mock
64         private ComponentPropertyToPolicyDeclarator componentPropertyToPolicyDeclarator;
65
66         @Mock
67         private ComponentInstanceInputPropertyDeclarator componentInstanceInputPropertyDecelerator;
68
69         @Mock
70         private ComponentInstancePropertyDeclarator componentInstancePropertyDecelerator;
71
72         @Mock
73         private ComponentPropertyDeclarator servicePropertyDeclarator;
74
75         @Mock
76         private PolicyPropertyDeclarator policyPropertyDecelerator;
77
78         @Mock
79         private GroupPropertyDeclarator groupPropertyDeclarator;;
80
81         private static final String PROP_UID = "propertyUid";
82         private static final String SERVICE_UID = "serviceUid";
83
84         private Service service;
85
86         @Before
87         public void setUp() throws Exception {
88
89                 MockitoAnnotations.initMocks(this);
90
91                 service = new ServiceBuilder().setUniqueId(SERVICE_UID).build();
92         }
93
94         @Test(expected = IllegalStateException.class)
95         public void testDeclarePropertiesToInputs() throws Exception {
96                 Component component = new Resource();
97                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
98                 Either<List<InputDefinition>, StorageOperationStatus> result;
99
100                 // default test
101                 result = testSubject.declarePropertiesToInputs(component, componentInstInputsMap);
102         }
103
104         @Test
105         public void testDeclarePropertiesToListInputs() throws Exception {
106                 Component component = new Resource();
107                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
108                 Map<String, List<ComponentInstancePropInput>> componentInstanceInputsMap = getPropertiesMapToDeclare();
109                 componentInstInputsMap.setComponentInstanceInputsMap(componentInstanceInputsMap);
110                 InputDefinition input = new InputDefinition();
111                 input.setUniqueId(PROP_UID);
112                 Either<InputDefinition, StorageOperationStatus> result;
113
114                 when(componentInstanceInputPropertyDecelerator.declarePropertiesAsListInput(eq(component), anyString(), anyList(), eq(input))).thenReturn(Either.left(input));
115                 // default test
116                 result = testSubject.declarePropertiesToListInput(component, componentInstInputsMap, input);
117
118                 assertTrue(result.isLeft());
119                 assertEquals(PROP_UID, result.left().value().getUniqueId());
120         }
121
122         @Test
123         public void testUnDeclarePropertiesAsInputs() throws Exception {
124                 Component component = new Resource();
125                 InputDefinition inputToDelete = new InputDefinition();
126                 StorageOperationStatus result;
127
128                 Iterator<PropertyDeclarator> mockIter = Mockito.mock(Iterator.class);
129                 when(propertyDeceleratorsMock.iterator()).thenReturn(mockIter);
130                 when(mockIter.hasNext()).thenReturn(false);
131
132                 setInputUndeclarationStubbings(component, inputToDelete);
133
134                 // default test
135                 result = testSubject.unDeclarePropertiesAsInputs(component, inputToDelete);
136
137                 assertEquals(StorageOperationStatus.OK, result);
138         }
139
140         @Test
141         public void testUnDeclarePropertiesAsListInputs() throws Exception {
142                 Component component = new Resource();
143                 InputDefinition inputToDelete = new InputDefinition();
144                 StorageOperationStatus result;
145
146                 Iterator<PropertyDeclarator> mockIter = Mockito.mock(Iterator.class);
147                 Mockito.when(propertyDeceleratorsMock.iterator()).thenReturn(mockIter);
148                 Mockito.when(mockIter.hasNext()).thenReturn(false);
149
150                 // default test
151                 result = testSubject.unDeclarePropertiesAsListInputs(component, inputToDelete);
152         }
153
154         @Test
155         public void testGetPropOwnerId() throws Exception {
156                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
157                 Map<String, List<ComponentInstancePropInput>> componentInstanceInputsMap = new HashMap<>();
158                 List<ComponentInstancePropInput> value = new LinkedList<>();
159                 componentInstanceInputsMap.put("mock", value);
160                 componentInstInputsMap.setComponentInstanceInputsMap(componentInstanceInputsMap);
161                 String result;
162
163                 // default test
164                 result = Deencapsulation.invoke(testSubject, "getPropOwnerId", componentInstInputsMap);
165         }
166
167         @Test(expected = IllegalStateException.class)
168         public void testGetPropertyDecelerator() throws Exception {
169                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
170                 PropertyDeclarator result;
171
172                 // default test
173                 result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap);
174         }
175
176         @Test
177         public void testGetPropertyDeceleratorWithInputsMap() throws Exception {
178                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
179                 Map<String, List<ComponentInstancePropInput>> componentInstanceInputsMap = getPropertiesMapToDeclare();
180                 componentInstInputsMap.setComponentInstanceInputsMap(componentInstanceInputsMap);
181                 PropertyDeclarator result;
182
183                 // default test
184                 result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap);
185
186                 assertTrue(result instanceof ComponentInstanceInputPropertyDeclarator);
187         }
188
189         @Test
190         public void testGetPropertyDeceleratorWithCIProperties() throws Exception {
191                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
192                 Map<String, List<ComponentInstancePropInput>> componentInstanceProperties = new HashMap<>();
193                 List<ComponentInstancePropInput> value = new LinkedList<>();
194                 componentInstanceProperties.put("mock", value);
195                 componentInstInputsMap.setComponentInstancePropInput(componentInstanceProperties);
196                 PropertyDeclarator result;
197
198                 // default test
199                 result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap);
200
201                 assertTrue(result instanceof ComponentInstancePropertyDeclarator);
202         }
203
204         @Test
205         public void testGetPropertyDeceleratorWithCIPolicy() throws Exception {
206                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
207                 Map<String, List<ComponentInstancePropInput>> policyProperties = getPropertiesMapToDeclare();
208                 componentInstInputsMap.setPolicyProperties(policyProperties);
209                 PropertyDeclarator result;
210
211                 // default test
212                 result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap);
213
214                 assertTrue(result instanceof PolicyPropertyDeclarator);
215         }
216
217         @Test
218         public void testDeclarePropertiesToPolicies() {
219                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
220                 Map<String, List<ComponentInstancePropInput>> policyProperties = getPropertiesMapToDeclare();
221                 componentInstInputsMap.setComponentInstancePropertiesToPolicies(policyProperties);
222
223                 PolicyDefinition declaredPolicy = getDeclaredPolicy();
224
225                 when(componentInstancePropertyToPolicyDeclarator.declarePropertiesAsPolicies(any(), anyString(), anyList())).thenReturn(
226                                 Either.left(Collections.singletonList(declaredPolicy)));
227
228                 Either<List<PolicyDefinition>, StorageOperationStatus> declareEither =
229                                 testSubject.declarePropertiesToPolicies(service, componentInstInputsMap);
230
231                 validatePolicyDeclaration(declaredPolicy, declareEither);
232         }
233
234         @Test
235         public void testUndeclarePropertiesAsPolicies() {
236                 when(componentInstancePropertyToPolicyDeclarator.unDeclarePropertiesAsPolicies(any(), any(PolicyDefinition.class))).thenReturn(StorageOperationStatus.OK);
237                 when(componentPropertyToPolicyDeclarator.unDeclarePropertiesAsPolicies(any(), any(PolicyDefinition.class))).thenReturn(StorageOperationStatus.OK);
238
239                 StorageOperationStatus undeclareStatus =
240                                 testSubject.unDeclarePropertiesAsPolicies(service, getDeclaredPolicy());
241
242                 assertEquals(StorageOperationStatus.OK, undeclareStatus);
243         }
244
245         @Test
246         public void testDeclareServiceProperties() {
247                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
248                 Map<String, List<ComponentInstancePropInput>> serviceProperties = getPropertiesMapToDeclare();
249                 componentInstInputsMap.setServiceProperties(serviceProperties);
250
251                 PolicyDefinition declaredPolicy = getDeclaredPolicy();
252
253                 when(servicePropertyDeclarator.declarePropertiesAsPolicies(any(), anyString(), anyList())).thenReturn(
254                                 Either.left(Collections.singletonList(declaredPolicy)));
255
256                 Either<List<PolicyDefinition>, StorageOperationStatus> declareEither =
257                                 testSubject.declarePropertiesToPolicies(service, componentInstInputsMap);
258
259                 validatePolicyDeclaration(declaredPolicy, declareEither);
260         }
261
262         private PolicyDefinition getDeclaredPolicy() {
263                 PolicyDefinition declaredPolicy = new PolicyDefinition();
264                 declaredPolicy.setUniqueId(PROP_UID);
265                 return declaredPolicy;
266         }
267
268         private Map<String, List<ComponentInstancePropInput>> getPropertiesMapToDeclare() {
269                 Map<String, List<ComponentInstancePropInput>> policyProperties = new HashMap<>();
270
271                 ComponentInstancePropInput propertyToDeclare = new ComponentInstancePropInput();
272                 propertyToDeclare.setUniqueId(PROP_UID);
273                 propertyToDeclare.setPropertiesName(PROP_UID);
274
275                 policyProperties.put("mock", Collections.singletonList(propertyToDeclare));
276                 return policyProperties;
277         }
278
279         private void validatePolicyDeclaration(PolicyDefinition declaredPolicy,
280                         Either<List<PolicyDefinition>, StorageOperationStatus> declareEither) {
281                 assertTrue(declareEither.isLeft());
282
283                 List<PolicyDefinition> declaredPolicies = declareEither.left().value();
284                 assertEquals(1, declaredPolicies.size());
285                 assertEquals(declaredPolicy.getUniqueId(), declaredPolicies.get(0).getUniqueId());
286         }
287
288         private void setInputUndeclarationStubbings(Component component, InputDefinition inputToDelete) {
289                 when(policyPropertyDecelerator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(
290                                 StorageOperationStatus.OK);
291                 when(servicePropertyDeclarator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(StorageOperationStatus.OK);
292                 when(componentInstancePropertyDecelerator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(StorageOperationStatus.OK);
293                 when(componentInstanceInputPropertyDecelerator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(StorageOperationStatus.OK);
294                 when(groupPropertyDeclarator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(StorageOperationStatus.OK);
295         }
296 }