Introduce top-level pom file.
[mdsal.git] / binding / mdsal-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.assertNull;
12 import static org.junit.Assert.assertTrue;
13 import java.io.File;
14 import java.io.IOException;
15 import java.net.URISyntaxException;
16 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
19 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
20 import org.opendaylight.yangtools.sal.binding.model.api.Type;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
23 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
24
25 public class BindingGeneratorImplTest {
26
27     @Test
28     public void choiceNodeGenerationTest() throws IOException,
29             YangSyntaxErrorException, URISyntaxException {
30         File resourceFile = new File(getClass().getResource(
31                 "/binding-generator-impl-test/choice-test.yang").toURI());
32         File resourceDir = resourceFile.getParentFile();
33
34         YangParserImpl parser = YangParserImpl.getInstance();
35         SchemaContext context = parser.parseFile(resourceFile, resourceDir);
36
37         List<Type> generateTypes = new BindingGeneratorImpl(false)
38                 .generateTypes(context);
39
40         GeneratedType choiceTestData = null;
41         GeneratedType myRootContainer = null;
42         GeneratedType myList = null;
43         GeneratedType myContainer = null;
44         GeneratedType myList2 = null;
45         GeneratedType myContainer2 = null;
46
47         for (Type type : generateTypes) {
48             switch (type.getName()) {
49             case "ChoiceTestData":
50                 choiceTestData = (GeneratedType) type;
51                 break;
52             case "Myrootcontainer":
53                 myRootContainer = (GeneratedType) type;
54                 break;
55             case "Mylist":
56                 myList = (GeneratedType) type;
57                 break;
58             case "Mylist2":
59                 myList2 = (GeneratedType) type;
60                 break;
61             case "Mycontainer":
62                 myContainer = (GeneratedType) type;
63                 break;
64             case "Mycontainer2":
65                 myContainer2 = (GeneratedType) type;
66                 break;
67             }
68         }
69
70         assertNotNull(choiceTestData);
71         assertNotNull(myRootContainer);
72         assertNotNull(myList);
73         assertNotNull(myContainer);
74         assertNotNull(myList2);
75         assertNotNull(myContainer2);
76
77         List<Type> implements1 = myContainer.getImplements();
78         Type childOfParamType = null;
79         for (Type type : implements1) {
80             if (type.getName().equals("ChildOf")) {
81                 childOfParamType = ((ParameterizedType) type)
82                         .getActualTypeArguments()[0];
83                 break;
84             }
85         }
86         assertNotNull(childOfParamType);
87         assertTrue(childOfParamType.getName().equals("ChoiceTestData"));
88
89         implements1 = myList.getImplements();
90         childOfParamType = null;
91         for (Type type : implements1) {
92             if (type.getName().equals("ChildOf")) {
93                 childOfParamType = ((ParameterizedType) type)
94                         .getActualTypeArguments()[0];
95                 break;
96             }
97         }
98         assertNotNull(childOfParamType);
99         assertTrue(childOfParamType.getName().equals("ChoiceTestData"));
100
101         implements1 = myContainer2.getImplements();
102         childOfParamType = null;
103         for (Type type : implements1) {
104             if (type.getName().equals("ChildOf")) {
105                 childOfParamType = ((ParameterizedType) type)
106                         .getActualTypeArguments()[0];
107                 break;
108             }
109         }
110         assertNotNull(childOfParamType);
111         assertTrue(childOfParamType.getName().equals("Myrootcontainer"));
112
113         implements1 = myList2.getImplements();
114         childOfParamType = null;
115         for (Type type : implements1) {
116             if (type.getName().equals("ChildOf")) {
117                 childOfParamType = ((ParameterizedType) type)
118                         .getActualTypeArguments()[0];
119                 break;
120             }
121         }
122         assertNotNull(childOfParamType);
123         assertTrue(childOfParamType.getName().equals("Myrootcontainer"));
124
125     }
126
127     @Test
128     public void notificationGenerationTest() throws IOException, YangSyntaxErrorException, URISyntaxException {
129         File resourceFile = new File(getClass().getResource("/binding-generator-impl-test/notification-test.yang")
130                 .toURI());
131         File resourceDir = resourceFile.getParentFile();
132
133         YangParserImpl parser = YangParserImpl.getInstance();
134         SchemaContext context = parser.parseFile(resourceFile, resourceDir);
135
136         List<Type> generateTypes = new BindingGeneratorImpl(false).generateTypes(context);
137
138         GeneratedType foo = null;
139         for (Type type : generateTypes) {
140             if (type.getName().equals("Foo")) {
141                 foo = (GeneratedType) type;
142                 break;
143             }
144         }
145
146         Type childOf = null;
147         Type dataObject = null;
148         List<Type> impl = foo.getImplements();
149         for (Type type : impl) {
150             switch (type.getName()) {
151             case "ChildOf":
152                 childOf = type;
153                 break;
154             case "DataObject":
155                 dataObject = type;
156                 break;
157             }
158         }
159
160         assertNull(childOf);
161         assertNotNull(dataObject);
162     }
163
164 }