Improved unit test coverage (covered some untested branches)
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / connection / SimpleRpcListenerTest.java
index db9d689ea9a47e5256c4bacadae54a28e36ec96b..3f49196e2c059383c446b48fb3abaabd942e416c 100644 (file)
@@ -9,12 +9,19 @@
 package org.opendaylight.openflowjava.protocol.impl.connection;\r
 \r
 import static org.junit.Assert.fail;\r
+import static org.mockito.Mockito.times;\r
+import static org.mockito.Mockito.verify;\r
+import static org.mockito.Mockito.when;\r
+import io.netty.util.concurrent.Future;\r
 \r
 import java.util.Collections;\r
 import java.util.concurrent.ExecutionException;\r
 \r
 import org.junit.Assert;\r
+import org.junit.Before;\r
 import org.junit.Test;\r
+import org.mockito.Mock;\r
+import org.mockito.MockitoAnnotations;\r
 import org.opendaylight.controller.sal.common.util.Rpcs;\r
 import org.opendaylight.yangtools.yang.common.RpcError;\r
 import org.opendaylight.yangtools.yang.common.RpcResult;\r
@@ -27,6 +34,16 @@ import com.google.common.util.concurrent.SettableFuture;
  */\r
 public class SimpleRpcListenerTest {\r
 \r
+    @Mock Future<Void> future;\r
+\r
+    /**\r
+     * Initializes mocks\r
+     */\r
+    @Before\r
+    public void startUp() {\r
+        MockitoAnnotations.initMocks(this);\r
+    }\r
+\r
     /**\r
      * Test SimpleRpcListener creation\r
      */\r
@@ -54,4 +71,37 @@ public class SimpleRpcListenerTest {
             fail("Problem accessing result");\r
         }\r
     }\r
+\r
+    /**\r
+     * Test rpc success\r
+     */\r
+    @Test\r
+    public void testOperationComplete() {\r
+        when(future.isSuccess()).thenReturn(false);\r
+        SimpleRpcListener listener = new SimpleRpcListener("MESSAGE", "Failed to send message");\r
+        listener.operationComplete(future);\r
+        verify(future, times(1)).cause();\r
+        try {\r
+            Assert.assertEquals("Wrong result", 1, listener.getResult().get().getErrors().size());\r
+        } catch (InterruptedException | ExecutionException e) {\r
+            Assert.fail();\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Test rpc success\r
+     */\r
+    @Test\r
+    public void testOperationComplete2() {\r
+        when(future.isSuccess()).thenReturn(true);\r
+        SimpleRpcListener listener = new SimpleRpcListener("MESSAGE", "Failed to send message");\r
+        listener.operationComplete(future);\r
+        verify(future, times(0)).cause();\r
+        try {\r
+            Assert.assertEquals("Wrong result", 0, listener.getResult().get().getErrors().size());\r
+            Assert.assertEquals("Wrong result", true, listener.getResult().get().isSuccessful());\r
+        } catch (InterruptedException | ExecutionException e) {\r
+            Assert.fail();\r
+        }\r
+    }\r
 }
\ No newline at end of file