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;
14 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
15 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
17 import java.io.FileNotFoundException;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.UnsupportedEncodingException;
21 import java.net.URISyntaxException;
22 import java.util.concurrent.Future;
24 import javax.ws.rs.client.Entity;
25 import javax.ws.rs.core.Application;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
29 import org.glassfish.jersey.server.ResourceConfig;
30 import org.glassfish.jersey.test.JerseyTest;
31 import org.junit.BeforeClass;
32 import org.junit.Ignore;
33 import org.junit.Test;
34 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
35 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
36 import org.opendaylight.controller.sal.core.api.mount.MountService;
37 import org.opendaylight.controller.sal.rest.api.Draft02;
38 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
39 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
40 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
41 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
42 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
43 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
44 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
45 import org.opendaylight.yangtools.yang.common.RpcResult;
46 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
47 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
50 public class RestPutOperationTest extends JerseyTest {
52 private static String xmlData;
53 private static String xmlData2;
54 private static String xmlData3;
56 private static BrokerFacade brokerFacade;
57 private static RestconfImpl restconfImpl;
58 private static SchemaContext schemaContextYangsIetf;
59 private static SchemaContext schemaContextTestModule;
62 public static void init() throws IOException {
63 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
64 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
65 ControllerContext controllerContext = ControllerContext.getInstance();
66 controllerContext.setSchemas(schemaContextYangsIetf);
67 brokerFacade = mock(BrokerFacade.class);
68 restconfImpl = RestconfImpl.getInstance();
69 restconfImpl.setBroker(brokerFacade);
70 restconfImpl.setControllerContext(controllerContext);
74 private static void loadData() throws IOException {
75 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
76 xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
77 InputStream xmlStream2 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml");
78 xmlData2 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream2));
79 InputStream xmlStream3 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data7.xml");
80 xmlData3 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream3));
84 protected Application configure() {
85 /* enable/disable Jersey logs to console */
86 // enable(TestProperties.LOG_TRAFFIC);
87 // enable(TestProperties.DUMP_ENTITY);
88 // enable(TestProperties.RECORD_LOG_LEVEL);
89 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
90 ResourceConfig resourceConfig = new ResourceConfig();
91 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
92 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
93 JsonToCompositeNodeProvider.INSTANCE);
94 return resourceConfig;
98 * Tests of status codes for "/config/{identifier}".
101 public void putConfigStatusCodes() throws UnsupportedEncodingException {
102 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
103 mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED);
104 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData));
106 mockCommitConfigurationDataPutMethod(TransactionStatus.FAILED);
107 assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
111 * Tests of status codes for "/datastore/{identifier}".
114 public void putDatastoreStatusCodes() throws UnsupportedEncodingException {
115 String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
116 mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED);
117 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData));
119 mockCommitConfigurationDataPutMethod(TransactionStatus.FAILED);
120 assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
124 public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException,
125 FileNotFoundException, URISyntaxException {
127 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
128 TransactionStatus.COMMITED).build();
129 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
131 brokerFacade.commitConfigurationDataPutBehindMountPoint(any(MountInstance.class),
132 any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
134 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml");
135 String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
136 Entity<String> entity = Entity.entity(xml, Draft02.MediaTypes.DATA + XML);
138 MountInstance mountInstance = mock(MountInstance.class);
139 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
140 MountService mockMountService = mock(MountService.class);
141 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
143 ControllerContext.getInstance().setMountService(mockMountService);
145 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont");
146 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
148 uri = createUri("/config/", "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont");
149 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
153 public void putDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException {
154 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
155 TransactionStatus.COMMITED).build();
156 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
158 brokerFacade.commitConfigurationDataPutBehindMountPoint(any(MountInstance.class),
159 any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
161 MountInstance mountInstance = mock(MountInstance.class);
162 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
163 MountService mockMountService = mock(MountService.class);
164 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
166 ControllerContext.getInstance().setMountService(mockMountService);
168 String uri = createUri("/config/", "ietf-interfaces:interfaces/yang-ext:mount");
169 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData3));
172 private int put(String uri, String mediaType, String data) throws UnsupportedEncodingException {
173 return target(uri).request(mediaType).put(Entity.entity(data, mediaType)).getStatus();
176 private void mockCommitConfigurationDataPutMethod(TransactionStatus statusName) {
177 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
179 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
180 when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
181 .thenReturn(dummyFuture);