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