c926c77a56be5a0330c9470d6d23ece5de53cdf7
[yangtools.git] / code-generator / binding-generator-impl / src / test / java / org / opendaylight / yangtools / sal / binding / generator / impl / BindingGeneratorImplTest.java
1 /*
2  * Copyright (c) 2015 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 import java.io.File;
13 import java.io.IOException;
14 import java.net.URISyntaxException;
15 import java.util.List;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
18 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
19 import org.opendaylight.yangtools.sal.binding.model.api.Type;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
22 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
23
24 public class BindingGeneratorImplTest {
25
26     @Test
27     public void choiceNodeGenerationTest() throws IOException,
28             YangSyntaxErrorException, URISyntaxException {
29         File resourceFile = new File(getClass().getResource(
30                 "/binding-generator-impl-test/choice-test.yang").toURI());
31         File resourceDir = resourceFile.getParentFile();
32
33         YangParserImpl parser = YangParserImpl.getInstance();
34         SchemaContext context = parser.parseFile(resourceFile, resourceDir);
35
36         List<Type> generateTypes = new BindingGeneratorImpl(false)
37                 .generateTypes(context);
38
39         GeneratedType choiceTestData = null;
40         GeneratedType myRootContainer = null;
41         GeneratedType myList = null;
42         GeneratedType myContainer = null;
43         GeneratedType myList2 = null;
44         GeneratedType myContainer2 = null;
45
46         for (Type type : generateTypes) {
47             switch (type.getName()) {
48             case "ChoiceTestData":
49                 choiceTestData = (GeneratedType) type;
50                 break;
51             case "Myrootcontainer":
52                 myRootContainer = (GeneratedType) type;
53                 break;
54             case "Mylist":
55                 myList = (GeneratedType) type;
56                 break;
57             case "Mylist2":
58                 myList2 = (GeneratedType) type;
59                 break;
60             case "Mycontainer":
61                 myContainer = (GeneratedType) type;
62                 break;
63             case "Mycontainer2":
64                 myContainer2 = (GeneratedType) type;
65                 break;
66             }
67         }
68
69         assertNotNull(choiceTestData);
70         assertNotNull(myRootContainer);
71         assertNotNull(myList);
72         assertNotNull(myContainer);
73         assertNotNull(myList2);
74         assertNotNull(myContainer2);
75
76         List<Type> implements1 = myContainer.getImplements();
77         Type childOfParamType = null;
78         for (Type type : implements1) {
79             if (type.getName().equals("ChildOf")) {
80                 childOfParamType = ((ParameterizedType) type)
81                         .getActualTypeArguments()[0];
82                 break;
83             }
84         }
85         assertNotNull(childOfParamType);
86         assertTrue(childOfParamType.getName().equals("ChoiceTestData"));
87
88         implements1 = myList.getImplements();
89         childOfParamType = null;
90         for (Type type : implements1) {
91             if (type.getName().equals("ChildOf")) {
92                 childOfParamType = ((ParameterizedType) type)
93                         .getActualTypeArguments()[0];
94                 break;
95             }
96         }
97         assertNotNull(childOfParamType);
98         assertTrue(childOfParamType.getName().equals("ChoiceTestData"));
99
100         implements1 = myContainer2.getImplements();
101         childOfParamType = null;
102         for (Type type : implements1) {
103             if (type.getName().equals("ChildOf")) {
104                 childOfParamType = ((ParameterizedType) type)
105                         .getActualTypeArguments()[0];
106                 break;
107             }
108         }
109         assertNotNull(childOfParamType);
110         assertTrue(childOfParamType.getName().equals("Myrootcontainer"));
111
112         implements1 = myList2.getImplements();
113         childOfParamType = null;
114         for (Type type : implements1) {
115             if (type.getName().equals("ChildOf")) {
116                 childOfParamType = ((ParameterizedType) type)
117                         .getActualTypeArguments()[0];
118                 break;
119             }
120         }
121         assertNotNull(childOfParamType);
122         assertTrue(childOfParamType.getName().equals("Myrootcontainer"));
123
124     }
125
126 }