Rework inlined union generation
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / mdsal / binding / generator / impl / Mdsal320Test.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 com.google.common.collect.Iterables;
15 import java.util.List;
16 import org.junit.Test;
17 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
20 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
21 import org.opendaylight.mdsal.binding.model.api.Type;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24
25 public class Mdsal320Test {
26
27     @Test
28     public void mdsal320Test() {
29         final SchemaContext context = YangParserTestUtils.parseYangResource("/mdsal320.yang");
30
31         final List<Type> generateTypes = new BindingGeneratorImpl().generateTypes(context);
32         assertNotNull(generateTypes);
33         assertEquals(4, generateTypes.size());
34
35         final Type fooType = generateTypes.stream().filter(type -> type.getFullyQualifiedName()
36             .equals("org.opendaylight.yang.gen.v1.urn.odl.yt320.norev.Foo")).findFirst().get();
37         assertTrue(fooType instanceof GeneratedType);
38         final GeneratedType foo = (GeneratedType) fooType;
39
40         GeneratedTransferObject bar = null;
41         GeneratedTransferObject bar1 = null;
42         for (GeneratedType enc : foo.getEnclosedTypes()) {
43             switch (enc.getName()) {
44                 case "Bar":
45                     assertTrue(enc instanceof GeneratedTransferObject);
46                     bar = (GeneratedTransferObject) enc;
47                     break;
48                 case "Bar$1":
49                     assertTrue(enc instanceof GeneratedTransferObject);
50                     bar1 = (GeneratedTransferObject) enc;
51                     break;
52                 default:
53                     throw new IllegalStateException("Unexpected type " + enc);
54             }
55         }
56         assertNotNull(bar);
57         assertTrue(bar.isUnionType());
58         assertNotNull(bar1);
59         assertTrue(bar1.isUnionType());
60
61         final MethodSignature getBar = Iterables.getOnlyElement(foo.getMethodDefinitions());
62         final Type getBarType = getBar.getReturnType();
63         assertTrue(getBarType instanceof GeneratedTransferObject);
64         final GeneratedTransferObject getBarTO = (GeneratedTransferObject) getBarType;
65         assertTrue(getBarTO.isUnionType());
66         assertEquals(bar, getBarTO);
67
68         final GeneratedProperty bar1Prop = bar.getProperties().stream().filter(prop -> "bar$1".equals(prop.getName()))
69                 .findFirst().get();
70         final Type bar1PropRet = bar1Prop.getReturnType();
71         assertEquals(bar1, bar1PropRet);
72     }
73 }