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.createUri;
16 import java.io.FileNotFoundException;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.UnsupportedEncodingException;
20 import java.net.URISyntaxException;
21 import java.util.concurrent.Future;
23 import javax.ws.rs.client.Entity;
24 import javax.ws.rs.core.Application;
25 import javax.ws.rs.core.MediaType;
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.StructuredDataToJsonProvider;
36 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
37 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
38 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
39 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
40 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
42 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
43 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
46 public class RestPutOperationTest extends JerseyTest {
48 private static String xmlData;
49 private static String xmlData2;
50 private static String xmlData3;
52 private static BrokerFacade brokerFacade;
53 private static RestconfImpl restconfImpl;
54 private static SchemaContext schemaContextYangsIetf;
55 private static SchemaContext schemaContextTestModule;
58 public static void init() throws IOException {
59 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
60 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
61 ControllerContext controllerContext = ControllerContext.getInstance();
62 controllerContext.setSchemas(schemaContextYangsIetf);
63 brokerFacade = mock(BrokerFacade.class);
64 restconfImpl = RestconfImpl.getInstance();
65 restconfImpl.setBroker(brokerFacade);
66 restconfImpl.setControllerContext(controllerContext);
70 private static void loadData() throws IOException {
71 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
72 xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
73 InputStream xmlStream2 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml");
74 xmlData2 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream2));
75 InputStream xmlStream3 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data7.xml");
76 xmlData3 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream3));
80 protected Application configure() {
81 /* enable/disable Jersey logs to console */
82 // enable(TestProperties.LOG_TRAFFIC);
83 // enable(TestProperties.DUMP_ENTITY);
84 // enable(TestProperties.RECORD_LOG_LEVEL);
85 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
86 ResourceConfig resourceConfig = new ResourceConfig();
87 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
88 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
89 JsonToCompositeNodeProvider.INSTANCE);
90 return resourceConfig;
94 * Tests of status codes for "/config/{identifier}".
97 public void putConfigStatusCodes() throws UnsupportedEncodingException {
98 String uri = createUri("/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));
107 public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException,
108 FileNotFoundException, URISyntaxException {
110 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
111 TransactionStatus.COMMITED).build();
112 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
114 brokerFacade.commitConfigurationDataPutBehindMountPoint(any(MountInstance.class),
115 any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
117 MountInstance mountInstance = mock(MountInstance.class);
118 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
119 MountService mockMountService = mock(MountService.class);
120 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
122 ControllerContext.getInstance().setMountService(mockMountService);
124 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont");
125 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
127 uri = createUri("/config/", "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont");
128 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
132 public void putDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException {
133 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
134 TransactionStatus.COMMITED).build();
135 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
137 brokerFacade.commitConfigurationDataPutBehindMountPoint(any(MountInstance.class),
138 any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
140 MountInstance mountInstance = mock(MountInstance.class);
141 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
142 MountService mockMountService = mock(MountService.class);
143 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
145 ControllerContext.getInstance().setMountService(mockMountService);
147 String uri = createUri("/config/", "ietf-interfaces:interfaces/yang-ext:mount");
148 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData3));
151 private int put(String uri, String mediaType, String data) throws UnsupportedEncodingException {
152 return target(uri).request(mediaType).put(Entity.entity(data, mediaType)).getStatus();
155 private void mockCommitConfigurationDataPutMethod(TransactionStatus statusName) {
156 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
158 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
159 when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
160 .thenReturn(dummyFuture);