BUG-7435: eliminate use of yang-parser-impl internals
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / yangtools / sal / binding / generator / impl / BindingGeneratorImplTest.java
1 /*
2  * Copyright (c) 2016 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.assertFalse;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14
15 import com.google.common.collect.ImmutableList;
16 import java.io.File;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.net.URISyntaxException;
20 import java.util.List;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
23 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
24 import org.opendaylight.yangtools.sal.binding.model.api.Type;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28
29 public class BindingGeneratorImplTest {
30
31     @Test
32     public void isisTotpologyStatementParserTest() throws IOException,
33             URISyntaxException, ReactorException {
34         final InputStream topo = getClass().getResourceAsStream("/isis-topology/network-topology@2013-10-21.yang");
35         final InputStream isis = getClass().getResourceAsStream("/isis-topology/isis-topology@2013-10-21.yang");
36         final InputStream l3 = getClass().getResourceAsStream("/isis-topology/l3-unicast-igp-topology@2013-10-21.yang");
37
38         SchemaContext context = YangParserTestUtils.parseYangStreams(ImmutableList.of(isis, l3, topo));
39         assertNotNull(context);
40
41         List<Type> generateTypes = new BindingGeneratorImpl(false)
42                 .generateTypes(context);
43
44         assertFalse(generateTypes.isEmpty());
45     }
46
47     @Test
48     public void choiceNodeGenerationTest() throws IOException,
49             URISyntaxException, ReactorException {
50         File resourceFile = new File(getClass().getResource(
51                 "/binding-generator-impl-test/choice-test.yang").toURI());
52
53         SchemaContext context = YangParserTestUtils.parseYangSources(resourceFile);
54
55         List<Type> generateTypes = new BindingGeneratorImpl(false)
56                 .generateTypes(context);
57
58         GeneratedType choiceTestData = null;
59         GeneratedType myRootContainer = null;
60         GeneratedType myList = null;
61         GeneratedType myContainer = null;
62         GeneratedType myList2 = null;
63         GeneratedType myContainer2 = null;
64
65         for (Type type : generateTypes) {
66             switch (type.getName()) {
67             case "ChoiceTestData":
68                 choiceTestData = (GeneratedType) type;
69                 break;
70             case "Myrootcontainer":
71                 myRootContainer = (GeneratedType) type;
72                 break;
73             case "Mylist":
74                 myList = (GeneratedType) type;
75                 break;
76             case "Mylist2":
77                 myList2 = (GeneratedType) type;
78                 break;
79             case "Mycontainer":
80                 myContainer = (GeneratedType) type;
81                 break;
82             case "Mycontainer2":
83                 myContainer2 = (GeneratedType) type;
84                 break;
85             }
86         }
87
88         assertNotNull(choiceTestData);
89         assertNotNull(myRootContainer);
90         assertNotNull(myList);
91         assertNotNull(myContainer);
92         assertNotNull(myList2);
93         assertNotNull(myContainer2);
94
95         List<Type> implements1 = myContainer.getImplements();
96         Type childOfParamType = null;
97         for (Type type : implements1) {
98             if (type.getName().equals("ChildOf")) {
99                 childOfParamType = ((ParameterizedType) type)
100                         .getActualTypeArguments()[0];
101                 break;
102             }
103         }
104         assertNotNull(childOfParamType);
105         assertTrue(childOfParamType.getName().equals("ChoiceTestData"));
106
107         implements1 = myList.getImplements();
108         childOfParamType = null;
109         for (Type type : implements1) {
110             if (type.getName().equals("ChildOf")) {
111                 childOfParamType = ((ParameterizedType) type)
112                         .getActualTypeArguments()[0];
113                 break;
114             }
115         }
116         assertNotNull(childOfParamType);
117         assertTrue(childOfParamType.getName().equals("ChoiceTestData"));
118
119         implements1 = myContainer2.getImplements();
120         childOfParamType = null;
121         for (Type type : implements1) {
122             if (type.getName().equals("ChildOf")) {
123                 childOfParamType = ((ParameterizedType) type)
124                         .getActualTypeArguments()[0];
125                 break;
126             }
127         }
128         assertNotNull(childOfParamType);
129         assertTrue(childOfParamType.getName().equals("Myrootcontainer"));
130
131         implements1 = myList2.getImplements();
132         childOfParamType = null;
133         for (Type type : implements1) {
134             if (type.getName().equals("ChildOf")) {
135                 childOfParamType = ((ParameterizedType) type)
136                         .getActualTypeArguments()[0];
137                 break;
138             }
139         }
140         assertNotNull(childOfParamType);
141         assertTrue(childOfParamType.getName().equals("Myrootcontainer"));
142
143     }
144
145     @Test
146     public void notificationGenerationTest() throws IOException, URISyntaxException, ReactorException {
147         File resourceFile = new File(getClass().getResource(
148                 "/binding-generator-impl-test/notification-test.yang").toURI());
149
150         SchemaContext context = YangParserTestUtils.parseYangSources(resourceFile);
151
152         List<Type> generateTypes = new BindingGeneratorImpl(false)
153                 .generateTypes(context);
154
155         GeneratedType foo = null;
156         for (Type type : generateTypes) {
157             if (type.getName().equals("Foo")) {
158                 foo = (GeneratedType) type;
159                 break;
160             }
161         }
162
163         Type childOf = null;
164         Type dataObject = null;
165         List<Type> impl = foo.getImplements();
166         for (Type type : impl) {
167             switch (type.getName()) {
168             case "ChildOf":
169                 childOf = type;
170                 break;
171             case "DataObject":
172                 dataObject = type;
173                 break;
174             }
175         }
176
177         assertNull(childOf);
178         assertNotNull(dataObject);
179     }
180
181 }