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;
21 import javax.ws.rs.client.Entity;
22 import javax.ws.rs.core.Application;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.Response;
25 import org.glassfish.jersey.server.ResourceConfig;
26 import org.glassfish.jersey.test.JerseyTest;
27 import org.junit.BeforeClass;
28 import org.junit.Test;
29 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
30 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
31 import org.opendaylight.controller.sal.core.api.mount.MountService;
32 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
33 import org.opendaylight.controller.sal.rest.impl.RestconfDocumentedExceptionMapper;
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;
45 public class RestPutOperationTest extends JerseyTest {
47 private static String xmlData;
48 private static String xmlData2;
49 private static String xmlData3;
51 private static BrokerFacade brokerFacade;
52 private static RestconfImpl restconfImpl;
53 private static SchemaContext schemaContextYangsIetf;
54 private static SchemaContext schemaContextTestModule;
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);
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));
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 resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
90 return resourceConfig;
94 * Tests of status codes for "/config/{identifier}".
97 public void putConfigStatusCodes() throws UnsupportedEncodingException {
98 String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
99 mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED);
100 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData));
102 mockCommitConfigurationDataPutMethod(TransactionStatus.FAILED);
103 assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
105 assertEquals(400, put(uri, MediaType.APPLICATION_JSON, ""));
109 public void putConfigStatusCodesEmptyBody() throws UnsupportedEncodingException {
110 String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
111 Response resp = target(uri).request(MediaType.APPLICATION_JSON).put(
112 Entity.entity("", MediaType.APPLICATION_JSON));
113 assertEquals(400, put(uri, MediaType.APPLICATION_JSON, ""));
117 public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException,
118 FileNotFoundException, URISyntaxException {
120 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
121 TransactionStatus.COMMITED).build();
122 Future<RpcResult<TransactionStatus>> dummyFuture = new DummyFuture.Builder<TransactionStatus>().rpcResult(
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 = new DummyFuture.Builder<TransactionStatus>().rpcResult(
149 brokerFacade.commitConfigurationDataPutBehindMountPoint(any(MountInstance.class),
150 any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
152 MountInstance mountInstance = mock(MountInstance.class);
153 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
154 MountService mockMountService = mock(MountService.class);
155 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
157 ControllerContext.getInstance().setMountService(mockMountService);
159 String uri = "/config/ietf-interfaces:interfaces/yang-ext:mount";
160 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData3));
163 private int put(String uri, String mediaType, String data) throws UnsupportedEncodingException {
164 return target(uri).request(mediaType).put(Entity.entity(data, mediaType)).getStatus();
167 private void mockCommitConfigurationDataPutMethod(TransactionStatus statusName) {
168 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
170 Future<RpcResult<TransactionStatus>> dummyFuture = new DummyFuture.Builder<TransactionStatus>().rpcResult(
172 when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
173 .thenReturn(dummyFuture);