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.doReturn;
13 import static org.mockito.Mockito.doThrow;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
17 import com.google.common.base.Optional;
18 import com.google.common.util.concurrent.CheckedFuture;
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.UnsupportedEncodingException;
24 import java.net.URISyntaxException;
26 import javax.ws.rs.client.Entity;
27 import javax.ws.rs.core.Application;
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response;
31 import org.glassfish.jersey.server.ResourceConfig;
32 import org.glassfish.jersey.test.JerseyTest;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
36 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
37 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
38 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
39 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
40 import org.opendaylight.controller.sal.rest.impl.RestconfDocumentedExceptionMapper;
41 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
42 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
43 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
44 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
45 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
46 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
47 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
50 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
52 public class RestPutOperationTest extends JerseyTest {
54 private static String xmlData;
55 private static String xmlData2;
56 private static String xmlData3;
58 private static BrokerFacade brokerFacade;
59 private static RestconfImpl restconfImpl;
60 private static SchemaContext schemaContextYangsIetf;
61 private static SchemaContext schemaContextTestModule;
64 public static void init() throws IOException {
65 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
66 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
67 ControllerContext controllerContext = ControllerContext.getInstance();
68 controllerContext.setSchemas(schemaContextYangsIetf);
69 brokerFacade = mock(BrokerFacade.class);
70 restconfImpl = RestconfImpl.getInstance();
71 restconfImpl.setBroker(brokerFacade);
72 restconfImpl.setControllerContext(controllerContext);
76 private static void loadData() throws IOException {
77 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
78 xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
79 InputStream xmlStream2 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml");
80 xmlData2 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream2));
81 InputStream xmlStream3 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data7.xml");
82 xmlData3 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream3));
86 protected Application configure() {
87 /* enable/disable Jersey logs to console */
88 // enable(TestProperties.LOG_TRAFFIC);
89 // enable(TestProperties.DUMP_ENTITY);
90 // enable(TestProperties.RECORD_LOG_LEVEL);
91 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
92 ResourceConfig resourceConfig = new ResourceConfig();
93 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
94 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
95 JsonToCompositeNodeProvider.INSTANCE);
96 resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
97 return resourceConfig;
101 * Tests of status codes for "/config/{identifier}".
104 public void putConfigStatusCodes() throws UnsupportedEncodingException {
105 String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
106 mockCommitConfigurationDataPutMethod(true);
107 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData));
109 mockCommitConfigurationDataPutMethod(false);
110 assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
112 assertEquals(400, put(uri, MediaType.APPLICATION_JSON, ""));
116 public void putConfigStatusCodesEmptyBody() throws UnsupportedEncodingException {
117 String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
118 Response resp = target(uri).request(MediaType.APPLICATION_JSON).put(
119 Entity.entity("", MediaType.APPLICATION_JSON));
120 assertEquals(400, put(uri, MediaType.APPLICATION_JSON, ""));
124 public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException,
125 FileNotFoundException, URISyntaxException {
127 CheckedFuture<Void, TransactionCommitFailedException> dummyFuture = mock(CheckedFuture.class);
130 brokerFacade.commitConfigurationDataPut(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
131 any(NormalizedNode.class))).thenReturn(dummyFuture);
133 DOMMountPoint mountInstance = mock(DOMMountPoint.class);
134 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
135 DOMMountPointService mockMountService = mock(DOMMountPointService.class);
136 when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
138 ControllerContext.getInstance().setMountService(mockMountService);
140 String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
141 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
143 uri = "/config/ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
144 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
148 public void putDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException {
149 CheckedFuture<Void, TransactionCommitFailedException> dummyFuture = mock(CheckedFuture.class);
151 brokerFacade.commitConfigurationDataPut(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
152 any(NormalizedNode.class))).thenReturn(dummyFuture);
154 DOMMountPoint mountInstance = mock(DOMMountPoint.class);
155 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
156 DOMMountPointService mockMountService = mock(DOMMountPointService.class);
157 when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
159 ControllerContext.getInstance().setMountService(mockMountService);
161 String uri = "/config/ietf-interfaces:interfaces/yang-ext:mount";
162 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData3));
166 public void putWithOptimisticLockFailedException() throws UnsupportedEncodingException {
168 String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
170 doThrow(OptimisticLockFailedException.class).
171 when(brokerFacade).commitConfigurationDataPut(
172 any(YangInstanceIdentifier.class), any(NormalizedNode.class));
174 assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
176 doThrow(OptimisticLockFailedException.class).doReturn(mock(CheckedFuture.class)).
177 when(brokerFacade).commitConfigurationDataPut(
178 any(YangInstanceIdentifier.class), any(NormalizedNode.class));
180 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData));
184 public void putWithTransactionCommitFailedException() throws UnsupportedEncodingException {
186 String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
188 doThrow(TransactionCommitFailedException.class).
189 when(brokerFacade).commitConfigurationDataPut(
190 any(YangInstanceIdentifier.class), any(NormalizedNode.class));
192 assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
195 private int put(String uri, String mediaType, String data) throws UnsupportedEncodingException {
196 return target(uri).request(mediaType).put(Entity.entity(data, mediaType)).getStatus();
199 private void mockCommitConfigurationDataPutMethod(final boolean noErrors) {
201 doReturn(mock(CheckedFuture.class)).when(brokerFacade).commitConfigurationDataPut(
202 any(YangInstanceIdentifier.class), any(NormalizedNode.class));
204 doThrow(RestconfDocumentedException.class).when(brokerFacade).commitConfigurationDataPut(
205 any(YangInstanceIdentifier.class), any(NormalizedNode.class));