From 01d82cd895e8c81011c613c4c6af9dcc362110ea Mon Sep 17 00:00:00 2001 From: Josh Date: Sun, 17 Sep 2017 15:16:51 +0300 Subject: [PATCH] Add unit tests for ListenerAdapter Test that json generated by ListenerAdapter is correct for regular notifications and "leaf nodes only" notifications. Change-Id: I62fa22b1e30ca6a8632b3d2614558e9ced0abf54 Signed-off-by: Josh --- restconf/restconf-nb-bierman02/pom.xml | 20 +++ .../listeners/ListenerAdapterTest.java | 168 ++++++++++++++++++ .../listener-adapter-test/notif-create.json | 55 ++++++ .../listener-adapter-test/notif-del.json | 36 ++++ .../notif-leaves-create.json | 31 ++++ .../notif-leaves-del.json | 27 +++ .../notif-leaves-update.json | 19 ++ .../listener-adapter-test/notif-update.json | 47 +++++ 8 files changed, 403 insertions(+) create mode 100644 restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/netconf/sal/streams/listeners/ListenerAdapterTest.java create mode 100644 restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-create.json create mode 100644 restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-del.json create mode 100644 restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-leaves-create.json create mode 100644 restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-leaves-del.json create mode 100644 restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-leaves-update.json create mode 100644 restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-update.json diff --git a/restconf/restconf-nb-bierman02/pom.xml b/restconf/restconf-nb-bierman02/pom.xml index 168bcc2c42..8a2b28ee2d 100644 --- a/restconf/restconf-nb-bierman02/pom.xml +++ b/restconf/restconf-nb-bierman02/pom.xml @@ -213,6 +213,26 @@ org.opendaylight.aaa aaa-shiro-api + + org.opendaylight.yangtools + testutils + test + + + org.opendaylight.controller + sal-binding-broker-impl + test-jar + test + + + com.google.guava + guava-testlib + + + org.skyscreamer + jsonassert + 1.5.0 + 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 index 0000000000..4249f53ecb --- /dev/null +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/netconf/sal/streams/listeners/ListenerAdapterTest.java @@ -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 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 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 index 0000000000..bc1cf521b4 --- /dev/null +++ b/restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-create.json @@ -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 index 0000000000..19afc1dec5 --- /dev/null +++ b/restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-del.json @@ -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 index 0000000000..52be56afbd --- /dev/null +++ b/restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-leaves-create.json @@ -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 index 0000000000..bffe4c3515 --- /dev/null +++ b/restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-leaves-del.json @@ -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 index 0000000000..0adf702b59 --- /dev/null +++ b/restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-leaves-update.json @@ -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 index 0000000000..065d6205a2 --- /dev/null +++ b/restconf/restconf-nb-bierman02/src/test/resources/listener-adapter-test/notif-update.json @@ -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" + } +} -- 2.36.6