Bug 5679 - implement ietf-restconf-monitoring - cleanup
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestconfImplNotificationSubscribingTest.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 /**
9  *
10  */
11 package org.opendaylight.controller.sal.restconf.impl.test;
12
13 import java.lang.reflect.Field;
14 import java.util.ArrayList;
15 import java.util.Date;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Map.Entry;
19 import java.util.Set;
20 import javax.ws.rs.core.MultivaluedMap;
21 import javax.ws.rs.core.UriBuilder;
22 import javax.ws.rs.core.UriInfo;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.MockitoAnnotations;
29 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
30 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
31 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
32 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
33 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
34 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
35 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
36 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
37 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
38 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
43 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
44
45 public class RestconfImplNotificationSubscribingTest {
46
47     private final String identifier = "data-change-event-subscription/datastore=OPERATIONAL/scope=ONE";
48
49     @Mock
50     private BrokerFacade broker;
51
52     @Mock
53     private DOMDataBroker domDataBroker;
54
55     @Mock
56     private UriInfo uriInfo;
57
58     @Before
59     public void setup() throws Exception {
60         MockitoAnnotations.initMocks(this);
61
62         this.broker.setDomDataBroker(this.domDataBroker);
63         RestconfImpl.getInstance().setBroker(this.broker);
64         ControllerContext.getInstance().setGlobalSchema(TestRestconfUtils.loadSchemaContext("/notifications"));
65
66         final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
67         final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-14-12", "localName"));
68         Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
69         Notificator.createListener(path, this.identifier, NotificationOutputType.XML);
70     }
71
72     @Test
73     public void startTimeTest() {
74         final List<Entry<String, List<String>>> list = new ArrayList<>();
75         final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
76         Mockito.when(entry.getKey()).thenReturn("start-time");
77         final List<String> time = new ArrayList<>();
78         time.add("2014-10-25T10:02:00Z");
79         Mockito.when(entry.getValue()).thenReturn(time);
80         list.add(entry);
81
82         subscribe(list);
83         Notificator.removeAllListeners();
84     }
85
86     @Test
87     public void milisecsTest() {
88         final List<Entry<String, List<String>>> list = new ArrayList<>();
89         final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
90         Mockito.when(entry.getKey()).thenReturn("start-time");
91         final List<String> time = new ArrayList<>();
92         time.add("2014-10-25T10:02:00.12345Z");
93         Mockito.when(entry.getValue()).thenReturn(time);
94         list.add(entry);
95
96         subscribe(list);
97         Notificator.removeAllListeners();
98     }
99
100     @Test
101     public void zonesPlusTest() {
102         final List<Entry<String, List<String>>> list = new ArrayList<>();
103         final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
104         Mockito.when(entry.getKey()).thenReturn("start-time");
105         final List<String> time = new ArrayList<>();
106         time.add("2014-10-25T10:02:00+01:00");
107         Mockito.when(entry.getValue()).thenReturn(time);
108         list.add(entry);
109
110         subscribe(list);
111         Notificator.removeAllListeners();
112     }
113
114     @Test
115     public void zonesMinusTest() {
116         final List<Entry<String, List<String>>> list = new ArrayList<>();
117         final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
118         Mockito.when(entry.getKey()).thenReturn("start-time");
119         final List<String> time = new ArrayList<>();
120         time.add("2014-10-25T10:02:00-01:00");
121         Mockito.when(entry.getValue()).thenReturn(time);
122         list.add(entry);
123
124         subscribe(list);
125         Notificator.removeAllListeners();
126     }
127
128     @Test
129     public void startAndStopTimeTest() {
130         final List<Entry<String, List<String>>> list = new ArrayList<>();
131         final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
132         Mockito.when(entry.getKey()).thenReturn("start-time");
133         final List<String> time = new ArrayList<>();
134         time.add("2014-10-25T10:02:00Z");
135         Mockito.when(entry.getValue()).thenReturn(time);
136
137         final Entry<String, List<String>> entry2 = Mockito.mock(Entry.class);
138         Mockito.when(entry2.getKey()).thenReturn("stop-time");
139         final List<String> time2 = new ArrayList<>();
140         time2.add("2014-10-25T12:31:00Z");
141         Mockito.when(entry2.getValue()).thenReturn(time2);
142
143         list.add(entry);
144         list.add(entry2);
145
146         subscribe(list);
147         Notificator.removeAllListeners();
148     }
149
150     @Test(expected = RestconfDocumentedException.class)
151     public void stopTimeTest() {
152         final List<Entry<String, List<String>>> list = new ArrayList<>();
153         final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
154         Mockito.when(entry.getKey()).thenReturn("stop-time");
155         final List<String> time = new ArrayList<>();
156         time.add("2014-10-25T12:31:00Z");
157         Mockito.when(entry.getValue()).thenReturn(time);
158         list.add(entry);
159
160         subscribe(list);
161         Notificator.removeAllListeners();
162     }
163
164     @Test(expected = RestconfDocumentedException.class)
165     public void badParamTest() {
166         final List<Entry<String, List<String>>> list = new ArrayList<>();
167         final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
168         Mockito.when(entry.getKey()).thenReturn("time");
169         final List<String> time = new ArrayList<>();
170         time.add("2014-10-25T12:31:00Z");
171         Mockito.when(entry.getValue()).thenReturn(time);
172         list.add(entry);
173
174         subscribe(list);
175         Notificator.removeAllListeners();
176     }
177
178     @Test(expected = IllegalArgumentException.class)
179     public void badValueTest() {
180         final List<Entry<String, List<String>>> list = new ArrayList<>();
181         final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
182         Mockito.when(entry.getKey()).thenReturn("start-time");
183         final List<String> time = new ArrayList<>();
184         time.add("badvalue");
185         Mockito.when(entry.getValue()).thenReturn(time);
186         list.add(entry);
187
188         subscribe(list);
189         Notificator.removeAllListeners();
190     }
191
192     @Test(expected = IllegalArgumentException.class)
193     public void badZonesTest() {
194         final List<Entry<String, List<String>>> list = new ArrayList<>();
195         final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
196         Mockito.when(entry.getKey()).thenReturn("start-time");
197         final List<String> time = new ArrayList<>();
198         time.add("2014-10-25T10:02:00Z+1:00");
199         Mockito.when(entry.getValue()).thenReturn(time);
200         list.add(entry);
201
202         subscribe(list);
203         Notificator.removeAllListeners();
204     }
205
206     @Test(expected = IllegalArgumentException.class)
207     public void badMilisecsTest() {
208         final List<Entry<String, List<String>>> list = new ArrayList<>();
209         final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
210         Mockito.when(entry.getKey()).thenReturn("start-time");
211         final List<String> time = new ArrayList<>();
212         time.add("2014-10-25T10:02:00:0026Z");
213         Mockito.when(entry.getValue()).thenReturn(time);
214         list.add(entry);
215
216         subscribe(list);
217         Notificator.removeAllListeners();
218     }
219
220     @Test
221     public void onNotifiTest() throws Exception {
222         final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
223         final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-14-12", "localName"));
224         Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
225         final ListenerAdapter listener = Notificator.createListener(path, this.identifier, NotificationOutputType.XML);
226
227         final List<Entry<String, List<String>>> list = new ArrayList<>();
228         final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
229         Mockito.when(entry.getKey()).thenReturn("start-time");
230         final List<String> time = new ArrayList<>();
231         time.add("2014-10-25T10:02:00Z");
232         Mockito.when(entry.getValue()).thenReturn(time);
233         list.add(entry);
234
235         subscribe(list);
236
237         final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change =
238                 Mockito.mock(AsyncDataChangeEvent.class);
239         final Class<?> superclass = listener.getClass().getSuperclass().getSuperclass();
240         Field start = superclass.getDeclaredField("start");
241         start.setAccessible(true);
242         Date startOrig = (Date) start.get(listener);
243         Assert.assertNotNull(startOrig);
244         listener.onDataChanged(change);
245
246         start = superclass.getDeclaredField("start");
247         start.setAccessible(true);
248         startOrig = (Date) start.get(listener);
249         Assert.assertNull(startOrig);
250     }
251
252     private void subscribe(final List<Entry<String, List<String>>> entries) {
253         final MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
254         Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map);
255         final UriBuilder uriBuilder = UriBuilder.fromPath("http://localhost:8181/" + this.identifier);
256         Mockito.when(this.uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
257         final Set<Entry<String, List<String>>> set = new HashSet<>();
258         for(final Entry<String, List<String>> entry : entries){
259             set.add(entry);
260         }
261         Mockito.when(map.entrySet()).thenReturn(set);
262         RestconfImpl.getInstance().subscribeToStream(this.identifier, this.uriInfo);
263     }
264
265 }