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