Merge "Bug 9092: revert to org.json temporarily"
[netconf.git] / restconf / restconf-nb-bierman02 / 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.AbstractMap.SimpleImmutableEntry;
12 import java.util.ArrayList;
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 List<String> time = new ArrayList<>();
75         time.add("2014-10-25T10:02:00Z");
76         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
77         list.add(entry);
78
79         subscribe(list);
80         Notificator.removeAllListeners();
81     }
82
83     @Test
84     public void milisecsTest() {
85         final List<Entry<String, List<String>>> list = new ArrayList<>();
86         final List<String> time = new ArrayList<>();
87         time.add("2014-10-25T10:02:00.12345Z");
88         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", 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 List<String> time = new ArrayList<>();
99         time.add("2014-10-25T10:02:00+01:00");
100         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
101         list.add(entry);
102
103         subscribe(list);
104         Notificator.removeAllListeners();
105     }
106
107     @Test
108     public void zonesMinusTest() {
109         final List<Entry<String, List<String>>> list = new ArrayList<>();
110         final List<String> time = new ArrayList<>();
111         time.add("2014-10-25T10:02:00-01:00");
112         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
113         list.add(entry);
114
115         subscribe(list);
116         Notificator.removeAllListeners();
117     }
118
119     @Test
120     public void startAndStopTimeTest() {
121         final List<Entry<String, List<String>>> list = new ArrayList<>();
122         final List<String> time = new ArrayList<>();
123         time.add("2014-10-25T10:02:00Z");
124         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
125
126         final List<String> time2 = new ArrayList<>();
127         time2.add("2014-10-25T12:31:00Z");
128         final Entry<String, List<String>> entry2 = new SimpleImmutableEntry<>("stop-time", time2);
129
130         list.add(entry);
131         list.add(entry2);
132
133         subscribe(list);
134         Notificator.removeAllListeners();
135     }
136
137     @Test(expected = RestconfDocumentedException.class)
138     public void stopTimeTest() {
139         final List<Entry<String, List<String>>> list = new ArrayList<>();
140         final List<String> time = new ArrayList<>();
141         time.add("2014-10-25T12:31:00Z");
142         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("stop-time", time);
143         list.add(entry);
144
145         subscribe(list);
146         Notificator.removeAllListeners();
147     }
148
149     @Test(expected = RestconfDocumentedException.class)
150     public void badParamTest() {
151         final List<Entry<String, List<String>>> list = new ArrayList<>();
152         final List<String> time = new ArrayList<>();
153         time.add("2014-10-25T12:31:00Z");
154         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("time", time);
155         list.add(entry);
156
157         subscribe(list);
158         Notificator.removeAllListeners();
159     }
160
161     @Test(expected = IllegalArgumentException.class)
162     public void badValueTest() {
163         final List<Entry<String, List<String>>> list = new ArrayList<>();
164         final List<String> time = new ArrayList<>();
165         time.add("badvalue");
166         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
167         list.add(entry);
168
169         subscribe(list);
170         Notificator.removeAllListeners();
171     }
172
173     @Test(expected = IllegalArgumentException.class)
174     public void badZonesTest() {
175         final List<Entry<String, List<String>>> list = new ArrayList<>();
176         final List<String> time = new ArrayList<>();
177         time.add("2014-10-25T10:02:00Z+1:00");
178         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
179         list.add(entry);
180
181         subscribe(list);
182         Notificator.removeAllListeners();
183     }
184
185     @Test(expected = IllegalArgumentException.class)
186     public void badMilisecsTest() {
187         final List<Entry<String, List<String>>> list = new ArrayList<>();
188         final List<String> time = new ArrayList<>();
189         time.add("2014-10-25T10:02:00:0026Z");
190         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
191         list.add(entry);
192
193         subscribe(list);
194         Notificator.removeAllListeners();
195     }
196
197     @Test
198     public void onNotifiTest() throws Exception {
199         final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
200         final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-14-12", "localName"));
201         Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
202         final ListenerAdapter listener = Notificator.createListener(path, this.identifier, NotificationOutputType.XML);
203
204         final List<Entry<String, List<String>>> list = new ArrayList<>();
205         final List<String> time = new ArrayList<>();
206         time.add("2014-10-25T10:02:00Z");
207         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
208         list.add(entry);
209
210         subscribe(list);
211
212         final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change =
213                 Mockito.mock(AsyncDataChangeEvent.class);
214         Instant startOrig = listener.getStart();
215         Assert.assertNotNull(startOrig);
216         listener.onDataChanged(change);
217
218         startOrig = listener.getStart();
219         Assert.assertNull(startOrig);
220     }
221
222     private void subscribe(final List<Entry<String, List<String>>> entries) {
223         final MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
224         Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map);
225         final UriBuilder uriBuilder = UriBuilder.fromPath("http://localhost:8181/" + this.identifier);
226         Mockito.when(this.uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
227         final Set<Entry<String, List<String>>> set = new HashSet<>();
228         for (final Entry<String, List<String>> entry : entries) {
229             set.add(entry);
230         }
231         Mockito.when(map.entrySet()).thenReturn(set);
232         RestconfImpl.getInstance().subscribeToStream(this.identifier, this.uriInfo);
233     }
234
235 }