2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.datamodel.utils;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.when;
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;
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;
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;
61 public class PropertyValueConstraintValidationUtilTest {
64 ApplicationDataTypeCache applicationDataTypeCache;
67 ToscaOperationFacade toscaOperationFacade;
71 PropertyValueConstraintValidationUtil propertyValueConstraintValidationUtil;
73 private Map<String, DataTypeDefinition> dataTypeDefinitionMap;
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);
88 public void primitiveValueSuccessTest() {
89 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
90 Mockito.when(applicationDataTypeCache.getAll(null)).thenReturn(either);
92 PropertyDefinition propertyDefinition = new PropertyDefinition();
93 propertyDefinition.setType("integer");
94 propertyDefinition.setValue("10");
96 Either<Boolean, ResponseFormat> responseEither =
97 propertyValueConstraintValidationUtil.validatePropertyConstraints(
98 Collections.singletonList(propertyDefinition), applicationDataTypeCache, null);
100 Assert.assertTrue(responseEither.isLeft());
104 public void primitiveValueFailTest() {
105 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
106 Mockito.when(applicationDataTypeCache.getAll(null)).thenReturn(either);
108 PropertyDefinition propertyDefinition = new PropertyDefinition();
109 propertyDefinition.setType("integer");
110 propertyDefinition.setValue("abcd");
112 Either<Boolean, ResponseFormat> responseEither = propertyValueConstraintValidationUtil.validatePropertyConstraints(
113 Collections.singletonList(propertyDefinition), applicationDataTypeCache, null);
115 Assert.assertTrue(responseEither.isRight());
119 public void complexWithValidValueSuccessTest() {
120 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
121 Mockito.when(applicationDataTypeCache.getAll(null)).thenReturn(either);
123 PropertyDefinition propertyDefinition = new PropertyDefinition();
124 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
125 propertyDefinition.setValue("{\"prefixlen\":\"4\"}");
127 Either<Boolean, ResponseFormat> responseEither =
128 propertyValueConstraintValidationUtil.validatePropertyConstraints(
129 Collections.singletonList(propertyDefinition), applicationDataTypeCache, null);
131 Assert.assertTrue(responseEither.isLeft());
135 public void complexWithValidValueFailTest() {
136 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
137 Mockito.when(applicationDataTypeCache.getAll(null)).thenReturn(either);
139 PropertyDefinition propertyDefinition = new PropertyDefinition();
140 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
141 propertyDefinition.setValue("{\"prefixlen\":\"5\"}");
143 Either<Boolean, ResponseFormat> responseEither = propertyValueConstraintValidationUtil
144 .validatePropertyConstraints(Collections.singletonList(propertyDefinition), applicationDataTypeCache, null);
146 Assert.assertTrue(responseEither.isRight());
150 public void complexWithListWithPrimitiveValueSuccessTest() {
151 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
152 Mockito.when(applicationDataTypeCache.getAll(null)).thenReturn(either);
154 PropertyDefinition propertyDefinition = new PropertyDefinition();
155 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
156 propertyDefinition.setValue("{\"allocation_pools\":[\"slaac\"]}");
158 Either<Boolean, ResponseFormat> responseEither =
159 propertyValueConstraintValidationUtil.validatePropertyConstraints(
160 Collections.singletonList(propertyDefinition), applicationDataTypeCache, null);
162 Assert.assertTrue(responseEither.isLeft());
166 public void complexWithListWithPrimitiveValueFailTest() {
167 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
168 Mockito.when(applicationDataTypeCache.getAll(null)).thenReturn(either);
170 PropertyDefinition propertyDefinition = new PropertyDefinition();
171 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
172 propertyDefinition.setValue("{\"allocation_pools\":[\"value\"]}");
174 Either<Boolean, ResponseFormat> responseEither =
175 propertyValueConstraintValidationUtil.validatePropertyConstraints(
176 Collections.singletonList(propertyDefinition), applicationDataTypeCache, null);
178 Assert.assertTrue(responseEither.isRight());
182 public void complexWithMapWithPrimitiveValueSuccessTest() {
183 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
184 Mockito.when(applicationDataTypeCache.getAll(null)).thenReturn(either);
186 PropertyDefinition propertyDefinition = new PropertyDefinition();
187 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
188 propertyDefinition.setValue("{\"value_specs\":{\"key\":\"slaac\"}}");
190 Either<Boolean, ResponseFormat> responseEither =
191 propertyValueConstraintValidationUtil.validatePropertyConstraints(
192 Collections.singletonList(propertyDefinition), applicationDataTypeCache, null);
194 Assert.assertTrue(responseEither.isLeft());
198 public void complexWithMapWithPrimitiveValueFailTest() {
199 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
200 Mockito.when(applicationDataTypeCache.getAll(null)).thenReturn(either);
202 PropertyDefinition propertyDefinition = new PropertyDefinition();
203 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
204 propertyDefinition.setValue("{\"value_specs\":{\"key\":\"value\"}}");
206 Either<Boolean, ResponseFormat> responseEither =
207 propertyValueConstraintValidationUtil.validatePropertyConstraints(
208 Collections.singletonList(propertyDefinition), applicationDataTypeCache, null);
210 Assert.assertTrue(responseEither.isRight());
214 public void inputValidValueSuccessTest() {
215 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
216 Mockito.when(applicationDataTypeCache.getAll(null)).thenReturn(either);
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));
226 Either<Boolean, ResponseFormat> responseEither =
227 propertyValueConstraintValidationUtil.validatePropertyConstraints(
228 Collections.singletonList(inputDefinition), applicationDataTypeCache, null);
230 Assert.assertTrue(responseEither.isLeft());
234 public void inputValidValueFailTest() {
235 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
236 Mockito.when(applicationDataTypeCache.getAll(null)).thenReturn(either);
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));
245 Either<Boolean, ResponseFormat> responseEither =
246 propertyValueConstraintValidationUtil.validatePropertyConstraints(
247 Collections.singletonList(inputDefinition), applicationDataTypeCache, null);
249 Assert.assertTrue(responseEither.isRight());
253 public void serviceConsumptionValidValueSuccessTest() {
254 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
255 Mockito.when(applicationDataTypeCache.getAll(null)).thenReturn(either);
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");
262 Either<Boolean, ResponseFormat> responseEither =
263 propertyValueConstraintValidationUtil.validatePropertyConstraints(
264 Collections.singletonList(propertyDefinition), applicationDataTypeCache, null);
266 Assert.assertTrue(responseEither.isLeft());
269 public void serviceConsumptionValidValueFailTest() {
270 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
271 Mockito.when(applicationDataTypeCache.getAll(null)).thenReturn(either);
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");
278 Either<Boolean, ResponseFormat> responseEither =
279 propertyValueConstraintValidationUtil.validatePropertyConstraints(
280 Collections.singletonList(propertyDefinition), applicationDataTypeCache, null);
282 Assert.assertTrue(responseEither.isRight());
286 public void bandwidthTypeValueSuccessTest(){
287 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
288 Mockito.when(applicationDataTypeCache.getAll(null)).thenReturn(either);
290 PropertyDefinition propertyDefinition = new PropertyDefinition();
291 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
292 propertyDefinition.setValue("{\"bandwidth_type\":\"guaranteed\"}");
293 propertyDefinition.setName("bandwidth_type");
295 Either<Boolean, ResponseFormat> responseEither = propertyValueConstraintValidationUtil.validatePropertyConstraints(
296 Collections.singletonList(propertyDefinition), applicationDataTypeCache, null);
297 Assert.assertTrue(responseEither.isLeft());
301 public void bandwidthTypeValueFailTest(){
302 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
303 Mockito.when(applicationDataTypeCache.getAll(null)).thenReturn(either);
305 PropertyDefinition propertyDefinition = new PropertyDefinition();
306 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
307 propertyDefinition.setValue("{\"bandwidth_type\":\"incorrectValue\"}");
308 propertyDefinition.setName("bandwidth_type");
310 Either<Boolean, ResponseFormat> responseEither = propertyValueConstraintValidationUtil.validatePropertyConstraints(
311 Collections.singletonList(propertyDefinition), applicationDataTypeCache, null);
313 Assert.assertTrue(responseEither.isRight());
317 public void bandwidthDownstreamValueSuccessTest(){
318 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
319 Mockito.when(applicationDataTypeCache.getAll(null)).thenReturn(either);
321 PropertyDefinition propertyDefinition = new PropertyDefinition();
322 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
323 propertyDefinition.setValue("{\"downstream\":\"128\"}");
324 propertyDefinition.setName("downstream");
326 Either<Boolean, ResponseFormat> responseEither =
327 propertyValueConstraintValidationUtil.validatePropertyConstraints(
328 Collections.singletonList(propertyDefinition), applicationDataTypeCache, null);
330 Assert.assertTrue(responseEither.isLeft());
334 public void bandwidthDownstreamValueFailTest(){
335 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
336 Mockito.when(applicationDataTypeCache.getAll(null)).thenReturn(either);
338 PropertyDefinition propertyDefinition = new PropertyDefinition();
339 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
340 propertyDefinition.setValue("{\"downstream\":\"incorrectValue\"}");
341 propertyDefinition.setName("downstream");
343 Either<Boolean, ResponseFormat> responseEither =
344 propertyValueConstraintValidationUtil.validatePropertyConstraints(
345 Collections.singletonList(propertyDefinition), applicationDataTypeCache, null);
347 Assert.assertTrue(responseEither.isRight());
351 public void bandwidthUpstreamValueSuccessTest(){
352 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
353 Mockito.when(applicationDataTypeCache.getAll(null)).thenReturn(either);
355 PropertyDefinition propertyDefinition = new PropertyDefinition();
356 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
357 propertyDefinition.setValue("{\"upstream\":\"128\"}");
358 propertyDefinition.setName("upstream");
360 Either<Boolean, ResponseFormat> responseEither =
361 propertyValueConstraintValidationUtil.validatePropertyConstraints(
362 Collections.singletonList(propertyDefinition), applicationDataTypeCache, null);
364 Assert.assertTrue(responseEither.isLeft());
368 public void bandwidthUpstreamValueFailTest(){
369 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
370 Mockito.when(applicationDataTypeCache.getAll(null)).thenReturn(either);
372 PropertyDefinition propertyDefinition = new PropertyDefinition();
373 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
374 propertyDefinition.setValue("{\"upstream\":\"incorrectValue\"}");
375 propertyDefinition.setName("upstream");
377 Either<Boolean, ResponseFormat> responseEither =
378 propertyValueConstraintValidationUtil.validatePropertyConstraints(
379 Collections.singletonList(propertyDefinition), applicationDataTypeCache, null);
381 Assert.assertTrue(responseEither.isRight());
385 public void bandwidthUnitsValueSuccessTest(){
386 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
387 Mockito.when(applicationDataTypeCache.getAll(null)).thenReturn(either);
389 PropertyDefinition propertyDefinition = new PropertyDefinition();
390 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
391 propertyDefinition.setValue("{\"units\":\"M\"}");
392 propertyDefinition.setName("units");
394 Either<Boolean, ResponseFormat> responseEither =
395 propertyValueConstraintValidationUtil.validatePropertyConstraints(
396 Collections.singletonList(propertyDefinition), applicationDataTypeCache, null);
398 Assert.assertTrue(responseEither.isLeft());
402 public void bandwidthUnitsValueFailTest(){
403 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
404 Mockito.when(applicationDataTypeCache.getAll(null)).thenReturn(either);
406 PropertyDefinition propertyDefinition = new PropertyDefinition();
407 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
408 propertyDefinition.setValue("{\"units\":\"incorrectValue\"}");
409 propertyDefinition.setName("units");
411 Either<Boolean, ResponseFormat> responseEither =
412 propertyValueConstraintValidationUtil.validatePropertyConstraints(
413 Collections.singletonList(propertyDefinition), applicationDataTypeCache, null);
415 Assert.assertTrue(responseEither.isRight());
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();
424 dataTypeDefinitionMap = gson.fromJson(readFile(), typeOfHashMap);
426 DataTypeDefinition dataTypeDefinition = dataTypeDefinitionMap.get("org.openecomp.datatypes.heat.network"
427 + ".neutron.Subnet");
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();
442 PropertyDefinition definition = new PropertyDefinition(mapProperty.getSchema().getProperty());
443 definition.setConstraints(constraints);
444 mapProperty.getSchema().setProperty(definition);
446 definition = new PropertyDefinition(listProperty.getSchema().getProperty());
447 definition.setConstraints(constraints);
448 listProperty.getSchema().setProperty(definition);
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());
463 return stringBuilder.toString();