2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
8 package org.opendaylight.yangtools.sal.binding.generator.impl;
\r
10 import static org.junit.Assert.*;
\r
12 import java.io.File;
\r
13 import java.util.ArrayList;
\r
14 import java.util.List;
\r
15 import java.util.Set;
\r
17 import org.junit.Test;
\r
18 import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl;
\r
19 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
\r
20 import org.opendaylight.yangtools.sal.binding.model.api.Enumeration;
\r
21 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
\r
22 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
\r
23 import org.opendaylight.yangtools.sal.binding.model.api.Type;
\r
24 import org.opendaylight.yangtools.yang.model.api.Module;
\r
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
\r
26 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
\r
27 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
\r
29 public class GenEnumResolvingTest {
\r
31 private SchemaContext resolveSchemaContextFromFiles(
\r
32 final String... yangFiles) {
\r
33 final YangModelParser parser = new YangParserImpl();
\r
35 final List<File> inputFiles = new ArrayList<File>();
\r
36 for (int i = 0; i < yangFiles.length; ++i) {
\r
37 inputFiles.add(new File(yangFiles[i]));
\r
40 final Set<Module> modules = parser.parseYangModels(inputFiles);
\r
41 return parser.resolveSchemaContext(modules);
\r
45 public void testLeafEnumResolving() {
\r
46 final String ietfInterfacesPath = getClass().getResource(
\r
47 "/enum-test-models/ietf-interfaces@2012-11-15.yang").getPath();
\r
48 final String ifTypePath = getClass().getResource(
\r
49 "/enum-test-models/iana-if-type@2012-06-05.yang").getPath();
\r
50 final String yangTypesPath = getClass().getResource(
\r
51 "/enum-test-models/ietf-yang-types@2010-09-24.yang").getPath();
\r
53 final SchemaContext context = resolveSchemaContextFromFiles(
\r
54 ietfInterfacesPath, ifTypePath, yangTypesPath);
\r
55 assertTrue(context != null);
\r
57 final BindingGenerator bindingGen = new BindingGeneratorImpl();
\r
58 final List<Type> genTypes = bindingGen.generateTypes(context);
\r
59 assertTrue(genTypes != null);
\r
61 assertEquals("Expected count of all Generated Types", 20, genTypes.size());
\r
63 GeneratedType genInterface = null;
\r
64 for (final Type type : genTypes) {
\r
65 if (type instanceof GeneratedType) {
\r
66 if (type.getName().equals("Interface")) {
\r
67 genInterface = (GeneratedType) type;
\r
71 assertNotNull("Generated Type Interface is not present in list of " +
\r
72 "Generated Types", genInterface);
\r
74 Enumeration linkUpDownTrapEnable = null;
\r
75 Enumeration operStatus = null;
\r
76 final List<Enumeration> enums = genInterface.getEnumerations();
\r
77 assertNotNull("Generated Type Interface cannot contain NULL reference" +
\r
78 " to Enumeration types!", enums);
\r
79 assertEquals("Generated Type Interface MUST contain 2 Enumeration " +
\r
80 "Types", 2, enums.size());
\r
81 for (final Enumeration e : enums) {
\r
82 if (e.getName().equals("LinkUpDownTrapEnable")) {
\r
83 linkUpDownTrapEnable = e;
\r
84 } else if (e.getName().equals("OperStatus")) {
\r
89 assertNotNull("Expected Enum LinkUpDownTrapEnable, but was NULL!",
\r
90 linkUpDownTrapEnable);
\r
91 assertNotNull("Expected Enum OperStatus, but was NULL!", operStatus);
\r
93 assertNotNull("Enum LinkUpDownTrapEnable MUST contain Values definition " +
\r
94 "not NULL reference!", linkUpDownTrapEnable.getValues());
\r
95 assertNotNull("Enum OperStatus MUST contain Values definition not " +
\r
96 "NULL reference!", operStatus.getValues());
\r
97 assertEquals("Enum LinkUpDownTrapEnable MUST contain 2 values!", 2,
\r
98 linkUpDownTrapEnable.getValues().size());
\r
99 assertEquals("Enum OperStatus MUST contain 7 values!", 7,
\r
100 operStatus.getValues().size());
\r
102 final List<MethodSignature> methods = genInterface
\r
103 .getMethodDefinitions();
\r
105 assertNotNull("Generated Interface cannot contain NULL reference for " +
\r
106 "Method Signature Definitions!", methods);
\r
108 assertEquals("Expected count of method signature definitions is 14",
\r
109 14, methods.size());
\r
110 Enumeration ianaIfType = null;
\r
111 for (final MethodSignature method : methods) {
\r
112 if (method.getName().equals("getType")) {
\r
113 if (method.getReturnType() instanceof Enumeration) {
\r
114 ianaIfType = (Enumeration)method.getReturnType();
\r
119 assertNotNull("Method getType MUST return Enumeration Type, " +
\r
120 "not NULL reference!", ianaIfType);
\r
121 assertEquals("Enumeration getType MUST contain 272 values!", 272,
\r
122 ianaIfType.getValues().size());
\r
126 public void testTypedefEnumResolving() {
\r
127 final String ianaIfTypePath = getClass().getResource(
\r
128 "/leafref-test-models/iana-if-type@2012-06-05.yang").getPath();
\r
130 final SchemaContext context = resolveSchemaContextFromFiles(ianaIfTypePath);
\r
131 assertTrue(context != null);
\r
132 final BindingGenerator bindingGen = new BindingGeneratorImpl();
\r
133 final List<Type> genTypes = bindingGen.generateTypes(context);
\r
134 assertTrue(genTypes != null);
\r
135 assertEquals(1, genTypes.size());
\r
137 final Type type = genTypes.get(0);
\r
138 assertTrue(type instanceof Enumeration);
\r
140 final Enumeration enumer = (Enumeration) type;
\r
141 assertEquals("Enumeration type MUST contain 272 values!", 272,
\r
142 enumer.getValues().size());
\r
146 public void testLeafrefEnumResolving() {
\r
147 final String ietfInterfacesPath = getClass().getResource(
\r
148 "/enum-test-models/ietf-interfaces@2012-11-15.yang").getPath();
\r
149 final String ifTypePath = getClass().getResource(
\r
150 "/enum-test-models/iana-if-type@2012-06-05.yang").getPath();
\r
151 final String yangTypesPath = getClass().getResource(
\r
152 "/enum-test-models/ietf-yang-types@2010-09-24.yang").getPath();
\r
153 final String topologyPath = getClass().getResource(
\r
154 "/enum-test-models/abstract-topology@2013-02-08.yang")
\r
156 final String inetTypesPath = getClass().getResource(
\r
157 "/enum-test-models/ietf-inet-types@2010-09-24.yang")
\r
159 final SchemaContext context = resolveSchemaContextFromFiles(
\r
160 ietfInterfacesPath, ifTypePath, yangTypesPath, topologyPath,
\r
163 assertNotNull(context);
\r
164 final BindingGenerator bindingGen = new BindingGeneratorImpl();
\r
165 final List<Type> genTypes = bindingGen.generateTypes(context);
\r
166 assertNotNull(genTypes);
\r
167 assertTrue(!genTypes.isEmpty());
\r
169 GeneratedType genInterface = null;
\r
170 for (final Type type : genTypes) {
\r
171 if (type instanceof GeneratedType) {
\r
172 if (type.getPackageName().equals("org.opendaylight.yang.gen.v1.urn.model._abstract.topology.rev201328.topology.interfaces")
\r
173 && type.getName().equals("Interface")) {
\r
174 genInterface = (GeneratedType) type;
\r
178 assertNotNull("Generated Type Interface is not present in list of " +
\r
179 "Generated Types", genInterface);
\r
181 Type linkUpDownTrapEnable = null;
\r
182 Type operStatus = null;
\r
183 final List<MethodSignature> methods = genInterface.getMethodDefinitions();
\r
184 assertNotNull("Generated Type Interface cannot contain NULL reference" +
\r
185 " to Enumeration types!", methods);
\r
186 assertEquals("Generated Type Interface MUST contain 4 Methods ",
\r
187 4, methods.size());
\r
188 for (final MethodSignature method : methods) {
\r
189 if (method.getName().equals("getLinkUpDownTrapEnable")) {
\r
190 linkUpDownTrapEnable = method.getReturnType();
\r
191 } else if (method.getName().equals("getOperStatus")) {
\r
192 operStatus = method.getReturnType();
\r
196 assertNotNull("Expected Referenced Enum LinkUpDownTrapEnable, but was NULL!",
\r
197 linkUpDownTrapEnable);
\r
198 assertTrue("Expected LinkUpDownTrapEnable of type ReferencedTypeImpl",
\r
199 linkUpDownTrapEnable instanceof ReferencedTypeImpl);
\r
200 assertEquals(linkUpDownTrapEnable.getPackageName(),
\r
201 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev20121115.interfaces.Interface");
\r
203 assertNotNull("Expected Referenced Enum OperStatus, but was NULL!",
\r
205 assertTrue("Expected OperStatus of type ReferencedTypeImpl",
\r
206 operStatus instanceof ReferencedTypeImpl);
\r
207 assertEquals(operStatus.getPackageName(),
\r
208 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev20121115.interfaces.Interface");
\r