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