Upgrade SDC from Titan to Janus Graph
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / datamodel / utils / PropertyValueConstraintValidationUtilTest.java
1 package org.openecomp.sdc.be.datamodel.utils;
2
3 import static org.mockito.ArgumentMatchers.any;
4 import static org.mockito.Mockito.when;
5
6 import com.google.gson.Gson;
7 import com.google.gson.GsonBuilder;
8 import com.google.gson.reflect.TypeToken;
9
10 import java.io.BufferedReader;
11 import java.io.File;
12 import java.lang.reflect.Type;
13 import java.nio.charset.StandardCharsets;
14 import java.nio.file.Files;
15 import java.nio.file.Path;
16 import java.nio.file.Paths;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Objects;
21
22 import fj.data.Either;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.MockitoAnnotations;
30 import org.mockito.Spy;
31 import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
32 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
33 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
34 import org.openecomp.sdc.be.model.DataTypeDefinition;
35 import org.openecomp.sdc.be.model.InputDefinition;
36 import org.openecomp.sdc.be.model.PropertyConstraint;
37 import org.openecomp.sdc.be.model.PropertyDefinition;
38 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
39 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
40 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
41 import org.openecomp.sdc.exception.ResponseFormat;
42
43 public class PropertyValueConstraintValidationUtilTest {
44
45         @Mock
46         ApplicationDataTypeCache applicationDataTypeCache;
47
48         @Mock
49         ToscaOperationFacade toscaOperationFacade;
50
51         @Spy
52         @InjectMocks
53         PropertyValueConstraintValidationUtil propertyValueConstraintValidationUtil;
54
55         private Map<String, DataTypeDefinition> dataTypeDefinitionMap;
56
57         @Before
58         public void init() {
59                 MockitoAnnotations.initMocks(this);
60                 ResponseFormatManager responseFormatManagerMock = Mockito.mock(ResponseFormatManager.class);
61                 when(responseFormatManagerMock.getResponseFormat(any())).thenReturn(new ResponseFormat());
62                 when(responseFormatManagerMock.getResponseFormat(any(), any())).thenReturn(new ResponseFormat());
63                 when(responseFormatManagerMock.getResponseFormat(any(), any(), any())).thenReturn(new ResponseFormat());
64                 when(propertyValueConstraintValidationUtil.getResponseFormatManager()).thenReturn(responseFormatManagerMock);
65
66                 createDataTypeMap();
67         }
68
69         @Test
70         public void primitiveValueSuccessTest() {
71                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
72                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
73
74                 PropertyDefinition propertyDefinition = new PropertyDefinition();
75                 propertyDefinition.setType("integer");
76                 propertyDefinition.setValue("10");
77
78                 Either<Boolean, ResponseFormat> responseEither =
79                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
80                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
81
82                 Assert.assertTrue(responseEither.isLeft());
83         }
84
85         @Test
86         public void primitiveValueFailTest() {
87                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
88                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
89
90                 PropertyDefinition propertyDefinition = new PropertyDefinition();
91                 propertyDefinition.setType("integer");
92                 propertyDefinition.setValue("abcd");
93
94                 Either<Boolean, ResponseFormat> responseEither =
95                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
96                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
97
98                 Assert.assertTrue(responseEither.isRight());
99         }
100
101         @Test
102         public void complexWithValidValueSuccessTest() {
103                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
104                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
105
106                 PropertyDefinition propertyDefinition = new PropertyDefinition();
107                 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
108                 propertyDefinition.setValue("{\"prefixlen\":\"4\"}");
109
110                 Either<Boolean, ResponseFormat> responseEither =
111                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
112                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
113
114                 Assert.assertTrue(responseEither.isLeft());
115         }
116
117         @Test
118         public void complexWithValidValueFailTest() {
119                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
120                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
121
122                 PropertyDefinition propertyDefinition = new PropertyDefinition();
123                 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
124                 propertyDefinition.setValue("{\"prefixlen\":\"5\"}");
125
126                 Either<Boolean, ResponseFormat> responseEither =
127                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
128                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
129
130                 Assert.assertTrue(responseEither.isRight());
131         }
132
133         @Test
134         public void complexWithListWithPrimitiveValueSuccessTest() {
135                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
136                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
137
138                 PropertyDefinition propertyDefinition = new PropertyDefinition();
139                 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
140                 propertyDefinition.setValue("{\"allocation_pools\":[\"slaac\"]}");
141
142                 Either<Boolean, ResponseFormat> responseEither =
143                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
144                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
145
146                 Assert.assertTrue(responseEither.isLeft());
147         }
148
149         @Test
150         public void complexWithListWithPrimitiveValueFailTest() {
151                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
152                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
153
154                 PropertyDefinition propertyDefinition = new PropertyDefinition();
155                 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
156                 propertyDefinition.setValue("{\"allocation_pools\":[\"value\"]}");
157
158                 Either<Boolean, ResponseFormat> responseEither =
159                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
160                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
161
162                 Assert.assertTrue(responseEither.isRight());
163         }
164
165         @Test
166         public void complexWithMapWithPrimitiveValueSuccessTest() {
167                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
168                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
169
170                 PropertyDefinition propertyDefinition = new PropertyDefinition();
171                 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
172                 propertyDefinition.setValue("{\"value_specs\":{\"key\":\"slaac\"}}");
173
174                 Either<Boolean, ResponseFormat> responseEither =
175                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
176                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
177
178                 Assert.assertTrue(responseEither.isLeft());
179         }
180
181         @Test
182         public void complexWithMapWithPrimitiveValueFailTest() {
183                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
184                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
185
186                 PropertyDefinition propertyDefinition = new PropertyDefinition();
187                 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
188                 propertyDefinition.setValue("{\"value_specs\":{\"key\":\"value\"}}");
189
190                 Either<Boolean, ResponseFormat> responseEither =
191                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
192                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
193
194                 Assert.assertTrue(responseEither.isRight());
195         }
196
197         @Test
198         public void inputValidValueSuccessTest() {
199                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
200                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
201
202                 InputDefinition inputDefinition = new InputDefinition();
203                 inputDefinition.setInputPath("propetyName#ipv6_ra_mode");
204                 inputDefinition.setDefaultValue("slaac");
205                 inputDefinition.setType("string");
206                 ComponentInstanceProperty propertyDefinition = new ComponentInstanceProperty();
207                 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
208                 inputDefinition.setProperties(Collections.singletonList(propertyDefinition));
209
210                 Either<Boolean, ResponseFormat> responseEither =
211                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
212                                                 Collections.singletonList(inputDefinition), applicationDataTypeCache);
213
214                 Assert.assertTrue(responseEither.isLeft());
215         }
216
217         @Test
218         public void inputValidValueFailTest() {
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("incorrectValue");
225                 ComponentInstanceProperty propertyDefinition = new ComponentInstanceProperty();
226                 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
227                 inputDefinition.setProperties(Collections.singletonList(propertyDefinition));
228
229                 Either<Boolean, ResponseFormat> responseEither =
230                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
231                                                 Collections.singletonList(inputDefinition), applicationDataTypeCache);
232
233                 Assert.assertTrue(responseEither.isRight());
234         }
235
236         @Test
237         public void serviceConsumptionValidValueSuccessTest() {
238                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
239                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
240
241                 PropertyDefinition propertyDefinition = new PropertyDefinition();
242                 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
243                 propertyDefinition.setValue("{\"ipv6_ra_mode\":\"slaac\"}");
244                 propertyDefinition.setName("ipv6_ra_mode");
245
246                 Either<Boolean, ResponseFormat> responseEither =
247                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
248                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
249
250                 Assert.assertTrue(responseEither.isLeft());
251         }
252         @Test
253         public void serviceConsumptionValidValueFailTest() {
254                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> either = Either.left(dataTypeDefinitionMap);
255                 Mockito.when(applicationDataTypeCache.getAll()).thenReturn(either);
256
257                 PropertyDefinition propertyDefinition = new PropertyDefinition();
258                 propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
259                 propertyDefinition.setValue("{\"ipv6_ra_mode\":\"incorrectValue\"}");
260                 propertyDefinition.setName("ipv6_ra_mode");
261
262                 Either<Boolean, ResponseFormat> responseEither =
263                                 propertyValueConstraintValidationUtil.validatePropertyConstraints(
264                                                 Collections.singletonList(propertyDefinition), applicationDataTypeCache);
265
266                 Assert.assertTrue(responseEither.isRight());
267         }
268
269         private void createDataTypeMap() {
270                 Type constraintType = new TypeToken<PropertyConstraint>() {}.getType();
271                 Type typeOfHashMap = new TypeToken<Map<String, DataTypeDefinition>>() { }.getType();
272                 Gson gson = new GsonBuilder().registerTypeAdapter(constraintType,
273                                 new PropertyOperation.PropertyConstraintDeserialiser()).create();
274
275                 dataTypeDefinitionMap = gson.fromJson(readFile(), typeOfHashMap);
276
277                 DataTypeDefinition dataTypeDefinition = dataTypeDefinitionMap.get("org.openecomp.datatypes.heat.network"
278                                 + ".neutron.Subnet");
279
280                 PropertyDefinition mapProperty = null;
281                 PropertyDefinition listProperty = null;
282                 List<PropertyConstraint> constraints = null;
283                 for (PropertyDefinition propertyDefinition : dataTypeDefinition.getProperties()) {
284                         if ("value_specs".equals(propertyDefinition.getName())) {
285                                 mapProperty = propertyDefinition;
286                         } else if ("allocation_pools".equals(propertyDefinition.getName())) {
287                                 listProperty = propertyDefinition;
288                         } else if ("ipv6_ra_mode".equals(propertyDefinition.getName())) {
289                                 constraints = propertyDefinition.getConstraints();
290                         }
291                 }
292
293                 PropertyDefinition definition = new PropertyDefinition(mapProperty.getSchema().getProperty());
294                 definition.setConstraints(constraints);
295                 mapProperty.getSchema().setProperty(definition);
296
297                 definition = new PropertyDefinition(listProperty.getSchema().getProperty());
298                 definition.setConstraints(constraints);
299                 listProperty.getSchema().setProperty(definition);
300         }
301
302         private static String readFile() {
303                 StringBuilder stringBuilder = new StringBuilder();
304                 File file = new File(Objects.requireNonNull(
305                                 PropertyValueConstraintValidationUtilTest.class.getClassLoader().getResource("types/datatypes"
306                                                 + "/constraintTest.json")).getFile());
307                 Path logFile = Paths.get(file.getAbsolutePath());
308                 try (BufferedReader reader = Files.newBufferedReader(logFile, StandardCharsets.UTF_8)) {
309                         reader.lines().forEach(stringBuilder::append);
310                 } catch (Exception e) {
311                         Assert.fail(e.getMessage());
312                         e.printStackTrace();
313                 }
314                 return stringBuilder.toString();
315         }
316
317 }