Introduce top-level pom file.
[mdsal.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.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12
13 import java.io.File;
14 import java.io.IOException;
15 import java.net.URISyntaxException;
16 import java.util.ArrayList;
17 import java.util.List;
18
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
22 import org.opendaylight.yangtools.sal.binding.model.api.Constant;
23 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
24 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
25 import org.opendaylight.yangtools.sal.binding.model.api.Type;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
28 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
29
30 public class GeneratedTypesStringTest {
31
32     private final static List<File> testModels = new ArrayList<File>();
33
34     @BeforeClass
35     public static void loadTestResources() throws URISyntaxException {
36         final File listModelFile = new File(GeneratedTypesStringTest.class.getResource("/simple-string-demo.yang")
37                 .toURI());
38         testModels.add(listModelFile);
39     }
40
41     @Test
42     public void constantGenerationTest() throws IOException {
43         final YangContextParser parser = new YangParserImpl();
44         final SchemaContext context = parser.parseFiles(testModels);
45
46         assertNotNull(context);
47         final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
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                         }
71                         ParameterizedType pType;
72                         if (con.getType() instanceof ParameterizedType) {
73                             pType = (ParameterizedType) con.getType();
74                         } else {
75                             break;
76                         }
77
78                         Type[] types;
79                         if (pType.getName().equals("List")) {
80                             constantRegExListTypeContainer = true;
81                             types = pType.getActualTypeArguments();
82                         } else {
83                             break;
84                         }
85
86                         if (types.length == 1) {
87                             constantRegExListTypeOneGeneric = true;
88                         } else {
89                             break;
90                         }
91
92                         if (types[0].getName().equals("String")) {
93                             constantRegExListTypeGeneric = true;
94                         } else {
95                             break;
96                         }
97
98                         if (con.getValue() instanceof List) {
99                             constantRegExListValueOK = true;
100                         } else {
101                             break;
102                         }
103
104                         for (Object obj : (List<?>) con.getValue()) {
105                             if (!(obj instanceof String)) {
106                                 noStringInReqExListFound = true;
107                                 break;
108                             }
109                         }
110
111                     }
112                 }
113             }
114
115         }
116
117         assertTrue("Typedef >>TypedefString<< wasn't found", typedefStringFound);
118         assertTrue("Constant PATTERN_CONSTANTS is missing in TO", constantRegExListFound);
119         assertTrue("Constant PATTERN_CONSTANTS doesn't have correct container type", constantRegExListTypeContainer);
120         assertTrue("Constant PATTERN_CONSTANTS has more than one generic type", constantRegExListTypeOneGeneric);
121         assertTrue("Constant PATTERN_CONSTANTS doesn't have correct generic type", constantRegExListTypeGeneric);
122         assertTrue("Constant PATTERN_CONSTANTS doesn't contain List object", constantRegExListValueOK);
123         assertTrue("In list found other type than String", !noStringInReqExListFound);
124
125     }
126
127 }