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