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