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