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