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 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;
47 import java.io.BufferedReader;
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;
57 import java.util.Objects;
59 import static org.mockito.ArgumentMatchers.any;
60 import static org.mockito.Mockito.when;
62 public class PropertyValueConstraintValidationUtilTest {
65 ApplicationDataTypeCache applicationDataTypeCache;
68 ToscaOperationFacade toscaOperationFacade;
72 PropertyValueConstraintValidationUtil propertyValueConstraintValidationUtil;
74 private Map<String, DataTypeDefinition> dataTypeDefinitionMap;
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);
89 public void primitiveValueSuccessTest() {
90 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
91 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
93 PropertyDefinition propertyDefinition = new PropertyDefinition();
94 propertyDefinition.setType("integer");
95 propertyDefinition.setValue("10");
97 Either<Boolean, ResponseFormat> responseEither =
98 propertyValueConstraintValidationUtil.validatePropertyConstraints(
99 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
101 Assert.assertTrue(responseEither.isLeft());
105 public void primitiveValueFailTest() {
106 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
107 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
109 PropertyDefinition propertyDefinition = new PropertyDefinition();
110 propertyDefinition.setType("integer");
111 propertyDefinition.setValue("abcd");
113 Either<Boolean, ResponseFormat> responseEither =
114 propertyValueConstraintValidationUtil.validatePropertyConstraints(
115 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
117 Assert.assertTrue(responseEither.isRight());
121 public void complexWithValidValueSuccessTest() {
122 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
123 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
125 PropertyDefinition propertyDefinition = new PropertyDefinition();
126 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
127 propertyDefinition.setValue("{\"prefixlen\":\"4\"}");
129 Either<Boolean, ResponseFormat> responseEither =
130 propertyValueConstraintValidationUtil.validatePropertyConstraints(
131 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
133 Assert.assertTrue(responseEither.isLeft());
137 public void complexWithValidValueFailTest() {
138 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
139 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
141 PropertyDefinition propertyDefinition = new PropertyDefinition();
142 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
143 propertyDefinition.setValue("{\"prefixlen\":\"5\"}");
145 Either<Boolean, ResponseFormat> responseEither =
146 propertyValueConstraintValidationUtil.validatePropertyConstraints(
147 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
149 Assert.assertTrue(responseEither.isRight());
153 public void complexWithListWithPrimitiveValueSuccessTest() {
154 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
155 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
157 PropertyDefinition propertyDefinition = new PropertyDefinition();
158 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
159 propertyDefinition.setValue("{\"allocation_pools\":[\"slaac\"]}");
161 Either<Boolean, ResponseFormat> responseEither =
162 propertyValueConstraintValidationUtil.validatePropertyConstraints(
163 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
165 Assert.assertTrue(responseEither.isLeft());
169 public void complexWithListWithPrimitiveValueFailTest() {
170 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
171 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
173 PropertyDefinition propertyDefinition = new PropertyDefinition();
174 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
175 propertyDefinition.setValue("{\"allocation_pools\":[\"value\"]}");
177 Either<Boolean, ResponseFormat> responseEither =
178 propertyValueConstraintValidationUtil.validatePropertyConstraints(
179 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
181 Assert.assertTrue(responseEither.isRight());
185 public void complexWithMapWithPrimitiveValueSuccessTest() {
186 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
187 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
189 PropertyDefinition propertyDefinition = new PropertyDefinition();
190 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
191 propertyDefinition.setValue("{\"value_specs\":{\"key\":\"slaac\"}}");
193 Either<Boolean, ResponseFormat> responseEither =
194 propertyValueConstraintValidationUtil.validatePropertyConstraints(
195 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
197 Assert.assertTrue(responseEither.isLeft());
201 public void complexWithMapWithPrimitiveValueFailTest() {
202 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
203 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
205 PropertyDefinition propertyDefinition = new PropertyDefinition();
206 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
207 propertyDefinition.setValue("{\"value_specs\":{\"key\":\"value\"}}");
209 Either<Boolean, ResponseFormat> responseEither =
210 propertyValueConstraintValidationUtil.validatePropertyConstraints(
211 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
213 Assert.assertTrue(responseEither.isRight());
217 public void inputValidValueSuccessTest() {
218 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
219 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
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));
229 Either<Boolean, ResponseFormat> responseEither =
230 propertyValueConstraintValidationUtil.validatePropertyConstraints(
231 Collections.singletonList(inputDefinition), applicationDataTypeCache);
233 Assert.assertTrue(responseEither.isLeft());
237 public void inputValidValueFailTest() {
238 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
239 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
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));
248 Either<Boolean, ResponseFormat> responseEither =
249 propertyValueConstraintValidationUtil.validatePropertyConstraints(
250 Collections.singletonList(inputDefinition), applicationDataTypeCache);
252 Assert.assertTrue(responseEither.isRight());
256 public void serviceConsumptionValidValueSuccessTest() {
257 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
258 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
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");
265 Either<Boolean, ResponseFormat> responseEither =
266 propertyValueConstraintValidationUtil.validatePropertyConstraints(
267 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
269 Assert.assertTrue(responseEither.isLeft());
272 public void serviceConsumptionValidValueFailTest() {
273 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
274 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
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");
281 Either<Boolean, ResponseFormat> responseEither =
282 propertyValueConstraintValidationUtil.validatePropertyConstraints(
283 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
285 Assert.assertTrue(responseEither.isRight());
289 public void bandwidthTypeValueSuccessTest(){
290 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
291 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
293 PropertyDefinition propertyDefinition = new PropertyDefinition();
294 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
295 propertyDefinition.setValue("{\"bandwidth_type\":\"guaranteed\"}");
296 propertyDefinition.setName("bandwidth_type");
298 Either<Boolean, ResponseFormat> responseEither =
299 propertyValueConstraintValidationUtil.validatePropertyConstraints(
300 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
302 Assert.assertTrue(responseEither.isLeft());
306 public void bandwidthTypeValueFailTest(){
307 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
308 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
310 PropertyDefinition propertyDefinition = new PropertyDefinition();
311 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
312 propertyDefinition.setValue("{\"bandwidth_type\":\"incorrectValue\"}");
313 propertyDefinition.setName("bandwidth_type");
315 Either<Boolean, ResponseFormat> responseEither =
316 propertyValueConstraintValidationUtil.validatePropertyConstraints(
317 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
319 Assert.assertTrue(responseEither.isRight());
323 public void bandwidthDownstreamValueSuccessTest(){
324 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
325 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
327 PropertyDefinition propertyDefinition = new PropertyDefinition();
328 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
329 propertyDefinition.setValue("{\"downstream\":\"128\"}");
330 propertyDefinition.setName("downstream");
332 Either<Boolean, ResponseFormat> responseEither =
333 propertyValueConstraintValidationUtil.validatePropertyConstraints(
334 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
336 Assert.assertTrue(responseEither.isLeft());
340 public void bandwidthDownstreamValueFailTest(){
341 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
342 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
344 PropertyDefinition propertyDefinition = new PropertyDefinition();
345 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
346 propertyDefinition.setValue("{\"downstream\":\"incorrectValue\"}");
347 propertyDefinition.setName("downstream");
349 Either<Boolean, ResponseFormat> responseEither =
350 propertyValueConstraintValidationUtil.validatePropertyConstraints(
351 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
353 Assert.assertTrue(responseEither.isRight());
357 public void bandwidthUpstreamValueSuccessTest(){
358 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
359 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
361 PropertyDefinition propertyDefinition = new PropertyDefinition();
362 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
363 propertyDefinition.setValue("{\"upstream\":\"128\"}");
364 propertyDefinition.setName("upstream");
366 Either<Boolean, ResponseFormat> responseEither =
367 propertyValueConstraintValidationUtil.validatePropertyConstraints(
368 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
370 Assert.assertTrue(responseEither.isLeft());
374 public void bandwidthUpstreamValueFailTest(){
375 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
376 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
378 PropertyDefinition propertyDefinition = new PropertyDefinition();
379 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
380 propertyDefinition.setValue("{\"upstream\":\"incorrectValue\"}");
381 propertyDefinition.setName("upstream");
383 Either<Boolean, ResponseFormat> responseEither =
384 propertyValueConstraintValidationUtil.validatePropertyConstraints(
385 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
387 Assert.assertTrue(responseEither.isRight());
391 public void bandwidthUnitsValueSuccessTest(){
392 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
393 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
395 PropertyDefinition propertyDefinition = new PropertyDefinition();
396 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
397 propertyDefinition.setValue("{\"units\":\"M\"}");
398 propertyDefinition.setName("units");
400 Either<Boolean, ResponseFormat> responseEither =
401 propertyValueConstraintValidationUtil.validatePropertyConstraints(
402 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
404 Assert.assertTrue(responseEither.isLeft());
408 public void bandwidthUnitsValueFailTest(){
409 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
410 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
412 PropertyDefinition propertyDefinition = new PropertyDefinition();
413 propertyDefinition.setType("onap.datatypes.partner.bandwidth");
414 propertyDefinition.setValue("{\"units\":\"incorrectValue\"}");
415 propertyDefinition.setName("units");
417 Either<Boolean, ResponseFormat> responseEither =
418 propertyValueConstraintValidationUtil.validatePropertyConstraints(
419 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
421 Assert.assertTrue(responseEither.isRight());
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();
430 dataTypeDefinitionMap = gson.fromJson(readFile(), typeOfHashMap);
432 DataTypeDefinition dataTypeDefinition = dataTypeDefinitionMap.get("org.openecomp.datatypes.heat.network"
433 + ".neutron.Subnet");
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();
448 PropertyDefinition definition = new PropertyDefinition(mapProperty.getSchema().getProperty());
449 definition.setConstraints(constraints);
450 mapProperty.getSchema().setProperty(definition);
452 definition = new PropertyDefinition(listProperty.getSchema().getProperty());
453 definition.setConstraints(constraints);
454 listProperty.getSchema().setProperty(definition);
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());
469 return stringBuilder.toString();