9e949e1ff34cb6764b112bf369e2033b25909924
[yangtools.git] / code-generator / binding-generator-impl / src / test / java / org / opendaylight / yangtools / sal / binding / generator / impl / 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;
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.net.URI;
16 import java.net.URISyntaxException;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Set;
20
21 import org.junit.Test;
22 import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl;
23 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
24 import org.opendaylight.yangtools.sal.binding.model.api.Enumeration;
25 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
26 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
27 import org.opendaylight.yangtools.sal.binding.model.api.Type;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
31 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
32
33 public class GenEnumResolvingTest {
34
35     private SchemaContext resolveSchemaContextFromFiles(
36             final URI... yangFiles) {
37         final YangModelParser parser = new YangParserImpl();
38
39         final List<File> inputFiles = new ArrayList<File>();
40         for (int i = 0; i < yangFiles.length; ++i) {
41             inputFiles.add(new File(yangFiles[i]));
42         }
43
44         final Set<Module> modules = parser.parseYangModels(inputFiles);
45         return parser.resolveSchemaContext(modules);
46     }
47
48     @Test
49     public void testLeafEnumResolving() throws URISyntaxException {
50         final URI ietfInterfacesPath = getClass().getResource(
51                 "/enum-test-models/ietf-interfaces@2012-11-15.yang").toURI();
52         final URI ifTypePath = getClass().getResource(
53                 "/enum-test-models/iana-if-type@2012-06-05.yang").toURI();
54         final URI yangTypesPath = getClass().getResource(
55                 "/enum-test-models/ietf-yang-types@2010-09-24.yang").toURI();
56
57         final SchemaContext context = resolveSchemaContextFromFiles(
58                 ietfInterfacesPath, ifTypePath, yangTypesPath);
59         assertTrue(context != null);
60
61         final BindingGenerator bindingGen = new BindingGeneratorImpl();
62         final List<Type> genTypes = bindingGen.generateTypes(context);
63         assertTrue(genTypes != null);
64
65         assertEquals("Expected count of all Generated Types", 20, genTypes.size());
66
67         GeneratedType genInterface = null;
68         for (final Type type : genTypes) {
69             if (type instanceof GeneratedType) {
70                 if (type.getName().equals("Interface")) {
71                     genInterface = (GeneratedType) type;
72                 }
73             }
74         }
75         assertNotNull("Generated Type Interface is not present in list of " +
76                 "Generated Types", genInterface);
77
78         Enumeration linkUpDownTrapEnable = null;
79         Enumeration operStatus = null;
80         final List<Enumeration> enums = genInterface.getEnumerations();
81         assertNotNull("Generated Type Interface cannot contain NULL reference" +
82                 " to Enumeration types!", enums);
83         assertEquals("Generated Type Interface MUST contain 2 Enumeration " +
84                 "Types", 2, enums.size());
85         for (final Enumeration e : enums) {
86             if (e.getName().equals("LinkUpDownTrapEnable")) {
87                 linkUpDownTrapEnable = e;
88             } else if (e.getName().equals("OperStatus")) {
89                 operStatus = e;
90             }
91         }
92
93         assertNotNull("Expected Enum LinkUpDownTrapEnable, but was NULL!",
94                 linkUpDownTrapEnable);
95         assertNotNull("Expected Enum OperStatus, but was NULL!", operStatus);
96
97         assertNotNull("Enum LinkUpDownTrapEnable MUST contain Values definition " +
98                 "not NULL reference!", linkUpDownTrapEnable.getValues());
99         assertNotNull("Enum OperStatus MUST contain Values definition not " +
100                 "NULL reference!", operStatus.getValues());
101         assertEquals("Enum LinkUpDownTrapEnable MUST contain 2 values!", 2,
102                 linkUpDownTrapEnable.getValues().size());
103         assertEquals("Enum OperStatus MUST contain 7 values!", 7,
104                 operStatus.getValues().size());
105
106         final List<MethodSignature> methods = genInterface
107                 .getMethodDefinitions();
108
109         assertNotNull("Generated Interface cannot contain NULL reference for " +
110                 "Method Signature Definitions!", methods);
111
112         assertEquals("Expected count of method signature definitions is 15",
113                 15, methods.size());
114         Enumeration ianaIfType = null;
115         for (final MethodSignature method : methods) {
116             if (method.getName().equals("getType")) {
117                 if (method.getReturnType() instanceof Enumeration) {
118                     ianaIfType = (Enumeration)method.getReturnType();
119                 }
120             }
121         }
122
123         assertNotNull("Method getType MUST return Enumeration Type, " +
124                 "not NULL reference!", ianaIfType);
125         assertEquals("Enumeration getType MUST contain 272 values!", 272,
126                 ianaIfType.getValues().size());
127     }
128
129     @Test
130     public void testTypedefEnumResolving() throws URISyntaxException {
131         final URI ianaIfTypePath = getClass().getResource(
132                 "/leafref-test-models/iana-if-type@2012-06-05.yang").toURI();
133
134         final SchemaContext context = resolveSchemaContextFromFiles(ianaIfTypePath);
135         assertTrue(context != null);
136         final BindingGenerator bindingGen = new BindingGeneratorImpl();
137         final List<Type> genTypes = bindingGen.generateTypes(context);
138         assertTrue(genTypes != null);
139         assertEquals(1, genTypes.size());
140
141         final Type type = genTypes.get(0);
142         assertTrue(type instanceof Enumeration);
143
144         final Enumeration enumer = (Enumeration) type;
145         assertEquals("Enumeration type MUST contain 272 values!", 272,
146                 enumer.getValues().size());
147     }
148
149     @Test
150     public void testLeafrefEnumResolving() throws URISyntaxException {
151         final URI ietfInterfacesPath = getClass().getResource(
152                 "/enum-test-models/ietf-interfaces@2012-11-15.yang").toURI();
153         final URI ifTypePath = getClass().getResource(
154                 "/enum-test-models/iana-if-type@2012-06-05.yang").toURI();
155         final URI yangTypesPath = getClass().getResource(
156                 "/enum-test-models/ietf-yang-types@2010-09-24.yang").toURI();
157         final URI topologyPath = getClass().getResource(
158                 "/enum-test-models/abstract-topology@2013-02-08.yang")
159                 .toURI();
160         final URI inetTypesPath = getClass().getResource(
161                 "/enum-test-models/ietf-inet-types@2010-09-24.yang")
162                 .toURI();
163         final SchemaContext context = resolveSchemaContextFromFiles(
164                 ietfInterfacesPath, ifTypePath, yangTypesPath, topologyPath,
165                 inetTypesPath);
166
167         assertNotNull(context);
168         final BindingGenerator bindingGen = new BindingGeneratorImpl();
169         final List<Type> genTypes = bindingGen.generateTypes(context);
170         assertNotNull(genTypes);
171         assertTrue(!genTypes.isEmpty());
172
173         GeneratedType genInterface = null;
174         for (final Type type : genTypes) {
175             if (type instanceof GeneratedType) {
176                 if (type.getPackageName().equals("org.opendaylight.yang.gen.v1.urn.model._abstract.topology.rev130208.topology.interfaces")
177                         && type.getName().equals("Interface")) {
178                     genInterface = (GeneratedType) type;
179                 }
180             }
181         }
182         assertNotNull("Generated Type Interface is not present in list of " +
183                 "Generated Types", genInterface);
184
185         Type linkUpDownTrapEnable = null;
186         Type operStatus = null;
187         final List<MethodSignature> methods = genInterface.getMethodDefinitions();
188         assertNotNull("Generated Type Interface cannot contain NULL reference" +
189                 " to Enumeration types!", methods);
190         assertEquals("Generated Type Interface MUST contain 5 Methods ",
191                 5, methods.size());
192         for (final MethodSignature method : methods) {
193             if (method.getName().equals("getLinkUpDownTrapEnable")) {
194                 linkUpDownTrapEnable = method.getReturnType();
195             } else if (method.getName().equals("getOperStatus")) {
196                 operStatus = method.getReturnType();
197             }
198         }
199
200         assertNotNull("Expected Referenced Enum LinkUpDownTrapEnable, but was NULL!",
201                 linkUpDownTrapEnable);
202         assertTrue("Expected LinkUpDownTrapEnable of type ReferencedTypeImpl",
203                 linkUpDownTrapEnable instanceof ReferencedTypeImpl);
204         assertEquals(linkUpDownTrapEnable.getPackageName(),
205                 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface");
206
207         assertNotNull("Expected Referenced Enum OperStatus, but was NULL!",
208                 operStatus);
209         assertTrue("Expected OperStatus of type ReferencedTypeImpl",
210                 operStatus instanceof  ReferencedTypeImpl);
211         assertEquals(operStatus.getPackageName(),
212                 "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev121115.interfaces.Interface");
213     }
214 }