788b037984cf2500c80a6f46ce0c33f56fe3763b
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / mdsal / binding / generator / impl / UnionTypeDefTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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
14 import java.io.File;
15 import java.util.List;
16 import org.junit.Test;
17 import org.opendaylight.mdsal.binding.generator.api.BindingGenerator;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
20 import org.opendaylight.mdsal.binding.model.api.Type;
21 import org.opendaylight.mdsal.binding.model.util.Types;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24
25 public class UnionTypeDefTest {
26
27     @Test
28     public void unionTypeResolvingTest() throws Exception {
29         final File abstractTopology = new File(getClass().getResource("/union-test-models/abstract-topology.yang").toURI());
30         final File ietfInetTypes = new File(getClass().getResource("/ietf/ietf-inet-types.yang").toURI());
31         final SchemaContext context = YangParserTestUtils.parseYangFiles(abstractTopology, ietfInetTypes);
32
33         assertNotNull("context is null", context);
34         final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
35         final List<Type> genTypes = bindingGen.generateTypes(context);
36
37         assertNotNull("genTypes is null", genTypes);
38         assertFalse("genTypes is empty", genTypes.isEmpty());
39
40         // TODO: implement test
41     }
42
43     @Test
44     public void unionTypedefLeafrefTest() throws Exception {
45         final File yang = new File(getClass().getResource("/leafref_typedef_union/bug8449.yang").toURI());
46         final SchemaContext schemaContext = YangParserTestUtils.parseYangFiles(yang);
47         assertNotNull(schemaContext);
48         final List<Type> generateTypes = new BindingGeneratorImpl(false).generateTypes(schemaContext);
49         assertNotNull(generateTypes);
50         for (final Type type : generateTypes) {
51             if (type.getName().equals("Cont")) {
52                 final GeneratedType gt = ((GeneratedType) type);
53                 assertNotNull(gt);
54                 final GeneratedType refType = gt.getEnclosedTypes().iterator().next();
55                 for (final GeneratedProperty generatedProperty : refType.getProperties()) {
56                     switch (generatedProperty.getName()) {
57                         case "stringRefValue":
58                             assertEquals(Types.STRING, generatedProperty.getReturnType());
59                             break;
60                         case "value":
61                             assertEquals(Types.CHAR_ARRAY, generatedProperty.getReturnType());
62                             break;
63                     }
64                 }
65             }
66         }
67     }
68 }