X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=restconf%2Fsal-rest-connector%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Frestconf%2Frestful%2Fservices%2Fimpl%2FRestconfStreamsSubscriptionServiceImplTest.java;h=cabbd00525cde19ba0d36d61d9a7ec3fd782d9c7;hb=17f4fc5e182e583114bb48bbcd693f69a6852e59;hp=c147f28a5ee9515cd825e3f0a544099e8fb134c4;hpb=9013e567cd592fccefd801bd8c97440a810c3574;p=netconf.git diff --git a/restconf/sal-rest-connector/src/test/java/org/opendaylight/restconf/restful/services/impl/RestconfStreamsSubscriptionServiceImplTest.java b/restconf/sal-rest-connector/src/test/java/org/opendaylight/restconf/restful/services/impl/RestconfStreamsSubscriptionServiceImplTest.java index c147f28a5e..cabbd00525 100644 --- a/restconf/sal-rest-connector/src/test/java/org/opendaylight/restconf/restful/services/impl/RestconfStreamsSubscriptionServiceImplTest.java +++ b/restconf/sal-rest-connector/src/test/java/org/opendaylight/restconf/restful/services/impl/RestconfStreamsSubscriptionServiceImplTest.java @@ -12,12 +12,15 @@ import static org.junit.Assert.assertEquals; import static org.mockito.Matchers.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; - import java.lang.reflect.Field; import java.util.HashMap; +import java.util.HashSet; +import java.util.List; import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import javax.ws.rs.core.Response; +import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriInfo; import org.junit.AfterClass; @@ -25,32 +28,47 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker; import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; +import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext; import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException; import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter; import org.opendaylight.netconf.sal.streams.listeners.Notificator; import org.opendaylight.restconf.handlers.DOMDataBrokerHandler; +import org.opendaylight.restconf.handlers.NotificationServiceHandler; +import org.opendaylight.restconf.handlers.SchemaContextHandler; import org.opendaylight.yangtools.concepts.ListenerRegistration; public class RestconfStreamsSubscriptionServiceImplTest { - private static final String uri = "/restconf/15/data/ietf-restconf-monitoring:restconf-state/streams/stream/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE"; + private static final String uri = "/restconf/18/data/ietf-restconf-monitoring:restconf-state/streams/stream/" + + "toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE"; private static Field listenersByStreamName; @Mock private DOMDataBrokerHandler dataBrokerHandler; @Mock private UriInfo uriInfo; + @Mock + private NotificationServiceHandler notificationServiceHandler; + + private final SchemaContextHandler schemaHandler = new SchemaContextHandler(); @Before - public void setUp() { + public void setUp() throws Exception { MockitoAnnotations.initMocks(this); final DOMDataBroker dataBroker = mock(DOMDataBroker.class); final ListenerRegistration listener = mock(ListenerRegistration.class); - doReturn(dataBroker).when(dataBrokerHandler).get(); + doReturn(dataBroker).when(this.dataBrokerHandler).get(); doReturn(listener).when(dataBroker).registerDataChangeListener(any(), any(), any(), any()); + final MultivaluedMap map = Mockito.mock(MultivaluedMap.class); + final Set>> set = new HashSet<>(); + Mockito.when(map.entrySet()).thenReturn(set); + Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map); + this.schemaHandler.onGlobalContextUpdated(TestRestconfUtils.loadSchemaContext("/notifications")); } @BeforeClass @@ -58,8 +76,10 @@ public class RestconfStreamsSubscriptionServiceImplTest { final Map listenersByStreamNameSetter = new HashMap<>(); final ListenerAdapter adapter = mock(ListenerAdapter.class); doReturn(false).when(adapter).isListening(); - listenersByStreamNameSetter.put("toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE", adapter); - listenersByStreamName = Notificator.class.getDeclaredField("listenersByStreamName"); + listenersByStreamNameSetter.put( + "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE", + adapter); + listenersByStreamName = Notificator.class.getDeclaredField("dataChangeListener"); listenersByStreamName.setAccessible(true); listenersByStreamName.set(Notificator.class, listenersByStreamNameSetter); @@ -75,27 +95,38 @@ public class RestconfStreamsSubscriptionServiceImplTest { @Test public void testSubscribeToStream() { final UriBuilder uriBuilder = UriBuilder.fromUri(uri); - doReturn(uriBuilder).when(uriInfo).getAbsolutePathBuilder(); - final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService = new RestconfStreamsSubscriptionServiceImpl(dataBrokerHandler); - final Response response = streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE", uriInfo); - assertEquals(200, response.getStatus()); - assertEquals("ws://:8181/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE", response.getHeaderString("Location")); + doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder(); + final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService = + new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler, + this.schemaHandler); + final NormalizedNodeContext response = streamsSubscriptionService + .subscribeToStream( + "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE", + this.uriInfo); + assertEquals( + "ws://:8181/data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE", + response.getNewHeaders().get("Location").toString()); } @Test(expected = RestconfDocumentedException.class) public void testSubscribeToStreamMissingDatastoreInPath() { final UriBuilder uriBuilder = UriBuilder.fromUri(uri); - doReturn(uriBuilder).when(uriInfo).getAbsolutePathBuilder(); - final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService = new RestconfStreamsSubscriptionServiceImpl(dataBrokerHandler); - final Response response = streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/scope=ONE", uriInfo); + doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder(); + final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService = + new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler, + this.schemaHandler); + streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/scope=ONE", this.uriInfo); } @Test(expected = RestconfDocumentedException.class) public void testSubscribeToStreamMissingScopeInPath() { final UriBuilder uriBuilder = UriBuilder.fromUri(uri); - doReturn(uriBuilder).when(uriInfo).getAbsolutePathBuilder(); - final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService = new RestconfStreamsSubscriptionServiceImpl(dataBrokerHandler); - final Response response = streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/datastore=OPERATIONAL", uriInfo); + doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder(); + final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService = + new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler, + this.schemaHandler); + streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/datastore=OPERATIONAL", + this.uriInfo); } }