From 26c882477a21ea9f763ec0efbb731b2a31a9fc2c Mon Sep 17 00:00:00 2001 From: Peter Nosal Date: Sat, 25 Jun 2016 22:14:05 +0200 Subject: [PATCH] Bug 5947: Increasing code coverage - mdsal-common-api Change-Id: I3dd94a2d2ff0e28daff7a6efc7d01074d26939e5 Signed-off-by: Peter Nosal --- common/mdsal-common-api/pom.xml | 5 + .../mdsal/common/api/BasicExceptionTests.java | 97 +++++++++++++++++++ .../common/api/LogicalDatastoreTypeTest.java | 20 ++++ .../mdsal/common/api/PostCommitStepTest.java | 32 ++++++ ...ityOwnershipCandidateRegistrationTest.java | 31 ++++++ ...tityOwnershipListenerRegistrationTest.java | 31 ++++++ .../EntityOwnershipChangeStateTest.java | 14 ++- .../clustering/EntityOwnershipStateTest.java | 22 +++++ .../GenericEntityOwnershipChangeTest.java | 32 ++++++ .../api/clustering/GenericEntityTest.java | 51 ++++++++++ 10 files changed, 334 insertions(+), 1 deletion(-) create mode 100644 common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/BasicExceptionTests.java create mode 100644 common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/LogicalDatastoreTypeTest.java create mode 100644 common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/PostCommitStepTest.java create mode 100644 common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/AbstractGenericEntityOwnershipCandidateRegistrationTest.java create mode 100644 common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/AbstractGenericEntityOwnershipListenerRegistrationTest.java create mode 100644 common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/EntityOwnershipStateTest.java create mode 100644 common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/GenericEntityOwnershipChangeTest.java create mode 100644 common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/GenericEntityTest.java diff --git a/common/mdsal-common-api/pom.xml b/common/mdsal-common-api/pom.xml index 1b8ae0e4c0..74e3d6c4a3 100644 --- a/common/mdsal-common-api/pom.xml +++ b/common/mdsal-common-api/pom.xml @@ -33,6 +33,11 @@ junit test + + org.opendaylight.yangtools + mockito-configuration + test + 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 index 0000000000..b1dd978b16 --- /dev/null +++ b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/BasicExceptionTests.java @@ -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 index 0000000000..74696ba334 --- /dev/null +++ b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/LogicalDatastoreTypeTest.java @@ -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 index 0000000000..97434165cb --- /dev/null +++ b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/PostCommitStepTest.java @@ -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 index 0000000000..ea74020f33 --- /dev/null +++ b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/AbstractGenericEntityOwnershipCandidateRegistrationTest.java @@ -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 index 0000000000..4bcef286f4 --- /dev/null +++ b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/AbstractGenericEntityOwnershipListenerRegistrationTest.java @@ -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 diff --git a/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/EntityOwnershipChangeStateTest.java b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/EntityOwnershipChangeStateTest.java index 3ad043cd18..fd71d79bf0 100644 --- a/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/EntityOwnershipChangeStateTest.java +++ b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/EntityOwnershipChangeStateTest.java @@ -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 index 0000000000..417f7f58ce --- /dev/null +++ b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/EntityOwnershipStateTest.java @@ -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 index 0000000000..4c08191798 --- /dev/null +++ b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/GenericEntityOwnershipChangeTest.java @@ -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 index 0000000000..1baa613a5f --- /dev/null +++ b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/clustering/GenericEntityTest.java @@ -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 -- 2.36.6