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