074b7cdc9e1d222a75bc4b17658e65b4f4bb5840
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / mdsal / binding / generator / impl / Mdsal406TypeObjectTest.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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 import static org.junit.Assert.assertTrue;
13 import static org.opendaylight.mdsal.binding.model.util.BindingTypes.TYPE_OBJECT;
14
15 import java.util.List;
16 import org.junit.Test;
17 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
18 import org.opendaylight.mdsal.binding.model.api.Type;
19 import org.opendaylight.mdsal.binding.model.util.BindingTypes;
20 import org.opendaylight.mdsal.binding.model.util.Types;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
23
24 public class Mdsal406TypeObjectTest {
25     @Test
26     public void typeObjectTest() {
27         final SchemaContext context = YangParserTestUtils.parseYangResources(ExtendedTypedefTest.class,
28                 "/type-provider/test.yang", "/ietf/ietf-inet-types.yang");
29
30         final List<Type> generateTypes = DefaultBindingGenerator.generateFor(context);
31         assertNotNull(generateTypes);
32
33         final Type typedefType = generateTypes.stream().filter(type -> type.getFullyQualifiedName()
34             .equals("org.opendaylight.yang.gen.v1.urn.opendaylight.test.rev131008.MyBinary")).findFirst().get();
35
36         assertTrue(typedefType instanceof GeneratedType);
37         assertNotNull(((GeneratedType)  typedefType).getImplements());
38         Type objectType = ((GeneratedType)  typedefType).getImplements().stream()
39                 .filter(type -> type.getFullyQualifiedName()
40                 .equals("org.opendaylight.yangtools.yang.binding.ScalarTypeObject")).findAny().get();
41         assertEquals(BindingTypes.scalarTypeObject(Types.BYTE_ARRAY), objectType);
42     }
43
44     @Test
45     public void typeObjectForBitsTypedefTest() {
46         final SchemaContext context = YangParserTestUtils.parseYangResources(ExtendedTypedefTest.class,
47                 "/type-provider/test.yang", "/ietf/ietf-inet-types.yang");
48
49         final List<Type> generateTypes = DefaultBindingGenerator.generateFor(context);
50         assertNotNull(generateTypes);
51
52         final Type typedefType = generateTypes.stream().filter(type -> type.getFullyQualifiedName()
53                 .equals("org.opendaylight.yang.gen.v1.urn.opendaylight.test.rev131008.MyBits")).findFirst().get();
54
55         assertTrue(typedefType instanceof GeneratedType);
56         assertNotNull(((GeneratedType)  typedefType).getImplements());
57         Type objectType = ((GeneratedType)  typedefType).getImplements().stream()
58                 .filter(type -> type.getFullyQualifiedName()
59                         .equals("org.opendaylight.yangtools.yang.binding.TypeObject")).findAny().get();
60         assertEquals(TYPE_OBJECT, objectType);
61     }
62
63     @Test
64     public void typeObjectForUnionTypedefTest() {
65         final SchemaContext context = YangParserTestUtils.parseYangResources(ExtendedTypedefTest.class,
66                 "/type-provider/test.yang", "/ietf/ietf-inet-types.yang");
67
68         final List<Type> generateTypes = DefaultBindingGenerator.generateFor(context);
69         assertNotNull(generateTypes);
70
71         final Type typedefType = generateTypes.stream().filter(type -> type.getFullyQualifiedName()
72                 .equals("org.opendaylight.yang.gen.v1.urn.opendaylight.test.rev131008.MyUnion")).findFirst().get();
73
74         assertTrue(typedefType instanceof GeneratedType);
75         assertNotNull(((GeneratedType)  typedefType).getImplements());
76         Type objectType = ((GeneratedType)  typedefType).getImplements().stream()
77                 .filter(type -> type.getFullyQualifiedName()
78                         .equals("org.opendaylight.yangtools.yang.binding.TypeObject")).findAny().get();
79         assertEquals(TYPE_OBJECT, objectType);
80     }
81 }