X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2Ftest%2FRestPostOperationTest.java;h=833d030b920b46f8cdeb039b2ff16b6da70d8da3;hb=f8225a74b7c11db1ebf77ab9de998c2e953f8ed8;hp=9bf6a0f03cd961ed4c03c0cf263bdd3528dacfa3;hpb=d60428fa21a7a0f041259d5ffccdb049a062f64d;p=controller.git diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPostOperationTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPostOperationTest.java index 9bf6a0f03c..af9dd28359 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPostOperationTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPostOperationTest.java @@ -1,39 +1,47 @@ +/* + * 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.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; 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 java.io.FileNotFoundException; +import com.google.common.util.concurrent.Futures; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Set; import java.util.concurrent.Future; -import java.util.logging.Level; - import javax.ws.rs.client.Entity; import javax.ws.rs.core.Application; import javax.ws.rs.core.MediaType; -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.mockito.ArgumentCaptor; import org.opendaylight.controller.md.sal.common.api.TransactionStatus; +import org.opendaylight.controller.sal.core.api.mount.MountInstance; 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.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; @@ -42,27 +50,34 @@ import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper; import org.opendaylight.controller.sal.restconf.impl.ControllerContext; import org.opendaylight.controller.sal.restconf.impl.RestconfImpl; import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.common.RpcError; +import org.opendaylight.yangtools.yang.common.RpcResultBuilder; +import org.opendaylight.yangtools.yang.common.RpcError.ErrorType; 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.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; public class RestPostOperationTest extends JerseyTest { - private static String xmlData; private static String xmlDataAbsolutePath; - private static String jsonData; - private static String jsonDataAbsolutePath; + private static String xmlDataInterfaceAbsolutePath; private static String xmlDataRpcInput; + private static String xmlBlockData; + private static String xmlTestInterface; private static CompositeNodeWrapper cnSnDataOutput; - private static String jsonDataRpcInput; - private static String xmlData2; + private static String xmlData3; + private static String xmlData4; private static ControllerContext controllerContext; private static BrokerFacade brokerFacade; private static RestconfImpl restconfImpl; private static SchemaContext schemaContextYangsIetf; private static SchemaContext schemaContextTestModule; + private static SchemaContext schemaContext; + + private static MountService mountService; @BeforeClass public static void init() throws URISyntaxException, IOException { @@ -73,231 +88,123 @@ public class RestPostOperationTest extends JerseyTest { restconfImpl = RestconfImpl.getInstance(); restconfImpl.setBroker(brokerFacade); restconfImpl.setControllerContext(controllerContext); - loadData(); - } - @Before - public void logs() throws IOException, URISyntaxException { - /* enable/disable Jersey logs to console */ - /* - * List loggedRecords = getLoggedRecords(); for (LogRecord l - * : loggedRecords) { System.out.println(l.getMessage()); } - */ + Set modules = TestUtils.loadModulesFrom("/test-config-data/yang1"); + schemaContext = TestUtils.loadSchemaContext(modules); + + loadData(); } @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 - public void postOperationsDataViaUrl200() throws URISyntaxException, IOException { + public void postOperationsStatusCodes() throws IOException { controllerContext.setSchemas(schemaContextTestModule); - postOperationsDataViaUrl(Draft02.MediaTypes.DATA + JSON, cnSnDataOutput, jsonDataRpcInput, 200); - postOperationsDataViaUrl(Draft01.MediaTypes.DATA + JSON, cnSnDataOutput, jsonDataRpcInput, 200); - postOperationsDataViaUrl(MediaType.APPLICATION_JSON, cnSnDataOutput, jsonDataRpcInput, 200); - - postOperationsDataViaUrl(Draft01.MediaTypes.DATA + XML, cnSnDataOutput, xmlDataRpcInput, 200); - postOperationsDataViaUrl(Draft02.MediaTypes.DATA + XML, cnSnDataOutput, xmlDataRpcInput, 200); - postOperationsDataViaUrl(MediaType.APPLICATION_XML, cnSnDataOutput, xmlDataRpcInput, 200); - postOperationsDataViaUrl(MediaType.TEXT_XML, cnSnDataOutput, xmlDataRpcInput, 200); + mockInvokeRpc(cnSnDataOutput, true); + String uri = "/operations/test-module:rpc-test"; + assertEquals(200, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput)); + + mockInvokeRpc(null, true); + assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput)); + + mockInvokeRpc(null, false); + assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput)); + + List rpcErrors = new ArrayList<>(); + rpcErrors.add( RpcResultBuilder.newError( ErrorType.RPC, "tag1", "message1", + "applicationTag1", "info1", null ) ); + rpcErrors.add( RpcResultBuilder.newWarning( ErrorType.PROTOCOL, "tag2", "message2", + "applicationTag2", "info2", null ) ); + mockInvokeRpc(null, false, rpcErrors); + assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput)); + + uri = "/operations/test-module:rpc-wrongtest"; + assertEquals(400, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput)); } @Test - public void postOperationsDataViaUrl204() throws URISyntaxException, IOException { - controllerContext.setSchemas(schemaContextTestModule); - postOperationsDataViaUrl(Draft02.MediaTypes.DATA + JSON, cnSnDataOutput, jsonDataRpcInput, 204); - postOperationsDataViaUrl(Draft01.MediaTypes.DATA + JSON, cnSnDataOutput, jsonDataRpcInput, 204); - postOperationsDataViaUrl(MediaType.APPLICATION_JSON, cnSnDataOutput, jsonDataRpcInput, 204); - - postOperationsDataViaUrl(Draft01.MediaTypes.DATA + XML, cnSnDataOutput, xmlDataRpcInput, 204); - postOperationsDataViaUrl(Draft02.MediaTypes.DATA + XML, cnSnDataOutput, xmlDataRpcInput, 204); - postOperationsDataViaUrl(MediaType.APPLICATION_XML, cnSnDataOutput, xmlDataRpcInput, 204); - postOperationsDataViaUrl(MediaType.TEXT_XML, cnSnDataOutput, xmlDataRpcInput, 204); - } - - @Test - public void postOperationsDataViaUrl500() throws URISyntaxException, IOException { - controllerContext.setSchemas(schemaContextTestModule); - postOperationsDataViaUrl(Draft02.MediaTypes.DATA + JSON, cnSnDataOutput, jsonDataRpcInput, 500); - postOperationsDataViaUrl(Draft01.MediaTypes.DATA + JSON, cnSnDataOutput, jsonDataRpcInput, 500); - postOperationsDataViaUrl(MediaType.APPLICATION_JSON, cnSnDataOutput, jsonDataRpcInput, 500); - - postOperationsDataViaUrl(Draft01.MediaTypes.DATA + XML, cnSnDataOutput, xmlDataRpcInput, 500); - postOperationsDataViaUrl(Draft02.MediaTypes.DATA + XML, cnSnDataOutput, xmlDataRpcInput, 500); - postOperationsDataViaUrl(MediaType.APPLICATION_XML, cnSnDataOutput, xmlDataRpcInput, 500); - postOperationsDataViaUrl(MediaType.TEXT_XML, cnSnDataOutput, xmlDataRpcInput, 500); - } - - @Test - public void postOperationsDataViaUrl400() throws URISyntaxException, IOException { - controllerContext.setSchemas(schemaContextTestModule); - postOperationsDataViaUrl(Draft02.MediaTypes.DATA + JSON, cnSnDataOutput, jsonDataRpcInput, 400); - postOperationsDataViaUrl(Draft01.MediaTypes.DATA + JSON, cnSnDataOutput, jsonDataRpcInput, 400); - postOperationsDataViaUrl(MediaType.APPLICATION_JSON, cnSnDataOutput, jsonDataRpcInput, 400); - - postOperationsDataViaUrl(Draft01.MediaTypes.DATA + XML, cnSnDataOutput, xmlDataRpcInput, 400); - postOperationsDataViaUrl(Draft02.MediaTypes.DATA + XML, cnSnDataOutput, xmlDataRpcInput, 400); - postOperationsDataViaUrl(MediaType.APPLICATION_XML, cnSnDataOutput, xmlDataRpcInput, 400); - postOperationsDataViaUrl(MediaType.TEXT_XML, cnSnDataOutput, xmlDataRpcInput, 400); - } - - @Test - public void postOperationsDataViaUrl404() throws URISyntaxException, IOException { - controllerContext.setSchemas(schemaContextTestModule); - postOperationsDataViaUrl(Draft02.MediaTypes.DATA + JSON, cnSnDataOutput, jsonDataRpcInput, 404); - postOperationsDataViaUrl(Draft01.MediaTypes.DATA + JSON, cnSnDataOutput, jsonDataRpcInput, 404); - postOperationsDataViaUrl(MediaType.APPLICATION_JSON, cnSnDataOutput, jsonDataRpcInput, 404); - - postOperationsDataViaUrl(Draft01.MediaTypes.DATA + XML, cnSnDataOutput, xmlDataRpcInput, 404); - postOperationsDataViaUrl(Draft02.MediaTypes.DATA + XML, cnSnDataOutput, xmlDataRpcInput, 404); - postOperationsDataViaUrl(MediaType.APPLICATION_XML, cnSnDataOutput, xmlDataRpcInput, 404); - postOperationsDataViaUrl(MediaType.TEXT_XML, cnSnDataOutput, xmlDataRpcInput, 404); - } - - @Test - public void postConfigDataViaUrlConfigOnlyTest204() throws UnsupportedEncodingException, FileNotFoundException { + public void postConfigOnlyStatusCodes() throws UnsupportedEncodingException { controllerContext.setSchemas(schemaContextYangsIetf); mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED); - postDataViaUrlTest("/config", "", Draft02.MediaTypes.DATA + JSON, jsonDataAbsolutePath, 204); - postDataViaUrlTest("/config", "", Draft02.MediaTypes.DATA + XML, xmlDataAbsolutePath, 204); - postDataViaUrlTest("/config", "", MediaType.APPLICATION_JSON, jsonDataAbsolutePath, 204); - postDataViaUrlTest("/config", "", MediaType.APPLICATION_XML, xmlDataAbsolutePath, 204); - postDataViaUrlTest("/config", "", MediaType.TEXT_XML, xmlDataAbsolutePath, 204); - } + String uri = "/config"; + assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath)); - @Test - public void postConfigDataViaUrlConfigOnlyTest202() throws UnsupportedEncodingException, FileNotFoundException { - controllerContext.setSchemas(schemaContextYangsIetf); mockCommitConfigurationDataPostMethod(null); - postDataViaUrlTest("/config", "", Draft02.MediaTypes.DATA + JSON, jsonDataAbsolutePath, 202); - postDataViaUrlTest("/config", "", Draft02.MediaTypes.DATA + XML, xmlDataAbsolutePath, 202); - postDataViaUrlTest("/config", "", MediaType.APPLICATION_JSON, jsonDataAbsolutePath, 202); - postDataViaUrlTest("/config", "", MediaType.APPLICATION_XML, xmlDataAbsolutePath, 202); - postDataViaUrlTest("/config", "", MediaType.TEXT_XML, xmlDataAbsolutePath, 202); - } + assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath)); - @Test - public void postConfigDataViaUrlConfigOnlyTest500() throws UnsupportedEncodingException, FileNotFoundException { - controllerContext.setSchemas(schemaContextYangsIetf); mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED); - postDataViaUrlTest("/config", "", Draft02.MediaTypes.DATA + JSON, jsonDataAbsolutePath, 500); - postDataViaUrlTest("/config", "", Draft02.MediaTypes.DATA + XML, xmlDataAbsolutePath, 500); - postDataViaUrlTest("/config", "", MediaType.APPLICATION_JSON, jsonDataAbsolutePath, 500); - postDataViaUrlTest("/config", "", MediaType.APPLICATION_XML, xmlDataAbsolutePath, 500); - postDataViaUrlTest("/config", "", MediaType.TEXT_XML, xmlDataAbsolutePath, 500); + assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath)); } @Test - public void postConfigDataViaUrlTest204() throws UnsupportedEncodingException { + public void postConfigStatusCodes() throws UnsupportedEncodingException { controllerContext.setSchemas(schemaContextYangsIetf); mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED); - String urlPath = "ietf-interfaces:interfaces"; - postDataViaUrlTest("/config/", urlPath, Draft02.MediaTypes.DATA + JSON, jsonData, 204); - postDataViaUrlTest("/config/", urlPath, Draft02.MediaTypes.DATA + XML, xmlData, 204); - postDataViaUrlTest("/config/", urlPath, MediaType.APPLICATION_JSON, jsonData, 204); - postDataViaUrlTest("/config/", urlPath, MediaType.APPLICATION_XML, xmlData, 204); - postDataViaUrlTest("/config/", urlPath, MediaType.TEXT_XML, xmlData, 204); - } + String uri = "/config/ietf-interfaces:interfaces"; + assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath)); - @Test - public void postConfigDataViaUrlTest202() throws UnsupportedEncodingException { - controllerContext.setSchemas(schemaContextYangsIetf); mockCommitConfigurationDataPostMethod(null); - String urlPath = "ietf-interfaces:interfaces"; - postDataViaUrlTest("/config/", urlPath, Draft02.MediaTypes.DATA + JSON, jsonData, 202); - postDataViaUrlTest("/config/", urlPath, Draft02.MediaTypes.DATA + XML, xmlData, 202); - postDataViaUrlTest("/config/", urlPath, MediaType.APPLICATION_JSON, jsonData, 202); - postDataViaUrlTest("/config/", urlPath, MediaType.APPLICATION_XML, xmlData, 202); - postDataViaUrlTest("/config/", urlPath, MediaType.TEXT_XML, xmlData, 202); - } + assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath)); - @Test - public void postConfigDataViaUrlTest500() throws UnsupportedEncodingException { - controllerContext.setSchemas(schemaContextYangsIetf); mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED); - String urlPath = "ietf-interfaces:interfaces"; - postDataViaUrlTest("/config/", urlPath, Draft02.MediaTypes.DATA + JSON, jsonData, 500); - postDataViaUrlTest("/config/", urlPath, Draft02.MediaTypes.DATA + XML, xmlData, 500); - postDataViaUrlTest("/config/", urlPath, MediaType.APPLICATION_JSON, jsonData, 500); - postDataViaUrlTest("/config/", urlPath, MediaType.APPLICATION_XML, xmlData, 500); - postDataViaUrlTest("/config/", urlPath, MediaType.TEXT_XML, xmlData, 500); - } - - @Test - public void postDatastoreDataViaUrlTest204() throws UnsupportedEncodingException { - controllerContext.setSchemas(schemaContextYangsIetf); - mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED); - String urlPath = "ietf-interfaces:interfaces"; - postDataViaUrlTest("/datastore/", urlPath, Draft01.MediaTypes.DATA + JSON, jsonData, 204); - postDataViaUrlTest("/datastore/", urlPath, Draft01.MediaTypes.DATA + XML, xmlData, 204); - postDataViaUrlTest("/datastore/", urlPath, MediaType.APPLICATION_JSON, jsonData, 204); - postDataViaUrlTest("/datastore/", urlPath, MediaType.APPLICATION_XML, xmlData, 204); - postDataViaUrlTest("/datastore/", urlPath, MediaType.TEXT_XML, xmlData, 204); - } + assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath)); - @Test - public void postDatastoreDataViaUrlTest202() throws UnsupportedEncodingException { - controllerContext.setSchemas(schemaContextYangsIetf); - mockCommitConfigurationDataPostMethod(null); - String urlPath = "ietf-interfaces:interfaces"; - postDataViaUrlTest("/datastore/", urlPath, Draft01.MediaTypes.DATA + JSON, jsonData, 202); - postDataViaUrlTest("/datastore/", urlPath, Draft01.MediaTypes.DATA + XML, xmlData, 202); - postDataViaUrlTest("/datastore/", urlPath, MediaType.APPLICATION_JSON, jsonData, 202); - postDataViaUrlTest("/datastore/", urlPath, MediaType.APPLICATION_XML, xmlData, 202); - postDataViaUrlTest("/datastore/", urlPath, MediaType.TEXT_XML, xmlData, 202); - } - - @Test - public void postDatastoreDataViaUrlTest500() throws UnsupportedEncodingException { - controllerContext.setSchemas(schemaContextYangsIetf); - mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED); - String urlPath = "ietf-interfaces:interfaces"; - postDataViaUrlTest("/datastore/", urlPath, Draft01.MediaTypes.DATA + JSON, jsonData, 500); - postDataViaUrlTest("/datastore/", urlPath, Draft01.MediaTypes.DATA + XML, xmlData, 500); - postDataViaUrlTest("/datastore/", urlPath, MediaType.APPLICATION_JSON, jsonData, 500); - postDataViaUrlTest("/datastore/", urlPath, MediaType.APPLICATION_XML, xmlData, 500); - postDataViaUrlTest("/datastore/", urlPath, MediaType.TEXT_XML, xmlData, 500); + assertEquals(400, post(uri, MediaType.APPLICATION_JSON, "")); } @Test public void postDataViaUrlMountPoint() throws UnsupportedEncodingException { controllerContext.setSchemas(schemaContextYangsIetf); - mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED); - + RpcResult rpcResult = new DummyRpcResult.Builder().result( + TransactionStatus.COMMITED).build(); + Future> dummyFuture = new DummyFuture.Builder().rpcResult( + rpcResult).build(); + when( + brokerFacade.commitConfigurationDataPostBehindMountPoint(any(MountInstance.class), + any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture); + + MountInstance mountInstance = mock(MountInstance.class); + when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule); MountService mockMountService = mock(MountService.class); - SchemaContext otherSchemaContext = schemaContextTestModule; - when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn( - new DummyMountInstanceImpl.Builder().setSchemaContext(otherSchemaContext).build()); + when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance); ControllerContext.getInstance().setMountService(mockMountService); - String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/test-module:cont/cont1"); - Response response = target(uri).request(Draft02.MediaTypes.DATA + XML).post( - entity(xmlData2, Draft02.MediaTypes.DATA + XML)); - // 204 code is returned when COMMITED transaction status is put as input - // to mock method - assertEquals(204, response.getStatus()); + String uri = "/config/ietf-interfaces:interfaces/interface/0/"; + assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4)); + uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont"; + assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3)); + + assertEquals(400, post(uri, MediaType.APPLICATION_JSON, "")); } - private void postDataViaUrlTest(String urlPrefix, String urlPath, String mediaType, String data, int responseStatus) - throws UnsupportedEncodingException { - String url = createUri(urlPrefix, urlPath); - Response response = target(url).request(mediaType).post(entity(data, mediaType)); - assertEquals(responseStatus, response.getStatus()); + private void mockInvokeRpc(CompositeNode result, boolean sucessful, Collection errors) { + + DummyRpcResult.Builder builder = new DummyRpcResult.Builder().result(result) + .isSuccessful(sucessful); + if (!errors.isEmpty()) { + builder.errors(errors); + } + RpcResult rpcResult = builder.build(); + when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenReturn( + Futures.> immediateFuture(rpcResult)); + } + + private void mockInvokeRpc(CompositeNode result, boolean sucessful) { + mockInvokeRpc(result, sucessful, Collections. emptyList()); } private void mockCommitConfigurationDataPostMethod(TransactionStatus statusName) { @@ -305,80 +212,99 @@ public class RestPostOperationTest extends JerseyTest { .build(); Future> dummyFuture = null; if (statusName != null) { - dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build(); + dummyFuture = new DummyFuture.Builder().rpcResult(rpcResult).build(); } else { - dummyFuture = DummyFuture.builder().build(); + dummyFuture = new DummyFuture.Builder().build(); } when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class))) .thenReturn(dummyFuture); } - private static CompositeNodeWrapper prepareCnSnRpcOutput() throws URISyntaxException { - CompositeNodeWrapper cnSnDataOutput = new CompositeNodeWrapper(new URI("test:module"), "output"); - CompositeNodeWrapper cont = new CompositeNodeWrapper(new URI("test:module"), "cont-output"); - cnSnDataOutput.addValue(cont); - cnSnDataOutput.unwrap(); - return cnSnDataOutput; - } + @Test + public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException { + initMocking(); + RpcResult rpcResult = new DummyRpcResult.Builder().result( + TransactionStatus.COMMITED).build(); + Future> dummyFuture = new DummyFuture.Builder().rpcResult( + rpcResult).build(); - private void mockInvokeRpc(CompositeNode compositeNode, boolean sucessful) { - RpcResult rpcResult = new DummyRpcResult.Builder().result(compositeNode) - .isSuccessful(sucessful).build(); - when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenReturn(rpcResult); - } + when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class))) + .thenReturn(dummyFuture); - private void postOperationsDataViaUrl(String mediaType, CompositeNode cnSnDataOut, String dataIn, int statusCode) - throws FileNotFoundException, UnsupportedEncodingException { - String url = createUri("/operations/", "test-module:rpc-test"); - Response response = null; - switch (statusCode) { - case 200: - mockInvokeRpc(cnSnDataOut, true); - break; - case 204: - mockInvokeRpc(null, true); - break; - case 500: - mockInvokeRpc(null, false); - break; - case 400: - response = target(url).request(mediaType).post(Entity.entity("{}", mediaType)); - break; - case 404: - url = createUri("/operations/", "test-module:rpc-wrongtest"); - break; - } - response = response == null ? target(url).request(mediaType).post(Entity.entity(dataIn, mediaType)) : response; - assertEquals(statusCode, response.getStatus()); + ArgumentCaptor instanceIdCaptor = ArgumentCaptor.forClass(InstanceIdentifier.class); + ArgumentCaptor compNodeCaptor = ArgumentCaptor.forClass(CompositeNode.class); + + String URI_1 = "/config"; + assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface)); + verify(brokerFacade).commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture()); + String identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]"; + assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString()); + + String URI_2 = "/config/test-interface:interfaces"; + assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData)); + verify(brokerFacade, times(2)) + .commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture()); + identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces, (urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)block]"; + assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString()); } - private static void loadData() throws IOException, URISyntaxException { + @Test + public void createConfigurationDataNullTest() throws UnsupportedEncodingException { + initMocking(); - InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml"); - xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream)); + when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class))) + .thenReturn(null); - xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces_absolute_path.xml"); - xmlDataAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream)); + String URI_1 = "/config"; + assertEquals(202, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface)); + + String URI_2 = "/config/test-interface:interfaces"; + assertEquals(202, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData)); + } - String jsonPath = RestconfImplTest.class.getResource("/parts/ietf-interfaces_interfaces.json").getPath(); - jsonData = TestUtils.loadTextFile(jsonPath); + private static void initMocking() { + controllerContext = ControllerContext.getInstance(); + controllerContext.setSchemas(schemaContext); + mountService = mock(MountService.class); + controllerContext.setMountService(mountService); + brokerFacade = mock(BrokerFacade.class); + restconfImpl = RestconfImpl.getInstance(); + restconfImpl.setBroker(brokerFacade); + restconfImpl.setControllerContext(controllerContext); + } - String jsonFullPath = RestconfImplTest.class - .getResource("/parts/ietf-interfaces_interfaces_absolute_path.json").getPath(); - jsonDataAbsolutePath = TestUtils.loadTextFile(jsonFullPath); + private int post(String uri, String mediaType, String data) { + return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus(); + } + private static void loadData() throws IOException, URISyntaxException { + InputStream xmlStream = RestconfImplTest.class + .getResourceAsStream("/parts/ietf-interfaces_interfaces_absolute_path.xml"); + xmlDataAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream)); + xmlStream = RestconfImplTest.class + .getResourceAsStream("/parts/ietf-interfaces_interfaces_interface_absolute_path.xml"); + xmlDataInterfaceAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream)); String xmlPathRpcInput = RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.xml") .getPath(); xmlDataRpcInput = TestUtils.loadTextFile(xmlPathRpcInput); - cnSnDataOutput = prepareCnSnRpcOutput(); - - String jsonPathToRpcInput = RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.json") + String xmlPathBlockData = RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath(); + xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData); + String xmlPathTestInterface = RestconfImplTest.class.getResource("/test-config-data/xml/test-interface.xml") .getPath(); - jsonDataRpcInput = TestUtils.loadTextFile(jsonPathToRpcInput); - - String data2Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data2.xml").getPath(); - xmlData2 = TestUtils.loadTextFile(data2Input); + xmlTestInterface = TestUtils.loadTextFile(xmlPathTestInterface); + cnSnDataOutput = prepareCnSnRpcOutput(); + String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath(); + xmlData3 = TestUtils.loadTextFile(data3Input); + String data4Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data7.xml").getPath(); + xmlData4 = TestUtils.loadTextFile(data4Input); + } + private static CompositeNodeWrapper prepareCnSnRpcOutput() throws URISyntaxException { + CompositeNodeWrapper cnSnDataOutput = new CompositeNodeWrapper(new URI("test:module"), "output"); + CompositeNodeWrapper cont = new CompositeNodeWrapper(new URI("test:module"), "cont-output"); + cnSnDataOutput.addValue(cont); + cnSnDataOutput.unwrap(); + return cnSnDataOutput; } }