2 * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.streams.listeners;
10 import static java.time.Instant.EPOCH;
11 import static org.junit.Assert.fail;
13 import com.google.common.util.concurrent.Uninterruptibles;
14 import java.io.IOException;
15 import java.net.URISyntaxException;
17 import java.nio.charset.StandardCharsets;
18 import java.nio.file.Files;
19 import java.nio.file.Paths;
20 import java.util.concurrent.CountDownLatch;
21 import java.util.concurrent.TimeUnit;
22 import org.json.JSONObject;
23 import org.junit.AfterClass;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.binding.api.WriteTransaction;
30 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
31 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
32 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
33 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
34 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
35 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
36 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
37 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
38 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.PatchCont;
39 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1;
40 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1Builder;
41 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1Key;
42 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
46 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
47 import org.skyscreamer.jsonassert.JSONAssert;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public class ListenerAdapterTest extends AbstractConcurrentDataBrokerTest {
52 private static final Logger LOG = LoggerFactory.getLogger(ListenerAdapterTest.class);
54 private static final String JSON_NOTIF_LEAVES_CREATE = "/listener-adapter-test/notif-leaves-create.json";
55 private static final String JSON_NOTIF_LEAVES_UPDATE = "/listener-adapter-test/notif-leaves-update.json";
56 private static final String JSON_NOTIF_LEAVES_DEL = "/listener-adapter-test/notif-leaves-del.json";
57 private static final String JSON_NOTIF_CREATE = "/listener-adapter-test/notif-create.json";
58 private static final String JSON_NOTIF_UPDATE = "/listener-adapter-test/notif-update.json";
59 private static final String JSON_NOTIF_DEL = "/listener-adapter-test/notif-del.json";
61 private static final YangInstanceIdentifier PATCH_CONT_YIID =
62 YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(PatchCont.QNAME));
64 private static SchemaContext SCHEMA_CONTEXT;
66 private DataBroker dataBroker;
67 private DOMDataBroker domDataBroker;
68 private TransactionChainHandler transactionChainHandler;
69 private SchemaContextHandler schemaContextHandler;
72 public static void beforeClass() {
73 SCHEMA_CONTEXT = YangParserTestUtils.parseYangResource(
74 "/instanceidentifier/yang/instance-identifier-patch-module.yang");
78 public static void afterClass() {
79 SCHEMA_CONTEXT = null;
83 public void setUp() throws Exception {
84 dataBroker = getDataBroker();
85 domDataBroker = getDomBroker();
87 transactionChainHandler = new TransactionChainHandler(domDataBroker);
88 schemaContextHandler = SchemaContextHandler.newInstance(transactionChainHandler,
89 Mockito.mock(DOMSchemaService.class));
90 schemaContextHandler.onGlobalContextUpdated(SCHEMA_CONTEXT);
93 class ListenerAdapterTester extends ListenerAdapter {
95 private volatile String lastNotification;
96 private CountDownLatch notificationLatch = new CountDownLatch(1);
98 ListenerAdapterTester(final YangInstanceIdentifier path, final String streamName,
99 final NotificationOutputTypeGrouping.NotificationOutputType outputType,
100 final boolean leafNodesOnly) {
101 super(path, streamName, outputType);
102 setQueryParams(EPOCH, null, null, leafNodesOnly);
106 protected void post(final String data) {
107 this.lastNotification = data;
108 notificationLatch.countDown();
111 public void assertGot(final String json) {
112 if (!Uninterruptibles.awaitUninterruptibly(notificationLatch, 5, TimeUnit.SECONDS)) {
113 fail("Timed out waiting for notification for: " + json);
116 LOG.info("lastNotification: {}", lastNotification);
117 String withFakeDate = withFakeDate(lastNotification);
118 LOG.info("Comparing: \n{}\n{}", json, withFakeDate);
120 JSONAssert.assertEquals(json, withFakeDate, false);
121 this.lastNotification = null;
122 notificationLatch = new CountDownLatch(1);
126 static String withFakeDate(final String in) {
127 JSONObject doc = new JSONObject(in);
128 JSONObject notification = doc.getJSONObject("notification");
129 if (notification == null) {
132 notification.put("eventTime", "someDate");
133 return doc.toString();
136 private String getNotifJson(final String path) throws IOException, URISyntaxException {
137 URL url = getClass().getResource(path);
138 byte[] bytes = Files.readAllBytes(Paths.get(url.toURI()));
139 return withFakeDate(new String(bytes, StandardCharsets.UTF_8));
143 public void testJsonNotifsLeaves() throws Exception {
144 ListenerAdapterTester adapter = new ListenerAdapterTester(PATCH_CONT_YIID, "Casey",
145 NotificationOutputTypeGrouping.NotificationOutputType.JSON, true);
146 adapter.setCloseVars(transactionChainHandler, schemaContextHandler);
148 DOMDataTreeChangeService changeService = domDataBroker.getExtensions()
149 .getInstance(DOMDataTreeChangeService.class);
150 DOMDataTreeIdentifier root = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, PATCH_CONT_YIID);
151 changeService.registerDataTreeChangeListener(root, adapter);
153 WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
154 MyList1Builder builder = new MyList1Builder().setMyLeaf11("Jed").setName("Althea");
155 InstanceIdentifier<MyList1> iid = InstanceIdentifier.create(PatchCont.class)
156 .child(MyList1.class, new MyList1Key("Althea"));
157 writeTransaction.put(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
158 writeTransaction.commit();
159 adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_CREATE));
161 writeTransaction = dataBroker.newWriteOnlyTransaction();
162 builder = new MyList1Builder().withKey(new MyList1Key("Althea")).setMyLeaf12("Bertha");
163 writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
164 writeTransaction.commit();
165 adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_UPDATE));
167 writeTransaction = dataBroker.newWriteOnlyTransaction();
168 writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
169 writeTransaction.commit();
170 adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_DEL));
174 public void testJsonNotifs() throws Exception {
175 ListenerAdapterTester adapter = new ListenerAdapterTester(PATCH_CONT_YIID, "Casey",
176 NotificationOutputTypeGrouping.NotificationOutputType.JSON, false);
177 adapter.setCloseVars(transactionChainHandler, schemaContextHandler);
179 DOMDataTreeChangeService changeService = domDataBroker.getExtensions()
180 .getInstance(DOMDataTreeChangeService.class);
181 DOMDataTreeIdentifier root = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, PATCH_CONT_YIID);
182 changeService.registerDataTreeChangeListener(root, adapter);
184 WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
185 MyList1Builder builder = new MyList1Builder().setMyLeaf11("Jed").setName("Althea");
186 InstanceIdentifier<MyList1> iid = InstanceIdentifier.create(PatchCont.class)
187 .child(MyList1.class, new MyList1Key("Althea"));
188 writeTransaction.put(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
189 writeTransaction.commit();
190 adapter.assertGot(getNotifJson(JSON_NOTIF_CREATE));
192 writeTransaction = dataBroker.newWriteOnlyTransaction();
193 builder = new MyList1Builder().withKey(new MyList1Key("Althea")).setMyLeaf12("Bertha");
194 writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, iid, builder.build(), true);
195 writeTransaction.commit();
196 adapter.assertGot(getNotifJson(JSON_NOTIF_UPDATE));
198 writeTransaction = dataBroker.newWriteOnlyTransaction();
199 writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
200 writeTransaction.commit();
201 adapter.assertGot(getNotifJson(JSON_NOTIF_DEL));