Bump versions to 2.0.0-SNAPSHOT
[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 @Deprecated(forRemoval = true)
49 @RunWith(MockitoJUnitRunner.StrictStubs.class)
50 public class NetconfEventSourceMountTest extends AbstractCodecTest {
51     public static final String STREAM_1 = "stream-1";
52     public static final String STREAM_2 = "stream-2";
53
54     @Mock
55     private DOMMountPoint domMountPoint;
56     @Mock
57     DOMDataBroker dataBroker;
58     @Mock
59     DOMRpcService rpcService;
60     @Mock
61     DOMSchemaService schemaService;
62     @Mock
63     private DOMDataTreeReadTransaction tx;
64     private NetconfEventSourceMount mount;
65
66     @Before
67     public void setUp() {
68         doReturn(Optional.of(dataBroker)).when(domMountPoint).getService(DOMDataBroker.class);
69         doReturn(Optional.of(rpcService)).when(domMountPoint).getService(DOMRpcService.class);
70         doReturn(Optional.of(mock(DOMNotificationService.class))).when(domMountPoint)
71                 .getService(DOMNotificationService.class);
72         doReturn(Optional.of(schemaService)).when(domMountPoint).getService(DOMSchemaService.class);
73         doReturn(tx).when(dataBroker).newReadOnlyTransaction();
74         final YangInstanceIdentifier path = YangInstanceIdentifier.builder().node(Netconf.QNAME).node(Streams.QNAME)
75                 .build();
76         final NormalizedNode<?, ?> streamsNode = NetconfTestUtils.getStreamsNode(STREAM_1, STREAM_2);
77         doReturn(FluentFutures.immediateFluentFuture(Optional.of(streamsNode)))
78                 .when(tx).read(LogicalDatastoreType.OPERATIONAL, path);
79         mount = new NetconfEventSourceMount(SERIALIZER, NetconfTestUtils.getNode("node-1"), domMountPoint);
80     }
81
82     @Test
83     public void testInvokeCreateSubscription() throws Exception {
84         Stream stream = new StreamBuilder()
85                 .setName(new StreamNameType(STREAM_1))
86                 .build();
87         mount.invokeCreateSubscription(stream, Optional.empty());
88         final QName type = QName.create(CreateSubscriptionInput.QNAME, "create-subscription");
89         ArgumentCaptor<ContainerNode> captor = ArgumentCaptor.forClass(ContainerNode.class);
90         verify(rpcService).invokeRpc(eq(type), captor.capture());
91         Assert.assertEquals(STREAM_1, getStreamName(captor.getValue()));
92     }
93
94     @Test
95     public void testInvokeCreateSubscription1() throws Exception {
96         Stream stream = new StreamBuilder()
97                 .setName(new StreamNameType(STREAM_1))
98                 .setReplaySupport(true)
99                 .build();
100         final Instant date = Instant.now();
101         mount.invokeCreateSubscription(stream, Optional.of(date));
102         final QName type = QName.create(CreateSubscriptionInput.QNAME, "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.atZone(ZoneId.systemDefault()));
107         final Optional<LeafNode> actual = (Optional<LeafNode>) getDate(captor.getValue());
108         Assert.assertTrue(actual.isPresent());
109         String actualDate = (String) actual.get().getValue();
110         Assert.assertEquals(expDate, actualDate);
111     }
112
113     @Test
114     public void testInvokeCreateSubscription2() throws Exception {
115         Stream stream = new StreamBuilder()
116                 .setName(new StreamNameType(STREAM_1))
117                 .setReplaySupport(true)
118                 .build();
119         mount.invokeCreateSubscription(stream, Optional.empty());
120         final QName type = QName.create(CreateSubscriptionInput.QNAME, "create-subscription");
121         ArgumentCaptor<ContainerNode> captor = ArgumentCaptor.forClass(ContainerNode.class);
122         verify(rpcService).invokeRpc(eq(type), captor.capture());
123         Assert.assertEquals(STREAM_1, getStreamName(captor.getValue()));
124         final Optional<LeafNode> date = (Optional<LeafNode>) getDate(captor.getValue());
125         Assert.assertFalse(date.isPresent());
126
127     }
128
129     @Test
130     public void testGetAvailableStreams() throws Exception {
131         final Collection<Stream> availableStreams = mount.getAvailableStreams();
132         Assert.assertEquals(2, availableStreams.size());
133         final Collection<String> streamNames = Collections2.transform(availableStreams,
134             input -> input.getName().getValue());
135         streamNames.contains(STREAM_1);
136         streamNames.contains(STREAM_2);
137     }
138
139     private static String getStreamName(final ContainerNode value) {
140         YangInstanceIdentifier.NodeIdentifier stream =
141                 new YangInstanceIdentifier.NodeIdentifier(QName.create(CreateSubscriptionInput.QNAME, "stream"));
142         return (String) value.getChild(stream).get().getValue();
143     }
144
145     private static Optional<?> getDate(final ContainerNode value) {
146         YangInstanceIdentifier.NodeIdentifier startTime =
147                 new YangInstanceIdentifier.NodeIdentifier(QName.create(CreateSubscriptionInput.QNAME, "startTime"));
148         return value.getChild(startTime);
149     }
150 }