Bug 5947: Increasing code coverage - mdsal-common-api 40/40840/3
authorPeter Nosal <peter.nosal@pantheon.tech>
Sat, 25 Jun 2016 20:14:05 +0000 (22:14 +0200)
committerRobert Varga <nite@hq.sk>
Wed, 29 Jun 2016 11:09:43 +0000 (11:09 +0000)
Change-Id: I3dd94a2d2ff0e28daff7a6efc7d01074d26939e5
Signed-off-by: Peter Nosal <peter.nosal@pantheon.tech>
common/mdsal-common-api/pom.xml
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/BasicExceptionTests.java [new file with mode: 0644]
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/LogicalDatastoreTypeTest.java [new file with mode: 0644]
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/PostCommitStepTest.java [new file with mode: 0644]
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/AbstractGenericEntityOwnershipCandidateRegistrationTest.java [new file with mode: 0644]
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/AbstractGenericEntityOwnershipListenerRegistrationTest.java [new file with mode: 0644]
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/EntityOwnershipChangeStateTest.java
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/EntityOwnershipStateTest.java [new file with mode: 0644]
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/GenericEntityOwnershipChangeTest.java [new file with mode: 0644]
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/GenericEntityTest.java [new file with mode: 0644]

index 1b8ae0e4c086a15dc5355f601ca5f0d77a2c52bc..74e3d6c4a362bb07cce836adf752959f9b57c50b 100644 (file)
       <artifactId>junit</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.opendaylight.yangtools</groupId>
