ab290f770872b79b166e482b01ff1db9d1505d91
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-impl / src / test / java / org / opendaylight / controller / sal / binding / generator / impl / GeneratedTypesStringTest.java
1 package org.opendaylight.controller.sal.binding.generator.impl;
2
3 import static org.junit.Assert.assertNotNull;
4 import static org.junit.Assert.assertTrue;
5
6 import java.io.File;
7 import java.util.ArrayList;
8 import java.util.List;
9 import java.util.Set;
10
11 import org.junit.BeforeClass;
12 import org.junit.Test;
13 import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
14 import org.opendaylight.controller.sal.binding.generator.impl.BindingGeneratorImpl;
15 import org.opendaylight.controller.sal.binding.model.api.Constant;
16 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
17 import org.opendaylight.controller.sal.binding.model.api.ParameterizedType;
18 import org.opendaylight.controller.sal.binding.model.api.Type;
19 import org.opendaylight.controller.yang.model.api.Module;
20 import org.opendaylight.controller.yang.model.api.SchemaContext;
21 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
22 import org.opendaylight.controller.yang.parser.impl.YangParserImpl;
23
24 public class GeneratedTypesStringTest {
25
26     private final static List<File> testModels = new ArrayList<File>();
27
28     @BeforeClass
29     public static void loadTestResources() {
30         final File listModelFile = new File(GeneratedTypesStringTest.class.getResource("/simple-string-demo.yang")
31                 .getPath());
32         testModels.add(listModelFile);
33     }
34
35     @Test
36     public void constantGenerationTest() {
37         final YangModelParser parser = new YangParserImpl();
38         final Set<Module> modules = parser.parseYangModels(testModels);
39         final SchemaContext context = parser.resolveSchemaContext(modules);
40
41         assertNotNull(context);
42         final BindingGenerator bindingGen = new BindingGeneratorImpl();
43         final List<Type> genTypes = bindingGen.generateTypes(context);
44
45         boolean typedefStringFound = false;
46         boolean constantRegExListFound = false;
47         boolean constantRegExListTypeGeneric = false;
48         boolean constantRegExListTypeContainer = false;
49         boolean noStringInReqExListFound = false;
50         boolean constantRegExListValueOK = false;
51         boolean constantRegExListTypeOneGeneric = false;
52         for (final Type type : genTypes) {
53             if (type instanceof GeneratedTransferObject) {
54                 final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
55
56                 if (genTO.getName().equals("TypedefString")) {
57                     typedefStringFound = true;
58
59                     List<Constant> constants = genTO.getConstantDefinitions();
60                     for (Constant con : constants) {
61                         if (con.getName().equals("PATTERN_CONSTANTS")) {
62                             constantRegExListFound = true;
63                         } else
64                             break;
65                         ParameterizedType pType;
66                         if (con.getType() instanceof ParameterizedType) {
67                             pType = (ParameterizedType) con.getType();
68                         } else
69                             break;
70
71                         Type[] types;
72                         if (pType.getName().equals("List")) {
73                             constantRegExListTypeContainer = true;
74                             types = pType.getActualTypeArguments();
75                         } else
76                             break;
77
78                         if (types.length == 1) {
79                             constantRegExListTypeOneGeneric = true;
80                         } else
81                             break;
82
83                         if (types[0].getName().equals("String")) {
84                             constantRegExListTypeGeneric = true;
85                         } else
86                             break;
87
88                         if (con.getValue() instanceof List) {
89                             constantRegExListValueOK = true;
90                         } else
91                             break;
92
93                         for (Object obj : (List<?>) con.getValue()) {
94                             if (!(obj instanceof String)) {
95                                 noStringInReqExListFound = true;
96                                 break;
97                             }
98                         }
99
100                     }
101                 }
102             }
103
104         }
105
106         assertTrue("Typedef >>TypedefString<< wasn't found", typedefStringFound);
107         assertTrue("Constant PATTERN_CONSTANTS is missing in TO", constantRegExListFound);
108         assertTrue("Constant PATTERN_CONSTANTS doesn't have correct container type", constantRegExListTypeContainer);
109         assertTrue("Constant PATTERN_CONSTANTS has more than one generic type", constantRegExListTypeOneGeneric);
110         assertTrue("Constant PATTERN_CONSTANTS doesn't have correct generic type", constantRegExListTypeGeneric);
111         assertTrue("Constant PATTERN_CONSTANTS doesn't contain List object", constantRegExListValueOK);
112         assertTrue("In list found other type than String", !noStringInReqExListFound);
113
114     }
115
116 }