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.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
15 import java.io.IOException;
16 import java.net.URISyntaxException;
17 import java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
20 import org.opendaylight.yangtools.sal.binding.model.api.Enumeration;
21 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
22 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
23 import org.opendaylight.yangtools.sal.binding.model.api.Type;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
28 public class GenEnumResolvingTest {
31 public void testLeafEnumResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
32 File ietfInterfaces = new File(getClass().getResource("/enum-test-models/ietf-interfaces@2012-11-15.yang")
34 File ianaIfTypeModel = new File(getClass().getResource("/ietf/iana-if-type.yang").toURI());
36 final SchemaContext context = TestUtils.parseYangSources(ietfInterfaces, ianaIfTypeModel);
37 assertTrue(context != null);
39 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
40 final List<Type> genTypes = bindingGen.generateTypes(context);
41 assertTrue(genTypes != null);
43 assertEquals("Expected count of all Generated Types", 6, genTypes.size());
45 GeneratedType genInterface = null;
46 for (final Type type : genTypes) {
47 if (type instanceof GeneratedType) {
48 if (type.getName().equals("Interface")) {
49 genInterface = (GeneratedType) type;
53 assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
55 Enumeration linkUpDownTrapEnable = null;
56 Enumeration operStatus = null;
57 final List<Enumeration> enums = genInterface.getEnumerations();
58 assertNotNull("Generated Type Interface cannot contain NULL reference to Enumeration types!", enums);
59 assertEquals("Generated Type Interface MUST contain 3 Enumeration Types", 3, enums.size());
60 for (final Enumeration e : enums) {
61 if (e.getName().equals("LinkUpDownTrapEnable")) {
62 linkUpDownTrapEnable = e;
63 } else if (e.getName().equals("OperStatus")) {
68 assertNotNull("Expected Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
69 assertNotNull("Expected Enum OperStatus, but was NULL!", operStatus);
71 assertNotNull("Enum LinkUpDownTrapEnable MUST contain Values definition not NULL reference!",
72 linkUpDownTrapEnable.getValues());
73 assertNotNull("Enum OperStatus MUST contain Values definition not NULL reference!", operStatus.getValues());
74 assertEquals("Enum LinkUpDownTrapEnable MUST contain 2 values!", 2, linkUpDownTrapEnable.getValues().size());
75 assertEquals("Enum OperStatus MUST contain 7 values!", 7, operStatus.getValues().size());
77 final List<MethodSignature> methods = genInterface.getMethodDefinitions();
79 assertNotNull("Generated Interface cannot contain NULL reference for Method Signature Definitions!", methods);
81 assertEquals("Expected count of method signature definitions is 15", 15, methods.size());
82 Enumeration ianaIfType = null;
83 for (final MethodSignature method : methods) {
84 if (method.getName().equals("getType")) {
85 if (method.getReturnType() instanceof Enumeration) {
86 ianaIfType = (Enumeration) method.getReturnType();
91 assertNotNull("Method getType MUST return Enumeration Type not NULL reference!", ianaIfType);
92 assertEquals("Enumeration getType MUST contain 272 values!", 272, ianaIfType.getValues().size());
96 public void testTypedefEnumResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
97 File ianaIfType = new File(getClass().getResource("/ietf/iana-if-type.yang").toURI());
98 final SchemaContext context = TestUtils.parseYangSources(ianaIfType);
99 assertTrue(context != null);
100 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
101 final List<Type> genTypes = bindingGen.generateTypes(context);
102 assertTrue(genTypes != null);
103 assertEquals(1, genTypes.size());
105 final Type type = genTypes.get(0);
106 assertTrue(type instanceof Enumeration);
108 final Enumeration enumer = (Enumeration) type;
109 assertEquals("Enumeration type MUST contain 272 values!", 272, enumer.getValues().size());
113 public void testLeafrefEnumResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
114 File abstractTopology = new File(getClass().getResource("/enum-test-models/abstract-topology@2013-02-08.yang")
116 File ietfInterfaces = new File(getClass().getResource("/enum-test-models/ietf-interfaces@2012-11-15.yang")
118 File ianaIfType = new File(getClass().getResource("/ietf/iana-if-type.yang").toURI());
120 final SchemaContext context = TestUtils.parseYangSources(abstractTopology, ietfInterfaces,
122 assertNotNull(context);
123 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
124 final List<Type> genTypes = bindingGen.generateTypes(context);
125 assertNotNull(genTypes);
126 assertTrue(!genTypes.isEmpty());
128 GeneratedType genInterface = null;
129 for (final Type type : genTypes) {
130 if (type instanceof GeneratedType) {
131 if (type.getPackageName().equals(
132 "org.opendaylight.yang.gen.v1.urn.model._abstract.topology.rev130208.topology.interfaces")
133 && type.getName().equals("Interface")) {
134 genInterface = (GeneratedType) type;
138 assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
140 Type linkUpDownTrapEnable = null;
141 Type operStatus = null;
142 final List<MethodSignature> methods = genInterface.getMethodDefinitions();
143 assertNotNull("Generated Type Interface cannot contain NULL reference to Enumeration types!", methods);
144 assertEquals("Generated Type Interface MUST contain 5 Methods ", 5, methods.size());
145 for (final MethodSignature method : methods) {
146 if (method.getName().equals("getLinkUpDownTrapEnable")) {
147 linkUpDownTrapEnable = method.getReturnType();
148 } else if (method.getName().equals("getOperStatus")) {
149 operStatus = method.getReturnType();
153 assertNotNull("Expected Referenced Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
154 assertTrue("Expected LinkUpDownTrapEnable of type Enumeration", linkUpDownTrapEnable instanceof Enumeration);
155 assertEquals(linkUpDownTrapEnable.getPackageName(),
156 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface");
158 assertNotNull("Expected Referenced Enum OperStatus, but was NULL!", operStatus);
159 assertTrue("Expected OperStatus of type Enumeration", operStatus instanceof Enumeration);
160 assertEquals(operStatus.getPackageName(),
161 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface");