Generate bindingToString() and use it in generated implementations
[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.test.util.YangParserTestUtils;
21
22 public class GenEnumResolvingTest {
23     @Test
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/iana-if-type.yang"));
28         assertTrue(genTypes != null);
29
30         assertEquals("Expected count of all Generated Types", 6, genTypes.size());
31
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;
37                 }
38             }
39         }
40         assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
41
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")) {
51                 operStatus = e;
52             }
53         }
54
55         assertNotNull("Expected Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
56         assertNotNull("Expected Enum OperStatus, but was NULL!", operStatus);
57
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());
63
64         final List<MethodSignature> methods = genInterface.getMethodDefinitions();
65
66         assertNotNull("Generated Interface cannot contain NULL reference for Method Signature Definitions!", methods);
67
68         // FIXME: split this into getter/default/static asserts
69         assertEquals(17, 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();
75                 }
76             }
77         }
78
79         assertNotNull("Method getType MUST return Enumeration Type not NULL reference!", ianaIfType);
80         assertEquals("Enumeration getType MUST contain 272 values!", 272, ianaIfType.getValues().size());
81     }
82
83     @Test
84     public void testTypedefEnumResolving() {
85         final List<Type> genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
86             "/ietf/iana-if-type.yang"));
87         assertTrue(genTypes != null);
88         assertEquals(1, genTypes.size());
89
90         final Type type = genTypes.get(0);
91         assertTrue(type instanceof Enumeration);
92
93         final Enumeration enumer = (Enumeration) type;
94         assertEquals("Enumeration type MUST contain 272 values!", 272, enumer.getValues().size());
95     }
96
97     @Test
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/iana-if-type.yang"));
103         assertNotNull(genTypes);
104         assertTrue(!genTypes.isEmpty());
105
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;
113                 }
114             }
115         }
116         assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
117
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);
122
123         // FIXME: split this into getter/default/static asserts
124         assertEquals(7, 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();
130             }
131         }
132
133         assertNotNull("Expected Referenced Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
134         assertTrue("Expected LinkUpDownTrapEnable of type Enumeration", linkUpDownTrapEnable instanceof Enumeration);
135         assertEquals(
136             "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface",
137             linkUpDownTrapEnable.getIdentifier().immediatelyEnclosingClass().get().toString());
138
139         assertNotNull("Expected Referenced Enum OperStatus, but was NULL!", operStatus);
140         assertTrue("Expected OperStatus of type Enumeration", operStatus instanceof Enumeration);
141         assertEquals(
142             "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface",
143             operStatus.getIdentifier().immediatelyEnclosingClass().get().toString());
144     }
145 }