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