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