Migrate netconf to MD-SAL APIs
[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.base.Function;
16 import com.google.common.collect.Lists;
17 import com.google.common.util.concurrent.Futures;
18 import java.time.Instant;
19 import java.time.ZoneId;
20 import java.time.format.DateTimeFormatter;
21 import java.util.List;
22 import java.util.Optional;
23 import javax.annotation.Nullable;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
31 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
32 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
33 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
34 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
35 import org.opendaylight.mdsal.dom.api.DOMRpcService;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamBuilder;
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 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
48
49 public class NetconfEventSourceMountTest {
50
51     public static final String STREAM_1 = "stream-1";
52     public static final String STREAM_2 = "stream-2";
53     @Mock
54     private DOMMountPoint domMountPoint;
55     @Mock
56     DOMDataBroker dataBroker;
57     @Mock
58     DOMRpcService rpcService;
59     @Mock
60     private DOMDataTreeReadTransaction tx;
61     private NetconfEventSourceMount mount;
62
63     @Before
64     public void setUp() throws Exception {
65         MockitoAnnotations.initMocks(this);
66         doReturn(Optional.of(dataBroker)).when(domMountPoint).getService(DOMDataBroker.class);
67         doReturn(Optional.of(rpcService)).when(domMountPoint).getService(DOMRpcService.class);
68         doReturn(Optional.of(mock(DOMNotificationService.class))).when(domMountPoint)
69                 .getService(DOMNotificationService.class);
70         doReturn(tx).when(dataBroker).newReadOnlyTransaction();
71         final YangInstanceIdentifier path = YangInstanceIdentifier.builder().node(Netconf.QNAME).node(Streams.QNAME)
72                 .build();
73         final NormalizedNode<?, ?> streamsNode = NetconfTestUtils.getStreamsNode(STREAM_1, STREAM_2);
74         doReturn(Futures.immediateCheckedFuture(Optional.of(streamsNode))).when(tx).read(LogicalDatastoreType
75                 .OPERATIONAL, path);
76         mount = new NetconfEventSourceMount(NetconfTestUtils.getNode("node-1"), domMountPoint);
77     }
78
79     @Test
80     public void testInvokeCreateSubscription() throws Exception {
81         Stream stream = new StreamBuilder()
82                 .setName(new StreamNameType(STREAM_1))
83                 .build();
84         mount.invokeCreateSubscription(stream, Optional.empty());
85         final SchemaPath type = SchemaPath.create(true, QName.create(CreateSubscriptionInput.QNAME,
86                 "create-subscription"));
87         ArgumentCaptor<ContainerNode> captor = ArgumentCaptor.forClass(ContainerNode.class);
88         verify(rpcService).invokeRpc(eq(type), captor.capture());
89         Assert.assertEquals(STREAM_1, getStreamName(captor.getValue()));
90     }
91
92     @Test
93     public void testInvokeCreateSubscription1() throws Exception {
94         Stream stream = new StreamBuilder()
95                 .setName(new StreamNameType(STREAM_1))
96                 .setReplaySupport(true)
97                 .build();
98         final Instant date = Instant.now();
99         mount.invokeCreateSubscription(stream, Optional.of(date));
100         final SchemaPath type = SchemaPath.create(true, QName.create(CreateSubscriptionInput.QNAME,
101                 "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 SchemaPath type = SchemaPath.create(true, QName.create(CreateSubscriptionInput.QNAME,
120                 "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 List<Stream> availableStreams = mount.getAvailableStreams();
132         Assert.assertEquals(2, availableStreams.size());
133         final List<String> streamNames = Lists.transform(availableStreams, new Function<Stream, String>() {
134             @Nullable
135             @Override
136             public String apply(@Nullable final Stream input) {
137                 return input.getName().getValue();
138             }
139         });
140         streamNames.contains(STREAM_1);
141         streamNames.contains(STREAM_2);
142     }
143
144     private static String getStreamName(final ContainerNode value) {
145         YangInstanceIdentifier.NodeIdentifier stream =
146                 new YangInstanceIdentifier.NodeIdentifier(QName.create(CreateSubscriptionInput.QNAME, "stream"));
147         return (String) value.getChild(stream).get().getValue();
148     }
149
150     private static Optional<?> getDate(final ContainerNode value) {
151         YangInstanceIdentifier.NodeIdentifier startTime =
152                 new YangInstanceIdentifier.NodeIdentifier(QName.create(CreateSubscriptionInput.QNAME, "startTime"));
153         return value.getChild(startTime);
154     }
155 }