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