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