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.mdsal.binding.generator.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
14 import java.util.List;
15 import org.junit.Test;
16 import org.opendaylight.mdsal.binding.model.api.Enumeration;
17 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
18 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
19 import org.opendaylight.mdsal.binding.model.api.Type;
20 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
22 public class GenEnumResolvingTest {
24 public void testLeafEnumResolving() {
25 final List<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResources(
26 GenEnumResolvingTest.class,
27 "/enum-test-models/ietf-interfaces@2012-11-15.yang", "/ietf-models/iana-if-type.yang"));
28 assertTrue(genTypes != null);
30 assertEquals("Expected count of all Generated Types", 6, genTypes.size());
32 GeneratedType genInterface = null;
33 for (final Type type : genTypes) {
34 if (type instanceof GeneratedType) {
35 if (type.getName().equals("Interface")) {
36 genInterface = (GeneratedType) type;
40 assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
42 Enumeration linkUpDownTrapEnable = null;
43 Enumeration operStatus = null;
44 final List<Enumeration> enums = genInterface.getEnumerations();
45 assertNotNull("Generated Type Interface cannot contain NULL reference to Enumeration types!", enums);
46 assertEquals("Generated Type Interface MUST contain 2 Enumeration Types", 2, enums.size());
47 for (final Enumeration e : enums) {
48 if (e.getName().equals("LinkUpDownTrapEnable")) {
49 linkUpDownTrapEnable = e;
50 } else if (e.getName().equals("OperStatus")) {
55 assertNotNull("Expected Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
56 assertNotNull("Expected Enum OperStatus, but was NULL!", operStatus);
58 assertNotNull("Enum LinkUpDownTrapEnable MUST contain Values definition not NULL reference!",
59 linkUpDownTrapEnable.getValues());
60 assertNotNull("Enum OperStatus MUST contain Values definition not NULL reference!", operStatus.getValues());
61 assertEquals("Enum LinkUpDownTrapEnable MUST contain 2 values!", 2, linkUpDownTrapEnable.getValues().size());
62 assertEquals("Enum OperStatus MUST contain 7 values!", 7, operStatus.getValues().size());
64 final List<MethodSignature> methods = genInterface.getMethodDefinitions();
66 assertNotNull("Generated Interface cannot contain NULL reference for Method Signature Definitions!", methods);
68 // FIXME: split this into getter/default/static asserts
69 assertEquals(20, methods.size());
70 Enumeration ianaIfType = null;
71 for (final MethodSignature method : methods) {
72 if (method.getName().equals("getType")) {
73 if (method.getReturnType() instanceof Enumeration) {
74 ianaIfType = (Enumeration) method.getReturnType();
79 assertNotNull("Method getType MUST return Enumeration Type not NULL reference!", ianaIfType);
80 assertEquals("Enumeration getType MUST contain 272 values!", 272, ianaIfType.getValues().size());
84 public void testTypedefEnumResolving() {
85 final List<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
86 "/ietf-models/iana-if-type.yang"));
87 assertTrue(genTypes != null);
88 assertEquals(1, genTypes.size());
90 final Type type = genTypes.get(0);
91 assertTrue(type instanceof Enumeration);
93 final Enumeration enumer = (Enumeration) type;
94 assertEquals("Enumeration type MUST contain 272 values!", 272, enumer.getValues().size());
98 public void testLeafrefEnumResolving() {
99 final List<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResources(
100 GenEnumResolvingTest.class,
101 "/enum-test-models/abstract-topology@2013-02-08.yang", "/enum-test-models/ietf-interfaces@2012-11-15.yang",
102 "/ietf-models/iana-if-type.yang"));
103 assertNotNull(genTypes);
104 assertTrue(!genTypes.isEmpty());
106 GeneratedType genInterface = null;
107 for (final Type type : genTypes) {
108 if (type instanceof GeneratedType) {
109 if (type.getPackageName().equals(
110 "org.opendaylight.yang.gen.v1.urn.model._abstract.topology.rev130208.topology.interfaces")
111 && type.getName().equals("Interface")) {
112 genInterface = (GeneratedType) type;
116 assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
118 Type linkUpDownTrapEnable = null;
119 Type operStatus = null;
120 final List<MethodSignature> methods = genInterface.getMethodDefinitions();
121 assertNotNull("Generated Type Interface cannot contain NULL reference to Enumeration types!", methods);
123 // FIXME: split this into getter/default/static asserts
124 assertEquals(9, methods.size());
125 for (final MethodSignature method : methods) {
126 if (method.getName().equals("getLinkUpDownTrapEnable")) {
127 linkUpDownTrapEnable = method.getReturnType();
128 } else if (method.getName().equals("getOperStatus")) {
129 operStatus = method.getReturnType();
133 assertNotNull("Expected Referenced Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
134 assertTrue("Expected LinkUpDownTrapEnable of type Enumeration", linkUpDownTrapEnable instanceof Enumeration);
136 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface",
137 linkUpDownTrapEnable.getIdentifier().immediatelyEnclosingClass().get().toString());
139 assertNotNull("Expected Referenced Enum OperStatus, but was NULL!", operStatus);
140 assertTrue("Expected OperStatus of type Enumeration", operStatus instanceof Enumeration);
142 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface",
143 operStatus.getIdentifier().immediatelyEnclosingClass().get().toString());