Bug 3670 (part 4/5 - mdsal): Use of new statement parser in yang-maven-plugin
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / yangtools / sal / binding / generator / impl / stmt / parser / retest / IdentityrefTypeTest.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
13 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
14 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
15
16 import org.opendaylight.yangtools.sal.binding.generator.impl.BindingGeneratorImpl;
17 import java.io.File;
18 import java.io.IOException;
19 import java.net.URI;
20 import java.net.URISyntaxException;
21 import java.util.ArrayList;
22 import java.util.List;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
26 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
27 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
28 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
29 import org.opendaylight.yangtools.sal.binding.model.api.Type;
30 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32
33 public class IdentityrefTypeTest {
34
35     private static List<File> testModels = null;
36
37     @Before
38     public void loadTestResources() throws URISyntaxException {
39         URI folderPath = IdentityrefTypeTest.class.getResource("/identityref.yang").toURI();
40         File folderFile = new File(folderPath);
41         testModels = new ArrayList<File>();
42
43         if (folderFile.isFile()) {
44             testModels.add(folderFile);
45         } else {
46             for (File file : folderFile.listFiles()) {
47                 if (file.isFile()) {
48                     testModels.add(file);
49                 }
50             }
51         }
52     }
53
54     /**
55      * Test mainly for the method
56      * {@link TypeProviderImpl#provideTypeForIdentityref()
57      * provideTypeForIdentityref}
58      * @throws ReactorException
59      * @throws SourceException
60      */
61     @Test
62     public void testIdentityrefYangBuiltInType() throws IOException, SourceException, ReactorException {
63         final SchemaContext context = RetestUtils.parseYangSources(testModels);
64
65         assertNotNull(context);
66         final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
67         final List<Type> genTypes = bindingGen.generateTypes(context);
68
69         GeneratedType moduleGenType = null;
70         for (Type type : genTypes) {
71             if (type.getName().equals("ModuleIdentityrefData")) {
72                 if (type instanceof GeneratedType) {
73                     moduleGenType = (GeneratedType) type;
74                 }
75             }
76         }
77
78         assertNotNull("Generated type for whole module wasn't found", moduleGenType);
79
80         String typeName = null;
81         String actualTypeName = "";
82         int numOfActualTypes = 0;
83         List<MethodSignature> methodSignatures = moduleGenType.getMethodDefinitions();
84         for (MethodSignature methodSignature : methodSignatures) {
85             if (methodSignature.getName().equals("getLf")) {
86                 Type returnType = methodSignature.getReturnType();
87                 if (returnType instanceof ParameterizedType) {
88                     typeName = returnType.getName();
89                     Type[] actualTypes = ((ParameterizedType) returnType).getActualTypeArguments();
90                     numOfActualTypes = actualTypes.length;
91                     actualTypeName = actualTypes[0].getName();
92                 }
93             }
94         }
95         assertNotNull("The method 'getLf' wasn't found", typeName);
96         assertEquals("Return type has incorrect name", "Class", typeName);
97         assertEquals("Incorrect number of type parameters", 1, numOfActualTypes);
98         assertEquals("Return type has incorrect actual parameter", "SomeIdentity", actualTypeName);
99
100     }
101
102 }