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.generator.api.BindingGenerator;
17 import org.opendaylight.mdsal.binding.model.api.Enumeration;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
19 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
20 import org.opendaylight.mdsal.binding.model.api.Type;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24 public class GenEnumResolvingTest {
27 public void testLeafEnumResolving() {
28 final SchemaContext context = YangParserTestUtils.parseYangResources(GenEnumResolvingTest.class,
29 "/enum-test-models/ietf-interfaces@2012-11-15.yang", "/ietf/iana-if-type.yang");
30 assertTrue(context != null);
32 final BindingGenerator bindingGen = new BindingGeneratorImpl();
33 final List<Type> genTypes = bindingGen.generateTypes(context);
34 assertTrue(genTypes != null);
36 assertEquals("Expected count of all Generated Types", 6, genTypes.size());
38 GeneratedType genInterface = null;
39 for (final Type type : genTypes) {
40 if (type instanceof GeneratedType) {
41 if (type.getName().equals("Interface")) {
42 genInterface = (GeneratedType) type;
46 assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
48 Enumeration linkUpDownTrapEnable = null;
49 Enumeration operStatus = null;
50 final List<Enumeration> enums = genInterface.getEnumerations();
51 assertNotNull("Generated Type Interface cannot contain NULL reference to Enumeration types!", enums);
52 assertEquals("Generated Type Interface MUST contain 2 Enumeration Types", 2, enums.size());
53 for (final Enumeration e : enums) {
54 if (e.getName().equals("LinkUpDownTrapEnable")) {
55 linkUpDownTrapEnable = e;
56 } else if (e.getName().equals("OperStatus")) {
61 assertNotNull("Expected Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
62 assertNotNull("Expected Enum OperStatus, but was NULL!", operStatus);
64 assertNotNull("Enum LinkUpDownTrapEnable MUST contain Values definition not NULL reference!",
65 linkUpDownTrapEnable.getValues());
66 assertNotNull("Enum OperStatus MUST contain Values definition not NULL reference!", operStatus.getValues());
67 assertEquals("Enum LinkUpDownTrapEnable MUST contain 2 values!", 2, linkUpDownTrapEnable.getValues().size());
68 assertEquals("Enum OperStatus MUST contain 7 values!", 7, operStatus.getValues().size());
70 final List<MethodSignature> methods = genInterface.getMethodDefinitions();
72 assertNotNull("Generated Interface cannot contain NULL reference for Method Signature Definitions!", methods);
74 assertEquals("Expected count of method signature definitions is 15", 15, methods.size());
75 Enumeration ianaIfType = null;
76 for (final MethodSignature method : methods) {
77 if (method.getName().equals("getType")) {
78 if (method.getReturnType() instanceof Enumeration) {
79 ianaIfType = (Enumeration) method.getReturnType();
84 assertNotNull("Method getType MUST return Enumeration Type not NULL reference!", ianaIfType);
85 assertEquals("Enumeration getType MUST contain 272 values!", 272, ianaIfType.getValues().size());
89 public void testTypedefEnumResolving() {
90 final SchemaContext context = YangParserTestUtils.parseYangResource("/ietf/iana-if-type.yang");
91 assertTrue(context != null);
92 final BindingGenerator bindingGen = new BindingGeneratorImpl();
93 final List<Type> genTypes = bindingGen.generateTypes(context);
94 assertTrue(genTypes != null);
95 assertEquals(1, genTypes.size());
97 final Type type = genTypes.get(0);
98 assertTrue(type instanceof Enumeration);
100 final Enumeration enumer = (Enumeration) type;
101 assertEquals("Enumeration type MUST contain 272 values!", 272, enumer.getValues().size());
105 public void testLeafrefEnumResolving() {
106 final SchemaContext context = YangParserTestUtils.parseYangResources(GenEnumResolvingTest.class,
107 "/enum-test-models/abstract-topology@2013-02-08.yang", "/enum-test-models/ietf-interfaces@2012-11-15.yang",
108 "/ietf/iana-if-type.yang");
109 assertNotNull(context);
110 final BindingGenerator bindingGen = new BindingGeneratorImpl();
111 final List<Type> genTypes = bindingGen.generateTypes(context);
112 assertNotNull(genTypes);
113 assertTrue(!genTypes.isEmpty());
115 GeneratedType genInterface = null;
116 for (final Type type : genTypes) {
117 if (type instanceof GeneratedType) {
118 if (type.getPackageName().equals(
119 "org.opendaylight.yang.gen.v1.urn.model._abstract.topology.rev130208.topology.interfaces")
120 && type.getName().equals("Interface")) {
121 genInterface = (GeneratedType) type;
125 assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
127 Type linkUpDownTrapEnable = null;
128 Type operStatus = null;
129 final List<MethodSignature> methods = genInterface.getMethodDefinitions();
130 assertNotNull("Generated Type Interface cannot contain NULL reference to Enumeration types!", methods);
131 assertEquals("Generated Type Interface MUST contain 5 Methods ", 5, methods.size());
132 for (final MethodSignature method : methods) {
133 if (method.getName().equals("getLinkUpDownTrapEnable")) {
134 linkUpDownTrapEnable = method.getReturnType();
135 } else if (method.getName().equals("getOperStatus")) {
136 operStatus = method.getReturnType();
140 assertNotNull("Expected Referenced Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
141 assertTrue("Expected LinkUpDownTrapEnable of type Enumeration", linkUpDownTrapEnable instanceof Enumeration);
143 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface",
144 linkUpDownTrapEnable.getIdentifier().immediatelyEnclosingClass().get().toString());
146 assertNotNull("Expected Referenced Enum OperStatus, but was NULL!", operStatus);
147 assertTrue("Expected OperStatus of type Enumeration", operStatus instanceof Enumeration);
149 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface",
150 operStatus.getIdentifier().immediatelyEnclosingClass().get().toString());