8a5ec25ec3ce28845d3f492901a7bc0067852a08
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / mdsal / binding / generator / impl / GenEnumResolvingTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.generator.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
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.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
22
23 public class GenEnumResolvingTest {
24
25     @Test
26     public void testLeafEnumResolving() {
27         final SchemaContext context = YangParserTestUtils.parseYangResources(GenEnumResolvingTest.class,
28             "/enum-test-models/ietf-interfaces@2012-11-15.yang", "/ietf/iana-if-type.yang");
29         assertTrue(context != null);
30
31         final List<Type> genTypes = DefaultBindingGenerator.generateFor(context);
32         assertTrue(genTypes != null);
33
34         assertEquals("Expected count of all Generated Types", 6, genTypes.size());
35
36         GeneratedType genInterface = null;
37         for (final Type type : genTypes) {
38             if (type instanceof GeneratedType) {
39                 if (type.getName().equals("Interface")) {
40                     genInterface = (GeneratedType) type;
41                 }
42             }
43         }
44         assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
45
46         Enumeration linkUpDownTrapEnable = null;
47         Enumeration operStatus = null;
48         final List<Enumeration> enums = genInterface.getEnumerations();
49         assertNotNull("Generated Type Interface cannot contain NULL reference to Enumeration types!", enums);
50         assertEquals("Generated Type Interface MUST contain 2 Enumeration Types", 2, enums.size());
51         for (final Enumeration e : enums) {
52             if (e.getName().equals("LinkUpDownTrapEnable")) {
53                 linkUpDownTrapEnable = e;
54             } else if (e.getName().equals("OperStatus")) {
55                 operStatus = e;
56             }
57         }
58
59         assertNotNull("Expected Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
60         assertNotNull("Expected Enum OperStatus, but was NULL!", operStatus);
61
62         assertNotNull("Enum LinkUpDownTrapEnable MUST contain Values definition not NULL reference!",
63                 linkUpDownTrapEnable.getValues());
64         assertNotNull("Enum OperStatus MUST contain Values definition not NULL reference!", operStatus.getValues());
65         assertEquals("Enum LinkUpDownTrapEnable MUST contain 2 values!", 2, linkUpDownTrapEnable.getValues().size());
66         assertEquals("Enum OperStatus MUST contain 7 values!", 7, operStatus.getValues().size());
67
68         final List<MethodSignature> methods = genInterface.getMethodDefinitions();
69
70         assertNotNull("Generated Interface cannot contain NULL reference for Method Signature Definitions!", methods);
71
72         assertEquals("Expected count of method signature definitions is 16", 16, methods.size());
73         Enumeration ianaIfType = null;
74         for (final MethodSignature method : methods) {
75             if (method.getName().equals("getType")) {
76                 if (method.getReturnType() instanceof Enumeration) {
77                     ianaIfType = (Enumeration) method.getReturnType();
78                 }
79             }
80         }
81
82         assertNotNull("Method getType MUST return Enumeration Type not NULL reference!", ianaIfType);
83         assertEquals("Enumeration getType MUST contain 272 values!", 272, ianaIfType.getValues().size());
84     }
85
86     @Test
87     public void testTypedefEnumResolving() {
88         final SchemaContext context = YangParserTestUtils.parseYangResource("/ietf/iana-if-type.yang");
89         assertTrue(context != null);
90         final List<Type> genTypes = DefaultBindingGenerator.generateFor(context);
91         assertTrue(genTypes != null);
92         assertEquals(1, genTypes.size());
93
94         final Type type = genTypes.get(0);
95         assertTrue(type instanceof Enumeration);
96
97         final Enumeration enumer = (Enumeration) type;
98         assertEquals("Enumeration type MUST contain 272 values!", 272, enumer.getValues().size());
99     }
100
101     @Test
102     public void testLeafrefEnumResolving() {
103         final SchemaContext context = YangParserTestUtils.parseYangResources(GenEnumResolvingTest.class,
104             "/enum-test-models/abstract-topology@2013-02-08.yang", "/enum-test-models/ietf-interfaces@2012-11-15.yang",
105             "/ietf/iana-if-type.yang");
106         assertNotNull(context);
107         final List<Type> genTypes = DefaultBindingGenerator.generateFor(context);
108         assertNotNull(genTypes);
109         assertTrue(!genTypes.isEmpty());
110
111         GeneratedType genInterface = null;
112         for (final Type type : genTypes) {
113             if (type instanceof GeneratedType) {
114                 if (type.getPackageName().equals(
115                         "org.opendaylight.yang.gen.v1.urn.model._abstract.topology.rev130208.topology.interfaces")
116                         && type.getName().equals("Interface")) {
117                     genInterface = (GeneratedType) type;
118                 }
119             }
120         }
121         assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
122
123         Type linkUpDownTrapEnable = null;
124         Type operStatus = null;
125         final List<MethodSignature> methods = genInterface.getMethodDefinitions();
126         assertNotNull("Generated Type Interface cannot contain NULL reference to Enumeration types!", methods);
127         assertEquals("Generated Type Interface MUST contain 6 Methods ", 6, methods.size());
128         for (final MethodSignature method : methods) {
129             if (method.getName().equals("getLinkUpDownTrapEnable")) {
130                 linkUpDownTrapEnable = method.getReturnType();
131             } else if (method.getName().equals("getOperStatus")) {
132                 operStatus = method.getReturnType();
133             }
134         }
135
136         assertNotNull("Expected Referenced Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
137         assertTrue("Expected LinkUpDownTrapEnable of type Enumeration", linkUpDownTrapEnable instanceof Enumeration);
138         assertEquals(
139             "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface",
140             linkUpDownTrapEnable.getIdentifier().immediatelyEnclosingClass().get().toString());
141
142         assertNotNull("Expected Referenced Enum OperStatus, but was NULL!", operStatus);
143         assertTrue("Expected OperStatus of type Enumeration", operStatus instanceof Enumeration);
144         assertEquals(
145             "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface",
146             operStatus.getIdentifier().immediatelyEnclosingClass().get().toString());
147     }
148
149 }