Implement NodeContainerProxy.getPath() 95/98295/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 4 Nov 2021 13:43:31 +0000 (14:43 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 4 Nov 2021 23:09:30 +0000 (00:09 +0100)
getPath() is being used for diagnostics in XML parser, let's implement
it in the obviously-correct way to get some more information.

Also modermize NodeContainerProxyTest and move it netconf-util, so it
is co-located with the class it is testing.

JIRA: NETCONF-828
Change-Id: Idfac9d779366d3465db29dcadc27af51dc16b956
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 6379e64a96f5d510b3f298ef207a2ea3c3fc0b63)

netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NodeContainerProxy.java
netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/NodeContainerProxyTest.java [new file with mode: 0644]
netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/util/NodeContainerProxyTest.java [deleted file]

index f32706242ff6876f571929c1a63dc320f2c0675f..139509a0e7cd1eb59f28b23a63415f2d2cfb4052 100644 (file)
@@ -9,17 +9,19 @@ package org.opendaylight.netconf.util;
 
 import static java.util.Objects.requireNonNull;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.Maps;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.Map;
 import java.util.Optional;
+import java.util.Set;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
@@ -37,25 +39,27 @@ import org.opendaylight.yangtools.yang.xpath.api.YangXPathExpression.QualifiedBo
  */
 @Deprecated(since = "2.0.0", forRemoval = true)
 public final class NodeContainerProxy implements ContainerSchemaNode {
-
-    private final Map<QName, ? extends DataSchemaNode> childNodes;
-    private final QName qualifiedName;
     private final Collection<? extends AugmentationSchemaNode> availableAugmentations;
-
-    public NodeContainerProxy(final QName qualifiedName, final Map<QName, ? extends DataSchemaNode> childNodes,
-                              final Collection<? extends AugmentationSchemaNode> availableAugmentations) {
+    private final @NonNull Map<QName, ? extends DataSchemaNode> childNodes;
+    private final @NonNull SchemaPath path;
+    private final @NonNull QName qname;
+
+    @VisibleForTesting
+    NodeContainerProxy(final QName qname, final SchemaPath path, final Map<QName, ? extends DataSchemaNode> childNodes,
+                       final Collection<? extends AugmentationSchemaNode> availableAugmentations) {
+        this.qname = requireNonNull(qname);
+        this.path = requireNonNull(path);
+        this.childNodes = requireNonNull(childNodes);
         this.availableAugmentations = availableAugmentations;
-        this.childNodes = requireNonNull(childNodes, "childNodes");
-        this.qualifiedName = qualifiedName;
     }
 
-    public NodeContainerProxy(final QName qualifiedName, final Collection<? extends DataSchemaNode> childNodes) {
-        this(qualifiedName, asMap(childNodes), Collections.emptySet());
+    public static @NonNull NodeContainerProxy ofModelContext(final QName qname, final EffectiveModelContext context) {
+        return new NodeContainerProxy(qname, SchemaPath.ROOT, asMap(context.getChildNodes()), Set.of());
     }
 
-    public NodeContainerProxy(final QName qualifiedName, final Collection<? extends DataSchemaNode> childNodes,
-                              final Collection<? extends AugmentationSchemaNode> availableAugmentations) {
-        this(qualifiedName, asMap(childNodes), availableAugmentations);
+    public static @NonNull NodeContainerProxy ofNotification(final NotificationDefinition notification) {
+        return new NodeContainerProxy(notification.getQName(), notification.getPath(),
+            asMap(notification.getChildNodes()), notification.getAvailableAugmentations());
     }
 
     private static Map<QName, ? extends DataSchemaNode> asMap(final Collection<? extends DataSchemaNode> childNodes) {
@@ -64,7 +68,7 @@ public final class NodeContainerProxy implements ContainerSchemaNode {
 
     @Override
     public Collection<? extends TypeDefinition<?>> getTypeDefinitions() {
-        return Collections.emptySet();
+        return Set.of();
     }
 
     @Override
@@ -74,7 +78,7 @@ public final class NodeContainerProxy implements ContainerSchemaNode {
 
     @Override
     public Collection<? extends GroupingDefinition> getGroupings() {
-        return Collections.emptySet();
+        return Set.of();
     }
 
     @Override
@@ -84,7 +88,7 @@ public final class NodeContainerProxy implements ContainerSchemaNode {
 
     @Override
     public Collection<? extends UsesNode> getUses() {
-        return Collections.emptySet();
+        return Set.of();
     }
 
     @Override
@@ -116,13 +120,13 @@ public final class NodeContainerProxy implements ContainerSchemaNode {
 
     @Override
     public QName getQName() {
-        return qualifiedName;
+        return qname;
     }
 
     @Override
     @Deprecated
     public SchemaPath getPath() {
-        throw new UnsupportedOperationException();
+        return path;
     }
 
     @Override
@@ -142,12 +146,12 @@ public final class NodeContainerProxy implements ContainerSchemaNode {
 
     @Override
     public Collection<? extends NotificationDefinition> getNotifications() {
-        return Collections.emptySet();
+        return Set.of();
     }
 
     @Override
     public Collection<? extends ActionDefinition> getActions() {
-        return Collections.emptySet();
+        return Set.of();
     }
 
     @Override
@@ -157,7 +161,7 @@ public final class NodeContainerProxy implements ContainerSchemaNode {
 
     @Override
     public Collection<? extends @NonNull MustDefinition> getMustConstraints() {
-        return Collections.emptySet();
+        return Set.of();
     }
 
     @Override
diff --git a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/NodeContainerProxyTest.java b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/NodeContainerProxyTest.java
new file mode 100644 (file)
index 0000000..d0a3528
--- /dev/null
@@ -0,0 +1,143 @@
+/*
+ * Copyright (c) 2016 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.netconf.util;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
+public class NodeContainerProxyTest {
+    private static final QName QNAME = QName.create("ns", "2016-10-19", "name");
+    private static final QName NODE_1_QNAME = QName.create(QNAME, "node-1");
+    private static final QName NODE_2_QNAME = QName.create(QNAME, "node-2");
+
+    @Mock
+    private AugmentationSchemaNode augSchema1;
+    @Mock
+    private AugmentationSchemaNode augSchema2;
+    @Mock
+    private DataSchemaNode schemaNode1;
+    @Mock
+    private DataSchemaNode schemaNode2;
+    private NodeContainerProxy proxy;
+
+    @Before
+    public void setUp() {
+        proxy = new NodeContainerProxy(QNAME, SchemaPath.SAME,
+            Map.of(NODE_1_QNAME, schemaNode1, NODE_2_QNAME, schemaNode2), Set.of(augSchema1, augSchema2));
+    }
+
+    @Test
+    public void testGetQName() {
+        assertSame(QNAME, proxy.getQName());
+    }
+
+    @Test
+    @Deprecated
+    public void testGetPath() {
+        assertSame(SchemaPath.SAME, proxy.getPath());
+    }
+
+    @Test
+    public void testGetChildNodes() {
+        final var children = proxy.getChildNodes();
+        assertEquals(2, children.size());
+        assertThat(children, containsInAnyOrder(schemaNode1, schemaNode2));
+    }
+
+    @Test
+    public void testGetAvailableAugmentations() {
+        final var augmentations = proxy.getAvailableAugmentations();
+        assertEquals(2, augmentations.size());
+        assertThat(augmentations, containsInAnyOrder(augSchema1, augSchema2));
+    }
+
+    @Test
+    public void testFindDataChildByName() {
+        assertEquals(Optional.of(schemaNode1), proxy.findDataChildByName(NODE_1_QNAME));
+    }
+
+    @Test
+    public void testGetTypeDefinitions() {
+        assertEmpty(proxy.getTypeDefinitions());
+    }
+
+    @Test
+    public void testGetGroupings() {
+        assertEmpty(proxy.getGroupings());
+    }
+
+    @Test
+    public void testGetUses() {
+        assertEmpty(proxy.getUses());
+    }
+
+    @Test
+    public void testGetUnknownSchemaNodes() {
+        assertEmpty(proxy.getUnknownSchemaNodes());
+    }
+
+    @Test
+    public void testIsPresenceContainer() {
+        assertThrows(UnsupportedOperationException.class, () -> proxy.isPresenceContainer());
+    }
+
+    @Test
+    @Deprecated
+    public void testIsAugmenting() {
+        assertThrows(UnsupportedOperationException.class, () -> proxy.isAugmenting());
+    }
+
+    @Test
+    @Deprecated
+    public void testIsAddedByUses() {
+        assertThrows(UnsupportedOperationException.class, () -> proxy.isAddedByUses());
+    }
+
+    @Test
+    public void testIsConfiguration() {
+        assertThrows(UnsupportedOperationException.class, () -> proxy.isConfiguration());
+    }
+
+    @Test
+    public void testGetDescription() {
+        assertThrows(UnsupportedOperationException.class, () -> proxy.getDescription());
+    }
+
+    @Test
+    public void testGetReference() {
+        assertThrows(UnsupportedOperationException.class, () -> proxy.getReference());
+    }
+
+    @Test
+    public void testGetStatus() {
+        assertThrows(UnsupportedOperationException.class, () -> proxy.getStatus());
+    }
+
+    static void assertEmpty(final Collection<?> coll) {
+        assertEquals(List.of(), List.copyOf(coll));
+    }
+}
diff --git a/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/util/NodeContainerProxyTest.java b/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/util/NodeContainerProxyTest.java
deleted file mode 100644 (file)
index 59cce4a..0000000
+++ /dev/null
@@ -1,139 +0,0 @@
-/*
- * Copyright (c) 2016 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.netconf.sal.connect.netconf.util;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Optional;
-import java.util.Set;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
-import org.opendaylight.netconf.util.NodeContainerProxy;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
-public class NodeContainerProxyTest {
-
-    private static final QName QNAME = QName.create("ns", "2016-10-19", "name");
-    private static final QName NODE_1_QNAME = QName.create(QNAME, "node-1");
-    private static final QName NODE_2_QNAME = QName.create(QNAME, "node-2");
-    @Mock
-    private AugmentationSchemaNode augSchema1;
-    @Mock
-    private AugmentationSchemaNode augSchema2;
-    @Mock
-    private DataSchemaNode schemaNode1;
-    @Mock
-    private DataSchemaNode schemaNode2;
-    private NodeContainerProxy proxy;
-
-    @Before
-    public void setUp() throws Exception {
-        final Map<QName, DataSchemaNode> childNodes = new HashMap<>();
-        childNodes.put(NODE_1_QNAME, schemaNode1);
-        childNodes.put(NODE_2_QNAME, schemaNode2);
-        final Set<AugmentationSchemaNode> augmentations = new HashSet<>();
-        augmentations.add(augSchema1);
-        augmentations.add(augSchema2);
-        proxy = new NodeContainerProxy(QNAME, childNodes, augmentations);
-    }
-
-    @Test
-    public void testGetQName() throws Exception {
-        assertEquals(QNAME, proxy.getQName());
-    }
-
-    @Test
-    public void testGetChildNodes() throws Exception {
-        assertEquals(2, proxy.getChildNodes().size());
-    }
-
-    @Test
-    public void testGetAvailableAugmentations() throws Exception {
-        final Collection<? extends AugmentationSchemaNode> augmentations = proxy.getAvailableAugmentations();
-        assertEquals(2, augmentations.size());
-        assertTrue(augmentations.contains(augSchema1));
-        assertTrue(augmentations.contains(augSchema2));
-    }
-
-    @Test
-    public void testFindDataChildByName() {
-        assertEquals(Optional.of(schemaNode1), proxy.findDataChildByName(NODE_1_QNAME));
-    }
-
-    @Test
-    public void testGetTypeDefinitions() throws Exception {
-        assertTrue(proxy.getTypeDefinitions().isEmpty());
-    }
-
-    @Test
-    public void testGetGroupings() throws Exception {
-        assertTrue(proxy.getGroupings().isEmpty());
-    }
-
-    @Test
-    public void testGetUses() throws Exception {
-        assertTrue(proxy.getUses().isEmpty());
-    }
-
-    @Test
-    public void testGetUnknownSchemaNodes() throws Exception {
-        assertTrue(proxy.getUnknownSchemaNodes().isEmpty());
-    }
-
-    @Test(expected = UnsupportedOperationException.class)
-    public void testIsPresenceContainer() throws Exception {
-        proxy.isPresenceContainer();
-    }
-
-    @Test(expected = UnsupportedOperationException.class)
-    public void testIsAugmenting() throws Exception {
-        proxy.isAugmenting();
-    }
-
-    @Test(expected = UnsupportedOperationException.class)
-    public void testIsAddedByUses() throws Exception {
-        proxy.isAddedByUses();
-    }
-
-    @Test(expected = UnsupportedOperationException.class)
-    public void testIsConfiguration() throws Exception {
-        proxy.isConfiguration();
-    }
-
-    @Test(expected = UnsupportedOperationException.class)
-    public void testGetPath() throws Exception {
-        proxy.getPath();
-    }
-
-    @Test(expected = UnsupportedOperationException.class)
-    public void testGetDescription() throws Exception {
-        proxy.getDescription();
-    }
-
-    @Test(expected = UnsupportedOperationException.class)
-    public void testGetReference() throws Exception {
-        proxy.getReference();
-    }
-
-    @Test(expected = UnsupportedOperationException.class)
-    public void testGetStatus() throws Exception {
-        proxy.getStatus();
-    }
-
-}