Bug 8237 - BI to BA conversion not resolving nested nodes 35/57135/4
authorJakub Toth <jatoth@cisco.com>
Tue, 16 May 2017 10:55:17 +0000 (12:55 +0200)
committerJakub Toth <jakub.toth@pantheon.tech>
Tue, 16 May 2017 12:08:14 +0000 (14:08 +0200)
  * not real bug - only example of used referencing in test

Change-Id: Ib5c0d2f27f368f6bca3f917a7ef922bcb5d75fa5
Signed-off-by: Jakub Toth <jakub.toth@pantheon.tech>
binding/mdsal-binding-dom-codec/src/test/java/org/opendaylight/yangtools/binding/data/codec/test/InstanceIdentifierNestedLeafSerializeDeserializeTest.java [new file with mode: 0644]
binding/mdsal-binding-test-model/src/main/yang/bug8237.yang [new file with mode: 0644]

diff --git a/binding/mdsal-binding-dom-codec/src/test/java/org/opendaylight/yangtools/binding/data/codec/test/InstanceIdentifierNestedLeafSerializeDeserializeTest.java b/binding/mdsal-binding-dom-codec/src/test/java/org/opendaylight/yangtools/binding/data/codec/test/InstanceIdentifierNestedLeafSerializeDeserializeTest.java
new file mode 100644 (file)
index 0000000..25649cd
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.binding.data.codec.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import java.util.Map.Entry;
+import javassist.ClassPool;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.bug8237.rev180405.SimpleContainer;
+import org.opendaylight.yang.gen.v1.bug8237.rev180405.simple.container.SimpleList;
+import org.opendaylight.yang.gen.v1.bug8237.rev180405.simple.container.SimpleListKey;
+import org.opendaylight.yang.gen.v1.bug8237.rev180405.simple.container.simple.list.ContUnderList;
+import org.opendaylight.yang.gen.v1.bug8237.rev180405.simple.container.simple.list.ContUnderListBuilder;
+import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
+import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
+import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+public class InstanceIdentifierNestedLeafSerializeDeserializeTest extends AbstractBindingRuntimeTest {
+
+    private static final String SIMPLE_LIST_KEY_NAME_VALUE = "simple-key-list-name";
+
+    private static final QName SIMPLE_CONTAINER_QNAME = QName.create("bug8237", "2017-16-05", "simple-container");
+    private static final QName SIMPLE_LIST_QNAME = QName.create(SIMPLE_CONTAINER_QNAME, "simple-list");
+    private static final QName SIMPLE_LIST_KEY_NAME_QNAME = QName.create(SIMPLE_CONTAINER_QNAME, "name");
+    private static final QName CONT_UNDER_LIST_QNAME = QName.create(SIMPLE_CONTAINER_QNAME, "cont-under-list");
+    private static final QName NESTED_NAME_QNAME = QName.create(SIMPLE_CONTAINER_QNAME, "nested-name");
+
+    private static final YangInstanceIdentifier BI_SIMPLE_CONTAINER_PATH =
+            YangInstanceIdentifier.builder().node(SIMPLE_CONTAINER_QNAME).build();
+    private static final YangInstanceIdentifier BI_SIMPLE_LIST_PATH = BI_SIMPLE_CONTAINER_PATH.node(SIMPLE_LIST_QNAME);
+    private static final YangInstanceIdentifier BI_SIMPLE_LIST_KEY_PATH =
+            BI_SIMPLE_LIST_PATH.node(new YangInstanceIdentifier.NodeIdentifierWithPredicates(SIMPLE_LIST_QNAME,
+                    SIMPLE_LIST_KEY_NAME_QNAME, SIMPLE_LIST_KEY_NAME_VALUE));
+    private static final YangInstanceIdentifier BI_CONT_UNDER_LIST_PATH =
+            BI_SIMPLE_LIST_KEY_PATH.node(CONT_UNDER_LIST_QNAME);
+    private static final YangInstanceIdentifier BI_NESTED_NAME_PATH =
+            BI_CONT_UNDER_LIST_PATH.node(new YangInstanceIdentifier.NodeIdentifier(NESTED_NAME_QNAME));
+
+    private BindingNormalizedNodeCodecRegistry registry;
+
+    @Override
+    @Before
+    public void setup() {
+        super.setup();
+        final JavassistUtils utils = JavassistUtils.forClassPool(ClassPool.getDefault());
+        this.registry = new BindingNormalizedNodeCodecRegistry(StreamWriterGenerator.create(utils));
+        this.registry.onBindingRuntimeContextUpdated(getRuntimeContext());
+    }
+
+    @Test
+    public void leafReferenceTest() {
+        final InstanceIdentifier<?> fromYangInstanceIdentifier =
+                this.registry.fromYangInstanceIdentifier(BI_NESTED_NAME_PATH);
+        assertNull(fromYangInstanceIdentifier);
+    }
+
+    @Test
+    public void containerReferenceTest() {
+        final InstanceIdentifier<?> fromYangInstanceIdentifier =
+                this.registry.fromYangInstanceIdentifier(BI_CONT_UNDER_LIST_PATH);
+        assertNotNull(fromYangInstanceIdentifier);
+        assertEquals(ContUnderList.class, fromYangInstanceIdentifier.getTargetType());
+    }
+
+    @Test
+    public void listReferenceTest() {
+        final InstanceIdentifier<?> fromYangInstanceIdentifier =
+                this.registry.fromYangInstanceIdentifier(BI_SIMPLE_LIST_KEY_PATH);
+        assertNotNull(fromYangInstanceIdentifier);
+        assertEquals(SimpleList.class, fromYangInstanceIdentifier.getTargetType());
+    }
+
+    @Test
+    public void leafBItoBATest() {
+        final SimpleListKey KEY = new SimpleListKey("key");
+        final InstanceIdentifier<ContUnderList> BA_II_CONT_UNDER_LIST = InstanceIdentifier
+                .builder(SimpleContainer.class).child(SimpleList.class, KEY).child(ContUnderList.class).build();
+        final ContUnderList data = new ContUnderListBuilder().setNestedName("name").build();
+        final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalizedNode =
+                this.registry.toNormalizedNode(BA_II_CONT_UNDER_LIST, data);
+        assertNotNull(normalizedNode);
+
+        final Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode =
+                this.registry.fromNormalizedNode(BI_CONT_UNDER_LIST_PATH, normalizedNode.getValue());
+        assertNotNull(fromNormalizedNode);
+        final ContUnderList value = (ContUnderList) fromNormalizedNode.getValue();
+        assertEquals("name", value.getNestedName());
+    }
+}
diff --git a/binding/mdsal-binding-test-model/src/main/yang/bug8237.yang b/binding/mdsal-binding-test-model/src/main/yang/bug8237.yang
new file mode 100644 (file)
index 0000000..26fff7b
--- /dev/null
@@ -0,0 +1,29 @@
+module bug8237 {
+    yang-version 1;
+    namespace "bug8237";
+    prefix "bug8237";
+
+    revision "2017-16-05" {
+    }
+
+    container simple-container {
+        list simple-list {
+            key name;
+            leaf name {
+                type string;
+            }
+            container cont-under-list {
+                leaf nested-name {
+                    type string;
+                }
+            }
+
+            list nested-list {
+                key nested-name;
+                leaf nested-name {
+                    type string;
+                }
+            }
+        }
+    }
+}