DTOs for anydata/anyxml are not generated within a list
[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.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.util.Iterator;
16 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
20 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
21 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
22 import org.opendaylight.mdsal.binding.model.api.Type;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25
26 public class Mdsal320Test {
27
28     @Test
29     public void mdsal320Test() {
30         final SchemaContext context = YangParserTestUtils.parseYangResource("/mdsal320.yang");
31
32         final List<Type> generateTypes = new BindingGeneratorImpl().generateTypes(context);
33         assertNotNull(generateTypes);
34         assertEquals(4, generateTypes.size());
35
36         final Type fooType = generateTypes.stream().filter(type -> type.getFullyQualifiedName()
37             .equals("org.opendaylight.yang.gen.v1.urn.odl.yt320.norev.Foo")).findFirst().get();
38         assertTrue(fooType instanceof GeneratedType);
39         final GeneratedType foo = (GeneratedType) fooType;
40
41         GeneratedTransferObject bar = null;
42         GeneratedTransferObject bar1 = null;
43         for (GeneratedType enc : foo.getEnclosedTypes()) {
44             switch (enc.getName()) {
45                 case "Bar":
46                     assertTrue(enc instanceof GeneratedTransferObject);
47                     bar = (GeneratedTransferObject) enc;
48                     break;
49                 case "Bar$1":
50                     assertTrue(enc instanceof GeneratedTransferObject);
51                     bar1 = (GeneratedTransferObject) enc;
52                     break;
53                 default:
54                     throw new IllegalStateException("Unexpected type " + enc);
55             }
56         }
57         assertNotNull(bar);
58         assertTrue(bar.isUnionType());
59         assertNotNull(bar1);
60         assertTrue(bar1.isUnionType());
61
62         final Iterator<MethodSignature> it = foo.getMethodDefinitions().iterator();
63         assertTrue(it.hasNext());
64         final MethodSignature getImplIface = it.next();
65         assertEquals("implementedInterface", getImplIface.getName());
66         assertTrue(getImplIface.isDefault());
67         assertTrue(it.hasNext());
68
69         final MethodSignature getBar = it.next();
70         assertFalse(it.hasNext());
71         final Type getBarType = getBar.getReturnType();
72         assertTrue(getBarType instanceof GeneratedTransferObject);
73         final GeneratedTransferObject getBarTO = (GeneratedTransferObject) getBarType;
74         assertTrue(getBarTO.isUnionType());
75         assertEquals(bar, getBarTO);
76
77         final GeneratedProperty bar1Prop = bar.getProperties().stream().filter(prop -> "bar$1".equals(prop.getName()))
78                 .findFirst().get();
79         final Type bar1PropRet = bar1Prop.getReturnType();
80         assertEquals(bar1, bar1PropRet);
81     }
82 }