Add unit tests for ListenerAdapter 03/63203/6
authorJosh <jhershbe@redhat.com>
Sun, 17 Sep 2017 12:16:51 +0000 (15:16 +0300)
committerJosh <jhershbe@redhat.com>
Sun, 15 Oct 2017 09:13:53 +0000 (12:13 +0300)
Test that json generated by ListenerAdapter is correct
for regular notifications and "leaf nodes only" notifications.

Change-Id: I62fa22b1e30ca6a8632b3d2614558e9ced0abf54
Signed-off-by: Josh <jhershbe@redhat.com>
restconf/restconf-nb-bierman02/pom.xml
restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/netconf/sal/streams/listeners/ListenerAdapterTest.java [new file with mode: 0644]
restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-create.json [new file with mode: 0644]
restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-del.json [new file with mode: 0644]
restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-leaves-create.json [new file with mode: 0644]
restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-leaves-del.json [new file with mode: 0644]
restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-leaves-update.json [new file with mode: 0644]
restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-update.json [new file with mode: 0644]

index 168bcc2c424cef37b06bd8e7d0a37ce9cac792c4..8a2b28ee2d82fde597151e8cf95338f50feedcaa 100644 (file)
       <groupId>org.opendaylight.aaa</groupId>
       <artifactId>aaa-shiro-api</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.opendaylight.yangtools</groupId>
+      <artifactId>testutils</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.controller</groupId>
+      <artifactId>sal-binding-broker-impl</artifactId>
+      <type>test-jar</type>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>com.google.guava</groupId>
+      <artifactId>guava-testlib</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.skyscreamer</groupId>
+      <artifactId>jsonassert</artifactId>
+      <version>1.5.0</version>
+    </dependency>
   </dependencies>
 
   <build>
diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/netconf/sal/streams/listeners/ListenerAdapterTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/netconf/sal/streams/listeners/ListenerAdapterTest.java
new file mode 100644 (file)
index 0000000..4249f53
--- /dev/null
@@ -0,0 +1,168 @@
+/*
+ * Copyright (c) 2017 Red Hat, 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.streams.listeners;
+
+import static java.time.Instant.EPOCH;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Optional;
+
+import org.json.JSONObject;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
+import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
+import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
+import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.PatchCont;
+import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1;
+import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1Builder;
+import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1Key;
+import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+import org.skyscreamer.jsonassert.JSONAssert;
+
+public class ListenerAdapterTest extends AbstractConcurrentDataBrokerTest {
+
+    private static final String JSON_NOTIF_LEAVES_CREATE = "/listener-adapter-test/notif-leaves-create.json";
+    private static final String JSON_NOTIF_LEAVES_UPDATE =  "/listener-adapter-test/notif-leaves-update.json";
+    private static final String JSON_NOTIF_LEAVES_DEL =  "/listener-adapter-test/notif-leaves-del.json";
+    private static final String JSON_NOTIF_CREATE = "/listener-adapter-test/notif-create.json";
+    private static final String JSON_NOTIF_UPDATE = "/listener-adapter-test/notif-update.json";
+    private static final String JSON_NOTIF_DEL = "/listener-adapter-test/notif-del.json";
+
+    private static YangInstanceIdentifier PATCH_CONT_YIID =
+            YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(PatchCont.QNAME));
+
+    private DataBroker dataBroker;
+    private DOMDataBroker domDataBroker;
+
+    @Before
+    public void setUp() throws Exception {
+        dataBroker = getDataBroker();
+        domDataBroker = getDomBroker();
+        SchemaContext sc = YangParserTestUtils.parseYangSource(
+                "/instanceidentifier/yang/instance-identifier-patch-module.yang");
+        ControllerContext.getInstance().setGlobalSchema(sc);
+    }
+
+    class ListenerAdapterTester extends ListenerAdapter {
+
+        private String lastNotification = null;
+
+        ListenerAdapterTester(YangInstanceIdentifier path, String streamName,
+                              NotificationOutputTypeGrouping.NotificationOutputType outputType, boolean leafNodesOnly) {
+            super(path, streamName, outputType);
+            setQueryParams(EPOCH, Optional.empty(), Optional.empty(), leafNodesOnly);
+        }
+
+        @Override
+        protected void post(final Event event) {
+            this.lastNotification = event.getData();
+        }
+
+        public void assertGot(String json) throws Exception {
+            long start = System.currentTimeMillis();
+            while (true) {
+                if (lastNotification != null) {
+                    break;
+                }
+                if (System.currentTimeMillis() - start > 1000) {
+                    throw new Exception("TIMED OUT waiting for notification with " + json);
+                }
+                Thread.currentThread().sleep(200);
+            }
+            JSONAssert.assertEquals(json, withFakeDate(lastNotification), false);
+            this.lastNotification = null;
+        }
+    }
+
+    static String withFakeDate(String in) {
+        JSONObject doc = new JSONObject(in);
+        JSONObject notification = doc.getJSONObject("notification");
+        if (notification == null) {
+            return in;
+        }
+        notification.put("eventTime", "someDate");
+        return doc.toString();
+    }
+
+    private String getNotifJson(String path) throws IOException, URISyntaxException {
+        URL url = getClass().getResource(path);
+        byte[] bytes = Files.readAllBytes(Paths.get(url.toURI()));
+        return withFakeDate(new String(bytes, StandardCharsets.UTF_8));
+    }
+
+    @Test
+    public void testJsonNotifsLeaves() throws Exception {
+
+        ListenerAdapterTester adapter = new ListenerAdapterTester(PATCH_CONT_YIID, "Casey",
+                                        NotificationOutputTypeGrouping.NotificationOutputType.JSON, true);
+        domDataBroker.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION, PATCH_CONT_YIID, adapter,
+                                                        AsyncDataBroker.DataChangeScope.SUBTREE);
+
+        WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
+        MyList1Builder builder = new MyList1Builder().setMyLeaf11("Jed").setName("Althea");
+        InstanceIdentifier<MyList1> iid = InstanceIdentifier.create(PatchCont.class)
+                .child(MyList1.class, new MyList1Key("Althea"));
+        writeTransaction.put(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
+        writeTransaction.submit();
+        adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_CREATE));
+
+        writeTransaction = dataBroker.newWriteOnlyTransaction();
+        builder.setMyLeaf12("Bertha");
+        writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
+        writeTransaction.submit();
+        adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_UPDATE));
+
+        writeTransaction = dataBroker.newWriteOnlyTransaction();
+        writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
+        writeTransaction.submit();
+        adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_DEL));
+    }
+
+    @Test
+    public void testJsonNotifs() throws Exception {
+
+        ListenerAdapterTester adapter = new ListenerAdapterTester(PATCH_CONT_YIID, "Casey",
+                NotificationOutputTypeGrouping.NotificationOutputType.JSON, false);
+        domDataBroker.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION, PATCH_CONT_YIID, adapter,
+                AsyncDataBroker.DataChangeScope.SUBTREE);
+
+        WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
+        MyList1Builder builder = new MyList1Builder().setMyLeaf11("Jed").setName("Althea");
+        InstanceIdentifier<MyList1> iid = InstanceIdentifier.create(PatchCont.class)
+                .child(MyList1.class, new MyList1Key("Althea"));
+        writeTransaction.put(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
+        writeTransaction.submit();
+        adapter.assertGot(getNotifJson(JSON_NOTIF_CREATE));
+
+        writeTransaction = dataBroker.newWriteOnlyTransaction();
+        builder.setMyLeaf12("Bertha");
+        writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
+        writeTransaction.submit();
+        adapter.assertGot(getNotifJson(JSON_NOTIF_UPDATE));
+
+        writeTransaction = dataBroker.newWriteOnlyTransaction();
+        writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
+        writeTransaction.submit();
+        adapter.assertGot(getNotifJson(JSON_NOTIF_DEL));
+    }
+}
diff --git a/restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-create.json b/restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-create.json
new file mode 100644 (file)
index 0000000..bc1cf52
--- /dev/null
@@ -0,0 +1,55 @@
+{
+    "notification": {
+        "data-changed-notification": {
+            "data-change-event": [
+                {
+                    "data": {
+                        "my-leaf11": {
+                            "content": "Jed",
+                            "xmlns": "instance:identifier:patch:module"
+                        }
+                    },
+                    "operation": "created",
+                    "path": "/instance-identifier-patch-module:patch-cont/instance-identifier-patch-module:my-list1[instance-identifier-patch-module:name='Althea']/instance-identifier-patch-module:my-leaf11"
+                },
+                {
+                    "data": {
+                        "name": {
+                            "content": "Althea",
+                            "xmlns": "instance:identifier:patch:module"
+                        }
+                    },
+                    "operation": "created",
+                    "path": "/instance-identifier-patch-module:patch-cont/instance-identifier-patch-module:my-list1[instance-identifier-patch-module:name='Althea']/instance-identifier-patch-module:name"
+                },
+                {
+                    "data": {
+                        "patch-cont": {
+                            "my-list1": {
+                                "my-leaf11": "Jed",
+                                "name": "Althea"
+                            },
+                            "xmlns": "instance:identifier:patch:module"
+                        }
+                    },
+                    "operation": "created",
+                    "path": "/instance-identifier-patch-module:patch-cont"
+                },
+                {
+                    "data": {
+                        "my-list1": {
+                            "my-leaf11": "Jed",
+                            "name": "Althea",
+                            "xmlns": "instance:identifier:patch:module"
+                        }
+                    },
+                    "operation": "created",
+                    "path": "/instance-identifier-patch-module:patch-cont/instance-identifier-patch-module:my-list1[instance-identifier-patch-module:name='Althea']"
+                }
+            ],
+            "xmlns": "urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote"
+        },
+        "eventTime": "2017-09-17T13:32:03.586+03:00",
+        "xmlns": "urn:ietf:params:xml:ns:netconf:notification:1.0"
+    }
+}
diff --git a/restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-del.json b/restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-del.json
new file mode 100644 (file)
index 0000000..19afc1d
--- /dev/null
@@ -0,0 +1,36 @@
+{
+    "notification": {
+        "data-changed-notification": {
+            "data-change-event": [
+                {
+                    "data": {
+                        "patch-cont": {
+                            "xmlns": "instance:identifier:patch:module"
+                        }
+                    },
+                    "operation": "updated",
+                    "path": "/instance-identifier-patch-module:patch-cont"
+                },
+                {
+                    "operation": "deleted",
+                    "path": "/instance-identifier-patch-module:patch-cont/instance-identifier-patch-module:my-list1[instance-identifier-patch-module:name='Althea']"
+                },
+                {
+                    "operation": "deleted",
+                    "path": "/instance-identifier-patch-module:patch-cont/instance-identifier-patch-module:my-list1[instance-identifier-patch-module:name='Althea']/instance-identifier-patch-module:name"
+                },
+                {
+                    "operation": "deleted",
+                    "path": "/instance-identifier-patch-module:patch-cont/instance-identifier-patch-module:my-list1[instance-identifier-patch-module:name='Althea']/instance-identifier-patch-module:my-leaf12"
+                },
+                {
+                    "operation": "deleted",
+                    "path": "/instance-identifier-patch-module:patch-cont/instance-identifier-patch-module:my-list1[instance-identifier-patch-module:name='Althea']/instance-identifier-patch-module:my-leaf11"
+                }
+            ],
+            "xmlns": "urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote"
+        },
+        "eventTime": "2017-09-17T14:18:53.404+03:00",
+        "xmlns": "urn:ietf:params:xml:ns:netconf:notification:1.0"
+    }
+}
diff --git a/restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-leaves-create.json b/restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-leaves-create.json
new file mode 100644 (file)
index 0000000..52be56a
--- /dev/null
@@ -0,0 +1,31 @@
+{
+    "notification": {
+        "data-changed-notification": {
+            "data-change-event": [
+                {
+                    "data": {
+                        "my-leaf11": {
+                            "content": "Jed",
+                            "xmlns": "instance:identifier:patch:module"
+                        }
+                    },
+                    "operation": "created",
+                    "path": "/instance-identifier-patch-module:patch-cont/instance-identifier-patch-module:my-list1[instance-identifier-patch-module:name='Althea']/instance-identifier-patch-module:my-leaf11"
+                },
+                {
+                    "data": {
+                        "name": {
+                            "content": "Althea",
+                            "xmlns": "instance:identifier:patch:module"
+                        }
+                    },
+                    "operation": "created",
+                    "path": "/instance-identifier-patch-module:patch-cont/instance-identifier-patch-module:my-list1[instance-identifier-patch-module:name='Althea']/instance-identifier-patch-module:name"
+                }
+            ],
+            "xmlns": "urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote"
+        },
+        "eventTime": "2017-09-17T11:23:10.323+03:00",
+        "xmlns": "urn:ietf:params:xml:ns:netconf:notification:1.0"
+    }
+}
diff --git a/restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-leaves-del.json b/restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-leaves-del.json
new file mode 100644 (file)
index 0000000..bffe4c3
--- /dev/null
@@ -0,0 +1,27 @@
+{
+    "notification": {
+        "data-changed-notification": {
+            "data-change-event": [
+                {
+                    "operation": "deleted",
+                    "path": "/instance-identifier-patch-module:patch-cont/instance-identifier-patch-module:my-list1[instance-identifier-patch-module:name='Althea']/instance-identifier-patch-module:my-leaf12"
+                },
+                {
+                    "operation": "deleted",
+                    "path": "/instance-identifier-patch-module:patch-cont/instance-identifier-patch-module:my-list1[instance-identifier-patch-module:name='Althea']/instance-identifier-patch-module:my-leaf11"
+                },
+                {
+                    "operation": "deleted",
+                    "path": "/instance-identifier-patch-module:patch-cont/instance-identifier-patch-module:my-list1[instance-identifier-patch-module:name='Althea']"
+                },
+                {
+                    "operation": "deleted",
+                    "path": "/instance-identifier-patch-module:patch-cont/instance-identifier-patch-module:my-list1[instance-identifier-patch-module:name='Althea']/instance-identifier-patch-module:name"
+                }
+            ],
+            "xmlns": "urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote"
+        },
+        "eventTime": "2017-09-17T14:03:35.261+03:00",
+        "xmlns": "urn:ietf:params:xml:ns:netconf:notification:1.0"
+    }
+}
diff --git a/restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-leaves-update.json b/restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-leaves-update.json
new file mode 100644 (file)
index 0000000..0adf702
--- /dev/null
@@ -0,0 +1,19 @@
+{
+    "notification": {
+        "data-changed-notification": {
+            "data-change-event": {
+                "data": {
+                    "my-leaf12": {
+                        "content": "Bertha",
+                        "xmlns": "instance:identifier:patch:module"
+                    }
+                },
+                "operation": "created",
+                "path": "/instance-identifier-patch-module:patch-cont/instance-identifier-patch-module:my-list1[instance-identifier-patch-module:name='Althea']/instance-identifier-patch-module:my-leaf12"
+            },
+            "xmlns": "urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote"
+        },
+        "eventTime": "2017-09-17T13:56:47.032+03:00",
+        "xmlns": "urn:ietf:params:xml:ns:netconf:notification:1.0"
+    }
+}
diff --git a/restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-update.json b/restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-update.json
new file mode 100644 (file)
index 0000000..065d620
--- /dev/null
@@ -0,0 +1,47 @@
+{
+    "notification": {
+        "data-changed-notification": {
+            "data-change-event": [
+                {
+                    "data": {
+                        "my-leaf12": {
+                            "content": "Bertha",
+                            "xmlns": "instance:identifier:patch:module"
+                        }
+                    },
+                    "operation": "created",
+                    "path": "/instance-identifier-patch-module:patch-cont/instance-identifier-patch-module:my-list1[instance-identifier-patch-module:name='Althea']/instance-identifier-patch-module:my-leaf12"
+                },
+                {
+                    "data": {
+                        "patch-cont": {
+                            "my-list1": {
+                                "my-leaf11": "Jed",
+                                "my-leaf12": "Bertha",
+                                "name": "Althea"
+                            },
+                            "xmlns": "instance:identifier:patch:module"
+                        }
+                    },
+                    "operation": "updated",
+                    "path": "/instance-identifier-patch-module:patch-cont"
+                },
+                {
+                    "data": {
+                        "my-list1": {
+                            "my-leaf11": "Jed",
+                            "my-leaf12": "Bertha",
+                            "name": "Althea",
+                            "xmlns": "instance:identifier:patch:module"
+                        }
+                    },
+                    "operation": "updated",
+                    "path": "/instance-identifier-patch-module:patch-cont/instance-identifier-patch-module:my-list1[instance-identifier-patch-module:name='Althea']"
+                }
+            ],
+            "xmlns": "urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote"
+        },
+        "eventTime": "2017-09-17T14:15:05.839+03:00",
+        "xmlns": "urn:ietf:params:xml:ns:netconf:notification:1.0"
+    }
+}