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