2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.test;
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;
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;
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;
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;
47 public class RestPutOperationTest extends JerseyTest {
49 private static String xmlData;
50 private static String xmlData2;
51 private static String xmlData3;
53 private static BrokerFacade brokerFacade;
54 private static RestconfImpl restconfImpl;
55 private static SchemaContext schemaContextYangsIetf;
56 private static SchemaContext schemaContextTestModule;
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);
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));
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;
96 * Tests of status codes for "/config/{identifier}".
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));
104 mockCommitConfigurationDataPutMethod(TransactionStatus.FAILED);
105 assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
107 assertEquals( 400, put(uri, MediaType.APPLICATION_JSON, "" ));
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, "" ));
118 public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException,
119 FileNotFoundException, URISyntaxException {
121 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
122 TransactionStatus.COMMITED).build();
123 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
125 brokerFacade.commitConfigurationDataPutBehindMountPoint(any(MountInstance.class),
126 any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
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);
133 ControllerContext.getInstance().setMountService(mockMountService);
135 String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
136 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
138 uri = "/config/ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
139 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
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();
148 brokerFacade.commitConfigurationDataPutBehindMountPoint(any(MountInstance.class),
149 any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
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);
156 ControllerContext.getInstance().setMountService(mockMountService);
158 String uri = "/config/ietf-interfaces:interfaces/yang-ext:mount";
159 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData3));
162 private int put(String uri, String mediaType, String data) throws UnsupportedEncodingException {
163 return target(uri).request(mediaType).put(Entity.entity(data, mediaType)).getStatus();
166 private void mockCommitConfigurationDataPutMethod(TransactionStatus statusName) {
167 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
169 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
170 when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
171 .thenReturn(dummyFuture);