From: miroslav.kovac Date: Fri, 10 Mar 2017 11:57:15 +0000 (+0100) Subject: Unit test for RequestSuccess.java and derived classes X-Git-Tag: release/carbon~156 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=249ea1d3475edb5980ebb2044892009edce49654 Unit test for RequestSuccess.java and derived classes Change-Id: Ic8998c07ae714f862e18bddccda790fc8c90ee9e Signed-off-by: miroslav.kovac --- diff --git a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/AbstractRequestSuccessTest.java b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/AbstractRequestSuccessTest.java new file mode 100644 index 0000000000..f0bb6f325f --- /dev/null +++ b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/AbstractRequestSuccessTest.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.controller.cluster.access.commands; + +import org.apache.commons.lang.SerializationUtils; +import org.junit.Assert; +import org.junit.Test; +import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier; +import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier; +import org.opendaylight.controller.cluster.access.concepts.FrontendType; +import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier; +import org.opendaylight.controller.cluster.access.concepts.MemberName; +import org.opendaylight.controller.cluster.access.concepts.RequestSuccess; + +public abstract class AbstractRequestSuccessTest { + + private static final FrontendIdentifier FRONTEND_IDENTIFIER = FrontendIdentifier.create( + MemberName.forName("test"), FrontendType.forName("one")); + protected static final ClientIdentifier CLIENT_IDENTIFIER = ClientIdentifier.create(FRONTEND_IDENTIFIER, 0); + protected static final LocalHistoryIdentifier HISTORY_IDENTIFIER = new LocalHistoryIdentifier( + CLIENT_IDENTIFIER, 0); + + protected abstract T object(); + + @SuppressWarnings("unchecked") + @Test + public void serializationTest() { + final Object deserialize = SerializationUtils.clone(object()); + + Assert.assertEquals(object().getTarget(), ((T) deserialize).getTarget()); + Assert.assertEquals(object().getVersion(), ((T) deserialize).getVersion()); + Assert.assertEquals(object().getSequence(), ((T) deserialize).getSequence()); + doAdditionalAssertions((T) deserialize); + } + + protected abstract void doAdditionalAssertions(final Object deserialize); +} diff --git a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/AbstractTransactionSuccessTest.java b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/AbstractTransactionSuccessTest.java index d67ebd8df1..de9d9ff3d5 100644 --- a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/AbstractTransactionSuccessTest.java +++ b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/AbstractTransactionSuccessTest.java @@ -7,35 +7,12 @@ */ package org.opendaylight.controller.cluster.access.commands; -import org.apache.commons.lang.SerializationUtils; -import org.junit.Assert; -import org.junit.Test; -import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier; -import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier; -import org.opendaylight.controller.cluster.access.concepts.FrontendType; -import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier; -import org.opendaylight.controller.cluster.access.concepts.MemberName; import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier; -public abstract class AbstractTransactionSuccessTest { - private static final FrontendIdentifier FRONTEND_IDENTIFIER = FrontendIdentifier.create( - MemberName.forName("test"), FrontendType.forName("one")); - private static final ClientIdentifier CLIENT_IDENTIFIER = ClientIdentifier.create(FRONTEND_IDENTIFIER, 0); - private static final LocalHistoryIdentifier HISTORY_IDENTIFIER = new LocalHistoryIdentifier( - CLIENT_IDENTIFIER, 0); +public abstract class AbstractTransactionSuccessTest + extends AbstractRequestSuccessTest { protected static final TransactionIdentifier TRANSACTION_IDENTIFIER = new TransactionIdentifier( HISTORY_IDENTIFIER, 0); - protected abstract T object(); - - @SuppressWarnings("unchecked") - @Test - public void serializationTest() { - final Object deserialize = SerializationUtils.clone(object()); - - Assert.assertEquals(object().getTarget(), ((T) deserialize).getTarget()); - Assert.assertEquals(object().getVersion(), ((T) deserialize).getVersion()); - Assert.assertEquals(object().getSequence(), ((T) deserialize).getSequence()); - } } diff --git a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/ConnectClientSuccessTest.java b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/ConnectClientSuccessTest.java new file mode 100644 index 0000000000..ea346e8788 --- /dev/null +++ b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/ConnectClientSuccessTest.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.controller.cluster.access.commands; + +import akka.actor.ActorRef; +import akka.actor.ActorSelection; +import akka.actor.ActorSystem; +import akka.actor.ExtendedActorSystem; +import akka.serialization.JavaSerializer; +import akka.testkit.TestProbe; +import com.google.common.base.MoreObjects; +import com.google.common.collect.ImmutableList; +import java.util.Collection; +import java.util.List; +import java.util.Optional; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.controller.cluster.access.ABIVersion; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree; +import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType; +import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory; + +public class ConnectClientSuccessTest extends AbstractRequestSuccessTest { + + private static final DataTree TREE = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL); + private static final ActorSystem SYSTEM = ActorSystem.create("test"); + private static final ActorRef ACTOR_REF = TestProbe.apply(SYSTEM).ref(); + private static final ActorSelection ACTOR_SELECTION = ActorSelection.apply(ACTOR_REF, "foo"); + private static final List ALTERNATES = ImmutableList.of(ACTOR_SELECTION); + private static final int MAX_MESSAGES = 10; + private static final ConnectClientSuccess OBJECT = new ConnectClientSuccess( + CLIENT_IDENTIFIER, 0, ACTOR_REF, ALTERNATES, TREE, MAX_MESSAGES); + + @Override + protected ConnectClientSuccess object() { + return OBJECT; + } + + @Before + public void setUp() { + JavaSerializer.currentSystem().value_$eq((ExtendedActorSystem) SYSTEM); + } + + @Test + public void testGetAlternates() { + final Collection alternates = OBJECT.getAlternates(); + Assert.assertArrayEquals(ALTERNATES.toArray(), alternates.toArray()); + } + + @Test + public void testGetBackend() { + final ActorRef actorRef = OBJECT.getBackend(); + Assert.assertEquals(ACTOR_REF, actorRef); + } + + @Test + public void testGetDataTree() { + final DataTree tree = OBJECT.getDataTree().get(); + Assert.assertEquals(TREE, tree); + } + + @Test + public void testGetMaxMessages() { + final int maxMessages = OBJECT.getMaxMessages(); + Assert.assertEquals(MAX_MESSAGES, maxMessages); + } + + @Test + public void cloneAsVersionTest() throws Exception { + final ConnectClientSuccess clone = OBJECT.cloneAsVersion(ABIVersion.BORON); + Assert.assertEquals(OBJECT, clone); + } + + @Test + public void addToStringAttributes() throws Exception { + final MoreObjects.ToStringHelper result = OBJECT.addToStringAttributes(MoreObjects.toStringHelper(OBJECT)); + Assert.assertTrue(result.toString().contains("alternates=" + ALTERNATES)); + Assert.assertTrue(result.toString().contains("dataTree=" + TREE)); + Assert.assertTrue(result.toString().contains("maxMessages=" + MAX_MESSAGES)); + } + + @Override + protected void doAdditionalAssertions(final Object deserialize) { + Assert.assertTrue(deserialize instanceof ConnectClientSuccess); + Assert.assertEquals(OBJECT.getAlternates().size(), ((ConnectClientSuccess) deserialize).getAlternates().size()); + Assert.assertEquals(OBJECT.getBackend(), ((ConnectClientSuccess) deserialize).getBackend()); + Assert.assertEquals(Optional.empty(), ((ConnectClientSuccess) deserialize).getDataTree()); + Assert.assertEquals(OBJECT.getMaxMessages(), ((ConnectClientSuccess) deserialize).getMaxMessages()); + } +} diff --git a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/ExistsTransactionSuccessTest.java b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/ExistsTransactionSuccessTest.java index fe823ef4c0..aa68eaa859 100644 --- a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/ExistsTransactionSuccessTest.java +++ b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/ExistsTransactionSuccessTest.java @@ -40,4 +40,10 @@ public class ExistsTransactionSuccessTest extends AbstractTransactionSuccessTest final MoreObjects.ToStringHelper result = OBJECT.addToStringAttributes(MoreObjects.toStringHelper(OBJECT)); Assert.assertTrue(result.toString().contains("exists=" + EXISTS)); } + + @Override + protected void doAdditionalAssertions(final Object deserialize) { + Assert.assertTrue(deserialize instanceof ExistsTransactionSuccess); + Assert.assertEquals(OBJECT.getExists(), ((ExistsTransactionSuccess) deserialize).getExists()); + } } \ No newline at end of file diff --git a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/LocalHistorySuccessTest.java b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/LocalHistorySuccessTest.java new file mode 100644 index 0000000000..219f9144b2 --- /dev/null +++ b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/LocalHistorySuccessTest.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.controller.cluster.access.commands; + +import org.junit.Assert; +import org.junit.Test; +import org.opendaylight.controller.cluster.access.ABIVersion; + +public class LocalHistorySuccessTest extends AbstractRequestSuccessTest { + + private static final LocalHistorySuccess OBJECT = new LocalHistorySuccess( + HISTORY_IDENTIFIER, 0); + + @Override + protected LocalHistorySuccess object() { + return OBJECT; + } + + @Test + public void cloneAsVersionTest() throws Exception { + final LocalHistorySuccess clone = OBJECT.cloneAsVersion(ABIVersion.BORON); + Assert.assertEquals(OBJECT.getSequence(), clone.getSequence()); + Assert.assertEquals(OBJECT.getTarget(), clone.getTarget()); + Assert.assertEquals(OBJECT.getVersion(), clone.getVersion()); + } + + @Override + protected void doAdditionalAssertions(final Object deserialize) { + Assert.assertTrue(deserialize instanceof LocalHistorySuccess); + } +} diff --git a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/ModifyTransactionSuccessTest.java b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/ModifyTransactionSuccessTest.java index 1f7e3d4dec..c31caf28a0 100644 --- a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/ModifyTransactionSuccessTest.java +++ b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/ModifyTransactionSuccessTest.java @@ -27,4 +27,9 @@ public class ModifyTransactionSuccessTest extends AbstractTransactionSuccessTest Assert.assertEquals(OBJECT.getSequence(), clone.getSequence()); Assert.assertEquals(OBJECT.getTarget(), clone.getTarget()); } + + @Override + protected void doAdditionalAssertions(final Object deserialize) { + Assert.assertTrue(deserialize instanceof ModifyTransactionSuccess); + } } \ No newline at end of file diff --git a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/ReadTransactionSuccessNoDataTest.java b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/ReadTransactionSuccessNoDataTest.java index 9e7c796ec2..5259fe4f21 100644 --- a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/ReadTransactionSuccessNoDataTest.java +++ b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/ReadTransactionSuccessNoDataTest.java @@ -33,4 +33,10 @@ public class ReadTransactionSuccessNoDataTest extends AbstractTransactionSuccess final ReadTransactionSuccess clone = OBJECT.cloneAsVersion(ABIVersion.BORON); Assert.assertEquals(OBJECT, clone); } + + @Override + protected void doAdditionalAssertions(Object deserialize) { + Assert.assertTrue(deserialize instanceof ReadTransactionSuccess); + Assert.assertEquals(OBJECT.getData(), ((ReadTransactionSuccess) deserialize).getData()); + } } \ No newline at end of file diff --git a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/ReadTransactionSuccessTest.java b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/ReadTransactionSuccessTest.java index 0f4871fa61..a76e6d37f8 100644 --- a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/ReadTransactionSuccessTest.java +++ b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/commands/ReadTransactionSuccessTest.java @@ -40,4 +40,10 @@ public class ReadTransactionSuccessTest extends AbstractTransactionSuccessTest