25988f431f9b6b870b9f09824be64945791a5531
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / websockets / test / RestStream.java
1 /*
2  * Copyright (c) 2014 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.websockets.test;
9
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 java.io.FileNotFoundException;
15 import java.io.UnsupportedEncodingException;
16 import java.net.URI;
17 import javax.ws.rs.client.Entity;
18 import javax.ws.rs.core.Application;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.Response;
21 import org.glassfish.jersey.server.ResourceConfig;
22 import org.glassfish.jersey.test.JerseyTest;
23 import org.junit.BeforeClass;
24 import org.junit.Ignore;
25 import org.junit.Test;
26 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
27 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
28 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
29 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
30 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
31 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
32 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
33 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.w3c.dom.Document;
36 import org.w3c.dom.Element;
37 import org.w3c.dom.Node;
38
39 public class RestStream extends JerseyTest {
40
41     private static BrokerFacade brokerFacade;
42     private static RestconfImpl restconfImpl;
43     private static SchemaContext schemaContextYangsIetf;
44
45     @BeforeClass
46     public static void init() throws FileNotFoundException {
47         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
48         final 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);
54     }
55
56     @Override
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;
68     }
69
70     @Test
71     @Ignore // FIXME : find problem with codec
72     public void testCallRpcCallGet() throws UnsupportedEncodingException, InterruptedException {
73         String uri = "/operations/sal-remote:create-data-change-event-subscription";
74         final Response responseWithStreamName = post(uri, MediaType.APPLICATION_XML, getRpcInput());
75         final Document xmlResponse = responseWithStreamName.readEntity(Document.class);
76         assertNotNull(xmlResponse);
77         final Element outputElement = xmlResponse.getDocumentElement();
78         assertEquals("output",outputElement.getLocalName());
79
80         final Node streamNameElement = outputElement.getFirstChild();
81         assertEquals("stream-name",streamNameElement.getLocalName());
82         assertEquals("ietf-interfaces:interfaces/ietf-interfaces:interface/eth0/datastore=CONFIGURATION/scope=BASE",streamNameElement.getTextContent());
83
84         uri = "/streams/stream/ietf-interfaces:interfaces/ietf-interfaces:interface/eth0/datastore=CONFIGURATION/scope=BASE";
85         final Response responseWithRedirectionUri = get(uri, MediaType.APPLICATION_XML);
86         final URI websocketServerUri = responseWithRedirectionUri.getLocation();
87         assertNotNull(websocketServerUri);
88         assertTrue(websocketServerUri.toString().matches(".*http://localhost:[\\d]+/ietf-interfaces:interfaces/ietf-interfaces:interface/eth0.*"));
89     }
90
91     private Response post(final String uri, final String mediaType, final String data) {
92         return target(uri).request(mediaType).post(Entity.entity(data, mediaType));
93     }
94
95     private Response get(final String uri, final String mediaType) {
96         return target(uri).request(mediaType).get();
97     }
98
99     private String getRpcInput() {
100         final StringBuilder sb = new StringBuilder();
101         sb.append("<input xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote\">");
102         sb.append("<path xmlns:int=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">/int:interfaces/int:interface[int:name='eth0']</path>");
103         sb.append("</input>");
104         return sb.toString();
105     }
106
107 }