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