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