Remove @Deprecated ClassLoaderUtils as it's now in yangtools.util
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / yangtools / sal / binding / generator / impl / stmt / parser / retest / 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.stmt.parser.retest;
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 import org.opendaylight.yangtools.sal.binding.generator.impl.BindingGeneratorImpl;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
16 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
17 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
18 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
19 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
20 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
21 import java.io.File;
22 import java.io.IOException;
23 import java.net.URISyntaxException;
24 import java.util.List;
25 import org.junit.Test;
26 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
27 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
28 import org.opendaylight.yangtools.sal.binding.model.api.Type;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
31
32 public class BindingGeneratorImplTest {
33
34     private static final YangStatementSourceImpl NETWORK_TOPOLOGY_20131021 = new YangStatementSourceImpl(
35             "/isis-topology/network-topology@2013-10-21.yang", false);
36
37     private static final YangStatementSourceImpl ISIS_20131021 = new YangStatementSourceImpl(
38             "/isis-topology/isis-topology@2013-10-21.yang", false);
39
40     private static final YangStatementSourceImpl L3_20131021 = new YangStatementSourceImpl(
41             "/isis-topology/l3-unicast-igp-topology@2013-10-21.yang", false);
42
43     @Test
44     public void isisTotpologyStatementParserTest() throws IOException,
45             YangSyntaxErrorException, URISyntaxException, SourceException,
46             ReactorException {
47         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
48                 .newBuild();
49
50         reactor.addSources(ISIS_20131021, L3_20131021,
51                 NETWORK_TOPOLOGY_20131021);
52
53         EffectiveSchemaContext context = reactor.buildEffective();
54         assertNotNull(context);
55
56         List<Type> generateTypes = new BindingGeneratorImpl(false)
57                 .generateTypes(context);
58
59         assertFalse(generateTypes.isEmpty());
60     }
61
62     @Test
63     public void choiceNodeGenerationTest() throws IOException,
64             YangSyntaxErrorException, URISyntaxException, SourceException, ReactorException {
65         File resourceFile = new File(getClass().getResource(
66                 "/binding-generator-impl-test/choice-test.yang").toURI());
67
68         SchemaContext context = RetestUtils.parseYangSources(resourceFile);
69
70         List<Type> generateTypes = new BindingGeneratorImpl(false)
71                 .generateTypes(context);
72
73         GeneratedType choiceTestData = null;
74         GeneratedType myRootContainer = null;
75         GeneratedType myList = null;
76         GeneratedType myContainer = null;
77         GeneratedType myList2 = null;
78         GeneratedType myContainer2 = null;
79
80         for (Type type : generateTypes) {
81             switch (type.getName()) {
82             case "ChoiceTestData":
83                 choiceTestData = (GeneratedType) type;
84                 break;
85             case "Myrootcontainer":
86                 myRootContainer = (GeneratedType) type;
87                 break;
88             case "Mylist":
89                 myList = (GeneratedType) type;
90                 break;
91             case "Mylist2":
92                 myList2 = (GeneratedType) type;
93                 break;
94             case "Mycontainer":
95                 myContainer = (GeneratedType) type;
96                 break;
97             case "Mycontainer2":
98                 myContainer2 = (GeneratedType) type;
99                 break;
100             }
101         }
102
103         assertNotNull(choiceTestData);
104         assertNotNull(myRootContainer);
105         assertNotNull(myList);
106         assertNotNull(myContainer);
107         assertNotNull(myList2);
108         assertNotNull(myContainer2);
109
110         List<Type> implements1 = myContainer.getImplements();
111         Type childOfParamType = null;
112         for (Type type : implements1) {
113             if (type.getName().equals("ChildOf")) {
114                 childOfParamType = ((ParameterizedType) type)
115                         .getActualTypeArguments()[0];
116                 break;
117             }
118         }
119         assertNotNull(childOfParamType);
120         assertTrue(childOfParamType.getName().equals("ChoiceTestData"));
121
122         implements1 = myList.getImplements();
123         childOfParamType = null;
124         for (Type type : implements1) {
125             if (type.getName().equals("ChildOf")) {
126                 childOfParamType = ((ParameterizedType) type)
127                         .getActualTypeArguments()[0];
128                 break;
129             }
130         }
131         assertNotNull(childOfParamType);
132         assertTrue(childOfParamType.getName().equals("ChoiceTestData"));
133
134         implements1 = myContainer2.getImplements();
135         childOfParamType = null;
136         for (Type type : implements1) {
137             if (type.getName().equals("ChildOf")) {
138                 childOfParamType = ((ParameterizedType) type)
139                         .getActualTypeArguments()[0];
140                 break;
141             }
142         }
143         assertNotNull(childOfParamType);
144         assertTrue(childOfParamType.getName().equals("Myrootcontainer"));
145
146         implements1 = myList2.getImplements();
147         childOfParamType = null;
148         for (Type type : implements1) {
149             if (type.getName().equals("ChildOf")) {
150                 childOfParamType = ((ParameterizedType) type)
151                         .getActualTypeArguments()[0];
152                 break;
153             }
154         }
155         assertNotNull(childOfParamType);
156         assertTrue(childOfParamType.getName().equals("Myrootcontainer"));
157
158     }
159
160     @Test
161     public void notificationGenerationTest() throws IOException,
162             YangSyntaxErrorException, URISyntaxException, SourceException, ReactorException {
163         File resourceFile = new File(getClass().getResource(
164                 "/binding-generator-impl-test/notification-test.yang").toURI());
165
166         SchemaContext context = RetestUtils.parseYangSources(resourceFile);
167
168         List<Type> generateTypes = new BindingGeneratorImpl(false)
169                 .generateTypes(context);
170
171         GeneratedType foo = null;
172         for (Type type : generateTypes) {
173             if (type.getName().equals("Foo")) {
174                 foo = (GeneratedType) type;
175                 break;
176             }
177         }
178
179         Type childOf = null;
180         Type dataObject = null;
181         List<Type> impl = foo.getImplements();
182         for (Type type : impl) {
183             switch (type.getName()) {
184             case "ChildOf":
185                 childOf = type;
186                 break;
187             case "DataObject":
188                 dataObject = type;
189                 break;
190             }
191         }
192
193         assertNull(childOf);
194         assertNotNull(dataObject);
195     }
196
197 }