X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2Ftest%2FRestPutOperationTest.java;h=3591bfb22bec85f90ef94215a30fdfbef5705784;hp=2bef9ba4ef221a93004b60479134cbf6cb1f5658;hb=d206d27042eef2185c875f85cf6eac61a1bd77c4;hpb=58fa0dd65634800eccaa1bbe85769835a0e6e071 diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPutOperationTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPutOperationTest.java index 2bef9ba4ef..3591bfb22b 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPutOperationTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPutOperationTest.java @@ -1,21 +1,27 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ package org.opendaylight.controller.sal.restconf.impl.test; import static org.junit.Assert.assertEquals; import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.JSON; -import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML; -import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri; -import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.entity; + +import com.google.common.base.Optional; +import com.google.common.util.concurrent.CheckedFuture; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URISyntaxException; -import java.util.concurrent.Future; -import java.util.logging.Level; import javax.ws.rs.client.Entity; import javax.ws.rs.core.Application; @@ -24,30 +30,30 @@ import javax.ws.rs.core.Response; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; -import org.glassfish.jersey.test.TestProperties; -import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; -import org.opendaylight.controller.md.sal.common.api.TransactionStatus; -import org.opendaylight.controller.sal.core.api.mount.MountService; -import org.opendaylight.controller.sal.rest.api.Draft01; -import org.opendaylight.controller.sal.rest.api.Draft02; +import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException; +import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; +import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; +import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService; import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider; +import org.opendaylight.controller.sal.rest.impl.RestconfDocumentedExceptionMapper; import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider; import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider; import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider; import org.opendaylight.controller.sal.restconf.impl.BrokerFacade; import org.opendaylight.controller.sal.restconf.impl.ControllerContext; +import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException; import org.opendaylight.controller.sal.restconf.impl.RestconfImpl; -import org.opendaylight.yangtools.yang.common.RpcResult; -import org.opendaylight.yangtools.yang.data.api.CompositeNode; -import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.model.api.SchemaContext; public class RestPutOperationTest extends JerseyTest { private static String xmlData; - private static String jsonData; + private static String xmlData2; + private static String xmlData3; private static BrokerFacade brokerFacade; private static RestconfImpl restconfImpl; @@ -67,144 +73,137 @@ public class RestPutOperationTest extends JerseyTest { loadData(); } - @Before - public void logs() throws IOException { - /* enable/disable Jersey logs to console */ - /* - * List loggedRecords = getLoggedRecords(); for (LogRecord l - * : loggedRecords) { System.out.println(l.getMessage()); } - */ + private static void loadData() throws IOException { + InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml"); + xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream)); + InputStream xmlStream2 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml"); + xmlData2 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream2)); + InputStream xmlStream3 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data7.xml"); + xmlData3 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream3)); } @Override protected Application configure() { /* enable/disable Jersey logs to console */ - /* - * enable(TestProperties.LOG_TRAFFIC); - */ - enable(TestProperties.DUMP_ENTITY); - enable(TestProperties.RECORD_LOG_LEVEL); - set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue()); - + // enable(TestProperties.LOG_TRAFFIC); + // enable(TestProperties.DUMP_ENTITY); + // enable(TestProperties.RECORD_LOG_LEVEL); + // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue()); ResourceConfig resourceConfig = new ResourceConfig(); resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE, StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE, JsonToCompositeNodeProvider.INSTANCE); + resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class); return resourceConfig; } /** - * Test method - * {@link RestconfImpl#updateConfigurationData(String, CompositeNode)} of - * RestconfImpl for "/config/...identifier..." URL. Return status code is - * 200. - * + * Tests of status codes for "/config/{identifier}". */ @Test - public void putConfigDataViaUrlTest200() throws UnsupportedEncodingException { - mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED); - putDataViaUrlTest("/config/", Draft02.MediaTypes.DATA + JSON, jsonData, 200); - putDataViaUrlTest("/config/", Draft02.MediaTypes.DATA + XML, xmlData, 200); - putDataViaUrlTest("/config/", MediaType.APPLICATION_JSON, jsonData, 200); - putDataViaUrlTest("/config/", MediaType.APPLICATION_XML, xmlData, 200); - putDataViaUrlTest("/config/", MediaType.TEXT_XML, xmlData, 200); + public void putConfigStatusCodes() throws UnsupportedEncodingException { + String uri = "/config/ietf-interfaces:interfaces/interface/eth0"; + mockCommitConfigurationDataPutMethod(true); + assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData)); + + mockCommitConfigurationDataPutMethod(false); + assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData)); + assertEquals(400, put(uri, MediaType.APPLICATION_JSON, "")); } - /** - * Test method - * {@link RestconfImpl#updateConfigurationData(String, CompositeNode)} of - * RestconfImpl for "/config/...identifier..." URL. Return status code is - * 500. - * - */ @Test - public void putConfigDataViaUrlTest500() throws UnsupportedEncodingException { - mockCommitConfigurationDataPutMethod(TransactionStatus.FAILED); - putDataViaUrlTest("/config/", Draft02.MediaTypes.DATA + JSON, jsonData, 500); - putDataViaUrlTest("/config/", Draft02.MediaTypes.DATA + XML, xmlData, 500); - putDataViaUrlTest("/config/", MediaType.APPLICATION_JSON, jsonData, 500); - putDataViaUrlTest("/config/", MediaType.APPLICATION_XML, xmlData, 500); - putDataViaUrlTest("/config/", MediaType.TEXT_XML, xmlData, 500); - + public void putConfigStatusCodesEmptyBody() throws UnsupportedEncodingException { + String uri = "/config/ietf-interfaces:interfaces/interface/eth0"; + Response resp = target(uri).request(MediaType.APPLICATION_JSON).put( + Entity.entity("", MediaType.APPLICATION_JSON)); + assertEquals(400, put(uri, MediaType.APPLICATION_JSON, "")); } - /** - * Test method - * {@link RestconfImpl#updateConfigurationData(String, CompositeNode)} of - * RestconfImpl for "/datastore/...identifier..." URL. Return status code is - * 200. - * - */ @Test - public void putDatastoreDataViaUrlTest200() throws UnsupportedEncodingException { - mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED); - putDataViaUrlTest("/datastore/", Draft01.MediaTypes.DATA + JSON, jsonData, 200); - putDataViaUrlTest("/datastore/", Draft01.MediaTypes.DATA + XML, xmlData, 200); - putDataViaUrlTest("/datastore/", MediaType.APPLICATION_JSON, jsonData, 200); - putDataViaUrlTest("/datastore/", MediaType.APPLICATION_XML, xmlData, 200); - putDataViaUrlTest("/datastore/", MediaType.TEXT_XML, xmlData, 200); + public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException, + FileNotFoundException, URISyntaxException { + + CheckedFuture dummyFuture = mock(CheckedFuture.class); + + when( + brokerFacade.commitConfigurationDataPut(any(DOMMountPoint.class), any(YangInstanceIdentifier.class), + any(NormalizedNode.class))).thenReturn(dummyFuture); + + DOMMountPoint mountInstance = mock(DOMMountPoint.class); + when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule); + DOMMountPointService mockMountService = mock(DOMMountPointService.class); + when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance)); + + ControllerContext.getInstance().setMountService(mockMountService); + + String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont"; + assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2)); + + uri = "/config/ietf-interfaces:interfaces/yang-ext:mount/test-module:cont"; + assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2)); } - /** - * Test method - * {@link RestconfImpl#updateConfigurationData(String, CompositeNode)} of - * RestconfImpl for "/datastore/...identifier..." URL. Return status code is - * 500. - * - */ @Test - public void putDatastoreDataViaUrlTest500() throws UnsupportedEncodingException { - mockCommitConfigurationDataPutMethod(TransactionStatus.FAILED); - putDataViaUrlTest("/datastore/", Draft01.MediaTypes.DATA + JSON, jsonData, 500); - putDataViaUrlTest("/datastore/", Draft01.MediaTypes.DATA + XML, xmlData, 500); - putDataViaUrlTest("/datastore/", MediaType.APPLICATION_JSON, jsonData, 500); - putDataViaUrlTest("/datastore/", MediaType.APPLICATION_XML, xmlData, 500); - putDataViaUrlTest("/datastore/", MediaType.TEXT_XML, xmlData, 500); + public void putDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException { + CheckedFuture dummyFuture = mock(CheckedFuture.class); + when( + brokerFacade.commitConfigurationDataPut(any(DOMMountPoint.class), any(YangInstanceIdentifier.class), + any(NormalizedNode.class))).thenReturn(dummyFuture); + + DOMMountPoint mountInstance = mock(DOMMountPoint.class); + when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule); + DOMMountPointService mockMountService = mock(DOMMountPointService.class); + when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance)); + + ControllerContext.getInstance().setMountService(mockMountService); + + String uri = "/config/ietf-interfaces:interfaces/yang-ext:mount"; + assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData3)); } @Test - public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException, - FileNotFoundException, URISyntaxException { + public void putWithOptimisticLockFailedException() throws UnsupportedEncodingException { - mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED); + String uri = "/config/ietf-interfaces:interfaces/interface/eth0"; - InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml"); - String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream)); - Entity entity = Entity.entity(xml, Draft02.MediaTypes.DATA + XML); + doThrow(OptimisticLockFailedException.class). + when(brokerFacade).commitConfigurationDataPut( + any(YangInstanceIdentifier.class), any(NormalizedNode.class)); - MountService mockMountService = mock(MountService.class); - when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn( - new DummyMountInstanceImpl.Builder().setSchemaContext(schemaContextTestModule).build()); + assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData)); - ControllerContext.getInstance().setMountService(mockMountService); + doThrow(OptimisticLockFailedException.class).doReturn(mock(CheckedFuture.class)). + when(brokerFacade).commitConfigurationDataPut( + any(YangInstanceIdentifier.class), any(NormalizedNode.class)); - String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/test-module:cont"); - Response response = target(uri).request(Draft02.MediaTypes.DATA + XML).put(entity); - assertEquals(200, response.getStatus()); + assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData)); } - private void putDataViaUrlTest(String uriPrefix, String mediaType, String data, int responseStatus) - throws UnsupportedEncodingException { - String uri = createUri(uriPrefix, "ietf-interfaces:interfaces/interface/eth0"); - Response response = target(uri).request(mediaType).put(entity(data, mediaType)); - assertEquals(responseStatus, response.getStatus()); - } + @Test + public void putWithTransactionCommitFailedException() throws UnsupportedEncodingException { - private static void loadData() throws IOException { - InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml"); - xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream)); + String uri = "/config/ietf-interfaces:interfaces/interface/eth0"; + + doThrow(TransactionCommitFailedException.class). + when(brokerFacade).commitConfigurationDataPut( + any(YangInstanceIdentifier.class), any(NormalizedNode.class)); + + assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData)); + } - String jsonPath = RestconfImplTest.class.getResource("/parts/ietf-interfaces_interfaces.json").getPath(); - jsonData = TestUtils.loadTextFile(jsonPath); + private int put(String uri, String mediaType, String data) throws UnsupportedEncodingException { + return target(uri).request(mediaType).put(Entity.entity(data, mediaType)).getStatus(); } - private void mockCommitConfigurationDataPutMethod(TransactionStatus statusName) { - RpcResult rpcResult = new DummyRpcResult.Builder().result(statusName) - .build(); - Future> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build(); - when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))) - .thenReturn(dummyFuture); + private void mockCommitConfigurationDataPutMethod(final boolean noErrors) { + if (noErrors) { + doReturn(mock(CheckedFuture.class)).when(brokerFacade).commitConfigurationDataPut( + any(YangInstanceIdentifier.class), any(NormalizedNode.class)); + } else { + doThrow(RestconfDocumentedException.class).when(brokerFacade).commitConfigurationDataPut( + any(YangInstanceIdentifier.class), any(NormalizedNode.class)); + } } }