2b9c296eb52a0627cff6242e77b3821b0a129310
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.model.operations.impl.util;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertNotNull;
28
29 import java.lang.reflect.Type;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
36 import java.util.Map.Entry;
37
38 import org.apache.commons.lang3.tuple.ImmutablePair;
39 import org.junit.Test;
40 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
41 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
42 import org.openecomp.sdc.be.model.DataTypeDefinition;
43 import org.openecomp.sdc.be.model.PropertyDefinition;
44 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
45 import org.openecomp.sdc.be.model.tosca.converters.ListConverter;
46 import org.openecomp.sdc.be.model.tosca.converters.MapConverter;
47 import org.openecomp.sdc.be.model.tosca.validators.DataTypeValidatorConverter;
48 import org.openecomp.sdc.be.model.tosca.validators.ListValidator;
49 import org.openecomp.sdc.be.model.tosca.validators.MapValidator;
50
51 import com.google.gson.Gson;
52 import com.google.gson.GsonBuilder;
53 import com.google.gson.JsonElement;
54 import com.google.gson.JsonObject;
55 import com.google.gson.JsonParser;
56 import com.google.gson.reflect.TypeToken;
57
58 import fj.data.Either;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61
62 public class DataTypeValidatorTest {
63         private static Logger log = LoggerFactory.getLogger(DataTypeValidatorTest.class.getName());
64         private static Gson gson = new Gson();
65
66         DataTypeValidatorConverter dataTypeValidator = DataTypeValidatorConverter.getInstance();
67
68         @Test
69         public void testDerivedFromPrimitiveEmptyValue() {
70
71                 Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>();
72                 allDataTypes.put("integer", getPrimitiveDataType("integer"));
73
74                 DataTypeDefinition fromIntegerType = buildDerivedFromIntegerType();
75                 ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate("", fromIntegerType,
76                                 allDataTypes);
77
78                 assertTrue("check result is valid", validate.right.booleanValue());
79                 assertEquals("check value is the same as sent", null, validate.left);
80
81                 validate = dataTypeValidator.validateAndUpdate(null, fromIntegerType, allDataTypes);
82
83                 assertTrue("check result is valid", validate.right.booleanValue());
84                 assertEquals("check value is the same as sent", null, validate.left);
85
86                 validate = dataTypeValidator.validateAndUpdate("88", fromIntegerType, allDataTypes);
87
88                 assertTrue("check result is valid", validate.right.booleanValue());
89                 assertEquals("check value is the same as sent", "88", validate.left.toString());
90
91         }
92
93         @Test
94         public void testCompositeWithParameterDerivedFromPrimitiveEmptyValue() {
95
96                 DataTypeDefinition derivedFromIntegerType = buildDerivedFromIntegerType();
97                 Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>();
98                 allDataTypes.put("myinteger", derivedFromIntegerType);
99
100                 DataTypeDefinition personDataType = buildPersonDataType();
101
102                 Person person = new Person("my address", 32);
103                 String json = gson.toJson(person);
104                 log.debug(json);
105
106                 ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, personDataType,
107                                 allDataTypes);
108                 assertTrue("check valid value", validate.right.booleanValue());
109
110                 person = new Person("my address", 32);
111                 json = gson.toJson(person);
112                 json = json.replace("32", "32a");
113                 log.debug(json);
114
115                 validate = dataTypeValidator.validateAndUpdate(json, personDataType, allDataTypes);
116                 assertFalse("check valid value", validate.right.booleanValue());
117
118         }
119
120         @Test
121         public void testCompositeWithEmptyListValue() {
122
123                 DataTypeDefinition dataTypeDefinition = buildCredentialDataType();
124
125                 String[] strArr = {};
126                 List<String> strList = Arrays.asList(strArr);
127
128                 // Check empty list
129                 Credential credential = new Credential("protcol<br>>", 5, "token_type", "token", null, "user", true, strList);
130                 City mycity = new City("myadd<br><<br>", 55);
131                 credential.setMycity(mycity);
132
133                 String json = gson.toJson(credential);
134                 log.debug(json);
135
136                 Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>();
137                 DataTypeDefinition cityDataType = buildCityDataType();
138                 allDataTypes.put("city", cityDataType);
139
140                 ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, dataTypeDefinition,
141                                 allDataTypes);
142                 assertTrue("check valid value", validate.right.booleanValue());
143
144                 Credential credentialRes = gson.fromJson(validate.left.toString(), Credential.class);
145                 assertEquals("check empty list", 0, credentialRes.getMylist().size());
146
147                 log.debug("Result is = {}", validate.left.toString());
148
149         }
150
151         @Test
152         public void testCompositeWithListNullValue() {
153                 DataTypeDefinition dataTypeDefinition = buildCredentialDataType();
154
155                 Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>();
156                 DataTypeDefinition cityDataType = buildCityDataType();
157                 allDataTypes.put("city", cityDataType);
158
159                 // Check list is NULL
160                 Credential credential = new Credential("protcol<br>>", 5, "token_type", "token", null, "user", true, null);
161                 City mycity = new City("myadd<br><<br>", 55);
162                 credential.setMycity(mycity);
163
164                 String json = gson.toJson(credential);
165
166                 ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, dataTypeDefinition,
167                                 allDataTypes);
168                 assertTrue("check valid value", validate.right.booleanValue());
169
170                 Credential credentialRes = gson.fromJson(validate.left.toString(), Credential.class);
171                 assertNull("check list is null", credentialRes.getMylist());
172                 log.debug("Result is = {}", validate.left.toString());
173
174         }
175
176         @Test
177         public void testCompositeWithUserNullValue() {
178                 DataTypeDefinition dataTypeDefinition = buildCredentialDataType();
179
180                 Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>();
181                 DataTypeDefinition cityDataType = buildCityDataType();
182                 allDataTypes.put("city", cityDataType);
183
184                 // Check user is null
185                 Credential credential = new Credential("protcol<br>>", 5, "token_type", "token", null, null, true, null);
186                 City mycity = new City("myadd<br><<br>", 55);
187                 credential.setMycity(mycity);
188
189                 String json = gson.toJson(credential);
190
191                 ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, dataTypeDefinition,
192                                 allDataTypes);
193                 assertTrue("check valid value", validate.right.booleanValue());
194
195                 Credential credentialRes = gson.fromJson(validate.left.toString(), Credential.class);
196                 assertNull("check list is null", credentialRes.getUser());
197                 log.debug("Result is = {}", validate.left.toString());
198         }
199
200         @Test
201         public void testCompositeWithEmptyUserValue() {
202
203                 DataTypeDefinition dataTypeDefinition = buildCredentialDataType();
204
205                 Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>();
206                 DataTypeDefinition cityDataType = buildCityDataType();
207                 allDataTypes.put("city", cityDataType);
208                 // Check user is empty
209                 Credential credential = new Credential("protcol<br>>", 5, "token_type", "token", null, "", true, null);
210                 City mycity = new City("myadd<br><<br>", 55);
211                 credential.setMycity(mycity);
212
213                 String json = gson.toJson(credential);
214                 log.debug(json);
215
216                 ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, dataTypeDefinition,
217                                 allDataTypes);
218                 assertTrue("check valid value", validate.right.booleanValue());
219
220                 Credential credentialRes = gson.fromJson(validate.left.toString(), Credential.class);
221                 assertNotNull("check list is not null", credentialRes.getUser());
222                 assertEquals("check user is empty", "", credentialRes.getUser());
223                 log.debug("Result is = {}", validate.left.toString());
224
225         }
226
227         @Test
228         public void testCompositeWithSumNullValue() {
229                 DataTypeDefinition dataTypeDefinition = buildCredentialDataType();
230
231                 Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>();
232                 DataTypeDefinition cityDataType = buildCityDataType();
233                 allDataTypes.put("city", cityDataType);
234
235                 // Check user is null
236                 Credential credential = new Credential("protcol<br>>", null, "token_type", "token", null, null, true, null);
237                 City mycity = new City("myadd<br><<br>", 55);
238                 credential.setMycity(mycity);
239
240                 String json = gson.toJson(credential);
241
242                 ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, dataTypeDefinition,
243                                 allDataTypes);
244                 assertTrue("check valid value", validate.right.booleanValue());
245
246                 Credential credentialRes = gson.fromJson(validate.left.toString(), Credential.class);
247                 assertNull("check list is null", credentialRes.getSum());
248                 log.debug("Result is = {}", validate.left.toString());
249         }
250
251         @Test
252         public void testInvalidJson() {
253                 DataTypeDefinition dataTypeDefinition = buildCredentialDataType();
254
255                 Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>();
256                 DataTypeDefinition cityDataType = buildCityDataType();
257                 allDataTypes.put("city", cityDataType);
258
259                 // Check user is null
260                 Credential credential = new Credential("protcol<br>>", null, "token_type", "token", null, null, true, null);
261                 City mycity = new City("myadd<br><<br>", 55);
262                 credential.setMycity(mycity);
263
264                 String json = gson.toJson(credential);
265
266                 json += "fdfd";
267
268                 ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, dataTypeDefinition,
269                                 allDataTypes);
270                 assertFalse("check valid value", validate.right.booleanValue());
271
272         }
273
274         @Test
275         public void testInvalidInnerValue() {
276
277                 DataTypeDefinition dataTypeDefinition = buildCredentialDataType();
278
279                 Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>();
280                 DataTypeDefinition cityDataType = buildCityDataType();
281                 allDataTypes.put("city", cityDataType);
282
283                 // Check user is null
284                 Credential credential = new Credential("protcol<br>>", null, "token_type", "token", null, null, true, null);
285                 City mycity = new City("myadd<br><<br>", 55);
286                 credential.setMycity(mycity);
287
288                 String json = gson.toJson(credential);
289
290                 json = json.replace("55", "a55b");
291
292                 ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, dataTypeDefinition,
293                                 allDataTypes);
294                 assertFalse("check valid value", validate.right.booleanValue());
295
296         }
297
298         @Test
299         public void testInvalidInnerJson() {
300
301                 DataTypeDefinition dataTypeDefinition = buildCredentialDataType();
302
303                 Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>();
304                 DataTypeDefinition cityDataType = buildCityDataType();
305                 allDataTypes.put("city", cityDataType);
306
307                 // Check user is null
308                 Credential credential = new Credential("protcol<br>>", null, "token_type", "token", null, null, true, null);
309                 City mycity = new City("", null);
310
311                 credential.setMycity(mycity);
312
313                 String json = gson.toJson(credential);
314
315                 json = json.replace("{\"address\":\"\"}", "scalar");
316
317                 ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, dataTypeDefinition,
318                                 allDataTypes);
319                 assertFalse("check valid value", validate.right.booleanValue());
320
321         }
322
323         @Test
324         public void testInvalidPropertyJson() {
325
326                 DataTypeDefinition dataTypeDefinition = buildCredentialDataType();
327
328                 Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>();
329                 DataTypeDefinition cityDataType = buildCityDataType();
330                 allDataTypes.put("city", cityDataType);
331
332                 // Check user is null
333                 Credential credential = new Credential("protcol<br>>", null, "token_type", "token", null, null, true, null);
334                 City mycity = new City("myadd<br><<br>", 55);
335                 credential.setMycity(mycity);
336
337                 String json = gson.toJson(credential);
338
339                 json = json.replace("55", "a55b");
340
341                 ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, dataTypeDefinition,
342                                 allDataTypes);
343                 assertFalse("check valid value", validate.right.booleanValue());
344
345         }
346
347         @Test
348         public void testCompositeDataTypeWithInternalComposite() {
349
350                 DataTypeDefinition dataTypeDefinition = buildCredentialDataType();
351
352                 String[] strArr = { "aaa", "bbb", "c<br>dcc" };
353                 List<String> strList = Arrays.asList(strArr);
354
355                 Credential credential = new Credential("protcol<br>>", 5, "token_type", "token", null, "user", true, strList);
356                 City mycity = new City("myadd<br><<br>", 55);
357                 credential.setMycity(mycity);
358
359                 String json = gson.toJson(credential);
360
361                 Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>();
362                 DataTypeDefinition cityDataType = buildCityDataType();
363                 allDataTypes.put("city", cityDataType);
364
365                 ImmutablePair<JsonElement, Boolean> validate = dataTypeValidator.validateAndUpdate(json, dataTypeDefinition,
366                                 allDataTypes);
367                 assertTrue("check valid value", validate.right.booleanValue());
368
369                 log.debug("Result is = {}", validate.left.toString());
370
371         }
372
373         @Test
374         public void testMapValidator() {
375
376                 MapValidator validator = new MapValidator();
377                 Gson gson = new Gson();
378                 // Happy Scenarios
379                 // 1 - Map<String,Integer> check OK
380                 Map<String, Integer> map_1 = new HashMap<>();
381                 map_1.put("key1", 2);
382                 map_1.put("key2", 3);
383                 String value = gson.toJson(map_1);
384                 String innerType = "integer";
385                 assertTrue("Test Map validation with inner integer type", validator.isValid(value, innerType, null));
386
387                 // 2 - Map<String,Boolean> check OK
388                 Map<String, Boolean> map_2 = new HashMap<>();
389                 map_2.put("key1", true);
390                 map_2.put("key2", false);
391                 value = gson.toJson(map_2);
392                 innerType = "boolean";
393                 assertTrue("Test Map validation with inner boolean type", validator.isValid(value, innerType, null));
394
395                 // 3 - give integer with quotes
396                 innerType = "integer";
397                 value = "{\"key1\":\"5\",\"key2\":\"7\"}";
398                 assertTrue("Test Map validation with inner integer type, but qouted values",
399                                 validator.isValid(value, innerType, null));
400
401                 // 4 - empty default value
402                 innerType = "float";
403                 value = "";
404                 assertTrue("Test Map validation with inner float type", validator.isValid(value, innerType, null));
405
406                 // Faulty Scenarios
407                 // 5 - mismatch in data type
408                 value = gson.toJson(map_1);
409                 innerType = "boolean";
410                 assertFalse("Test Map faulty validation with inner boolean type", validator.isValid(value, innerType, null));
411                 // 6 - mismatch in data type
412                 value = gson.toJson(map_2);
413                 innerType = "integer";
414                 assertFalse("Test Map faulty validation with inner integer type", validator.isValid(value, innerType, null));
415
416         }
417
418         @Test
419         public void testMapConverter() {
420
421                 MapConverter converter = new MapConverter();
422                 Gson gson = new Gson();
423                 // Happy Scenarios
424                 Map<String, String> map_1 = new HashMap<>();
425                 Map<String, String> resMap_1 = new HashMap<>();
426
427                 // 1 - check Spaces eliminated + html square brackets eliminated
428                 map_1.put("key1", "<b>test</b>");
429                 map_1.put("key2", "        test");
430                 resMap_1.put("key1", "test");
431                 resMap_1.put("key2", " test");
432                 String value = gson.toJson(map_1);
433                 String expectedVal = gson.toJson(resMap_1);
434                 String innerType = "string";
435                 assertEquals("Test Map validation with inner string type", expectedVal,
436                                 converter.convert(value, innerType, null));
437
438                 // 2 - float converter
439                 innerType = "float";
440                 value = "{\"key1\":0.4545,\"key2\":0.2f}";
441                 expectedVal = "{\"key1\":0.4545,\"key2\":0.2}";
442                 assertEquals("Test Map validation with inner float type", expectedVal,
443                                 converter.convert(value, innerType, null));
444
445                 // 3 - check default empty value converter
446                 innerType = "float";
447                 value = "";
448                 expectedVal = "";
449                 assertEquals("Test Map validation with inner float type", expectedVal,
450                                 converter.convert(value, innerType, null));
451
452                 // 4 - invalid json
453                 // 3 - check default empty value converter
454                 innerType = "float";
455                 value = "{1345234556@#(";
456                 expectedVal = null;
457                 assertEquals("Test Map validation with inner float type", expectedVal,
458                                 converter.convert(value, innerType, null));
459
460         }
461
462         @Test
463         public void testCompositeDataTypeWithMapComposite() {
464
465                 DataTypeDefinition fileDataTypeDefinition = buildFileDataType();
466                 Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>();
467                 DataTypeDefinition cityDataType = buildCityDataType();
468                 allDataTypes.put("city", cityDataType);
469
470                 MyFile myFile = new MyFile();
471                 myFile.setAge(88);
472                 Map<String, City> attributes = new HashMap<>();
473                 attributes.put("key1", new City("address1<br>", 11));
474                 attributes.put("key2", new City("address2<br>", 22));
475                 myFile.setAttributes(attributes);
476
477                 String str = gson.toJson(myFile);
478                 log.debug(str);
479
480                 ImmutablePair<JsonElement, Boolean> convert = dataTypeValidator.validateAndUpdate(str, fileDataTypeDefinition,
481                                 allDataTypes);
482
483                 assertTrue("check map converter succeed", convert.right);
484
485                 JsonElement convertedValue = convert.left;
486
487                 log.debug("{}", convertedValue);
488                 MyFile fromJson = gson.fromJson(convertedValue, MyFile.class);
489
490                 assertEquals("check age", 88, fromJson.getAge().intValue());
491                 assertEquals("check address 1", "address1", fromJson.getAttributes().get("key1").getAddress());
492                 assertEquals("check address 2", "address2", fromJson.getAttributes().get("key2").getAddress());
493
494         }
495
496         @Test
497         public void testMapConverterWithComplexInnerType() {
498
499                 Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>();
500                 DataTypeDefinition credentialDataTypeDefinition = buildCredentialDataType();
501                 DataTypeDefinition cityDataType = buildCityDataType();
502                 allDataTypes.put("city", cityDataType);
503                 allDataTypes.put("credential", credentialDataTypeDefinition);
504
505                 Gson gson = new Gson();
506                 // Happy Scenarios
507                 Map<String, Object> map_1 = new HashMap<>();
508
509                 // 1 - check Spaces eliminated + html square brackets eliminated
510
511                 String[] strArr = { "aaa", "bbb", "c<br>dcc" };
512                 List<String> strList = Arrays.asList(strArr);
513                 Credential credential1 = new Credential("protocol;:,.\"<br>>", 5, "token_type", "token", null, "user", true,
514                                 strList);
515                 City mycity1 = new City("myadd<br><<br>", 55);
516                 credential1.setMycity(mycity1);
517
518                 Credential credential2 = new Credential("protocol;:,.\"<br>>", 5, "token_type", "token", null, "user", true,
519                                 strList);
520                 City mycity2 = new City("myadd<br><<br>", 66);
521                 credential2.setMycity(mycity2);
522
523                 map_1.put("key1", credential1);
524                 map_1.put("key2", credential2);
525
526                 String str = gson.toJson(map_1);
527                 log.debug(str);
528
529                 MapConverter mapConverter = new MapConverter();
530                 Either<String, Boolean> convert = mapConverter.convertWithErrorResult(str, "credential", allDataTypes);
531
532                 assertTrue("check map converter succeed", convert.isLeft());
533
534                 String convertedValue = convert.left().value();
535
536                 Type type = new TypeToken<Map<String, Credential>>() {
537                 }.getType();
538
539                 Map<String, Credential> fromJson = gson.fromJson(convertedValue, type);
540
541                 Credential actualCredential1 = fromJson.get("key1");
542                 assertEquals("check sum", 5, actualCredential1.getSum().intValue());
543                 assertEquals("check protocol", "protocol;:,.\">", actualCredential1.getProtocol());
544                 String[] convertedStrArr = { "aaa", "bbb", "cdcc" };
545                 List<String> convertedStrList = Arrays.asList(convertedStrArr);
546                 assertEquals("check list", convertedStrList, actualCredential1.getMylist());
547
548                 assertEquals("check city address", "myadd<", actualCredential1.getMycity().getAddress());
549                 assertEquals("check city address", 55, actualCredential1.getMycity().getAge().intValue());
550
551                 Credential actualCredential2 = fromJson.get("key2");
552                 assertEquals("check city address", 66, actualCredential2.getMycity().getAge().intValue());
553
554         }
555
556         @Test
557         public void testListConverterWithComplexInnerType() {
558
559                 Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>();
560                 DataTypeDefinition credentialDataTypeDefinition = buildCredentialDataType();
561                 DataTypeDefinition cityDataType = buildCityDataType();
562                 allDataTypes.put("city", cityDataType);
563                 allDataTypes.put("credential", credentialDataTypeDefinition);
564
565                 Gson gson = new Gson();
566
567                 List<Object> list = buildListOf2CredentialObjects();
568
569                 String str = gson.toJson(list);
570                 log.debug(str);
571
572                 ListConverter listConverter = new ListConverter();
573
574                 Either<String, Boolean> convert = listConverter.convertWithErrorResult(str, "credential", allDataTypes);
575
576                 assertTrue("check map converter succeed", convert.isLeft());
577
578                 String convertedValue = convert.left().value();
579
580                 validateListOfCredential(gson, convertedValue);
581
582                 list.add(null);
583
584                 str = gson.toJson(list);
585                 log.debug(str);
586
587                 convert = listConverter.convertWithErrorResult(str, "credential", allDataTypes);
588
589                 assertTrue("check map converter succeed", convert.isLeft());
590
591                 validateListOfCredential(gson, convertedValue);
592         }
593
594         @Test
595         public void testListValidatorWithComplexInnerType() {
596
597                 Map<String, DataTypeDefinition> allDataTypes = new HashMap<String, DataTypeDefinition>();
598                 DataTypeDefinition credentialDataTypeDefinition = buildCredentialDataType();
599                 DataTypeDefinition cityDataType = buildCityDataType();
600                 allDataTypes.put("city", cityDataType);
601                 allDataTypes.put("credential", credentialDataTypeDefinition);
602
603                 Gson gson = new Gson();
604                 // Happy Scenarios
605                 List<Object> list = buildListOf2CredentialObjects();
606
607                 String str = gson.toJson(list);
608                 log.debug(str);
609
610                 ListValidator listValidator = new ListValidator();
611
612                 boolean isValid = listValidator.isValid(str, "credential", allDataTypes);
613
614                 assertTrue("check valid value", isValid);
615
616                 String badStr = str.replace("protocol", "protocol1");
617
618                 isValid = listValidator.isValid(badStr, "credential", allDataTypes);
619
620                 assertFalse("check valid value", isValid);
621
622                 badStr = str.replace("55", "\"aa\"");
623
624                 isValid = listValidator.isValid(badStr, "credential", allDataTypes);
625
626                 assertFalse("check valid value", isValid);
627
628         }
629
630         private List<Object> buildListOf2CredentialObjects() {
631                 List<Object> list = new ArrayList<>();
632
633                 String[] strArr = { "aaa", "bbb", "c<br>dcc" };
634                 List<String> strList = Arrays.asList(strArr);
635                 Credential credential1 = new Credential("protocol.,\":;<br>>", 5, "token_type", "token", null, "user", true,
636                                 strList);
637                 City mycity1 = new City("myadd<br><<br>", 55);
638                 credential1.setMycity(mycity1);
639
640                 Credential credential2 = new Credential("protocol.,\":;<br>>", 5, "token_type", "token", null, "user", true,
641                                 strList);
642                 City mycity2 = new City("myadd<br><<br>", 66);
643                 credential2.setMycity(mycity2);
644
645                 list.add(credential1);
646                 list.add(credential2);
647                 return list;
648         }
649
650         private void validateListOfCredential(Gson gson, String convertedValue) {
651
652                 log.debug(convertedValue);
653                 Type type = new TypeToken<List<Credential>>() {
654                 }.getType();
655
656                 List<Credential> fromJson = gson.fromJson(convertedValue, type);
657
658                 assertEquals("check list size", 2, fromJson.size());
659
660                 // Credential actualCredential1 = gson.fromJson(list.get(0).toString(),
661                 // Credential.class);
662                 Credential actualCredential1 = fromJson.get(0);
663                 assertEquals("check sum", 5, actualCredential1.getSum().intValue());
664                 assertEquals("check protocol", "protocol.,\":;>", actualCredential1.getProtocol());
665                 String[] convertedStrArr = { "aaa", "bbb", "cdcc" };
666                 List<String> convertedStrList = Arrays.asList(convertedStrArr);
667                 assertEquals("check list", convertedStrList, actualCredential1.getMylist());
668
669                 assertEquals("check city address", "myadd<", actualCredential1.getMycity().getAddress());
670                 assertEquals("check city address", 55, actualCredential1.getMycity().getAge().intValue());
671
672                 // Credential actualCredential2 = gson.fromJson(list.get(1).toString(),
673                 // Credential.class);
674                 Credential actualCredential2 = fromJson.get(1);
675                 assertEquals("check city address", 66, actualCredential2.getMycity().getAge().intValue());
676         }
677
678         private DataTypeDefinition buildCredentialDataType() {
679                 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
680                 dataTypeDefinition.setName("datatype.1");
681                 List<PropertyDefinition> properties = new ArrayList<>();
682                 PropertyDefinition propertyDefinition1 = new PropertyDefinition();
683                 propertyDefinition1.setName("sum");
684                 propertyDefinition1.setType(ToscaPropertyType.INTEGER.getType());
685                 PropertyDefinition propertyDefinition2 = new PropertyDefinition();
686                 propertyDefinition2.setName("protocol");
687                 propertyDefinition2.setType(ToscaPropertyType.STRING.getType());
688                 PropertyDefinition propertyDefinition3 = new PropertyDefinition();
689                 propertyDefinition3.setName("token_type");
690                 propertyDefinition3.setType(ToscaPropertyType.STRING.getType());
691                 PropertyDefinition propertyDefinition4 = new PropertyDefinition();
692                 propertyDefinition4.setName("token");
693                 propertyDefinition4.setType(ToscaPropertyType.STRING.getType());
694                 PropertyDefinition propertyDefinition5 = new PropertyDefinition();
695                 propertyDefinition5.setName("keys");
696                 propertyDefinition5.setType(ToscaPropertyType.MAP.getType());
697                 PropertyDefinition propertyDefinition6 = new PropertyDefinition();
698                 propertyDefinition6.setName("mylist");
699                 propertyDefinition6.setType(ToscaPropertyType.LIST.getType());
700                 SchemaDefinition entrySchema = new SchemaDefinition();
701                 PropertyDataDefinition property = new PropertyDataDefinition();
702                 property.setType("string");
703                 entrySchema.setProperty(property);
704                 propertyDefinition6.setSchema(entrySchema);
705                 PropertyDefinition propertyDefinition7 = new PropertyDefinition();
706                 propertyDefinition7.setName("user");
707                 propertyDefinition7.setType(ToscaPropertyType.STRING.getType());
708                 PropertyDefinition propertyDefinition8 = new PropertyDefinition();
709                 propertyDefinition8.setName("isMandatory");
710                 propertyDefinition8.setType(ToscaPropertyType.BOOLEAN.getType());
711
712                 PropertyDefinition propertyDefinition9 = new PropertyDefinition();
713                 propertyDefinition9.setName("mycity");
714                 propertyDefinition9.setType("city");
715
716                 properties.add(propertyDefinition1);
717                 properties.add(propertyDefinition2);
718                 properties.add(propertyDefinition3);
719                 properties.add(propertyDefinition4);
720                 properties.add(propertyDefinition5);
721                 properties.add(propertyDefinition6);
722                 properties.add(propertyDefinition7);
723                 properties.add(propertyDefinition8);
724                 properties.add(propertyDefinition9);
725
726                 dataTypeDefinition.setProperties(properties);
727                 return dataTypeDefinition;
728         }
729
730         private static DataTypeDefinition buildCityDataType() {
731                 DataTypeDefinition cityDataType = new DataTypeDefinition();
732                 cityDataType.setName("city");
733                 List<PropertyDefinition> cityProperties = new ArrayList<>();
734                 PropertyDefinition cityPropertyDefinition1 = new PropertyDefinition();
735                 cityPropertyDefinition1.setName("age");
736                 cityPropertyDefinition1.setType(ToscaPropertyType.INTEGER.getType());
737                 PropertyDefinition cityPropertyDefinition2 = new PropertyDefinition();
738                 cityPropertyDefinition2.setName("address");
739                 cityPropertyDefinition2.setType(ToscaPropertyType.STRING.getType());
740
741                 cityProperties.add(cityPropertyDefinition1);
742                 cityProperties.add(cityPropertyDefinition2);
743
744                 cityDataType.setProperties(cityProperties);
745                 return cityDataType;
746         }
747
748         private static DataTypeDefinition buildPersonDataType() {
749                 DataTypeDefinition personDataType = new DataTypeDefinition();
750                 personDataType.setName("person");
751                 List<PropertyDefinition> personProperties = new ArrayList<>();
752                 PropertyDefinition personPropertyDefinition1 = new PropertyDefinition();
753                 personPropertyDefinition1.setName("age");
754                 personPropertyDefinition1.setType("myinteger");
755                 PropertyDefinition personPropertyDefinition2 = new PropertyDefinition();
756                 personPropertyDefinition2.setName("address");
757                 personPropertyDefinition2.setType(ToscaPropertyType.STRING.getType());
758
759                 personProperties.add(personPropertyDefinition1);
760                 personProperties.add(personPropertyDefinition2);
761
762                 personDataType.setProperties(personProperties);
763                 return personDataType;
764         }
765
766         private static DataTypeDefinition buildFileDataType() {
767                 DataTypeDefinition fileDataType = new DataTypeDefinition();
768                 fileDataType.setName("file");
769                 List<PropertyDefinition> fileProperties = new ArrayList<>();
770                 PropertyDefinition filePropertyDefinition1 = new PropertyDefinition();
771                 filePropertyDefinition1.setName("age");
772                 filePropertyDefinition1.setType("integer");
773
774                 PropertyDefinition filePropertyDefinition2 = new PropertyDefinition();
775                 filePropertyDefinition2.setName("attributes");
776                 filePropertyDefinition2.setType(ToscaPropertyType.MAP.getType());
777
778                 fileProperties.add(filePropertyDefinition1);
779                 fileProperties.add(filePropertyDefinition2);
780
781                 SchemaDefinition entrySchema = new SchemaDefinition();
782                 PropertyDataDefinition property = new PropertyDataDefinition();
783                 property.setType("city");
784                 entrySchema.setProperty(property);
785                 filePropertyDefinition2.setSchema(entrySchema);
786
787                 fileDataType.setProperties(fileProperties);
788                 return fileDataType;
789         }
790
791         private static DataTypeDefinition getPrimitiveDataType(String type) {
792
793                 DataTypeDefinition derivedFrom = new DataTypeDefinition();
794                 derivedFrom.setName(type);
795
796                 return derivedFrom;
797
798         }
799
800         private static DataTypeDefinition buildDerivedFromIntegerType() {
801
802                 DataTypeDefinition derivedFrom = getPrimitiveDataType("integer");
803
804                 DataTypeDefinition myIntegerDataType = new DataTypeDefinition();
805                 myIntegerDataType.setDerivedFrom(derivedFrom);
806
807                 myIntegerDataType.setName("myinteger");
808
809                 return myIntegerDataType;
810         }
811
812         public static class MyFile {
813
814                 Integer age;
815
816                 Map<String, City> attributes;
817
818                 public Integer getAge() {
819                         return age;
820                 }
821
822                 public void setAge(Integer age) {
823                         this.age = age;
824                 }
825
826                 public Map<String, City> getAttributes() {
827                         return attributes;
828                 }
829
830                 public void setAttributes(Map<String, City> attributes) {
831                         this.attributes = attributes;
832                 }
833
834         }
835
836         public static class City {
837
838                 String address;
839                 Integer age;
840
841                 public City(String address, Integer age) {
842                         super();
843                         this.address = address;
844                         this.age = age;
845                 }
846
847                 public String getAddress() {
848                         return address;
849                 }
850
851                 public void setAddress(String address) {
852                         this.address = address;
853                 }
854
855                 public Integer getAge() {
856                         return age;
857                 }
858
859                 public void setAge(Integer age) {
860                         this.age = age;
861                 }
862
863         }
864
865         public static class Person {
866
867                 String address;
868                 Integer age;
869
870                 public Person(String address, Integer age) {
871                         super();
872                         this.address = address;
873                         this.age = age;
874                 }
875
876                 public String getAddress() {
877                         return address;
878                 }
879
880                 public void setAddress(String address) {
881                         this.address = address;
882                 }
883
884                 public Integer getAge() {
885                         return age;
886                 }
887
888                 public void setAge(Integer age) {
889                         this.age = age;
890                 }
891
892                 @Override
893                 public String toString() {
894                         return "Person [address=" + address + ", age=" + age + "]";
895                 }
896
897         }
898
899         public static class Credential {
900
901                 String protocol;
902                 Integer sum;
903                 String token_type;
904                 String token;
905                 Map<String, String> keys;
906                 String user;
907                 Boolean isMandatory;
908                 List<String> mylist;
909                 City mycity;
910
911                 public Credential(String protocol, Integer sum, String token_type, String token, Map<String, String> keys,
912                                 String user, Boolean isMandatory, List<String> mylist) {
913                         super();
914                         this.protocol = protocol;
915                         this.sum = sum;
916                         this.token_type = token_type;
917                         this.token = token;
918                         this.keys = keys;
919                         this.user = user;
920                         this.isMandatory = isMandatory;
921                         this.mylist = mylist;
922                 }
923
924                 public String getProtocol() {
925                         return protocol;
926                 }
927
928                 public void setProtocol(String protocol) {
929                         this.protocol = protocol;
930                 }
931
932                 public String getToken_type() {
933                         return token_type;
934                 }
935
936                 public void setToken_type(String token_type) {
937                         this.token_type = token_type;
938                 }
939
940                 public String getToken() {
941                         return token;
942                 }
943
944                 public void setToken(String token) {
945                         this.token = token;
946                 }
947
948                 public Map<String, String> getKeys() {
949                         return keys;
950                 }
951
952                 public void setKeys(Map<String, String> keys) {
953                         this.keys = keys;
954                 }
955
956                 public String getUser() {
957                         return user;
958                 }
959
960                 public void setUser(String user) {
961                         this.user = user;
962                 }
963
964                 public Boolean getIsMandatory() {
965                         return isMandatory;
966                 }
967
968                 public void setIsMandatory(Boolean isMandatory) {
969                         this.isMandatory = isMandatory;
970                 }
971
972                 public Integer getSum() {
973                         return sum;
974                 }
975
976                 public void setSum(Integer sum) {
977                         this.sum = sum;
978                 }
979
980                 public List<String> getMylist() {
981                         return mylist;
982                 }
983
984                 public void setMylist(List<String> mylist) {
985                         this.mylist = mylist;
986                 }
987
988                 public City getMycity() {
989                         return mycity;
990                 }
991
992                 public void setMycity(City mycity) {
993                         this.mycity = mycity;
994                 }
995
996                 @Override
997                 public String toString() {
998                         return "Credential [protocol=" + protocol + ", token_type=" + token_type + ", token=" + token + ", keys="
999                                         + keys + ", user=" + user + ", isMandatory=" + isMandatory + "]";
1000                 }
1001
1002         }
1003
1004 }