b69d10517bf07582f19d66a83dc86ace778f8e94
[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 static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14
15 import java.io.FileNotFoundException;
16 import java.time.Instant;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Map.Entry;
20 import java.util.Set;
21 import javax.ws.rs.core.MultivaluedMap;
22 import javax.ws.rs.core.UriBuilder;
23 import javax.ws.rs.core.UriInfo;
24 import org.junit.AfterClass;
25 import org.junit.Before;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mock;
30 import org.mockito.junit.MockitoJUnitRunner;
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.RestconfImpl;
35 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
36 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
37 import org.opendaylight.netconf.sal.streams.websockets.WebSocketServer;
38 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
39 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
40 import org.opendaylight.yangtools.yang.common.QName;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
44 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
45 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
46
47 @RunWith(MockitoJUnitRunner.StrictStubs.class)
48 public class RestconfImplNotificationSubscribingTest {
49
50     private final String identifier = "data-change-event-subscription/datastore=OPERATIONAL/scope=ONE";
51
52     private static EffectiveModelContext schemaContext;
53
54     @Mock
55     private BrokerFacade broker;
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     @AfterClass
69     public static void cleanUp() {
70         WebSocketServer.destroyInstance(); // NETCONF-604
71     }
72
73     @Before
74     public void setup() {
75         controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
76         restconfImpl = RestconfImpl.newInstance(broker, controllerContext);
77
78         final YangInstanceIdentifier path = mock(YangInstanceIdentifier.class);
79         Notificator.createListener(path, identifier, NotificationOutputType.XML, controllerContext);
80     }
81
82     @Test
83     public void startTimeTest() {
84         subscribe(Set.of(Map.entry("start-time",  List.of("2014-10-25T10:02:00Z"))));
85         Notificator.removeAllListeners();
86     }
87
88     @Test
89     public void milisecsTest() {
90         subscribe(Set.of(Map.entry("start-time", List.of("2014-10-25T10:02:00.12345Z"))));
91         Notificator.removeAllListeners();
92     }
93
94     @Test
95     public void zonesPlusTest() {
96         subscribe(Set.of(Map.entry("start-time", List.of("2014-10-25T10:02:00+01:00"))));
97         Notificator.removeAllListeners();
98     }
99
100     @Test
101     public void zonesMinusTest() {
102         subscribe(Set.of(Map.entry("start-time", List.of("2014-10-25T10:02:00-01:00"))));
103         Notificator.removeAllListeners();
104     }
105
106     @Test
107     public void startAndStopTimeTest() {
108         subscribe(Set.of(Map.entry("start-time", List.of("2014-10-25T10:02:00Z")),
109             Map.entry("stop-time", List.of("2014-10-25T12:31:00Z"))));
110         Notificator.removeAllListeners();
111     }
112
113     @Test(expected = RestconfDocumentedException.class)
114     public void stopTimeTest() {
115         subscribe(Set.of(Map.entry("stop-time", List.of("2014-10-25T12:31:00Z"))));
116         Notificator.removeAllListeners();
117     }
118
119     @Test(expected = RestconfDocumentedException.class)
120     public void badParamTest() {
121         subscribe(Set.of(Map.entry("time", List.of("2014-10-25T12:31:00Z"))));
122         Notificator.removeAllListeners();
123     }
124
125     @Test(expected = IllegalArgumentException.class)
126     public void badValueTest() {
127         subscribe(Set.of(Map.entry("start-time", List.of("badvalue"))));
128         Notificator.removeAllListeners();
129     }
130
131     @Test(expected = IllegalArgumentException.class)
132     public void badZonesTest() {
133         subscribe(Set.of(Map.entry("start-time", List.of("2014-10-25T10:02:00Z+1:00"))));
134         Notificator.removeAllListeners();
135     }
136
137     @Test(expected = IllegalArgumentException.class)
138     public void badMilisecsTest() {
139         subscribe(Set.of(Map.entry("start-time", List.of("2014-10-25T10:02:00:0026Z"))));
140         Notificator.removeAllListeners();
141     }
142
143     @Test
144     public void onNotifiTest() throws Exception {
145         final YangInstanceIdentifier path = mock(YangInstanceIdentifier.class);
146         final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-12-14", "localName"));
147         final ListenerAdapter listener = Notificator.createListener(path, identifier, NotificationOutputType.XML,
148                 controllerContext);
149
150         subscribe(Set.of(Map.entry("start-time", List.of("2014-10-25T10:02:00Z"))));
151
152         Instant startOrig = listener.getStart();
153         assertNotNull(startOrig);
154         listener.onDataTreeChanged(List.of());
155
156         startOrig = listener.getStart();
157         assertNull(startOrig);
158     }
159
160     private void subscribe(final Set<Entry<String, List<String>>> entries) {
161         final MultivaluedMap<String, String> map = mock(MultivaluedMap.class);
162         when(uriInfo.getQueryParameters()).thenReturn(map);
163         final UriBuilder uriBuilder = UriBuilder.fromPath("http://localhost:8181/" + identifier);
164         when(uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
165         when(map.entrySet()).thenReturn(entries);
166         restconfImpl.subscribeToStream(identifier, uriInfo);
167     }
168 }