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