Bug 8237 - BI to BA conversion not resolving nested nodes
[mdsal.git] / binding / mdsal-binding-dom-codec / src / test / java / org / opendaylight / yangtools / binding / data / codec / test / InstanceIdentifierNestedLeafSerializeDeserializeTest.java
1 /*
2  * Copyright (c) 2017 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.yangtools.binding.data.codec.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import java.util.Map.Entry;
14 import javassist.ClassPool;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.yang.gen.v1.bug8237.rev180405.SimpleContainer;
18 import org.opendaylight.yang.gen.v1.bug8237.rev180405.simple.container.SimpleList;
19 import org.opendaylight.yang.gen.v1.bug8237.rev180405.simple.container.SimpleListKey;
20 import org.opendaylight.yang.gen.v1.bug8237.rev180405.simple.container.simple.list.ContUnderList;
21 import org.opendaylight.yang.gen.v1.bug8237.rev180405.simple.container.simple.list.ContUnderListBuilder;
22 import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
23 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
24 import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30
31 public class InstanceIdentifierNestedLeafSerializeDeserializeTest extends AbstractBindingRuntimeTest {
32
33     private static final String SIMPLE_LIST_KEY_NAME_VALUE = "simple-key-list-name";
34
35     private static final QName SIMPLE_CONTAINER_QNAME = QName.create("bug8237", "2017-16-05", "simple-container");
36     private static final QName SIMPLE_LIST_QNAME = QName.create(SIMPLE_CONTAINER_QNAME, "simple-list");
37     private static final QName SIMPLE_LIST_KEY_NAME_QNAME = QName.create(SIMPLE_CONTAINER_QNAME, "name");
38     private static final QName CONT_UNDER_LIST_QNAME = QName.create(SIMPLE_CONTAINER_QNAME, "cont-under-list");
39     private static final QName NESTED_NAME_QNAME = QName.create(SIMPLE_CONTAINER_QNAME, "nested-name");
40
41     private static final YangInstanceIdentifier BI_SIMPLE_CONTAINER_PATH =
42             YangInstanceIdentifier.builder().node(SIMPLE_CONTAINER_QNAME).build();
43     private static final YangInstanceIdentifier BI_SIMPLE_LIST_PATH = BI_SIMPLE_CONTAINER_PATH.node(SIMPLE_LIST_QNAME);
44     private static final YangInstanceIdentifier BI_SIMPLE_LIST_KEY_PATH =
45             BI_SIMPLE_LIST_PATH.node(new YangInstanceIdentifier.NodeIdentifierWithPredicates(SIMPLE_LIST_QNAME,
46                     SIMPLE_LIST_KEY_NAME_QNAME, SIMPLE_LIST_KEY_NAME_VALUE));
47     private static final YangInstanceIdentifier BI_CONT_UNDER_LIST_PATH =
48             BI_SIMPLE_LIST_KEY_PATH.node(CONT_UNDER_LIST_QNAME);
49     private static final YangInstanceIdentifier BI_NESTED_NAME_PATH =
50             BI_CONT_UNDER_LIST_PATH.node(new YangInstanceIdentifier.NodeIdentifier(NESTED_NAME_QNAME));
51
52     private BindingNormalizedNodeCodecRegistry registry;
53
54     @Override
55     @Before
56     public void setup() {
57         super.setup();
58         final JavassistUtils utils = JavassistUtils.forClassPool(ClassPool.getDefault());
59         this.registry = new BindingNormalizedNodeCodecRegistry(StreamWriterGenerator.create(utils));
60         this.registry.onBindingRuntimeContextUpdated(getRuntimeContext());
61     }
62
63     @Test
64     public void leafReferenceTest() {
65         final InstanceIdentifier<?> fromYangInstanceIdentifier =
66                 this.registry.fromYangInstanceIdentifier(BI_NESTED_NAME_PATH);
67         assertNull(fromYangInstanceIdentifier);
68     }
69
70     @Test
71     public void containerReferenceTest() {
72         final InstanceIdentifier<?> fromYangInstanceIdentifier =
73                 this.registry.fromYangInstanceIdentifier(BI_CONT_UNDER_LIST_PATH);
74         assertNotNull(fromYangInstanceIdentifier);
75         assertEquals(ContUnderList.class, fromYangInstanceIdentifier.getTargetType());
76     }
77
78     @Test
79     public void listReferenceTest() {
80         final InstanceIdentifier<?> fromYangInstanceIdentifier =
81                 this.registry.fromYangInstanceIdentifier(BI_SIMPLE_LIST_KEY_PATH);
82         assertNotNull(fromYangInstanceIdentifier);
83         assertEquals(SimpleList.class, fromYangInstanceIdentifier.getTargetType());
84     }
85
86     @Test
87     public void leafBItoBATest() {
88         final SimpleListKey KEY = new SimpleListKey("key");
89         final InstanceIdentifier<ContUnderList> BA_II_CONT_UNDER_LIST = InstanceIdentifier
90                 .builder(SimpleContainer.class).child(SimpleList.class, KEY).child(ContUnderList.class).build();
91         final ContUnderList data = new ContUnderListBuilder().setNestedName("name").build();
92         final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalizedNode =
93                 this.registry.toNormalizedNode(BA_II_CONT_UNDER_LIST, data);
94         assertNotNull(normalizedNode);
95
96         final Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode =
97                 this.registry.fromNormalizedNode(BI_CONT_UNDER_LIST_PATH, normalizedNode.getValue());
98         assertNotNull(fromNormalizedNode);
99         final ContUnderList value = (ContUnderList) fromNormalizedNode.getValue();
100         assertEquals("name", value.getNestedName());
101     }
102 }