/* * Copyright (c) 2016 Cisco Systems, 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.restconf.nb.rfc8040.streams; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import com.google.common.collect.ImmutableSet; import java.time.Instant; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; import org.opendaylight.mdsal.dom.api.DOMNotification; import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; import org.opendaylight.yangtools.yang.data.api.schema.LeafNode; import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; import org.opendaylight.yangtools.yang.data.impl.schema.Builders; import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @RunWith(MockitoJUnitRunner.StrictStubs.class) public class JsonNotificationListenerTest extends AbstractNotificationListenerTest { private static final Logger LOG = LoggerFactory.getLogger(JsonNotificationListenerTest.class); private final ListenersBroker listenersBroker = new ListenersBroker.ServerSentEvents(); @Test public void notifi_leafTest() throws Exception { final QName schemaPathNotifi = QName.create(MODULE, "notifi-leaf"); final DOMNotification notificationData = mock(DOMNotification.class); final LeafNode leaf = mockLeaf(QName.create(MODULE, "lf")); final ContainerNode notifiBody = mockCont(schemaPathNotifi, leaf); when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi)); when(notificationData.getBody()).thenReturn(notifiBody); final String result = prepareJson(notificationData, schemaPathNotifi); LOG.info("json result: {}", result); assertTrue(result.contains("ietf-restconf:notification")); assertTrue(result.contains("event-time")); assertTrue(result.contains("notifi-module:notifi-leaf")); assertTrue(result.contains("lf" + '"' + ":" + '"' + "value")); } @Test public void notifi_cont_leafTest() throws Exception { final QName schemaPathNotifi = QName.create(MODULE, "notifi-cont"); final DOMNotification notificationData = mock(DOMNotification.class); final LeafNode leaf = mockLeaf(QName.create(MODULE, "lf")); final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), leaf); final ContainerNode notifiBody = mockCont(schemaPathNotifi, cont); when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi)); when(notificationData.getBody()).thenReturn(notifiBody); final String result = prepareJson(notificationData, schemaPathNotifi); assertTrue(result.contains("ietf-restconf:notification")); assertTrue(result.contains("event-time")); assertTrue(result.contains("notifi-module:notifi-cont")); assertTrue(result.contains("cont")); assertTrue(result.contains("lf" + '"' + ":" + '"' + "value")); } @Test public void notifi_list_Test() throws Exception { final QName schemaPathNotifi = QName.create(MODULE, "notifi-list"); final DOMNotification notificationData = mock(DOMNotification.class); final LeafNode leaf = mockLeaf(QName.create(MODULE, "lf")); final MapEntryNode entry = mockMapEntry(QName.create(MODULE, "lst"), leaf); final ContainerNode notifiBody = mockCont(schemaPathNotifi, Builders.mapBuilder() .withNodeIdentifier(NodeIdentifier.create(QName.create(MODULE, "lst"))) .withChild(entry) .build()); when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi)); when(notificationData.getBody()).thenReturn(notifiBody); final String result = prepareJson(notificationData, schemaPathNotifi); assertTrue(result.contains("ietf-restconf:notification")); assertTrue(result.contains("event-time")); assertTrue(result.contains("notifi-module:notifi-list")); assertTrue(result.contains("lst")); assertTrue(result.contains("lf" + '"' + ":" + '"' + "value")); } @Test public void notifi_grpTest() throws Exception { final QName schemaPathNotifi = QName.create(MODULE, "notifi-grp"); final DOMNotification notificationData = mock(DOMNotification.class); final LeafNode leaf = mockLeaf(QName.create(MODULE, "lf")); final ContainerNode notifiBody = mockCont(schemaPathNotifi, leaf); when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi)); when(notificationData.getBody()).thenReturn(notifiBody); final String result = prepareJson(notificationData, schemaPathNotifi); assertTrue(result.contains("ietf-restconf:notification")); assertTrue(result.contains("event-time")); assertTrue(result.contains("lf" + '"' + ":" + '"' + "value")); } @Test public void notifi_augmTest() throws Exception { final QName schemaPathNotifi = QName.create(MODULE, "notifi-augm"); final DOMNotification notificationData = mock(DOMNotification.class); final LeafNode leaf = mockLeaf(QName.create(MODULE, "lf-augm")); final ContainerNode notifiBody = mockCont(schemaPathNotifi, leaf); when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi)); when(notificationData.getBody()).thenReturn(notifiBody); final String result = prepareJson(notificationData, schemaPathNotifi); assertTrue(result.contains("ietf-restconf:notification")); assertTrue(result.contains("event-time")); assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value")); } private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode leaf) { return Builders.mapEntryBuilder() .withNodeIdentifier(NodeIdentifierWithPredicates.of(entryQName, leaf.name().getNodeType(), leaf.body())) .withChild(leaf) .build(); } private static ContainerNode mockCont(final QName contQName, final DataContainerChild child) { return Builders.containerBuilder() .withNodeIdentifier(NodeIdentifier.create(contQName)) .withChild(child) .build(); } private static LeafNode mockLeaf(final QName leafQName) { return ImmutableNodes.leafNode(leafQName, "value"); } private String prepareJson(final DOMNotification notificationData, final QName schemaPathNotifi) throws Exception { final var notifiAdapter = listenersBroker.registerNotificationListener(MODEL_CONTEXT, ImmutableSet.of(schemaPathNotifi), NotificationOutputType.JSON); return notifiAdapter.formatter().eventData(MODEL_CONTEXT, notificationData, Instant.now()).orElseThrow(); } }