Move generatedTypeForExtendedDefinitionTypeWithIdentityrefBaseTypeTest()
[mdsal.git] / binding / mdsal-binding-generator / src / test / java / org / opendaylight / mdsal / binding / generator / impl / DefaultBindingGeneratorTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  * Copyright (c) 2021 PANTHEON.tech, s.r.o.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9 package org.opendaylight.mdsal.binding.generator.impl;
10
11 import static org.hamcrest.CoreMatchers.instanceOf;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14
15 import java.util.List;
16 import org.junit.AfterClass;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.junit.MockitoJUnitRunner;
21 import org.opendaylight.mdsal.binding.model.api.Enumeration;
22 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
23 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
24 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
25 import org.opendaylight.mdsal.binding.model.ri.Types;
26 import org.opendaylight.mdsal.binding.yang.types.TypeProviderTest;
27 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
28 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29
30 /**
31  * General test suite revolving around {@link DefaultBindingGenerator}. This class holds tests originally aimed at
32  * specific implementation methods, but now they really are all about integration testing.
33  */
34 @RunWith(MockitoJUnitRunner.class)
35 public class DefaultBindingGeneratorTest {
36     public static final String TEST_TYPE_PROVIDER =
37         "org.opendaylight.yang.gen.v1.urn.opendaylight.org.test.type.provider.model.rev140912";
38     public static final String TEST_TYPE_PROVIDER_B =
39         "org.opendaylight.yang.gen.v1.urn.opendaylight.org.test.type.provider.b.model.rev140915";
40     public static EffectiveModelContext SCHEMA_CONTEXT;
41     public static List<GeneratedType> TYPES;
42
43     @BeforeClass
44     public static void beforeClass() {
45         SCHEMA_CONTEXT = YangParserTestUtils.parseYangResources(TypeProviderTest.class,
46             "/base-yang-types.yang", "/test-type-provider-b.yang", "/test-type-provider.yang");
47         TYPES = DefaultBindingGenerator.generateFor(SCHEMA_CONTEXT);
48     }
49
50     @AfterClass
51     public static void afterClass() {
52         SCHEMA_CONTEXT = null;
53         TYPES = null;
54     }
55
56     @Test
57     public void javaTypeForSchemaDefinitionLeafrefToEnumTypeTest() {
58         final var bDataName = JavaTypeName.create(TEST_TYPE_PROVIDER_B, "TestTypeProviderBData");
59         final var bData = TYPES.stream()
60             .filter(type -> type.getIdentifier().equals(bDataName))
61             .findFirst()
62             .orElseThrow();
63
64         final var bDataMethods = bData.getMethodDefinitions();
65         assertEquals(8, bDataMethods.size());
66
67         final var bEnumType = bDataMethods.stream()
68             .filter(method -> method.getName().equals("getEnum"))
69             .findFirst()
70             .orElseThrow()
71             .getReturnType();
72         assertThat(bEnumType, instanceOf(Enumeration.class));
73         assertEquals(TEST_TYPE_PROVIDER + ".Foo.ResolveDirectUseOfEnum", bEnumType.getFullyQualifiedName());
74
75         final var bEnumsType = bDataMethods.stream()
76             .filter(method -> method.getName().equals("getEnums"))
77             .findFirst()
78             .orElseThrow()
79             .getReturnType();
80
81         assertThat(bEnumsType, instanceOf(ParameterizedType.class));
82         final var enumsType = (ParameterizedType) bEnumsType;
83
84         assertEquals(Types.typeForClass(List.class), enumsType.getRawType());
85         final var enumsTypeArgs = enumsType.getActualTypeArguments();
86         assertEquals(1, enumsTypeArgs.length);
87         assertEquals(TEST_TYPE_PROVIDER + ".Foo.ListOfEnums", enumsTypeArgs[0].getFullyQualifiedName());
88     }
89
90     @Test
91     public void generatedTypeForExtendedDefinitionTypeWithIdentityrefBaseTypeTest() {
92         final var cttName = JavaTypeName.create(TEST_TYPE_PROVIDER, "ConstructionTypeTest");
93         final var ctt = TYPES.stream()
94             .filter(type -> type.getIdentifier().equals(cttName))
95             .findFirst()
96             .orElseThrow();
97
98         final var methods = ctt.getMethodDefinitions();
99         assertEquals(56, methods.size());
100
101         final var type = methods.stream().filter(method -> method.getName().equals("getAesIdentityrefType"))
102             .findFirst()
103             .orElseThrow()
104             .getReturnType();
105         assertThat(type, instanceOf(ParameterizedType.class));
106         final var pType = (ParameterizedType) type;
107         assertEquals(Types.CLASS, pType.getRawType());
108         final var pTypeArgs = pType.getActualTypeArguments();
109         assertEquals(1, pTypeArgs.length);
110         assertEquals(TEST_TYPE_PROVIDER + ".Aes", pTypeArgs[0].getFullyQualifiedName());
111     }
112 }