a4ec36e57fad1e572bb36378d09f3742644ddf38
[mdsal.git] / binding / mdsal-binding-generator / 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.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13
14 import java.io.File;
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.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.mdsal.binding.model.ri.Types;
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 List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(
56             YangParserTestUtils.parseYangFiles(testModels));
57         assertEquals(2, genTypes.size());
58
59         GeneratedType moduleGenType = genTypes.stream()
60             .filter(type -> type.getName().equals("ModuleIdentityrefData"))
61             .findFirst()
62             .orElseThrow();
63
64         List<MethodSignature> methodSignatures = moduleGenType.getMethodDefinitions();
65         assertEquals(2, methodSignatures.size());
66
67         MethodSignature methodSignature = methodSignatures.get(0);
68         assertEquals("getLf", methodSignature.getName());
69         assertEquals("requireLf", methodSignatures.get(1).getName());
70
71         Type returnType = methodSignature.getReturnType();
72         assertThat(returnType, instanceOf(ParameterizedType.class));
73         ParameterizedType parameterized = (ParameterizedType) returnType;
74         assertEquals(Types.CLASS, parameterized.getRawType());
75
76         Type[] actualTypes = parameterized.getActualTypeArguments();
77         assertEquals("Incorrect number of type parameters", 1, actualTypes.length);
78         assertEquals("Return type has incorrect actual parameter",
79             "org.opendaylight.yang.gen.v1.urn.identityref.module.rev131109.SomeIdentity",
80             actualTypes[0].getFullyQualifiedName());
81     }
82 }