Reuse schemacontext in ListenerAdapterTest
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / streams / listeners / ListenerAdapterTest.java
1 /*
2  * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.nb.rfc8040.streams.listeners;
9
10 import static java.time.Instant.EPOCH;
11 import static org.junit.Assert.fail;
12
13 import com.google.common.util.concurrent.Uninterruptibles;
14 import java.io.IOException;
15 import java.net.URISyntaxException;
16 import java.net.URL;
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;
50
51 public class ListenerAdapterTest extends AbstractConcurrentDataBrokerTest {
52     private static final Logger LOG = LoggerFactory.getLogger(ListenerAdapterTest.class);
53
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";
60
61     private static final YangInstanceIdentifier PATCH_CONT_YIID =
62             YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(PatchCont.QNAME));
63
64     private static SchemaContext SCHEMA_CONTEXT;
65
66     private DataBroker dataBroker;
67     private DOMDataBroker domDataBroker;
68     private TransactionChainHandler transactionChainHandler;
69     private SchemaContextHandler schemaContextHandler;
70
71     @BeforeClass
72     public static void beforeClass() {
73         SCHEMA_CONTEXT = YangParserTestUtils.parseYangResource(
74                 "/instanceidentifier/yang/instance-identifier-patch-module.yang");
75     }
76
77     @AfterClass
78     public static void afterClass() {
79         SCHEMA_CONTEXT = null;
80     }
81
82     @Before
83     public void setUp() throws Exception {
84         dataBroker = getDataBroker();
85         domDataBroker = getDomBroker();
86
87         transactionChainHandler = new TransactionChainHandler(domDataBroker);
88         schemaContextHandler = SchemaContextHandler.newInstance(transactionChainHandler,
89                 Mockito.mock(DOMSchemaService.class));
90         schemaContextHandler.onGlobalContextUpdated(SCHEMA_CONTEXT);
91     }
92
93     class ListenerAdapterTester extends ListenerAdapter {
94
95         private volatile String lastNotification;
96         private CountDownLatch notificationLatch = new CountDownLatch(1);
97
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);
103         }
104
105         @Override
106         protected void post(final String data) {
107             this.lastNotification = data;
108             notificationLatch.countDown();
109         }
110
111         public void assertGot(final String json) {
112             if (!Uninterruptibles.awaitUninterruptibly(notificationLatch, 5, TimeUnit.SECONDS)) {
113                 fail("Timed out waiting for notification for: " + json);
114             }
115
116             LOG.info("lastNotification: {}", lastNotification);
117             String withFakeDate = withFakeDate(lastNotification);
118             LOG.info("Comparing: \n{}\n{}", json, withFakeDate);
119
120             JSONAssert.assertEquals(json, withFakeDate, false);
121             this.lastNotification = null;
122             notificationLatch = new CountDownLatch(1);
123         }
124     }
125
126     static String withFakeDate(final String in) {
127         JSONObject doc = new JSONObject(in);
128         JSONObject notification = doc.getJSONObject("notification");
129         if (notification == null) {
130             return in;
131         }
132         notification.put("eventTime", "someDate");
133         return doc.toString();
134     }
135
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));
140     }
141
142     @Test
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);
147
148         DOMDataTreeChangeService changeService = domDataBroker.getExtensions()
149                 .getInstance(DOMDataTreeChangeService.class);
150         DOMDataTreeIdentifier root = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, PATCH_CONT_YIID);
151         changeService.registerDataTreeChangeListener(root, adapter);
152
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));
160
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));
166
167         writeTransaction = dataBroker.newWriteOnlyTransaction();
168         writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
169         writeTransaction.commit();
170         adapter.assertGot(getNotifJson(JSON_NOTIF_LEAVES_DEL));
171     }
172
173     @Test
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);
178
179         DOMDataTreeChangeService changeService = domDataBroker.getExtensions()
180                 .getInstance(DOMDataTreeChangeService.class);
181         DOMDataTreeIdentifier root = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, PATCH_CONT_YIID);
182         changeService.registerDataTreeChangeListener(root, adapter);
183
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));
191
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));
197
198         writeTransaction = dataBroker.newWriteOnlyTransaction();
199         writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
200         writeTransaction.commit();
201         adapter.assertGot(getNotifJson(JSON_NOTIF_DEL));
202     }
203 }