bug 344 interpretation of slashes ('/') in URI
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestPutOperationTest.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.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14
15 import java.io.FileNotFoundException;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.UnsupportedEncodingException;
19 import java.net.URISyntaxException;
20 import java.util.concurrent.Future;
21
22 import javax.ws.rs.client.Entity;
23 import javax.ws.rs.core.Application;
24 import javax.ws.rs.core.MediaType;
25
26 import org.glassfish.jersey.server.ResourceConfig;
27 import org.glassfish.jersey.test.JerseyTest;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
31 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
32 import org.opendaylight.controller.sal.core.api.mount.MountService;
33 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
34 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
35 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
36 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
37 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
38 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
39 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
42 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44
45 public class RestPutOperationTest extends JerseyTest {
46
47     private static String xmlData;
48     private static String xmlData2;
49     private static String xmlData3;
50
51     private static BrokerFacade brokerFacade;
52     private static RestconfImpl restconfImpl;
53     private static SchemaContext schemaContextYangsIetf;
54     private static SchemaContext schemaContextTestModule;
55
56     @BeforeClass
57     public static void init() throws IOException {
58         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
59         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
60         ControllerContext controllerContext = ControllerContext.getInstance();
61         controllerContext.setSchemas(schemaContextYangsIetf);
62         brokerFacade = mock(BrokerFacade.class);
63         restconfImpl = RestconfImpl.getInstance();
64         restconfImpl.setBroker(brokerFacade);
65         restconfImpl.setControllerContext(controllerContext);
66         loadData();
67     }
68
69     private static void loadData() throws IOException {
70         InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
71         xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
72         InputStream xmlStream2 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml");
73         xmlData2 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream2));
74         InputStream xmlStream3 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data7.xml");
75         xmlData3 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream3));
76     }
77
78     @Override
79     protected Application configure() {
80         /* enable/disable Jersey logs to console */
81         // enable(TestProperties.LOG_TRAFFIC);
82         // enable(TestProperties.DUMP_ENTITY);
83         // enable(TestProperties.RECORD_LOG_LEVEL);
84         // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
85         ResourceConfig resourceConfig = new ResourceConfig();
86         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
87                 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
88                 JsonToCompositeNodeProvider.INSTANCE);
89         return resourceConfig;
90     }
91
92     /**
93      * Tests of status codes for "/config/{identifier}".
94      */
95     @Test
96     public void putConfigStatusCodes() throws UnsupportedEncodingException {
97         String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
98         mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED);
99         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData));
100
101         mockCommitConfigurationDataPutMethod(TransactionStatus.FAILED);
102         assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
103     }
104
105     @Test
106     public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException,
107             FileNotFoundException, URISyntaxException {
108
109         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
110                 TransactionStatus.COMMITED).build();
111         Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
112         when(
113                 brokerFacade.commitConfigurationDataPutBehindMountPoint(any(MountInstance.class),
114                         any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
115
116         MountInstance mountInstance = mock(MountInstance.class);
117         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
118         MountService mockMountService = mock(MountService.class);
119         when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
120
121         ControllerContext.getInstance().setMountService(mockMountService);
122
123         String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
124         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
125
126         uri = "/config/ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
127         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
128     }
129
130     @Test
131     public void putDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException {
132         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
133                 TransactionStatus.COMMITED).build();
134         Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
135         when(
136                 brokerFacade.commitConfigurationDataPutBehindMountPoint(any(MountInstance.class),
137                         any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
138
139         MountInstance mountInstance = mock(MountInstance.class);
140         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
141         MountService mockMountService = mock(MountService.class);
142         when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
143
144         ControllerContext.getInstance().setMountService(mockMountService);
145
146         String uri = "/config/ietf-interfaces:interfaces/yang-ext:mount";
147         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData3));
148     }
149
150     private int put(String uri, String mediaType, String data) throws UnsupportedEncodingException {
151         return target(uri).request(mediaType).put(Entity.entity(data, mediaType)).getStatus();
152     }
153
154     private void mockCommitConfigurationDataPutMethod(TransactionStatus statusName) {
155         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
156                 .build();
157         Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
158         when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
159                 .thenReturn(dummyFuture);
160     }
161
162 }