Added oparent to sdc main
[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
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         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();
294
295                 dataTypeDefinitionMap = gson.fromJson(readFile(), typeOfHashMap);
296
297                 DataTypeDefinition dataTypeDefinition = dataTypeDefinitionMap.get("org.openecomp.datatypes.heat.network"
298                                 + ".neutron.Subnet");
299
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();
310                         }
311                 }
312
313                 PropertyDefinition definition = new PropertyDefinition(mapProperty.getSchema().getProperty());
314                 definition.setConstraints(constraints);
315                 mapProperty.getSchema().setProperty(definition);
316
317                 definition = new PropertyDefinition(listProperty.getSchema().getProperty());
318                 definition.setConstraints(constraints);
319                 listProperty.getSchema().setProperty(definition);
320         }
321
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());
332                         e.printStackTrace();
333                 }
334                 return stringBuilder.toString();
335         }
336
337 }