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;
19 import java.io.FileNotFoundException;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.UnsupportedEncodingException;
23 import java.net.URISyntaxException;
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;
28 import org.glassfish.jersey.server.ResourceConfig;
29 import org.glassfish.jersey.test.JerseyTest;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
33 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
34 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
35 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
36 import org.opendaylight.controller.sal.rest.impl.RestconfDocumentedExceptionMapper;
37 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
38 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
39 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
40 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
41 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
42 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
43 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
46 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
48 public class RestPutOperationTest extends JerseyTest {
50 private static String xmlData;
51 private static String xmlData2;
52 private static String xmlData3;
54 private static BrokerFacade brokerFacade;
55 private static RestconfImpl restconfImpl;
56 private static SchemaContext schemaContextYangsIetf;
57 private static SchemaContext schemaContextTestModule;
60 public static void init() throws IOException {
61 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
62 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
63 ControllerContext controllerContext = ControllerContext.getInstance();
64 controllerContext.setSchemas(schemaContextYangsIetf);
65 brokerFacade = mock(BrokerFacade.class);
66 restconfImpl = RestconfImpl.getInstance();
67 restconfImpl.setBroker(brokerFacade);
68 restconfImpl.setControllerContext(controllerContext);
72 private static void loadData() throws IOException {
73 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
74 xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
75 InputStream xmlStream2 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml");
76 xmlData2 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream2));
77 InputStream xmlStream3 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data7.xml");
78 xmlData3 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream3));
82 protected Application configure() {
83 /* enable/disable Jersey logs to console */
84 // enable(TestProperties.LOG_TRAFFIC);
85 // enable(TestProperties.DUMP_ENTITY);
86 // enable(TestProperties.RECORD_LOG_LEVEL);
87 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
88 ResourceConfig resourceConfig = new ResourceConfig();
89 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
90 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
91 JsonToCompositeNodeProvider.INSTANCE);
92 resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
93 return resourceConfig;
97 * Tests of status codes for "/config/{identifier}".
100 public void putConfigStatusCodes() throws UnsupportedEncodingException {
101 String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
102 mockCommitConfigurationDataPutMethod(true);
103 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData));
105 mockCommitConfigurationDataPutMethod(false);
106 assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
108 assertEquals(400, put(uri, MediaType.APPLICATION_JSON, ""));
112 public void putConfigStatusCodesEmptyBody() throws UnsupportedEncodingException {
113 String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
114 Response resp = target(uri).request(MediaType.APPLICATION_JSON).put(
115 Entity.entity("", MediaType.APPLICATION_JSON));
116 assertEquals(400, put(uri, MediaType.APPLICATION_JSON, ""));
120 public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException,
121 FileNotFoundException, URISyntaxException {
123 CheckedFuture<Void, TransactionCommitFailedException> dummyFuture = mock(CheckedFuture.class);
126 brokerFacade.commitConfigurationDataPut(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
127 any(NormalizedNode.class))).thenReturn(dummyFuture);
129 DOMMountPoint mountInstance = mock(DOMMountPoint.class);
130 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
131 DOMMountPointService mockMountService = mock(DOMMountPointService.class);
132 when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
134 ControllerContext.getInstance().setMountService(mockMountService);
136 String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
137 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
139 uri = "/config/ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
140 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
144 public void putDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException {
145 CheckedFuture<Void, TransactionCommitFailedException> dummyFuture = mock(CheckedFuture.class);
147 brokerFacade.commitConfigurationDataPut(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
148 any(NormalizedNode.class))).thenReturn(dummyFuture);
150 DOMMountPoint mountInstance = mock(DOMMountPoint.class);
151 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
152 DOMMountPointService mockMountService = mock(DOMMountPointService.class);
153 when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
155 ControllerContext.getInstance().setMountService(mockMountService);
157 String uri = "/config/ietf-interfaces:interfaces/yang-ext:mount";
158 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData3));
161 private int put(String uri, String mediaType, String data) throws UnsupportedEncodingException {
162 return target(uri).request(mediaType).put(Entity.entity(data, mediaType)).getStatus();
165 private void mockCommitConfigurationDataPutMethod(final boolean noErrors) {
167 doReturn(mock(CheckedFuture.class)).when(brokerFacade).commitConfigurationDataPut(
168 any(YangInstanceIdentifier.class), any(NormalizedNode.class));
170 doThrow(RestconfDocumentedException.class).when(brokerFacade).commitConfigurationDataPut(
171 any(YangInstanceIdentifier.class), any(NormalizedNode.class));