2 * Copyright (c) 2016 Cisco Systems, 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.server.mdsal.streams.notif;
10 import static org.junit.jupiter.api.Assertions.assertNotNull;
11 import static org.junit.jupiter.api.Assertions.assertTrue;
12 import static org.mockito.Mockito.when;
14 import java.time.Instant;
15 import org.junit.jupiter.api.Test;
16 import org.junit.jupiter.api.extension.ExtendWith;
17 import org.mockito.Mock;
18 import org.mockito.junit.jupiter.MockitoExtension;
19 import org.opendaylight.mdsal.dom.api.DOMNotification;
20 import org.opendaylight.restconf.nb.rfc8040.streams.AbstractNotificationListenerTest;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
24 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
26 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
28 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
29 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
30 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
32 @ExtendWith(MockitoExtension.class)
33 class JSONNotificationFormatterTest extends AbstractNotificationListenerTest {
35 private DOMNotification notificationData;
38 void notifi_leafTest() throws Exception {
39 final QName schemaPathNotifi = QName.create(MODULE, "notifi-leaf");
41 final var leaf = mockLeaf(QName.create(MODULE, "lf"));
42 final var notifiBody = mockCont(schemaPathNotifi, leaf);
44 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
45 when(notificationData.getBody()).thenReturn(notifiBody);
47 final String result = prepareJson(schemaPathNotifi);
49 assertTrue(result.contains("ietf-restconf:notification"));
50 assertTrue(result.contains("event-time"));
51 assertTrue(result.contains("notifi-module:notifi-leaf"));
52 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
56 void notifi_cont_leafTest() throws Exception {
57 final QName schemaPathNotifi = QName.create(MODULE, "notifi-cont");
59 final var leaf = mockLeaf(QName.create(MODULE, "lf"));
60 final var cont = mockCont(QName.create(MODULE, "cont"), leaf);
61 final var notifiBody = mockCont(schemaPathNotifi, cont);
63 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
64 when(notificationData.getBody()).thenReturn(notifiBody);
66 final String result = prepareJson(schemaPathNotifi);
68 assertTrue(result.contains("ietf-restconf:notification"));
69 assertTrue(result.contains("event-time"));
70 assertTrue(result.contains("notifi-module:notifi-cont"));
71 assertTrue(result.contains("cont"));
72 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
76 void notifi_list_Test() throws Exception {
77 final QName schemaPathNotifi = QName.create(MODULE, "notifi-list");
79 final var leaf = mockLeaf(QName.create(MODULE, "lf"));
80 final var entry = mockMapEntry(QName.create(MODULE, "lst"), leaf);
81 final var notifiBody = mockCont(schemaPathNotifi, Builders.mapBuilder()
82 .withNodeIdentifier(NodeIdentifier.create(QName.create(MODULE, "lst")))
86 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
87 when(notificationData.getBody()).thenReturn(notifiBody);
89 final String result = prepareJson(schemaPathNotifi);
91 assertTrue(result.contains("ietf-restconf:notification"));
92 assertTrue(result.contains("event-time"));
93 assertTrue(result.contains("notifi-module:notifi-list"));
94 assertTrue(result.contains("lst"));
95 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
99 void notifi_grpTest() throws Exception {
100 final QName schemaPathNotifi = QName.create(MODULE, "notifi-grp");
102 final var leaf = mockLeaf(QName.create(MODULE, "lf"));
103 final var notifiBody = mockCont(schemaPathNotifi, leaf);
105 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
106 when(notificationData.getBody()).thenReturn(notifiBody);
108 final String result = prepareJson(schemaPathNotifi);
110 assertTrue(result.contains("ietf-restconf:notification"));
111 assertTrue(result.contains("event-time"));
112 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
116 void notifi_augmTest() throws Exception {
117 final QName schemaPathNotifi = QName.create(MODULE, "notifi-augm");
119 final var leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
120 final var notifiBody = mockCont(schemaPathNotifi, leaf);
122 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
123 when(notificationData.getBody()).thenReturn(notifiBody);
125 final String result = prepareJson(schemaPathNotifi);
127 assertTrue(result.contains("ietf-restconf:notification"));
128 assertTrue(result.contains("event-time"));
129 assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
132 private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
133 return Builders.mapEntryBuilder()
134 .withNodeIdentifier(NodeIdentifierWithPredicates.of(entryQName, leaf.name().getNodeType(), leaf.body()))
139 private static ContainerNode mockCont(final QName contQName, final DataContainerChild child) {
140 return Builders.containerBuilder()
141 .withNodeIdentifier(NodeIdentifier.create(contQName))
146 private static LeafNode<String> mockLeaf(final QName leafQName) {
147 return ImmutableNodes.leafNode(leafQName, "value");
150 private String prepareJson(final QName schemaPathNotifi) throws Exception {
151 final var ret = JSONNotificationFormatter.EMPTY.eventData(MODEL_CONTEXT, notificationData, Instant.now());