e87d7980c40300b8dbbe96fa624da73d81efd4b4
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / mdsal / binding / generator / impl / Mdsal161Test.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
14 import java.util.Collection;
15 import java.util.Optional;
16 import org.junit.Test;
17 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
18 import org.opendaylight.mdsal.binding.model.api.Type;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
21
22 public class Mdsal161Test {
23     /**
24      * Test if leaves with inner union type defined in groupings can be used as list keys at the place of instantiation.
25      */
26     @Test
27     public void mdsal161Test() {
28         final SchemaContext context = YangParserTestUtils.parseYangResource("/mdsal161.yang");
29         final Collection<Type> types = DefaultBindingGenerator.generateFor(context);
30         assertNotNull(types);
31         assertEquals(24, types.size());
32
33         assertKeyStructure(types, "org.opendaylight.yang.gen.v1.mdsal161.norev.WithGrpKey");
34         assertKeyStructure(types, "org.opendaylight.yang.gen.v1.mdsal161.norev.WithGrpExtKey");
35         assertKeyStructure(types, "org.opendaylight.yang.gen.v1.mdsal161.norev.WithGrpTypedefKey");
36         assertKeyStructure(types, "org.opendaylight.yang.gen.v1.mdsal161.norev.WithoutGrpKey");
37         assertKeyStructure(types, "org.opendaylight.yang.gen.v1.mdsal161.norev.WithoutGrpExtKey");
38         assertKeyStructure(types, "org.opendaylight.yang.gen.v1.mdsal161.norev.WithoutGrpTypedefKey");
39     }
40
41     private static void assertKeyStructure(final Collection<Type> types, final String className) {
42         final Optional<Type> optType = types.stream().filter(t -> t.getFullyQualifiedName().equals(className))
43                 .findFirst();
44         assertTrue("Type for " + className + " not found", optType.isPresent());
45
46         final Type type = optType.get();
47         assertTrue(type instanceof GeneratedTransferObject);
48         final GeneratedTransferObject gto = (GeneratedTransferObject) type;
49         assertEquals(2, gto.getEqualsIdentifiers().size());
50     }
51 }