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