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