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