b40af545987693637bf714c4bdc24b58f6bbccae
[mdsal.git] / binding / mdsal-binding-generator / 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.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13
14 import java.util.List;
15 import org.junit.Test;
16 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
17 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
18 import org.opendaylight.mdsal.binding.model.api.Type;
19 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
20
21 public class BindingGeneratorImplTest {
22     @Test
23     public void isisTotpologyStatementParserTest()  {
24         List<GeneratedType> generateTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResources(
25             BindingGeneratorImplTest.class,
26             "/isis-topology/network-topology@2013-10-21.yang", "/isis-topology/isis-topology@2013-10-21.yang",
27             "/isis-topology/l3-unicast-igp-topology@2013-10-21.yang"));
28         assertEquals(9, generateTypes.size());
29     }
30
31     @Test
32     public void choiceNodeGenerationTest() {
33         List<GeneratedType> generateTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
34                 "/binding-generator-impl-test/choice-test.yang"));
35
36         GeneratedType choiceTestData = null;
37         GeneratedType myRootContainer = null;
38         GeneratedType myList = null;
39         GeneratedType myContainer = null;
40         GeneratedType myList2 = null;
41         GeneratedType myContainer2 = null;
42
43         for (GeneratedType type : generateTypes) {
44             switch (type.getName()) {
45                 case "ChoiceTestData":
46                     choiceTestData = type;
47                     break;
48                 case "Myrootcontainer":
49                     myRootContainer = type;
50                     break;
51                 case "Mylist":
52                     myList = type;
53                     break;
54                 case "Mylist2":
55                     myList2 = type;
56                     break;
57                 case "Mycontainer":
58                     myContainer = type;
59                     break;
60                 case "Mycontainer2":
61                     myContainer2 = type;
62                     break;
63                 default:
64                     // ignore
65             }
66         }
67
68         assertNotNull(choiceTestData);
69         assertNotNull(myRootContainer);
70         assertNotNull(myList);
71         assertNotNull(myContainer);
72         assertNotNull(myList2);
73         assertNotNull(myContainer2);
74
75         Type childOfParamType = null;
76         for (Type type : myContainer.getImplements()) {
77             if (type.getName().equals("ChildOf")) {
78                 childOfParamType = ((ParameterizedType) type).getActualTypeArguments()[0];
79                 break;
80             }
81         }
82         assertNotNull(childOfParamType);
83         assertEquals("ChoiceTestData", childOfParamType.getName());
84
85         childOfParamType = null;
86         for (Type type : myList.getImplements()) {
87             if (type.getName().equals("ChildOf")) {
88                 childOfParamType = ((ParameterizedType) type).getActualTypeArguments()[0];
89                 break;
90             }
91         }
92         assertNotNull(childOfParamType);
93         assertEquals("ChoiceTestData", childOfParamType.getName());
94
95         childOfParamType = null;
96         for (Type type : myContainer2.getImplements()) {
97             if (type.getName().equals("ChildOf")) {
98                 childOfParamType = ((ParameterizedType) type).getActualTypeArguments()[0];
99                 break;
100             }
101         }
102         assertNotNull(childOfParamType);
103         assertEquals("Myrootcontainer", childOfParamType.getName());
104
105         childOfParamType = null;
106         for (Type type : myList2.getImplements()) {
107             if (type.getName().equals("ChildOf")) {
108                 childOfParamType = ((ParameterizedType) type).getActualTypeArguments()[0];
109                 break;
110             }
111         }
112         assertNotNull(childOfParamType);
113         assertEquals("Myrootcontainer", childOfParamType.getName());
114     }
115
116     @Test
117     public void notificationGenerationTest() {
118         List<GeneratedType> generateTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
119                 "/binding-generator-impl-test/notification-test.yang"));
120
121         GeneratedType foo = null;
122         for (GeneratedType type : generateTypes) {
123             if (type.getName().equals("Foo")) {
124                 foo = type;
125                 break;
126             }
127         }
128
129         Type childOf = null;
130         Type dataObject = null;
131         for (Type type :  foo.getImplements()) {
132             switch (type.getName()) {
133                 case "ChildOf":
134                     childOf = type;
135                     break;
136                 case "DataObject":
137                     dataObject = type;
138                     break;
139                 default:
140                     // ignore
141             }
142         }
143
144         assertNull(childOf);
145         assertNotNull(dataObject);
146     }
147
148     @Test
149     public void testBaseYangTypes() {
150         final List<GeneratedType> types = DefaultBindingGenerator.generateFor(
151             YangParserTestUtils.parseYangResource("/base-yang-types.yang"));
152         assertEquals(19, types.size());
153     }
154 }