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