a2d1adbcc1aaf1d9c284aa990ffedee00ae12255
[netconf.git] / netconf / messagebus-netconf / src / test / java / org / opendaylight / netconf / messagebus / eventsources / netconf / NetconfEventSourceMountTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, 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.netconf.messagebus.eventsources.netconf;
9
10 import static org.mockito.ArgumentMatchers.eq;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.verify;
14
15 import com.google.common.collect.Collections2;
16 import java.time.Instant;
17 import java.time.ZoneId;
18 import java.time.format.DateTimeFormatter;
19 import java.util.Collection;
20 import java.util.Optional;
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
29 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
31 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
32 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
33 import org.opendaylight.mdsal.dom.api.DOMRpcService;
34 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamBuilder;
41 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
42 import org.opendaylight.yangtools.yang.common.QName;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
47
48 @RunWith(MockitoJUnitRunner.StrictStubs.class)
49 public class NetconfEventSourceMountTest extends AbstractCodecTest {
50     public static final String STREAM_1 = "stream-1";
51     public static final String STREAM_2 = "stream-2";
52
53     @Mock
54     private DOMMountPoint domMountPoint;
55     @Mock
56     DOMDataBroker dataBroker;
57     @Mock
58     DOMRpcService rpcService;
59     @Mock
60     DOMSchemaService schemaService;
61     @Mock
62     private DOMDataTreeReadTransaction tx;
63     private NetconfEventSourceMount mount;
64
65     @Before
66     public void setUp() {
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(Optional.of(schemaService)).when(domMountPoint).getService(DOMSchemaService.class);
72         doReturn(tx).when(dataBroker).newReadOnlyTransaction();
73         final YangInstanceIdentifier path = YangInstanceIdentifier.builder().node(Netconf.QNAME).node(Streams.QNAME)
74                 .build();
75         final NormalizedNode<?, ?> streamsNode = NetconfTestUtils.getStreamsNode(STREAM_1, STREAM_2);
76         doReturn(FluentFutures.immediateFluentFuture(Optional.of(streamsNode)))
77                 .when(tx).read(LogicalDatastoreType.OPERATIONAL, path);
78         mount = new NetconfEventSourceMount(SERIALIZER, NetconfTestUtils.getNode("node-1"), domMountPoint);
79     }
80
81     @Test
82     public void testInvokeCreateSubscription() throws Exception {
83         Stream stream = new StreamBuilder()
84                 .setName(new StreamNameType(STREAM_1))
85                 .build();
86         mount.invokeCreateSubscription(stream, Optional.empty());
87         final QName type = QName.create(CreateSubscriptionInput.QNAME, "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()));
91     }
92
93     @Test
94     public void testInvokeCreateSubscription1() throws Exception {
95         Stream stream = new StreamBuilder()
96                 .setName(new StreamNameType(STREAM_1))
97                 .setReplaySupport(true)
98                 .build();
99         final Instant date = Instant.now();
100         mount.invokeCreateSubscription(stream, Optional.of(date));
101         final QName type = QName.create(CreateSubscriptionInput.QNAME, "create-subscription");
102         ArgumentCaptor<ContainerNode> captor = ArgumentCaptor.forClass(ContainerNode.class);
103         verify(rpcService).invokeRpc(eq(type), captor.capture());
104         Assert.assertEquals(STREAM_1, getStreamName(captor.getValue()));
105         final String expDate = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(date.atZone(ZoneId.systemDefault()));
106         final Optional<LeafNode> actual = (Optional<LeafNode>) getDate(captor.getValue());
107         Assert.assertTrue(actual.isPresent());
108         String actualDate = (String) actual.get().getValue();
109         Assert.assertEquals(expDate, actualDate);
110     }
111
112     @Test
113     public void testInvokeCreateSubscription2() throws Exception {
114         Stream stream = new StreamBuilder()
115                 .setName(new StreamNameType(STREAM_1))
116                 .setReplaySupport(true)
117                 .build();
118         mount.invokeCreateSubscription(stream, Optional.empty());
119         final QName type = QName.create(CreateSubscriptionInput.QNAME, "create-subscription");
120         ArgumentCaptor<ContainerNode> captor = ArgumentCaptor.forClass(ContainerNode.class);
121         verify(rpcService).invokeRpc(eq(type), captor.capture());
122         Assert.assertEquals(STREAM_1, getStreamName(captor.getValue()));
123         final Optional<LeafNode> date = (Optional<LeafNode>) getDate(captor.getValue());
124         Assert.assertFalse(date.isPresent());
125
126     }
127
128     @Test
129     public void testGetAvailableStreams() throws Exception {
130         final Collection<Stream> availableStreams = mount.getAvailableStreams();
131         Assert.assertEquals(2, availableStreams.size());
132         final Collection<String> streamNames = Collections2.transform(availableStreams,
133             input -> input.getName().getValue());
134         streamNames.contains(STREAM_1);
135         streamNames.contains(STREAM_2);
136     }
137
138     private static String getStreamName(final ContainerNode value) {
139         YangInstanceIdentifier.NodeIdentifier stream =
140                 new YangInstanceIdentifier.NodeIdentifier(QName.create(CreateSubscriptionInput.QNAME, "stream"));
141         return (String) value.getChild(stream).get().getValue();
142     }
143
144     private static Optional<?> getDate(final ContainerNode value) {
145         YangInstanceIdentifier.NodeIdentifier startTime =
146                 new YangInstanceIdentifier.NodeIdentifier(QName.create(CreateSubscriptionInput.QNAME, "startTime"));
147         return value.getChild(startTime);
148     }
149 }