54742ce0d866fb0ac3a7371561d317177d531504
[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.dom.api.DOMDataBroker;
29 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
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
62     @BeforeClass
63     public static void init() throws FileNotFoundException {
64         schemaContext = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/notifications"));
65     }
66
67     @Before
68     public void setup() throws Exception {
69         MockitoAnnotations.initMocks(this);
70
71         RestconfImpl.getInstance().setBroker(this.broker);
72
73         controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
74         RestconfImpl.getInstance().setControllerContext(controllerContext);
75
76         final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
77         final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-12-14", "localName"));
78         Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
79         Notificator.createListener(path, this.identifier, NotificationOutputType.XML, controllerContext);
80     }
81
82     @Test
83     public void startTimeTest() {
84         final List<Entry<String, List<String>>> list = new ArrayList<>();
85         final List<String> time = new ArrayList<>();
86         time.add("2014-10-25T10:02:00Z");
87         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
88         list.add(entry);
89
90         subscribe(list);
91         Notificator.removeAllListeners();
92     }
93
94     @Test
95     public void milisecsTest() {
96         final List<Entry<String, List<String>>> list = new ArrayList<>();
97         final List<String> time = new ArrayList<>();
98         time.add("2014-10-25T10:02:00.12345Z");
99         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
100         list.add(entry);
101
102         subscribe(list);
103         Notificator.removeAllListeners();
104     }
105
106     @Test
107     public void zonesPlusTest() {
108         final List<Entry<String, List<String>>> list = new ArrayList<>();
109         final List<String> time = new ArrayList<>();
110         time.add("2014-10-25T10:02:00+01:00");
111         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
112         list.add(entry);
113
114         subscribe(list);
115         Notificator.removeAllListeners();
116     }
117
118     @Test
119     public void zonesMinusTest() {
120         final List<Entry<String, List<String>>> list = new ArrayList<>();
121         final List<String> time = new ArrayList<>();
122         time.add("2014-10-25T10:02:00-01:00");
123         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
124         list.add(entry);
125
126         subscribe(list);
127         Notificator.removeAllListeners();
128     }
129
130     @Test
131     public void startAndStopTimeTest() {
132         final List<Entry<String, List<String>>> list = new ArrayList<>();
133         final List<String> time = new ArrayList<>();
134         time.add("2014-10-25T10:02:00Z");
135         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
136
137         final List<String> time2 = new ArrayList<>();
138         time2.add("2014-10-25T12:31:00Z");
139         final Entry<String, List<String>> entry2 = new SimpleImmutableEntry<>("stop-time", 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 List<String> time = new ArrayList<>();
152         time.add("2014-10-25T12:31:00Z");
153         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("stop-time", time);
154         list.add(entry);
155
156         subscribe(list);
157         Notificator.removeAllListeners();
158     }
159
160     @Test(expected = RestconfDocumentedException.class)
161     public void badParamTest() {
162         final List<Entry<String, List<String>>> list = new ArrayList<>();
163         final List<String> time = new ArrayList<>();
164         time.add("2014-10-25T12:31:00Z");
165         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("time", time);
166         list.add(entry);
167
168         subscribe(list);
169         Notificator.removeAllListeners();
170     }
171
172     @Test(expected = IllegalArgumentException.class)
173     public void badValueTest() {
174         final List<Entry<String, List<String>>> list = new ArrayList<>();
175         final List<String> time = new ArrayList<>();
176         time.add("badvalue");
177         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
178         list.add(entry);
179
180         subscribe(list);
181         Notificator.removeAllListeners();
182     }
183
184     @Test(expected = IllegalArgumentException.class)
185     public void badZonesTest() {
186         final List<Entry<String, List<String>>> list = new ArrayList<>();
187         final List<String> time = new ArrayList<>();
188         time.add("2014-10-25T10:02:00Z+1:00");
189         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
190         list.add(entry);
191
192         subscribe(list);
193         Notificator.removeAllListeners();
194     }
195
196     @Test(expected = IllegalArgumentException.class)
197     public void badMilisecsTest() {
198         final List<Entry<String, List<String>>> list = new ArrayList<>();
199         final List<String> time = new ArrayList<>();
200         time.add("2014-10-25T10:02:00:0026Z");
201         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
202         list.add(entry);
203
204         subscribe(list);
205         Notificator.removeAllListeners();
206     }
207
208     @Test
209     public void onNotifiTest() throws Exception {
210         final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
211         final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-12-14", "localName"));
212         Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
213         final ListenerAdapter listener = Notificator.createListener(path, this.identifier, NotificationOutputType.XML,
214                 controllerContext);
215
216         final List<Entry<String, List<String>>> list = new ArrayList<>();
217         final List<String> time = new ArrayList<>();
218         time.add("2014-10-25T10:02:00Z");
219         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
220         list.add(entry);
221
222         subscribe(list);
223
224         ArrayList<DataTreeCandidate> candidates = new ArrayList<>(0);
225         Instant startOrig = listener.getStart();
226         Assert.assertNotNull(startOrig);
227         listener.onDataTreeChanged(candidates);
228
229         startOrig = listener.getStart();
230         Assert.assertNull(startOrig);
231     }
232
233     private void subscribe(final List<Entry<String, List<String>>> entries) {
234         final MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
235         Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map);
236         final UriBuilder uriBuilder = UriBuilder.fromPath("http://localhost:8181/" + this.identifier);
237         Mockito.when(this.uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
238         final Set<Entry<String, List<String>>> set = new HashSet<>();
239         for (final Entry<String, List<String>> entry : entries) {
240             set.add(entry);
241         }
242         Mockito.when(map.entrySet()).thenReturn(set);
243         RestconfImpl.getInstance().subscribeToStream(this.identifier, this.uriInfo);
244     }
245
246 }