+      <artifactId>mockito-configuration</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <scm>
diff --git a/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/BasicExceptionTests.java b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/BasicExceptionTests.java
new file mode 100644 (file)
index 0000000..b1dd978
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2016 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.mdsal.common.api;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+
+import javax.annotation.Nonnull;
+import org.junit.Test;
+import org.opendaylight.mdsal.common.api.clustering.CandidateAlreadyRegisteredException;
+import org.opendaylight.mdsal.common.api.clustering.GenericEntity;
+import org.opendaylight.yangtools.concepts.Path;
+import org.opendaylight.yangtools.yang.common.RpcError;
+
+public class BasicExceptionTests {
+
+    private static final RpcError RPC_ERROR = mock(RpcError.class);
+
+    @Test(expected = TransactionCommitFailedException.class)
+    public void transactionCommitFailedExceptionTest() throws Exception {
+        throw new TransactionCommitFailedException("test", RPC_ERROR);
+    }
+
+    @Test(expected = TransactionCommitDeadlockException.class)
+    public void transactionCommitDeadlockExceptionTest() throws Exception {
+        throw new TransactionCommitDeadlockException(TransactionCommitDeadlockException.DEADLOCK_EXCEPTION_SUPPLIER
+                .get().getMessage(), RPC_ERROR);
+    }
+
+    @Test(expected = TransactionChainClosedException.class)
+    public void transactionChainClosedExceptionTest() throws Exception {
+        throw new TransactionChainClosedException("test");
+    }
+
+    @Test(expected = TransactionChainClosedException.class)
+    public void transactionChainClosedExceptionWithNullCauseTest() throws Exception {
+        throw new TransactionChainClosedException("test", null);
+    }
+
+    @Test(expected = ReadFailedException.class)
+    public void readFailedExceptionTest() throws Exception {
+        throw new ReadFailedException("test", RPC_ERROR);
+    }
+
+    @Test(expected = ReadFailedException.class)
+    public void readFailedExceptionWithThrowableTest() throws Exception {
+
+        throw new ReadFailedException("test", ReadFailedException.MAPPER.apply(
+                new NullPointerException()).getCause(), RPC_ERROR);
+    }
+
+    @Test(expected = OptimisticLockFailedException.class)
+    public void optimisticLockFailedExceptionTest() throws Exception {
+        throw new OptimisticLockFailedException("test");
+    }
+
+    @Test(expected = DataStoreUnavailableException.class)
+    public void dataStoreUnavailableExceptionTest() throws Exception {
+        throw new DataStoreUnavailableException("test", null);
+    }
+
+    @Test(expected = DataValidationFailedException.class)
+    public void dataValidationFailedExceptionTest() throws Exception {
+        final TestClass testClass = new TestClass();
+        final DataValidationFailedException dataValidationFailedException =
+                new DataValidationFailedException(TestClass.class, testClass, "test");
+
+        assertEquals(testClass, dataValidationFailedException.getPath());
+        assertEquals(TestClass.class, dataValidationFailedException.getPathType());
+
+        throw dataValidationFailedException;
+    }
+
+    @Test(expected = CandidateAlreadyRegisteredException.class)
+    public void candidateAlreadyRegisteredExceptionTest() throws Exception {
+        final GenericEntity genericEntity = mock(GenericEntity.class);
+        doReturn("testEntity").when(genericEntity).toString();
+        CandidateAlreadyRegisteredException candidateAlreadyRegisteredException =
+                new CandidateAlreadyRegisteredException(genericEntity);
+        assertEquals(genericEntity, candidateAlreadyRegisteredException.getEntity());
+
+        throw candidateAlreadyRegisteredException;
+    }
+
+    private final class TestClass implements Path {
+        @Override
+        public boolean contains(@Nonnull Path other) {
+            return false;
+        }
+    }
+}
\ No newline at end of file
diff --git a/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/LogicalDatastoreTypeTest.java b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/LogicalDatastoreTypeTest.java
new file mode 100644 (file)
index 0000000..74696ba
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2016 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.mdsal.common.api;
+
+import static org.junit.Assert.assertFalse;
+
+import org.junit.Test;
+
+public class LogicalDatastoreTypeTest {
+
+    @Test
+    public void basicTest() {
+        assertFalse(LogicalDatastoreType.CONFIGURATION.equals(LogicalDatastoreType.OPERATIONAL));
+    }
+}
\ No newline at end of file
diff --git a/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/PostCommitStepTest.java b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/PostCommitStepTest.java
new file mode 100644 (file)
index 0000000..9743416
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2016 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.mdsal.common.api;
+
+import static org.junit.Assert.assertEquals;
+import static org.opendaylight.mdsal.common.api.PostPreCommitStep.NOOP_COMMIT_FUTURE;
+
+import org.junit.Test;
+
+public class PostCommitStepTest {
+
+    @Test
+    public void preCommitTest() throws Exception {
+        final PostCanCommitStep postCanCommitStep = PostCanCommitStep.NOOP;
+
+        assertEquals(ThreePhaseCommitStep.NOOP_ABORT_FUTURE ,postCanCommitStep.abort());
+        assertEquals(PostPreCommitStep.NOOP_FUTURE, postCanCommitStep.preCommit());
+    }
+
+    @Test
+    public void canCommitTest() throws Exception {
+        final PostPreCommitStep postPreCommitStep = PostPreCommitStep.NOOP;
+
+        assertEquals(ThreePhaseCommitStep.NOOP_ABORT_FUTURE ,postPreCommitStep.abort());
+        assertEquals(NOOP_COMMIT_FUTURE, postPreCommitStep.commit());
+    }
+}
\ No newline at end of file
diff --git a/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/AbstractGenericEntityOwnershipCandidateRegistrationTest.java b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/AbstractGenericEntityOwnershipCandidateRegistrationTest.java
new file mode 100644 (file)
index 0000000..ea74020
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2016 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.mdsal.common.api.clustering;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.mock;
+
+import org.junit.Test;
+
+public class AbstractGenericEntityOwnershipCandidateRegistrationTest
+        extends AbstractGenericEntityOwnershipCandidateRegistration {
+
+    @Test
+    public void basicTest() {
+        assertNotNull(this);
+    }
+
+    public AbstractGenericEntityOwnershipCandidateRegistrationTest() {
+        super(mock(GenericEntity.class));
+    }
+
+    @Override
+    protected void removeRegistration() {
+        //NOOP
+    }
+}
\ No newline at end of file
diff --git a/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/AbstractGenericEntityOwnershipListenerRegistrationTest.java b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/AbstractGenericEntityOwnershipListenerRegistrationTest.java
new file mode 100644 (file)
index 0000000..4bcef28
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2016 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.mdsal.common.api.clustering;
+
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+import org.junit.Test;
+
+public class AbstractGenericEntityOwnershipListenerRegistrationTest
+        extends AbstractGenericEntityOwnershipListenerRegistration {
+
+    @Test
+    public void basicTest() throws Exception {
+        assertTrue(this.getEntityType().contains("testEntity"));
+    }
+
+    public AbstractGenericEntityOwnershipListenerRegistrationTest() {
+        super(mock(GenericEntityOwnershipListener.class), "testEntity");
+    }
+
+    @Override
+    protected void removeRegistration() {
+        // NOOP
+    }
+}
\ No newline at end of file
index 3ad043cd181076cc08e9b95564d2a8e0aa2d1f35..fd71d79bf08ec0da380df78af0bca32a0d54d4b6 100644 (file)
@@ -8,6 +8,9 @@
 package org.opendaylight.mdsal.common.api.clustering;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
 import org.junit.Test;
 
 /**
@@ -18,7 +21,7 @@ import org.junit.Test;
 public class EntityOwnershipChangeStateTest {
 
     @Test
-    public void testFromWithValid() {
+    public void testFromWithValid() throws Exception {
         assertEquals("from(false, true, true)", EntityOwnershipChangeState.LOCAL_OWNERSHIP_GRANTED,
                 EntityOwnershipChangeState.from(false, true, true));
         assertEquals("from(true, false, true)", EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NEW_OWNER,
@@ -45,4 +48,13 @@ public class EntityOwnershipChangeStateTest {
     public void testFromWithInvalidTrueTrueTrue() {
         EntityOwnershipChangeState.from(true, true, true);
     }
+
+    @Test
+    public void basicTest() throws Exception {
+        EntityOwnershipChangeState entityOwnershipChangeState = EntityOwnershipChangeState.from(false, true, true);
+        assertTrue(entityOwnershipChangeState.hasOwner());
+        assertTrue(entityOwnershipChangeState.isOwner());
+        assertFalse(entityOwnershipChangeState.wasOwner());
+        assertTrue(entityOwnershipChangeState.toString().matches(".*false.*true.*true.*"));
+    }
 }
\ No newline at end of file
diff --git a/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/EntityOwnershipStateTest.java b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/EntityOwnershipStateTest.java
new file mode 100644 (file)
index 0000000..417f7f5
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2016 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.mdsal.common.api.clustering;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class EntityOwnershipStateTest {
+
+    @Test
+    public void fromTest() throws Exception {
+        assertEquals(EntityOwnershipState.NO_OWNER, EntityOwnershipState.from(false, false));
+        assertEquals(EntityOwnershipState.IS_OWNER, EntityOwnershipState.from(true, false));
+        assertEquals(EntityOwnershipState.OWNED_BY_OTHER, EntityOwnershipState.from(false, true));
+    }
+}
\ No newline at end of file
diff --git a/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/GenericEntityOwnershipChangeTest.java b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/GenericEntityOwnershipChangeTest.java
new file mode 100644 (file)
index 0000000..4c08191
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2016 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.mdsal.common.api.clustering;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+
+import org.junit.Test;
+
+public class GenericEntityOwnershipChangeTest {
+
+    @Test
+    public void basicTest() throws Exception {
+        final GenericEntity genericEntity = mock(GenericEntity.class);
+        doReturn("testEntity").when(genericEntity).toString();
+        final GenericEntityOwnershipChange genericEntityOwnershipChange =
+                new GenericEntityOwnershipChange(genericEntity, EntityOwnershipChangeState.LOCAL_OWNERSHIP_GRANTED);
+
+        assertEquals(genericEntity, genericEntityOwnershipChange.getEntity());
+        assertEquals(EntityOwnershipChangeState.LOCAL_OWNERSHIP_GRANTED, genericEntityOwnershipChange.getState());
+        assertFalse(genericEntityOwnershipChange.inJeopardy());
+        assertTrue(genericEntityOwnershipChange.toString().contains("testEntity"));
+    }
+}
\ No newline at end of file
diff --git a/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/GenericEntityTest.java b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/GenericEntityTest.java
new file mode 100644 (file)
index 0000000..1baa613
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2016 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.mdsal.common.api.clustering;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertTrue;
+
+import javax.annotation.Nonnull;
+import org.junit.Test;
+import org.opendaylight.yangtools.concepts.Path;
+
+public class GenericEntityTest {
+
+    @Test
+    public void basicTest() throws Exception {
+        final TestClass testClass = new TestClass();
+        final GenericEntity genericEntity = new GenericEntity<>("testType", testClass);
+        final GenericEntity genericEntityDiff = new GenericEntity<>("differentTestType", new TestClassDiff());
+
+        assertEquals(TestClass.class, genericEntity.getIdentifier().getClass());
+        assertEquals("testType", genericEntity.getType());
+        assertTrue(genericEntity.toString().contains("testType"));
+        assertNotEquals(genericEntity.hashCode(), genericEntityDiff.hashCode());
+        assertTrue(genericEntity.equals(genericEntity));
+        assertTrue(genericEntity.equals(new GenericEntity<>("testType", testClass)));
+        assertFalse(genericEntity.equals(genericEntityDiff));
+        assertFalse(genericEntity.equals(new String()));
+        assertFalse(genericEntity.equals(new GenericEntity<>("differentTestType", testClass)));
+    }
+
+    private final class TestClass implements Path {
+        @Override
+        public boolean contains(@Nonnull Path other) {
+            return false;
+        }
+    }
+
+    private final class TestClassDiff implements Path {
+        @Override
+        public boolean contains(@Nonnull Path other) {
+            return false;
+        }
+    }
+}
\ No newline at end of file