2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.websockets.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.mock;
14 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
16 import java.io.FileNotFoundException;
17 import java.io.UnsupportedEncodingException;
20 import javax.ws.rs.client.Entity;
21 import javax.ws.rs.core.Application;
22 import javax.ws.rs.core.MediaType;
23 import javax.ws.rs.core.Response;
25 import org.glassfish.jersey.server.ResourceConfig;
26 import org.glassfish.jersey.test.JerseyTest;
27 import org.junit.BeforeClass;
28 import org.junit.Test;
29 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
30 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
31 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
32 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
33 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
34 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
35 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
36 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 public class RestStream extends JerseyTest {
41 private static BrokerFacade brokerFacade;
42 private static RestconfImpl restconfImpl;
43 private static SchemaContext schemaContextYangsIetf;
46 public static void init() throws FileNotFoundException {
47 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
48 ControllerContext controllerContext = ControllerContext.getInstance();
49 controllerContext.setSchemas(schemaContextYangsIetf);
50 brokerFacade = mock(BrokerFacade.class);
51 restconfImpl = RestconfImpl.getInstance();
52 restconfImpl.setBroker(brokerFacade);
53 restconfImpl.setControllerContext(controllerContext);
57 protected Application configure() {
58 /* enable/disable Jersey logs to console */
59 // enable(TestProperties.LOG_TRAFFIC);
60 // enable(TestProperties.DUMP_ENTITY);
61 // enable(TestProperties.RECORD_LOG_LEVEL);
62 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
63 ResourceConfig resourceConfig = new ResourceConfig();
64 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
65 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
66 JsonToCompositeNodeProvider.INSTANCE);
67 return resourceConfig;
71 public void testCallRpcCallGet() throws UnsupportedEncodingException, InterruptedException {
72 String uri = createUri("/operations/", "sal-remote:create-data-change-event-subscription");
73 Response responseWithStreamName = post(uri, MediaType.APPLICATION_XML, getRpcInput());
74 String xmlResponse = responseWithStreamName.readEntity(String.class);
75 assertNotNull(xmlResponse);
76 assertTrue(xmlResponse.contains("<stream-name>ietf-interfaces:interfaces/ietf-interfaces:interface/eth0</stream-name>"));
78 uri = createUri("/streams/stream/", "ietf-interfaces:interfaces/ietf-interfaces:interface/eth0");
79 Response responseWithRedirectionUri = get(uri, MediaType.APPLICATION_XML);
80 final URI websocketServerUri = responseWithRedirectionUri.getLocation();
81 assertNotNull(websocketServerUri);
82 assertEquals(websocketServerUri.toString(), "http://localhost:8181/ietf-interfaces:interfaces/ietf-interfaces:interface/eth0");
85 private Response post(String uri, String mediaType, String data) {
86 return target(uri).request(mediaType).post(Entity.entity(data, mediaType));
89 private Response get(String uri, String mediaType) {
90 return target(uri).request(mediaType).get();
93 private String getRpcInput() {
94 StringBuilder sb = new StringBuilder();
95 sb.append("<input xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote\">");
96 sb.append("<path xmlns:int=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">/int:interfaces/int:interface[int:name='eth0']</path>");
97 sb.append("</input>");