Propagate grouping inference flag
[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 TypeProviderImpl#provideTypeForIdentityref(IdentityrefTypeDefinition)
51      * provideTypeForIdentityref}.
52      */
53     @Test
54     public void testIdentityrefYangBuiltInType() {
55         final SchemaContext context = YangParserTestUtils.parseYangFiles(testModels);
56
57         assertNotNull(context);
58         final BindingGenerator bindingGen = new BindingGeneratorImpl();
59         final List<Type> genTypes = bindingGen.generateTypes(context);
60
61         GeneratedType moduleGenType = null;
62         for (Type type : genTypes) {
63             if (type.getName().equals("ModuleIdentityrefData")) {
64                 if (type instanceof GeneratedType) {
65                     moduleGenType = (GeneratedType) type;
66                 }
67             }
68         }
69
70         assertNotNull("Generated type for whole module wasn't found", moduleGenType);
71
72         String typeName = null;
73         String actualTypeName = "";
74         int numOfActualTypes = 0;
75         List<MethodSignature> methodSignatures = moduleGenType.getMethodDefinitions();
76         for (MethodSignature methodSignature : methodSignatures) {
77             if (methodSignature.getName().equals("getLf")) {
78                 Type returnType = methodSignature.getReturnType();
79                 if (returnType instanceof ParameterizedType) {
80                     typeName = returnType.getName();
81                     Type[] actualTypes = ((ParameterizedType) returnType).getActualTypeArguments();
82                     numOfActualTypes = actualTypes.length;
83                     actualTypeName = actualTypes[0].getName();
84                 }
85             }
86         }
87         assertNotNull("The method 'getLf' was not found", typeName);
88         assertEquals("Return type has incorrect name", "Class", typeName);
89         assertEquals("Incorrect number of type parameters", 1, numOfActualTypes);
90         assertEquals("Return type has incorrect actual parameter", "SomeIdentity", actualTypeName);
91     }
92 }