BUG 1179 - rpcResult = false but response 200 36/8236/2
authorJozef Gloncak <jgloncak@cisco.com>
Mon, 23 Jun 2014 08:59:15 +0000 (10:59 +0200)
committerJozef Gloncak <jgloncak@cisco.com>
Wed, 25 Jun 2014 06:08:05 +0000 (08:08 +0200)
Case for case when collection of errors is returned was added to
RestPostOperationTest.

Change-Id: Iea6c830d957001b01996e2640d8869105cf805ca
Signed-off-by: Jozef Gloncak <jgloncak@cisco.com>
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/DummyFuture.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestDeleteOperationTest.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPostOperationTest.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPutOperationTest.java

index 22b34a4..19a7eff 100644 (file)
@@ -11,16 +11,14 @@ import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
-
-import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 
-public class DummyFuture implements Future<RpcResult<TransactionStatus>> {
+public class DummyFuture<T> implements Future<RpcResult<T>> {
 
     private final boolean cancel;
     private final boolean isCancelled;
     private final boolean isDone;
-    private final RpcResult<TransactionStatus> result;
+    private final RpcResult<T> result;
 
     public DummyFuture() {
         cancel = false;
@@ -29,16 +27,13 @@ public class DummyFuture implements Future<RpcResult<TransactionStatus>> {
         result = null;
     }
 
-    private DummyFuture(final Builder builder) {
+    private DummyFuture(final Builder<T> builder) {
         cancel = builder.cancel;
         isCancelled = builder.isCancelled;
         isDone = builder.isDone;
         result = builder.result;
     }
 
-    public static Builder builder() {
-        return new DummyFuture.Builder();
-    }
 
     @Override
     public boolean cancel(final boolean mayInterruptIfRunning) {
@@ -56,45 +51,45 @@ public class DummyFuture implements Future<RpcResult<TransactionStatus>> {
     }
 
     @Override
-    public RpcResult<TransactionStatus> get() throws InterruptedException, ExecutionException {
+    public RpcResult<T> get() throws InterruptedException, ExecutionException {
         return result;
     }
 
     @Override
-    public RpcResult<TransactionStatus> get(final long timeout, final TimeUnit unit) throws InterruptedException,
+    public RpcResult<T> get(final long timeout, final TimeUnit unit) throws InterruptedException,
     ExecutionException, TimeoutException {
         return result;
     }
 
-    public static class Builder {
+    public static class Builder<T> {
 
         private boolean cancel;
         private boolean isCancelled;
         private boolean isDone;
-        private RpcResult<TransactionStatus> result;
+        private RpcResult<T> result;
 
-        public Builder cancel(final boolean cancel) {
+        public Builder<T> cancel(final boolean cancel) {
             this.cancel = cancel;
             return this;
         }
 
-        public Builder isCancelled(final boolean isCancelled) {
+        public Builder<T> isCancelled(final boolean isCancelled) {
             this.isCancelled = isCancelled;
             return this;
         }
 
-        public Builder isDone(final boolean isDone) {
+        public Builder<T> isDone(final boolean isDone) {
             this.isDone = isDone;
             return this;
         }
 
-        public Builder rpcResult(final RpcResult<TransactionStatus> result) {
+        public Builder<T> rpcResult(final RpcResult<T> result) {
             this.result = result;
             return this;
         }
 
-        public Future<RpcResult<TransactionStatus>> build() {
-            return new DummyFuture(this);
+        public Future<RpcResult<T>> build() {
+            return new DummyFuture<T>(this);
         }
     }
 }
index 56a58ee..e8cbf31 100644 (file)
@@ -17,11 +17,9 @@ import java.io.FileNotFoundException;
 import java.io.UnsupportedEncodingException;
 import java.util.Set;
 import java.util.concurrent.Future;
-
 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.junit.BeforeClass;
@@ -85,7 +83,7 @@ public class RestDeleteOperationTest extends JerseyTest {
 
     private Future<RpcResult<TransactionStatus>> createFuture(TransactionStatus statusName) {
         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName).build();
-        return DummyFuture.builder().rpcResult(rpcResult).build();
+        return new DummyFuture.Builder<TransactionStatus>().rpcResult(rpcResult).build();
     }
 
 }
index cfbc9fd..5b36fd5 100644 (file)
@@ -15,25 +15,29 @@ import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
 
+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 javax.ws.rs.client.Entity;
 import javax.ws.rs.core.Application;
 import javax.ws.rs.core.MediaType;
-
 import org.glassfish.jersey.server.ResourceConfig;
 import org.glassfish.jersey.test.JerseyTest;
 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.common.util.RpcErrors;
 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.Draft02;
@@ -47,14 +51,15 @@ 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.RpcError.ErrorSeverity;
+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;
 
-import com.google.common.util.concurrent.Futures;
-
 public class RestPostOperationTest extends JerseyTest {
 
     private static String xmlDataAbsolutePath;
@@ -102,12 +107,12 @@ public class RestPostOperationTest extends JerseyTest {
         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
                 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
                 JsonToCompositeNodeProvider.INSTANCE);
-        resourceConfig.registerClasses( RestconfDocumentedExceptionMapper.class );
+        resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
         return resourceConfig;
     }
 
     @Test
-    public void postOperationsStatusCodes() throws UnsupportedEncodingException {
+    public void postOperationsStatusCodes() throws IOException {
         controllerContext.setSchemas(schemaContextTestModule);
         mockInvokeRpc(cnSnDataOutput, true);
         String uri = "/operations/test-module:rpc-test";
@@ -119,6 +124,12 @@ public class RestPostOperationTest extends JerseyTest {
         mockInvokeRpc(null, false);
         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
 
+        List<RpcError> rpcErrors = new ArrayList<>();
+        rpcErrors.add( RpcErrors.getRpcError("applicationTag1", "tag1", "info1", ErrorSeverity.ERROR, "message1", ErrorType.RPC, null));
+        rpcErrors.add( RpcErrors.getRpcError("applicationTag2", "tag2", "info2", ErrorSeverity.WARNING, "message2", ErrorType.PROTOCOL, 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));
     }
@@ -150,7 +161,7 @@ public class RestPostOperationTest extends JerseyTest {
         mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
 
-        assertEquals( 400, post(uri, MediaType.APPLICATION_JSON, "" ));
+        assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
     }
 
     @Test
@@ -158,7 +169,8 @@ public class RestPostOperationTest extends JerseyTest {
         controllerContext.setSchemas(schemaContextYangsIetf);
         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
                 TransactionStatus.COMMITED).build();
-        Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
+        Future<RpcResult<TransactionStatus>> dummyFuture = new DummyFuture.Builder<TransactionStatus>().rpcResult(
+                rpcResult).build();
         when(
                 brokerFacade.commitConfigurationDataPostBehindMountPoint(any(MountInstance.class),
                         any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
@@ -175,14 +187,23 @@ public class RestPostOperationTest extends JerseyTest {
         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, "" ));
+        assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
     }
 
-    private void mockInvokeRpc(CompositeNode result, boolean sucessful) {
-        RpcResult<CompositeNode> rpcResult = new DummyRpcResult.Builder<CompositeNode>().result(result)
-                .isSuccessful(sucessful).build();
+    private void mockInvokeRpc(CompositeNode result, boolean sucessful, Collection<RpcError> errors) {
+
+        DummyRpcResult.Builder<CompositeNode> builder = new DummyRpcResult.Builder<CompositeNode>().result(result)
+                .isSuccessful(sucessful);
+        if (!errors.isEmpty()) {
+            builder.errors(errors);
+        }
+        RpcResult<CompositeNode> rpcResult = builder.build();
         when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class)))
-            .thenReturn(Futures.<RpcResult<CompositeNode>>immediateFuture( rpcResult ));
+                .thenReturn(Futures.<RpcResult<CompositeNode>> immediateFuture(rpcResult));
+    }
+
+    private void mockInvokeRpc(CompositeNode result, boolean sucessful) {
+        mockInvokeRpc(result, sucessful, Collections.<RpcError> emptyList());
     }
 
     private void mockCommitConfigurationDataPostMethod(TransactionStatus statusName) {
@@ -190,9 +211,9 @@ public class RestPostOperationTest extends JerseyTest {
                 .build();
         Future<RpcResult<TransactionStatus>> dummyFuture = null;
         if (statusName != null) {
-            dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
+            dummyFuture = new DummyFuture.Builder<TransactionStatus>().rpcResult(rpcResult).build();
         } else {
-            dummyFuture = DummyFuture.builder().build();
+            dummyFuture = new DummyFuture.Builder<TransactionStatus>().build();
         }
 
         when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
@@ -204,7 +225,8 @@ public class RestPostOperationTest extends JerseyTest {
         initMocking();
         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
                 TransactionStatus.COMMITED).build();
-        Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
+        Future<RpcResult<TransactionStatus>> dummyFuture = new DummyFuture.Builder<TransactionStatus>().rpcResult(
+                rpcResult).build();
 
         when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
                 .thenReturn(dummyFuture);
index 77b39b7..44b5f49 100644 (file)
@@ -18,12 +18,10 @@ import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 import java.net.URISyntaxException;
 import java.util.concurrent.Future;
-
 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.junit.BeforeClass;
@@ -88,7 +86,7 @@ public class RestPutOperationTest extends JerseyTest {
         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
                 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
                 JsonToCompositeNodeProvider.INSTANCE);
-        resourceConfig.registerClasses( RestconfDocumentedExceptionMapper.class );
+        resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
         return resourceConfig;
     }
 
@@ -104,14 +102,15 @@ public class RestPutOperationTest extends JerseyTest {
         mockCommitConfigurationDataPutMethod(TransactionStatus.FAILED);
         assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
 
-        assertEquals( 400, put(uri, MediaType.APPLICATION_JSON, "" ));
+        assertEquals(400, put(uri, MediaType.APPLICATION_JSON, ""));
     }
 
     @Test
     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, "" ));
+        Response resp = target(uri).request(MediaType.APPLICATION_JSON).put(
+                Entity.entity("", MediaType.APPLICATION_JSON));
+        assertEquals(400, put(uri, MediaType.APPLICATION_JSON, ""));
     }
 
     @Test
@@ -120,7 +119,8 @@ public class RestPutOperationTest extends JerseyTest {
 
         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
                 TransactionStatus.COMMITED).build();
-        Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
+        Future<RpcResult<TransactionStatus>> dummyFuture = new DummyFuture.Builder<TransactionStatus>().rpcResult(
+                rpcResult).build();
         when(
                 brokerFacade.commitConfigurationDataPutBehindMountPoint(any(MountInstance.class),
                         any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
@@ -143,7 +143,8 @@ public class RestPutOperationTest extends JerseyTest {
     public void putDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException {
         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
                 TransactionStatus.COMMITED).build();
-        Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
+        Future<RpcResult<TransactionStatus>> dummyFuture = new DummyFuture.Builder<TransactionStatus>().rpcResult(
+                rpcResult).build();
         when(
                 brokerFacade.commitConfigurationDataPutBehindMountPoint(any(MountInstance.class),
                         any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
@@ -166,7 +167,8 @@ public class RestPutOperationTest extends JerseyTest {
     private void mockCommitConfigurationDataPutMethod(TransactionStatus statusName) {
         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
                 .build();
-        Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
+        Future<RpcResult<TransactionStatus>> dummyFuture = new DummyFuture.Builder<TransactionStatus>().rpcResult(
+                rpcResult).build();
         when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
                 .thenReturn(dummyFuture);
     }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.