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