2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.sal.binding.generator.impl;
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;
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;
33 public class BindingGeneratorImplTest {
35 private static final YangStatementSourceImpl NETWORK_TOPOLOGY_20131021 = new YangStatementSourceImpl(
36 "/isis-topology/network-topology@2013-10-21.yang", false);
38 private static final YangStatementSourceImpl ISIS_20131021 = new YangStatementSourceImpl(
39 "/isis-topology/isis-topology@2013-10-21.yang", false);
41 private static final YangStatementSourceImpl L3_20131021 = new YangStatementSourceImpl(
42 "/isis-topology/l3-unicast-igp-topology@2013-10-21.yang", false);
45 public void isisTotpologyStatementParserTest() throws IOException,
46 YangSyntaxErrorException, URISyntaxException, SourceException,
48 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
51 reactor.addSources(ISIS_20131021, L3_20131021,
52 NETWORK_TOPOLOGY_20131021);
54 EffectiveSchemaContext context = reactor.buildEffective();
55 assertNotNull(context);
57 List<Type> generateTypes = new BindingGeneratorImpl(false)
58 .generateTypes(context);
60 assertFalse(generateTypes.isEmpty());
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());
69 SchemaContext context = YangParserTestUtils.parseYangSources(resourceFile);
71 List<Type> generateTypes = new BindingGeneratorImpl(false)
72 .generateTypes(context);
74 GeneratedType choiceTestData = null;
75 GeneratedType myRootContainer = null;
76 GeneratedType myList = null;
77 GeneratedType myContainer = null;
78 GeneratedType myList2 = null;
79 GeneratedType myContainer2 = null;
81 for (Type type : generateTypes) {
82 switch (type.getName()) {
83 case "ChoiceTestData":
84 choiceTestData = (GeneratedType) type;
86 case "Myrootcontainer":
87 myRootContainer = (GeneratedType) type;
90 myList = (GeneratedType) type;
93 myList2 = (GeneratedType) type;
96 myContainer = (GeneratedType) type;
99 myContainer2 = (GeneratedType) type;
104 assertNotNull(choiceTestData);
105 assertNotNull(myRootContainer);
106 assertNotNull(myList);
107 assertNotNull(myContainer);
108 assertNotNull(myList2);
109 assertNotNull(myContainer2);
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];
120 assertNotNull(childOfParamType);
121 assertTrue(childOfParamType.getName().equals("ChoiceTestData"));
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];
132 assertNotNull(childOfParamType);
133 assertTrue(childOfParamType.getName().equals("ChoiceTestData"));
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];
144 assertNotNull(childOfParamType);
145 assertTrue(childOfParamType.getName().equals("Myrootcontainer"));
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];
156 assertNotNull(childOfParamType);
157 assertTrue(childOfParamType.getName().equals("Myrootcontainer"));
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());
167 SchemaContext context = YangParserTestUtils.parseYangSources(resourceFile);
169 List<Type> generateTypes = new BindingGeneratorImpl(false)
170 .generateTypes(context);
172 GeneratedType foo = null;
173 for (Type type : generateTypes) {
174 if (type.getName().equals("Foo")) {
175 foo = (GeneratedType) type;
181 Type dataObject = null;
182 List<Type> impl = foo.getImplements();
183 for (Type type : impl) {
184 switch (type.getName()) {
195 assertNotNull(dataObject);