Bug 6859 #3 Binding generator v1 refactoring
[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.io.File;
15 import java.io.IOException;
16 import java.net.URISyntaxException;
17 import java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.mdsal.binding.generator.api.BindingGenerator;
20 import org.opendaylight.mdsal.binding.generator.impl.BindingGeneratorImpl;
21 import org.opendaylight.mdsal.binding.model.api.Enumeration;
22 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
23 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
24 import org.opendaylight.mdsal.binding.model.api.Type;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
28 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29
30 public class GenEnumResolvingTest {
31
32     @Test
33     public void testLeafEnumResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
34         File ietfInterfaces = new File(getClass().getResource("/enum-test-models/ietf-interfaces@2012-11-15.yang")
35                 .toURI());
36         File ianaIfTypeModel = new File(getClass().getResource("/ietf/iana-if-type.yang").toURI());
37
38         final SchemaContext context = YangParserTestUtils.parseYangSources(ietfInterfaces, ianaIfTypeModel);
39         assertTrue(context != null);
40
41         final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
42         final List<Type> genTypes = bindingGen.generateTypes(context);
43         assertTrue(genTypes != null);
44
45         assertEquals("Expected count of all Generated Types", 6, genTypes.size());
46
47         GeneratedType genInterface = null;
48         for (final Type type : genTypes) {
49             if (type instanceof GeneratedType) {
50                 if (type.getName().equals("Interface")) {
51                     genInterface = (GeneratedType) type;
52                 }
53             }
54         }
55         assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
56
57         Enumeration linkUpDownTrapEnable = null;
58         Enumeration operStatus = null;
59         final List<Enumeration> enums = genInterface.getEnumerations();
60         assertNotNull("Generated Type Interface cannot contain NULL reference to Enumeration types!", enums);
61         assertEquals("Generated Type Interface MUST contain 2 Enumeration Types", 2, enums.size());
62         for (final Enumeration e : enums) {
63             if (e.getName().equals("LinkUpDownTrapEnable")) {
64                 linkUpDownTrapEnable = e;
65             } else if (e.getName().equals("OperStatus")) {
66                 operStatus = e;
67             }
68         }
69
70         assertNotNull("Expected Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
71         assertNotNull("Expected Enum OperStatus, but was NULL!", operStatus);
72
73         assertNotNull("Enum LinkUpDownTrapEnable MUST contain Values definition not NULL reference!",
74                 linkUpDownTrapEnable.getValues());
75         assertNotNull("Enum OperStatus MUST contain Values definition not NULL reference!", operStatus.getValues());
76         assertEquals("Enum LinkUpDownTrapEnable MUST contain 2 values!", 2, linkUpDownTrapEnable.getValues().size());
77         assertEquals("Enum OperStatus MUST contain 7 values!", 7, operStatus.getValues().size());
78
79         final List<MethodSignature> methods = genInterface.getMethodDefinitions();
80
81         assertNotNull("Generated Interface cannot contain NULL reference for Method Signature Definitions!", methods);
82
83         assertEquals("Expected count of method signature definitions is 15", 15, methods.size());
84         Enumeration ianaIfType = null;
85         for (final MethodSignature method : methods) {
86             if (method.getName().equals("getType")) {
87                 if (method.getReturnType() instanceof Enumeration) {
88                     ianaIfType = (Enumeration) method.getReturnType();
89                 }
90             }
91         }
92
93         assertNotNull("Method getType MUST return Enumeration Type not NULL reference!", ianaIfType);
94         assertEquals("Enumeration getType MUST contain 272 values!", 272, ianaIfType.getValues().size());
95     }
96
97     @Test
98     public void testTypedefEnumResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
99         File ianaIfType = new File(getClass().getResource("/ietf/iana-if-type.yang").toURI());
100         final SchemaContext context = YangParserTestUtils.parseYangSources(ianaIfType);
101         assertTrue(context != null);
102         final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
103         final List<Type> genTypes = bindingGen.generateTypes(context);
104         assertTrue(genTypes != null);
105         assertEquals(1, genTypes.size());
106
107         final Type type = genTypes.get(0);
108         assertTrue(type instanceof Enumeration);
109
110         final Enumeration enumer = (Enumeration) type;
111         assertEquals("Enumeration type MUST contain 272 values!", 272, enumer.getValues().size());
112     }
113
114     @Test
115     public void testLeafrefEnumResolving() throws URISyntaxException, IOException, SourceException, ReactorException {
116         File abstractTopology = new File(getClass().getResource("/enum-test-models/abstract-topology@2013-02-08.yang")
117                 .toURI());
118         File ietfInterfaces = new File(getClass().getResource("/enum-test-models/ietf-interfaces@2012-11-15.yang")
119                 .toURI());
120         File ianaIfType = new File(getClass().getResource("/ietf/iana-if-type.yang").toURI());
121
122         final SchemaContext context = YangParserTestUtils.parseYangSources(abstractTopology, ietfInterfaces,
123                 ianaIfType);
124         assertNotNull(context);
125         final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
126         final List<Type> genTypes = bindingGen.generateTypes(context);
127         assertNotNull(genTypes);
128         assertTrue(!genTypes.isEmpty());
129
130         GeneratedType genInterface = null;
131         for (final Type type : genTypes) {
132             if (type instanceof GeneratedType) {
133                 if (type.getPackageName().equals(
134                         "org.opendaylight.yang.gen.v1.urn.model._abstract.topology.rev130208.topology.interfaces")
135                         && type.getName().equals("Interface")) {
136                     genInterface = (GeneratedType) type;
137                 }
138             }
139         }
140         assertNotNull("Generated Type Interface is not present in list of Generated Types", genInterface);
141
142         Type linkUpDownTrapEnable = null;
143         Type operStatus = null;
144         final List<MethodSignature> methods = genInterface.getMethodDefinitions();
145         assertNotNull("Generated Type Interface cannot contain NULL reference to Enumeration types!", methods);
146         assertEquals("Generated Type Interface MUST contain 5 Methods ", 5, methods.size());
147         for (final MethodSignature method : methods) {
148             if (method.getName().equals("getLinkUpDownTrapEnable")) {
149                 linkUpDownTrapEnable = method.getReturnType();
150             } else if (method.getName().equals("getOperStatus")) {
151                 operStatus = method.getReturnType();
152             }
153         }
154
155         assertNotNull("Expected Referenced Enum LinkUpDownTrapEnable, but was NULL!", linkUpDownTrapEnable);
156         assertTrue("Expected LinkUpDownTrapEnable of type Enumeration", linkUpDownTrapEnable instanceof Enumeration);
157         assertEquals(linkUpDownTrapEnable.getPackageName(),
158                 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface");
159
160         assertNotNull("Expected Referenced Enum OperStatus, but was NULL!", operStatus);
161         assertTrue("Expected OperStatus of type Enumeration", operStatus instanceof Enumeration);
162         assertEquals(operStatus.getPackageName(),
163                 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface");
164     }
165
166 }