Merge "Fixed for bug 1168 : Issue while update subnet"
[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
15 import java.io.FileNotFoundException;
16 import java.io.UnsupportedEncodingException;
17 import java.net.URI;
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.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
36 public class RestStream extends JerseyTest {
37
38     private static BrokerFacade brokerFacade;
39     private static RestconfImpl restconfImpl;
40     private static SchemaContext schemaContextYangsIetf;
41
42     @BeforeClass
43     public static void init() throws FileNotFoundException {
44         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
45         ControllerContext controllerContext = ControllerContext.getInstance();
46         controllerContext.setSchemas(schemaContextYangsIetf);
47         brokerFacade = mock(BrokerFacade.class);
48         restconfImpl = RestconfImpl.getInstance();
49         restconfImpl.setBroker(brokerFacade);
50         restconfImpl.setControllerContext(controllerContext);
51     }
52
53     @Override
54     protected Application configure() {
55         /* enable/disable Jersey logs to console */
56         // enable(TestProperties.LOG_TRAFFIC);
57         // enable(TestProperties.DUMP_ENTITY);
58         // enable(TestProperties.RECORD_LOG_LEVEL);
59         // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
60         ResourceConfig resourceConfig = new ResourceConfig();
61         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
62                 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
63                 JsonToCompositeNodeProvider.INSTANCE);
64         return resourceConfig;
65     }
66
67     @Test
68     public void testCallRpcCallGet() throws UnsupportedEncodingException, InterruptedException {
69         String uri = "/operations/sal-remote:create-data-change-event-subscription";
70         Response responseWithStreamName = post(uri, MediaType.APPLICATION_XML, getRpcInput());
71         String xmlResponse = responseWithStreamName.readEntity(String.class);
72         assertNotNull(xmlResponse);
73         assertTrue(xmlResponse
74                 .contains("<stream-name>ietf-interfaces:interfaces/ietf-interfaces:interface/eth0</stream-name>"));
75
76         uri = "/streams/stream/ietf-interfaces:interfaces/ietf-interfaces:interface/eth0";
77         Response responseWithRedirectionUri = get(uri, MediaType.APPLICATION_XML);
78         final URI websocketServerUri = responseWithRedirectionUri.getLocation();
79         assertNotNull(websocketServerUri);
80         assertEquals(websocketServerUri.toString(),
81                 "http://localhost:8181/ietf-interfaces:interfaces/ietf-interfaces:interface/eth0");
82     }
83
84     private Response post(String uri, String mediaType, String data) {
85         return target(uri).request(mediaType).post(Entity.entity(data, mediaType));
86     }
87
88     private Response get(String uri, String mediaType) {
89         return target(uri).request(mediaType).get();
90     }
91
92     private String getRpcInput() {
93         StringBuilder sb = new StringBuilder();
94         sb.append("<input xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote\">");
95         sb.append("<path xmlns:int=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">/int:interfaces/int:interface[int:name='eth0']</path>");
96         sb.append("</input>");
97         return sb.toString();
98     }
99
100 }