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;
15 import java.io.FileNotFoundException;
16 import java.io.UnsupportedEncodingException;
18 import javax.ws.rs.client.Entity;
19 import javax.ws.rs.core.Application;
20 import javax.ws.rs.core.MediaType;
21 import javax.ws.rs.core.Response;
22 import org.glassfish.jersey.server.ResourceConfig;
23 import org.glassfish.jersey.test.JerseyTest;
24 import org.junit.BeforeClass;
25 import org.junit.Ignore;
26 import org.junit.Test;
27 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
28 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
29 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
30 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
31 import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper;
32 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
33 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
34 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
35 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
38 import org.w3c.dom.Document;
39 import org.w3c.dom.Element;
40 import org.w3c.dom.Node;
42 public class RestStream extends JerseyTest {
44 private static BrokerFacade brokerFacade;
45 private static RestconfImpl restconfImpl;
46 private static SchemaContext schemaContextYangsIetf;
49 public static void init() throws FileNotFoundException, ReactorException {
50 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
51 final ControllerContext controllerContext = ControllerContext.getInstance();
52 controllerContext.setSchemas(schemaContextYangsIetf);
53 brokerFacade = mock(BrokerFacade.class);
54 restconfImpl = RestconfImpl.getInstance();
55 restconfImpl.setBroker(brokerFacade);
56 restconfImpl.setControllerContext(controllerContext);
60 protected Application configure() {
61 /* enable/disable Jersey logs to console */
62 // enable(TestProperties.LOG_TRAFFIC);
63 // enable(TestProperties.DUMP_ENTITY);
64 // enable(TestProperties.RECORD_LOG_LEVEL);
65 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
66 ResourceConfig resourceConfig = new ResourceConfig();
67 resourceConfig = resourceConfig.registerInstances(restconfImpl, new NormalizedNodeJsonBodyWriter(),
68 new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(), new JsonNormalizedNodeBodyReader());
69 resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
70 return resourceConfig;
74 @Ignore // FIXME : find problem with codec
75 public void testCallRpcCallGet() throws UnsupportedEncodingException, InterruptedException {
76 String uri = "/operations/sal-remote:create-data-change-event-subscription";
77 final Response responseWithStreamName = post(uri, MediaType.APPLICATION_XML, getRpcInput());
78 final Document xmlResponse = responseWithStreamName.readEntity(Document.class);
79 assertNotNull(xmlResponse);
80 final Element outputElement = xmlResponse.getDocumentElement();
81 assertEquals("output",outputElement.getLocalName());
83 final Node streamNameElement = outputElement.getFirstChild();
84 assertEquals("stream-name",streamNameElement.getLocalName());
85 assertEquals("ietf-interfaces:interfaces/ietf-interfaces:interface/eth0/datastore=CONFIGURATION/scope=BASE",streamNameElement.getTextContent());
87 uri = "/streams/stream/ietf-interfaces:interfaces/ietf-interfaces:interface/eth0/datastore=CONFIGURATION/scope=BASE";
88 final Response responseWithRedirectionUri = get(uri, MediaType.APPLICATION_XML);
89 final URI websocketServerUri = responseWithRedirectionUri.getLocation();
90 assertNotNull(websocketServerUri);
91 assertTrue(websocketServerUri.toString().matches(".*http://localhost:[\\d]+/ietf-interfaces:interfaces/ietf-interfaces:interface/eth0.*"));
94 private Response post(final String uri, final String mediaType, final String data) {
95 return target(uri).request(mediaType).post(Entity.entity(data, mediaType));
98 private Response get(final String uri, final String mediaType) {
99 return target(uri).request(mediaType).get();
102 private static String getRpcInput() {
103 final StringBuilder sb = new StringBuilder();
104 sb.append("<input xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote\">");
105 sb.append("<path xmlns:int=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">/int:interfaces/int:interface[int:name='eth0']</path>");
106 sb.append("</input>");
107 return sb.toString();