Integrate JavaTypeName as Identifier
[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.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;
23
24 public class GenEnumResolvingTest {
25
26     @Test
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);
31
32         final BindingGenerator bindingGen = new BindingGeneratorImpl();
33         final List<Type> genTypes = bindingGen.generateTypes(context);
34         assertTrue(genTypes != null);
35
36         assertEquals("Expected count of all Generated Types", 6, genTypes.size());
37
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;
43                 }
44             }
45         }
46         assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
47
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")) {
57                 operStatus = e;
58             }
59         }
60
61         assertNotNull("Expected Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
62         assertNotNull("Expected Enum OperStatus, but was NULL!", operStatus);
63
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());
69
70         final List<MethodSignature> methods = genInterface.getMethodDefinitions();
71
72         assertNotNull("Generated Interface cannot contain NULL reference for Method Signature Definitions!", methods);
73
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();
80                 }
81             }
82         }
83
84         assertNotNull("Method getType MUST return Enumeration Type not NULL reference!", ianaIfType);
85         assertEquals("Enumeration getType MUST contain 272 values!", 272, ianaIfType.getValues().size());
86     }
87
88     @Test
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());
96
97         final Type type = genTypes.get(0);
98         assertTrue(type instanceof Enumeration);
99
100         final Enumeration enumer = (Enumeration) type;
101         assertEquals("Enumeration type MUST contain 272 values!", 272, enumer.getValues().size());
102     }
103
104     @Test
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());
114
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;
122                 }
123             }
124         }
125         assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
126
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();
137             }
138         }
139
140         assertNotNull("Expected Referenced Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
141         assertTrue("Expected LinkUpDownTrapEnable of type Enumeration", linkUpDownTrapEnable instanceof Enumeration);
142         assertEquals(
143             "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface",
144             linkUpDownTrapEnable.getIdentifier().immediatelyEnclosingClass().get().toString());
145
146         assertNotNull("Expected Referenced Enum OperStatus, but was NULL!", operStatus);
147         assertTrue("Expected OperStatus of type Enumeration", operStatus instanceof Enumeration);
148         assertEquals(
149             "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface",
150             operStatus.getIdentifier().immediatelyEnclosingClass().get().toString());
151     }
152
153 }