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