Bug 7240 - Restconf returns Status.Ok if delete fails 83/48783/1
authorJakub Toth <jatoth@cisco.com>
Fri, 25 Nov 2016 15:03:24 +0000 (16:03 +0100)
committerJakub Toth <jatoth@cisco.com>
Tue, 29 Nov 2016 12:46:33 +0000 (12:46 +0000)
  * add a private class for setting Throwable object
  * if operation failed, set Throwable object to object created from
    private class
  * throw RestconfDocumentedException in Restconf (doesn't in hijack
    thread) if Throwable object isn't null
  * add test

Change-Id: I413c5d3c969969903951da876277439ac2b67d66
Signed-off-by: Jakub Toth <jatoth@cisco.com>
restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfImpl.java
restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestDeleteOperationTest.java

index 637d345dc79c5e1d074e3eb174ae4d291e8032c8..edb32d48c6a863f44724de664ff2ba968dd911c4 100644 (file)
@@ -1096,18 +1096,19 @@ public class RestconfImpl implements RestconfService {
         }
 
         final CountDownLatch waiter = new CountDownLatch(1);
+        final ResultOperation result = new ResultOperation();
         Futures.addCallback(future, new FutureCallback<Void>() {
 
             @Override
             public void onSuccess(final Void result) {
-                handlerLoggerDelete(null);
+                LOG.trace("Successfuly delete data.");
                 waiter.countDown();
             }
 
             @Override
             public void onFailure(final Throwable t) {
                 waiter.countDown();
-                handlerLoggerDelete(t);
+                result.setFailed(t);
             }
 
         });
@@ -1119,17 +1120,25 @@ public class RestconfImpl implements RestconfService {
             LOG.warn(msg);
             throw new RestconfDocumentedException(msg, e);
         }
-
+        if (result.failed() != null) {
+            final Throwable t = result.failed();
+            final String errMsg = "Error while deleting data";
+            LOG.info(errMsg, t);
+            throw new RestconfDocumentedException(errMsg, RestconfError.ErrorType.APPLICATION,
+                    RestconfError.ErrorTag.OPERATION_FAILED, t);
+        }
         return Response.status(Status.OK).build();
     }
 
-    protected void handlerLoggerDelete(final Throwable t) {
-        if (t != null) {
-            final String errMsg = "Error while deleting data";
-            LOG.info(errMsg, t);
-            throw new RestconfDocumentedException(errMsg, t);
-        } else {
-            LOG.trace("Successfuly delete data.");
+    private class ResultOperation {
+        private Throwable t = null;
+
+        public void setFailed(final Throwable t) {
+            this.t = t;
+        }
+
+        public Throwable failed() {
+            return this.t;
         }
     }
 
index b5435e149a1c25ae48646a4662f039a80168646f..a341ec9121c912419e33875498882cdf2fb38fe6 100644 (file)
@@ -13,6 +13,7 @@ import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
+import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.Futures;
 import java.io.FileNotFoundException;
 import java.io.UnsupportedEncodingException;
@@ -24,6 +25,7 @@ import org.glassfish.jersey.server.ResourceConfig;
 import org.glassfish.jersey.test.JerseyTest;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
@@ -85,4 +87,14 @@ public class RestDeleteOperationTest extends JerseyTest {
         response = target(uri).request(MediaType.APPLICATION_XML).delete();
         assertEquals(500, response.getStatus());
     }
+
+    @Test
+    public void deleteFailTest() throws Exception {
+        final String uri = "/config/test-interface:interfaces";
+        final Exception exception = new TransactionCommitFailedException("failed test");
+        final CheckedFuture future = Futures.immediateFailedCheckedFuture(exception);
+        when(brokerFacade.commitConfigurationDataDelete(any(YangInstanceIdentifier.class))).thenReturn(future);
+        final Response response = target(uri).request(MediaType.APPLICATION_XML).delete();
+        assertEquals(500, response.getStatus());
+    }
 }