70889494319f9235c82e4601bdc0874f24349ac3
[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.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
20 import org.junit.Test;
21 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
22 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
23 import org.opendaylight.yangtools.sal.binding.model.api.Type;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
26 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
28 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
29 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
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         SchemaContext 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 {
66         File resourceFile = new File(getClass().getResource(
67                 "/binding-generator-impl-test/choice-test.yang").toURI());
68         File resourceDir = resourceFile.getParentFile();
69
70         YangParserImpl parser = YangParserImpl.getInstance();
71         SchemaContext context = parser.parseFile(resourceFile, resourceDir);
72
73         List<Type> generateTypes = new BindingGeneratorImpl(false)
74                 .generateTypes(context);
75
76         GeneratedType choiceTestData = null;
77         GeneratedType myRootContainer = null;
78         GeneratedType myList = null;
79         GeneratedType myContainer = null;
80         GeneratedType myList2 = null;
81         GeneratedType myContainer2 = null;
82
83         for (Type type : generateTypes) {
84             switch (type.getName()) {
85             case "ChoiceTestData":
86                 choiceTestData = (GeneratedType) type;
87                 break;
88             case "Myrootcontainer":
89                 myRootContainer = (GeneratedType) type;
90                 break;
91             case "Mylist":
92                 myList = (GeneratedType) type;
93                 break;
94             case "Mylist2":
95                 myList2 = (GeneratedType) type;
96                 break;
97             case "Mycontainer":
98                 myContainer = (GeneratedType) type;
99                 break;
100             case "Mycontainer2":
101                 myContainer2 = (GeneratedType) type;
102                 break;
103             }
104         }
105
106         assertNotNull(choiceTestData);
107         assertNotNull(myRootContainer);
108         assertNotNull(myList);
109         assertNotNull(myContainer);
110         assertNotNull(myList2);
111         assertNotNull(myContainer2);
112
113         List<Type> implements1 = myContainer.getImplements();
114         Type 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("ChoiceTestData"));
124
125         implements1 = myList.getImplements();
126         childOfParamType = null;
127         for (Type type : implements1) {
128             if (type.getName().equals("ChildOf")) {
129                 childOfParamType = ((ParameterizedType) type)
130                         .getActualTypeArguments()[0];
131                 break;
132             }
133         }
134         assertNotNull(childOfParamType);
135         assertTrue(childOfParamType.getName().equals("ChoiceTestData"));
136
137         implements1 = myContainer2.getImplements();
138         childOfParamType = null;
139         for (Type type : implements1) {
140             if (type.getName().equals("ChildOf")) {
141                 childOfParamType = ((ParameterizedType) type)
142                         .getActualTypeArguments()[0];
143                 break;
144             }
145         }
146         assertNotNull(childOfParamType);
147         assertTrue(childOfParamType.getName().equals("Myrootcontainer"));
148
149         implements1 = myList2.getImplements();
150         childOfParamType = null;
151         for (Type type : implements1) {
152             if (type.getName().equals("ChildOf")) {
153                 childOfParamType = ((ParameterizedType) type)
154                         .getActualTypeArguments()[0];
155                 break;
156             }
157         }
158         assertNotNull(childOfParamType);
159         assertTrue(childOfParamType.getName().equals("Myrootcontainer"));
160
161     }
162
163     @Test
164     public void notificationGenerationTest() throws IOException, YangSyntaxErrorException, URISyntaxException {
165         File resourceFile = new File(getClass().getResource("/binding-generator-impl-test/notification-test.yang")
166                 .toURI());
167         File resourceDir = resourceFile.getParentFile();
168
169         YangParserImpl parser = YangParserImpl.getInstance();
170         SchemaContext context = parser.parseFile(resourceFile, resourceDir);
171
172         List<Type> generateTypes = new BindingGeneratorImpl(false).generateTypes(context);
173
174         GeneratedType foo = null;
175         for (Type type : generateTypes) {
176             if (type.getName().equals("Foo")) {
177                 foo = (GeneratedType) type;
178                 break;
179             }
180         }
181
182         Type childOf = null;
183         Type dataObject = null;
184         List<Type> impl = foo.getImplements();
185         for (Type type : impl) {
186             switch (type.getName()) {
187             case "ChildOf":
188                 childOf = type;
189                 break;
190             case "DataObject":
191                 dataObject = type;
192                 break;
193             }
194         }
195
196         assertNull(childOf);
197         assertNotNull(dataObject);
198     }
199
200 }