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;
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;
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;
63 public class PropertyValueConstraintValidationUtilTest {
66 ApplicationDataTypeCache applicationDataTypeCache;
69 ToscaOperationFacade toscaOperationFacade;
73 PropertyValueConstraintValidationUtil propertyValueConstraintValidationUtil;
75 private Map<String, DataTypeDefinition> dataTypeDefinitionMap;
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);
90 public void primitiveValueSuccessTest() {
91 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
92 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
94 PropertyDefinition propertyDefinition = new PropertyDefinition();
95 propertyDefinition.setType("integer");
96 propertyDefinition.setValue("10");
98 Either<Boolean, ResponseFormat> responseEither =
99 propertyValueConstraintValidationUtil.validatePropertyConstraints(
100 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
102 Assert.assertTrue(responseEither.isLeft());
106 public void primitiveValueFailTest() {
107 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
108 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
110 PropertyDefinition propertyDefinition = new PropertyDefinition();
111 propertyDefinition.setType("integer");
112 propertyDefinition.setValue("abcd");
114 Either<Boolean, ResponseFormat> responseEither =
115 propertyValueConstraintValidationUtil.validatePropertyConstraints(
116 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
118 Assert.assertTrue(responseEither.isRight());
122 public void complexWithValidValueSuccessTest() {
123 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
124 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
126 PropertyDefinition propertyDefinition = new PropertyDefinition();
127 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
128 propertyDefinition.setValue("{\"prefixlen\":\"4\"}");
130 Either<Boolean, ResponseFormat> responseEither =
131 propertyValueConstraintValidationUtil.validatePropertyConstraints(
132 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
134 Assert.assertTrue(responseEither.isLeft());
138 public void complexWithValidValueFailTest() {
139 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
140 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
142 PropertyDefinition propertyDefinition = new PropertyDefinition();
143 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
144 propertyDefinition.setValue("{\"prefixlen\":\"5\"}");
146 Either<Boolean, ResponseFormat> responseEither =
147 propertyValueConstraintValidationUtil.validatePropertyConstraints(
148 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
150 Assert.assertTrue(responseEither.isRight());
154 public void complexWithListWithPrimitiveValueSuccessTest() {
155 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
156 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
158 PropertyDefinition propertyDefinition = new PropertyDefinition();
159 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
160 propertyDefinition.setValue("{\"allocation_pools\":[\"slaac\"]}");
162 Either<Boolean, ResponseFormat> responseEither =
163 propertyValueConstraintValidationUtil.validatePropertyConstraints(
164 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
166 Assert.assertTrue(responseEither.isLeft());
170 public void complexWithListWithPrimitiveValueFailTest() {
171 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
172 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
174 PropertyDefinition propertyDefinition = new PropertyDefinition();
175 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
176 propertyDefinition.setValue("{\"allocation_pools\":[\"value\"]}");
178 Either<Boolean, ResponseFormat> responseEither =
179 propertyValueConstraintValidationUtil.validatePropertyConstraints(
180 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
182 Assert.assertTrue(responseEither.isRight());
186 public void complexWithMapWithPrimitiveValueSuccessTest() {
187 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
188 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
190 PropertyDefinition propertyDefinition = new PropertyDefinition();
191 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
192 propertyDefinition.setValue("{\"value_specs\":{\"key\":\"slaac\"}}");
194 Either<Boolean, ResponseFormat> responseEither =
195 propertyValueConstraintValidationUtil.validatePropertyConstraints(
196 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
198 Assert.assertTrue(responseEither.isLeft());
202 public void complexWithMapWithPrimitiveValueFailTest() {
203 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
204 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
206 PropertyDefinition propertyDefinition = new PropertyDefinition();
207 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
208 propertyDefinition.setValue("{\"value_specs\":{\"key\":\"value\"}}");
210 Either<Boolean, ResponseFormat> responseEither =
211 propertyValueConstraintValidationUtil.validatePropertyConstraints(
212 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
214 Assert.assertTrue(responseEither.isRight());
218 public void inputValidValueSuccessTest() {
219 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
220 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
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));
230 Either<Boolean, ResponseFormat> responseEither =
231 propertyValueConstraintValidationUtil.validatePropertyConstraints(
232 Collections.singletonList(inputDefinition), applicationDataTypeCache);
234 Assert.assertTrue(responseEither.isLeft());
238 public void inputValidValueFailTest() {
239 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
240 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
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));
249 Either<Boolean, ResponseFormat> responseEither =
250 propertyValueConstraintValidationUtil.validatePropertyConstraints(
251 Collections.singletonList(inputDefinition), applicationDataTypeCache);
253 Assert.assertTrue(responseEither.isRight());
257 public void serviceConsumptionValidValueSuccessTest() {
258 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
259 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
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");
266 Either<Boolean, ResponseFormat> responseEither =
267 propertyValueConstraintValidationUtil.validatePropertyConstraints(
268 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
270 Assert.assertTrue(responseEither.isLeft());
273 public void serviceConsumptionValidValueFailTest() {
274 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
275 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
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");
282 Either<Boolean, ResponseFormat> responseEither =
283 propertyValueConstraintValidationUtil.validatePropertyConstraints(
284 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
286 Assert.assertTrue(responseEither.isRight());
289 private void createDataTypeMap() {
290 Type constraintType = new TypeToken<PropertyConstraint>() {}.getType();
291 Type typeOfHashMap = new TypeToken<Map<String, DataTypeDefinition>>() { }.getType();
292 Gson gson = new GsonBuilder().registerTypeAdapter(constraintType,
293 new PropertyOperation.PropertyConstraintDeserialiser()).create();
295 dataTypeDefinitionMap = gson.fromJson(readFile(), typeOfHashMap);
297 DataTypeDefinition dataTypeDefinition = dataTypeDefinitionMap.get("org.openecomp.datatypes.heat.network"
298 + ".neutron.Subnet");
300 PropertyDefinition mapProperty = null;
301 PropertyDefinition listProperty = null;
302 List<PropertyConstraint> constraints = null;
303 for (PropertyDefinition propertyDefinition : dataTypeDefinition.getProperties()) {
304 if ("value_specs".equals(propertyDefinition.getName())) {
305 mapProperty = propertyDefinition;
306 } else if ("allocation_pools".equals(propertyDefinition.getName())) {
307 listProperty = propertyDefinition;
308 } else if ("ipv6_ra_mode".equals(propertyDefinition.getName())) {
309 constraints = propertyDefinition.getConstraints();
313 PropertyDefinition definition = new PropertyDefinition(mapProperty.getSchema().getProperty());
314 definition.setConstraints(constraints);
315 mapProperty.getSchema().setProperty(definition);
317 definition = new PropertyDefinition(listProperty.getSchema().getProperty());
318 definition.setConstraints(constraints);
319 listProperty.getSchema().setProperty(definition);
322 private static String readFile() {
323 StringBuilder stringBuilder = new StringBuilder();
324 File file = new File(Objects.requireNonNull(
325 PropertyValueConstraintValidationUtilTest.class.getClassLoader().getResource("types/datatypes"
326 + "/constraintTest.json")).getFile());
327 Path logFile = Paths.get(file.getAbsolutePath());
328 try (BufferedReader reader = Files.newBufferedReader(logFile, StandardCharsets.UTF_8)) {
329 reader.lines().forEach(stringBuilder::append);
330 } catch (Exception e) {
331 Assert.fail(e.getMessage());
334 return stringBuilder.toString();