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
9 package org.opendaylight.netconf.messagebus.eventsources.netconf;
11 import static org.mockito.Matchers.eq;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.verify;
16 import com.google.common.base.Function;
17 import com.google.common.base.Optional;
18 import com.google.common.collect.Lists;
19 import com.google.common.util.concurrent.Futures;
20 import java.time.ZoneId;
21 import java.time.format.DateTimeFormatter;
22 import java.util.Date;
23 import java.util.List;
24 import javax.annotation.Nullable;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.ArgumentCaptor;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
33 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
34 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
35 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
36 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamBuilder;
43 import org.opendaylight.yangtools.yang.common.QName;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
48 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
50 public class NetconfEventSourceMountTest {
52 public static final String STREAM_1 = "stream-1";
53 public static final String STREAM_2 = "stream-2";
55 private DOMMountPoint domMountPoint;
57 DOMDataBroker dataBroker;
59 DOMRpcService rpcService;
61 private DOMDataReadOnlyTransaction tx;
62 private NetconfEventSourceMount mount;
65 public void setUp() throws Exception {
66 MockitoAnnotations.initMocks(this);
67 doReturn(Optional.of(dataBroker)).when(domMountPoint).getService(DOMDataBroker.class);
68 doReturn(Optional.of(rpcService)).when(domMountPoint).getService(DOMRpcService.class);
69 doReturn(Optional.of(mock(DOMNotificationService.class))).when(domMountPoint)
70 .getService(DOMNotificationService.class);
71 doReturn(tx).when(dataBroker).newReadOnlyTransaction();
72 final YangInstanceIdentifier path = YangInstanceIdentifier.builder().node(Netconf.QNAME).node(Streams.QNAME)
74 final NormalizedNode<?, ?> streamsNode = NetconfTestUtils.getStreamsNode(STREAM_1, STREAM_2);
75 doReturn(Futures.immediateCheckedFuture(Optional.of(streamsNode))).when(tx).read(LogicalDatastoreType
77 mount = new NetconfEventSourceMount(NetconfTestUtils.getNode("node-1"), domMountPoint);
81 public void testInvokeCreateSubscription() throws Exception {
82 Stream stream = new StreamBuilder()
83 .setName(new StreamNameType(STREAM_1))
85 mount.invokeCreateSubscription(stream, Optional.absent());
86 final SchemaPath type = SchemaPath.create(true, QName.create(CreateSubscriptionInput.QNAME,
87 "create-subscription"));
88 ArgumentCaptor<ContainerNode> captor = ArgumentCaptor.forClass(ContainerNode.class);
89 verify(rpcService).invokeRpc(eq(type), captor.capture());
90 Assert.assertEquals(STREAM_1, getStreamName(captor.getValue()));
94 public void testInvokeCreateSubscription1() throws Exception {
95 Stream stream = new StreamBuilder()
96 .setName(new StreamNameType(STREAM_1))
97 .setReplaySupport(true)
99 final Date date = new Date();
100 mount.invokeCreateSubscription(stream, Optional.of(date));
101 final SchemaPath type = SchemaPath.create(true, QName.create(CreateSubscriptionInput.QNAME,
102 "create-subscription"));
103 ArgumentCaptor<ContainerNode> captor = ArgumentCaptor.forClass(ContainerNode.class);
104 verify(rpcService).invokeRpc(eq(type), captor.capture());
105 Assert.assertEquals(STREAM_1, getStreamName(captor.getValue()));
106 final String expDate = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(date.toInstant().atZone(ZoneId
108 final Optional<LeafNode> actual = (Optional<LeafNode>) getDate(captor.getValue());
109 Assert.assertTrue(actual.isPresent());
110 String actualDate = (String) actual.get().getValue();
111 Assert.assertEquals(expDate, actualDate);
115 public void testInvokeCreateSubscription2() throws Exception {
116 Stream stream = new StreamBuilder()
117 .setName(new StreamNameType(STREAM_1))
118 .setReplaySupport(true)
120 mount.invokeCreateSubscription(stream, Optional.absent());
121 final SchemaPath type = SchemaPath.create(true, QName.create(CreateSubscriptionInput.QNAME,
122 "create-subscription"));
123 ArgumentCaptor<ContainerNode> captor = ArgumentCaptor.forClass(ContainerNode.class);
124 verify(rpcService).invokeRpc(eq(type), captor.capture());
125 Assert.assertEquals(STREAM_1, getStreamName(captor.getValue()));
126 final Optional<LeafNode> date = (Optional<LeafNode>) getDate(captor.getValue());
127 Assert.assertFalse(date.isPresent());
132 public void testGetAvailableStreams() throws Exception {
133 final List<Stream> availableStreams = mount.getAvailableStreams();
134 Assert.assertEquals(2, availableStreams.size());
135 final List<String> streamNames = Lists.transform(availableStreams, new Function<Stream, String>() {
138 public String apply(@Nullable final Stream input) {
139 return input.getName().getValue();
142 streamNames.contains(STREAM_1);
143 streamNames.contains(STREAM_2);
146 private static String getStreamName(final ContainerNode value) {
147 YangInstanceIdentifier.NodeIdentifier stream =
148 new YangInstanceIdentifier.NodeIdentifier(QName.create(CreateSubscriptionInput.QNAME, "stream"));
149 return (String) value.getChild(stream).get().getValue();
152 private static Optional<?> getDate(final ContainerNode value) {
153 YangInstanceIdentifier.NodeIdentifier startTime =
154 new YangInstanceIdentifier.NodeIdentifier(QName.create(CreateSubscriptionInput.QNAME, "startTime"));
155 return Optional.fromJavaUtil(value.getChild(startTime));