755e92f5a3dd5584dbf211c2fdd1016e5c0fd3d0
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / mdsal / 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.mdsal.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.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.test.util.YangParserTestUtils;
27
28 public class IdentityrefTypeTest {
29
30     private static List<File> testModels = null;
31
32     @Before
33     public void loadTestResources() throws URISyntaxException {
34         URI folderPath = IdentityrefTypeTest.class.getResource("/identityref.yang").toURI();
35         File folderFile = new File(folderPath);
36         testModels = new ArrayList<>();
37
38         if (folderFile.isFile()) {
39             testModels.add(folderFile);
40         } else {
41             for (File file : folderFile.listFiles()) {
42                 if (file.isFile()) {
43                     testModels.add(file);
44                 }
45             }
46         }
47     }
48
49     /**
50      * Test mainly for the method
51      * TypeProviderImpl#provideTypeForIdentityref(IdentityrefTypeDefinition)
52      * provideTypeForIdentityref}
53      */
54     @Test
55     public void testIdentityrefYangBuiltInType() {
56         final SchemaContext context = YangParserTestUtils.parseYangFiles(testModels);
57
58         assertNotNull(context);
59         final BindingGenerator bindingGen = new BindingGeneratorImpl();
60         final List<Type> genTypes = bindingGen.generateTypes(context);
61
62         GeneratedType moduleGenType = null;
63         for (Type type : genTypes) {
64             if (type.getName().equals("ModuleIdentityrefData")) {
65                 if (type instanceof GeneratedType) {
66                     moduleGenType = (GeneratedType) type;
67                 }
68             }
69         }
70
71         assertNotNull("Generated type for whole module wasn't found", moduleGenType);
72
73         String typeName = null;
74         String actualTypeName = "";
75         int numOfActualTypes = 0;
76         List<MethodSignature> methodSignatures = moduleGenType.getMethodDefinitions();
77         for (MethodSignature methodSignature : methodSignatures) {
78             if (methodSignature.getName().equals("getLf")) {
79                 Type returnType = methodSignature.getReturnType();
80                 if (returnType instanceof ParameterizedType) {
81                     typeName = returnType.getName();
82                     Type[] actualTypes = ((ParameterizedType) returnType).getActualTypeArguments();
83                     numOfActualTypes = actualTypes.length;
84                     actualTypeName = actualTypes[0].getName();
85                 }
86             }
87         }
88         assertNotNull("The method 'getLf' wasn't found", typeName);
89         assertEquals("Return type has incorrect name", "Class", typeName);
90         assertEquals("Incorrect number of type parameters", 1, numOfActualTypes);
91         assertEquals("Return type has incorrect actual parameter", "SomeIdentity", actualTypeName);
92
93     }
94
95 }