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