Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / datamodel / utils / PropertyValueConstraintValidationUtilTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.datamodel.utils;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.reflect.TypeToken;
26 import fj.data.Either;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.mockito.MockitoAnnotations;
34 import org.mockito.Spy;
35 import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
36 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
37 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
38 import org.openecomp.sdc.be.model.DataTypeDefinition;
39 import org.openecomp.sdc.be.model.InputDefinition;
40 import org.openecomp.sdc.be.model.PropertyConstraint;
41 import org.openecomp.sdc.be.model.PropertyDefinition;
42 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
43 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
44 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
45 import org.openecomp.sdc.exception.ResponseFormat;
46
47 import java.io.BufferedReader;
48 import java.io.File;
49 import java.lang.reflect.Type;
50 import java.nio.charset.StandardCharsets;
51 import java.nio.file.Files;
52 import java.nio.file.Path;
53 import java.nio.file.Paths;
54 import java.util.Collections;
55 import java.util.List;
56 import java.util.Map;
57 import java.util.Objects;
58
59 import static org.mockito.ArgumentMatchers.any;
60 import static org.mockito.Mockito.when;
61
62 public class PropertyValueConstraintValidationUtilTest {
63
64         @Mock
65         ApplicationDataTypeCache applicationDataTypeCache;
66
67         @Mock
68         ToscaOperationFacade toscaOperationFacade;
69
70         @Spy
71         @InjectMocks
72         PropertyValueConstraintValidationUtil propertyValueConstraintValidationUtil;
73
74         private Map<String, DataTypeDefinition> dataTypeDefinitionMap;
75
76         @Before
77         public void init() {
78                 MockitoAnnotations.initMocks(this);
79                 ResponseFormatManager responseFormatManagerMock = Mockito.mock(ResponseFormatManager.class);
80                 when(responseFormatManagerMock.getResponseFormat(any())).thenReturn(new ResponseFormat());
81                 when(responseFormatManagerMock.getResponseFormat(any(), any())).thenReturn(new ResponseFormat());
82                 when(responseFormatManagerMock.getResponseFormat(any(), any(), any())).thenReturn(new ResponseFormat());
83                 when(propertyValueConstraintValidationUtil.getResponseFormatManager()).thenReturn(responseFormatManagerMock);
84
85                 createDataTypeMap();
86         }
87
88         @Test
89         public void primitiveValueSuccessTest() {
90                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
91                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
92
93                 PropertyDefinition propertyDefinition = new PropertyDefinition();
94                 propertyDefinition.setType("integer");
95                 propertyDefinition.setValue("10");
96
97                 Either<Boolean, ResponseFormat> responseEither =
98                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
99                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
100
101                 Assert.assertTrue(responseEither.isLeft());
102         }
103
104         @Test
105         public void primitiveValueFailTest() {
106                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
107                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
108
109                 PropertyDefinition propertyDefinition = new PropertyDefinition();
110                 propertyDefinition.setType("integer");
111                 propertyDefinition.setValue("abcd");
112
113                 Either<Boolean, ResponseFormat> responseEither =
114                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
115                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
116
117                 Assert.assertTrue(responseEither.isRight());
118         }
119
120         @Test
121         public void complexWithValidValueSuccessTest() {
122                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
123                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
124
125                 PropertyDefinition propertyDefinition = new PropertyDefinition();
126                 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
127                 propertyDefinition.setValue("{\"prefixlen\":\"4\"}");
128
129                 Either<Boolean, ResponseFormat> responseEither =
130                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
131                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
132
133                 Assert.assertTrue(responseEither.isLeft());
134         }
135
136         @Test
137         public void complexWithValidValueFailTest() {
138                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
139                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
140
141                 PropertyDefinition propertyDefinition = new PropertyDefinition();
142                 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
143                 propertyDefinition.setValue("{\"prefixlen\":\"5\"}");
144
145                 Either<Boolean, ResponseFormat> responseEither =
146                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
147                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
148
149                 Assert.assertTrue(responseEither.isRight());
150         }
151
152         @Test
153         public void complexWithListWithPrimitiveValueSuccessTest() {
154                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
155                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
156
157                 PropertyDefinition propertyDefinition = new PropertyDefinition();
158                 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
159                 propertyDefinition.setValue("{\"allocation_pools\":[\"slaac\"]}");
160
161                 Either<Boolean, ResponseFormat> responseEither =
162                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
163                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
164
165                 Assert.assertTrue(responseEither.isLeft());
166         }
167
168         @Test
169         public void complexWithListWithPrimitiveValueFailTest() {
170                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
171                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
172
173                 PropertyDefinition propertyDefinition = new PropertyDefinition();
174                 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
175                 propertyDefinition.setValue("{\"allocation_pools\":[\"value\"]}");
176
177                 Either<Boolean, ResponseFormat> responseEither =
178                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
179                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
180
181                 Assert.assertTrue(responseEither.isRight());
182         }
183
184         @Test
185         public void complexWithMapWithPrimitiveValueSuccessTest() {
186                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
187                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
188
189                 PropertyDefinition propertyDefinition = new PropertyDefinition();
190                 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
191                 propertyDefinition.setValue("{\"value_specs\":{\"key\":\"slaac\"}}");
192
193                 Either<Boolean, ResponseFormat> responseEither =
194                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
195                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
196
197                 Assert.assertTrue(responseEither.isLeft());
198         }
199
200         @Test
201         public void complexWithMapWithPrimitiveValueFailTest() {
202                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
203                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
204
205                 PropertyDefinition propertyDefinition = new PropertyDefinition();
206                 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
207                 propertyDefinition.setValue("{\"value_specs\":{\"key\":\"value\"}}");
208
209                 Either<Boolean, ResponseFormat> responseEither =
210                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
211                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
212
213                 Assert.assertTrue(responseEither.isRight());
214         }
215
216         @Test
217         public void inputValidValueSuccessTest() {
218                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
219                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
220
221                 InputDefinition inputDefinition = new InputDefinition();
222                 inputDefinition.setInputPath("propetyName#ipv6_ra_mode");
223                 inputDefinition.setDefaultValue("slaac");
224                 inputDefinition.setType("string");
225                 ComponentInstanceProperty propertyDefinition = new ComponentInstanceProperty();
226                 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
227                 inputDefinition.setProperties(Collections.singletonList(propertyDefinition));
228
229                 Either<Boolean, ResponseFormat> responseEither =
230                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
231                                                 Collections.singletonList(inputDefinition), applicationDataTypeCache);
232
233                 Assert.assertTrue(responseEither.isLeft());
234         }
235
236         @Test
237         public void inputValidValueFailTest() {
238                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
239                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
240
241                 InputDefinition inputDefinition = new InputDefinition();
242                 inputDefinition.setInputPath("propetyName#ipv6_ra_mode");
243                 inputDefinition.setDefaultValue("incorrectValue");
244                 ComponentInstanceProperty propertyDefinition = new ComponentInstanceProperty();
245                 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
246                 inputDefinition.setProperties(Collections.singletonList(propertyDefinition));
247
248                 Either<Boolean, ResponseFormat> responseEither =
249                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
250                                                 Collections.singletonList(inputDefinition), applicationDataTypeCache);
251
252                 Assert.assertTrue(responseEither.isRight());
253         }
254
255         @Test
256         public void serviceConsumptionValidValueSuccessTest() {
257                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
258                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
259
260                 PropertyDefinition propertyDefinition = new PropertyDefinition();
261                 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
262                 propertyDefinition.setValue("{\"ipv6_ra_mode\":\"slaac\"}");
263                 propertyDefinition.setName("ipv6_ra_mode");
264
265                 Either<Boolean, ResponseFormat> responseEither =
266                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
267                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
268
269                 Assert.assertTrue(responseEither.isLeft());
270         }
271         @Test
272         public void serviceConsumptionValidValueFailTest() {
273                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
274                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
275
276                 PropertyDefinition propertyDefinition = new PropertyDefinition();
277                 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
278                 propertyDefinition.setValue("{\"ipv6_ra_mode\":\"incorrectValue\"}");
279                 propertyDefinition.setName("ipv6_ra_mode");
280
281                 Either<Boolean, ResponseFormat> responseEither =
282                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
283                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
284
285                 Assert.assertTrue(responseEither.isRight());
286         }
287
288         @Test
289         public void bandwidthTypeValueSuccessTest(){
290                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
291                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
292
293                 PropertyDefinition propertyDefinition = new PropertyDefinition();
294                 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
295                 propertyDefinition.setValue("{\"bandwidth_type\":\"guaranteed\"}");
296                 propertyDefinition.setName("bandwidth_type");
297
298                 Either<Boolean, ResponseFormat> responseEither =
299                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
300                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
301
302                 Assert.assertTrue(responseEither.isLeft());
303         }
304
305         @Test
306         public void bandwidthTypeValueFailTest(){
307                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
308                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
309
310                 PropertyDefinition propertyDefinition = new PropertyDefinition();
311                 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
312                 propertyDefinition.setValue("{\"bandwidth_type\":\"incorrectValue\"}");
313                 propertyDefinition.setName("bandwidth_type");
314
315                 Either<Boolean, ResponseFormat> responseEither =
316                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
317                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
318
319                 Assert.assertTrue(responseEither.isRight());
320         }
321
322         @Test
323         public void bandwidthDownstreamValueSuccessTest(){
324                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
325                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
326
327                 PropertyDefinition propertyDefinition = new PropertyDefinition();
328                 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
329                 propertyDefinition.setValue("{\"downstream\":\"128\"}");
330                 propertyDefinition.setName("downstream");
331
332                 Either<Boolean, ResponseFormat> responseEither =
333                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
334                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
335
336                 Assert.assertTrue(responseEither.isLeft());
337         }
338
339         @Test
340         public void bandwidthDownstreamValueFailTest(){
341                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
342                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
343
344                 PropertyDefinition propertyDefinition = new PropertyDefinition();
345                 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
346                 propertyDefinition.setValue("{\"downstream\":\"incorrectValue\"}");
347                 propertyDefinition.setName("downstream");
348
349                 Either<Boolean, ResponseFormat> responseEither =
350                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
351                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
352
353                 Assert.assertTrue(responseEither.isRight());
354         }
355
356         @Test
357         public void bandwidthUpstreamValueSuccessTest(){
358                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
359                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
360
361                 PropertyDefinition propertyDefinition = new PropertyDefinition();
362                 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
363                 propertyDefinition.setValue("{\"upstream\":\"128\"}");
364                 propertyDefinition.setName("upstream");
365
366                 Either<Boolean, ResponseFormat> responseEither =
367                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
368                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
369
370                 Assert.assertTrue(responseEither.isLeft());
371         }
372
373         @Test
374         public void bandwidthUpstreamValueFailTest(){
375                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
376                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
377
378                 PropertyDefinition propertyDefinition = new PropertyDefinition();
379                 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
380                 propertyDefinition.setValue("{\"upstream\":\"incorrectValue\"}");
381                 propertyDefinition.setName("upstream");
382
383                 Either<Boolean, ResponseFormat> responseEither =
384                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
385                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
386
387                 Assert.assertTrue(responseEither.isRight());
388         }
389
390         @Test
391         public void bandwidthUnitsValueSuccessTest(){
392                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
393                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
394
395                 PropertyDefinition propertyDefinition = new PropertyDefinition();
396                 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
397                 propertyDefinition.setValue("{\"units\":\"M\"}");
398                 propertyDefinition.setName("units");
399
400                 Either<Boolean, ResponseFormat> responseEither =
401                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
402                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
403
404                 Assert.assertTrue(responseEither.isLeft());
405         }
406
407         @Test
408         public void bandwidthUnitsValueFailTest(){
409                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
410                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
411
412                 PropertyDefinition propertyDefinition = new PropertyDefinition();
413                 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
414                 propertyDefinition.setValue("{\"units\":\"incorrectValue\"}");
415                 propertyDefinition.setName("units");
416
417                 Either<Boolean, ResponseFormat> responseEither =
418                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
419                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
420
421                 Assert.assertTrue(responseEither.isRight());
422         }
423
424         private void createDataTypeMap() {
425                 Type constraintType = new TypeToken<PropertyConstraint>() {}.getType();
426                 Type typeOfHashMap = new TypeToken<Map<String, DataTypeDefinition>>() { }.getType();
427                 Gson gson = new GsonBuilder().registerTypeAdapter(constraintType,
428                                 new PropertyOperation.PropertyConstraintDeserialiser()).create();
429
430                 dataTypeDefinitionMap = gson.fromJson(readFile(), typeOfHashMap);
431
432                 DataTypeDefinition dataTypeDefinition = dataTypeDefinitionMap.get("org.openecomp.datatypes.heat.network"
433                                 + ".neutron.Subnet");
434
435                 PropertyDefinition mapProperty = null;
436                 PropertyDefinition listProperty = null;
437                 List<PropertyConstraint> constraints = null;
438                 for (PropertyDefinition propertyDefinition : dataTypeDefinition.getProperties()) {
439                         if ("value_specs".equals(propertyDefinition.getName())) {
440                                 mapProperty = propertyDefinition;
441                         } else if ("allocation_pools".equals(propertyDefinition.getName())) {
442                                 listProperty = propertyDefinition;
443                         } else if ("ipv6_ra_mode".equals(propertyDefinition.getName())) {
444                                 constraints = propertyDefinition.getConstraints();
445                         }
446                 }
447
448                 PropertyDefinition definition = new PropertyDefinition(mapProperty.getSchema().getProperty());
449                 definition.setConstraints(constraints);
450                 mapProperty.getSchema().setProperty(definition);
451
452                 definition = new PropertyDefinition(listProperty.getSchema().getProperty());
453                 definition.setConstraints(constraints);
454                 listProperty.getSchema().setProperty(definition);
455         }
456
457         private static String readFile() {
458                 StringBuilder stringBuilder = new StringBuilder();
459                 File file = new File(Objects.requireNonNull(
460                                 PropertyValueConstraintValidationUtilTest.class.getClassLoader().getResource("types/datatypes"
461                                                 + "/constraintTest.json")).getFile());
462                 Path logFile = Paths.get(file.getAbsolutePath());
463                 try (BufferedReader reader = Files.newBufferedReader(logFile, StandardCharsets.UTF_8)) {
464                         reader.lines().forEach(stringBuilder::append);
465                 } catch (Exception e) {
466                         Assert.fail(e.getMessage());
467                         e.printStackTrace();
468                 }
469                 return stringBuilder.toString();
470         }
471
472 }
473