From: matus.kubica Date: Wed, 29 Mar 2017 14:13:57 +0000 (+0200) Subject: AbstractClientHistory derived classes tests X-Git-Tag: release/carbon~109 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=ca5129914385dd43bf4c2af2757374878e785979 AbstractClientHistory derived classes tests Change-Id: I1261eb764c730bbce6eb833644db99e4bbf0605c Signed-off-by: matus.kubica Signed-off-by: Ivan Hrasko --- diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/actors/dds/AbstractClientHistoryTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/actors/dds/AbstractClientHistoryTest.java new file mode 100644 index 0000000000..bd170ee34f --- /dev/null +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/actors/dds/AbstractClientHistoryTest.java @@ -0,0 +1,189 @@ +/* + * 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.databroker.actors.dds; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.CLIENT_ID; +import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.TRANSACTION_ID; + +import akka.actor.ActorRef; +import akka.actor.ActorSelection; +import akka.actor.ActorSystem; +import com.google.common.primitives.UnsignedLong; +import java.util.Optional; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.opendaylight.controller.cluster.access.ABIVersion; +import org.opendaylight.controller.cluster.access.client.AccessClientUtil; +import org.opendaylight.controller.cluster.access.client.ClientActorContext; +import org.opendaylight.controller.cluster.access.client.ConnectedClientConnection; +import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier; +import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo; +import org.opendaylight.controller.cluster.datastore.utils.ActorContext; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree; +import scala.concurrent.Promise; +import scala.concurrent.impl.Promise.DefaultPromise; + +public abstract class AbstractClientHistoryTest { + protected static final String SHARD_NAME = "default"; + protected static final String PERSISTENCE_ID = "per-1"; + protected static final LocalHistoryIdentifier HISTORY_ID = new LocalHistoryIdentifier(CLIENT_ID, 1L); + + @Mock + private DataTree tree; + + protected abstract T object(); + + protected abstract ClientActorContext clientActorContext(); + + @Test + public abstract void testDoCreateSnapshot() throws Exception; + + @Test + public abstract void testDoCreateTransaction() throws Exception; + + @Test + public abstract void testCreateHistoryProxy() throws Exception; + + @Test + public abstract void testOnTransactionComplete() throws Exception; + + @Test + public abstract void testOnTransactionAbort() throws Exception; + + @Test + public abstract void testOnTransactionReady() throws Exception; + + @Test + public abstract void testOnTransactionReadyDuplicate() throws Exception; + + @Test + public void testCreateSnapshotProxy() throws Exception { + final AbstractProxyTransaction snapshotProxy = object().createSnapshotProxy(TRANSACTION_ID, 0L); + Assert.assertNotNull(snapshotProxy); + Assert.assertNotEquals(TRANSACTION_ID, snapshotProxy.getIdentifier()); + } + + @Test + public void testCreateTransactionProxy() throws Exception { + AbstractProxyTransaction transactionProxy = object().createTransactionProxy(TRANSACTION_ID, 0L); + Assert.assertNotNull(transactionProxy); + Assert.assertNotEquals(TRANSACTION_ID, transactionProxy.getIdentifier()); + } + + @Test + public void testState() throws Exception { + Assert.assertEquals(AbstractClientHistory.State.IDLE, object().state()); + } + + @Test + public void testUpdateState() throws Exception { + object().updateState(AbstractClientHistory.State.IDLE, AbstractClientHistory.State.CLOSED); + Assert.assertEquals(AbstractClientHistory.State.CLOSED, object().state()); + } + + @Test + public void testDoClose() throws Exception { + object().createTransactionProxy(TRANSACTION_ID, 0L); + object().doClose(); + Assert.assertEquals(AbstractClientHistory.State.CLOSED, object().state()); + } + + @Test + public void testGetIdentifier() throws Exception { + Assert.assertEquals(HISTORY_ID, object().getIdentifier()); + } + + @Test + public void testNextTx() throws Exception { + Assert.assertTrue(object().nextTx() + 1 == object().nextTx()); + } + + @Test + public void testResolveShardForPath() throws Exception { + final Long shardForPath = object().resolveShardForPath(YangInstanceIdentifier.EMPTY); + Assert.assertEquals(0L, shardForPath.longValue()); + } + + @Test + public void testLocalAbort() throws Exception { + object().localAbort(new Throwable()); + Assert.assertEquals(AbstractClientHistory.State.CLOSED, object().state()); + } + + @Test + public void testOnProxyDestroyed() throws Exception { + final ProxyHistory proxyHistory = Mockito.mock(ProxyHistory.class); + when(proxyHistory.getIdentifier()).thenReturn(HISTORY_ID); + + object().onProxyDestroyed(proxyHistory); + verify(proxyHistory).getIdentifier(); + } + + @Test + public void testCreateTransaction() throws Exception { + final ClientTransaction transaction = object().createTransaction(); + Assert.assertNotNull(transaction); + } + + @Test + public void testTakeSnapshot() throws Exception { + final ClientSnapshot clientSnapshot = object().takeSnapshot(); + Assert.assertEquals(object().getIdentifier(), clientSnapshot.getIdentifier().getHistoryId()); + } + + @Test + @SuppressWarnings("unchecked") + public void testStartReconnect() throws Exception { + // cookie and shard are the same + final Long cookie = 0L; + final Long shard = cookie; + + final ShardBackendInfo info = new ShardBackendInfo(clientActorContext().self(), 0L, ABIVersion.current(), + SHARD_NAME, UnsignedLong.ZERO, Optional.of(tree), 10); + final ConnectedClientConnection newConn = AccessClientUtil.createConnectedConnection( + clientActorContext(), cookie, info); + object().createSnapshotProxy(TRANSACTION_ID, shard); + + final HistoryReconnectCohort reconnectCohort = object().startReconnect(newConn); + Assert.assertNotNull(reconnectCohort); + } + + @Test + @SuppressWarnings("unchecked") + public void testStartReconnectMissingOldProxy() throws Exception { + // cookie and shard are different + final Long cookie = 1L; + final Long shard = 0L; + + final ShardBackendInfo info = new ShardBackendInfo(clientActorContext().self(), 0L, ABIVersion.current(), + SHARD_NAME, UnsignedLong.ZERO, Optional.of(tree), 10); + final ConnectedClientConnection newConn = AccessClientUtil.createConnectedConnection( + clientActorContext(), cookie, info); + object().createSnapshotProxy(TRANSACTION_ID, shard); + + final HistoryReconnectCohort reconnectCohort = object().startReconnect(newConn); + Assert.assertNull(reconnectCohort); + } + + protected static ActorContext createActorContextMock(final ActorSystem system, final ActorRef actor) { + final ActorContext mock = mock(ActorContext.class); + final Promise promise = new DefaultPromise<>(); + final ActorSelection selection = system.actorSelection(actor.path()); + final PrimaryShardInfo shardInfo = new PrimaryShardInfo(selection, (short) 0); + promise.success(shardInfo); + when(mock.findPrimaryShardAsync(any())).thenReturn(promise.future()); + return mock; + } +} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/actors/dds/ClientLocalHistoryTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/actors/dds/ClientLocalHistoryTest.java new file mode 100644 index 0000000000..8aadd86ddb --- /dev/null +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/actors/dds/ClientLocalHistoryTest.java @@ -0,0 +1,157 @@ +/* + * 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.databroker.actors.dds; + +import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.CLIENT_ID; + +import akka.actor.ActorSystem; +import akka.testkit.JavaTestKit; +import akka.testkit.TestProbe; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.controller.cluster.access.client.AbstractClientConnection; +import org.opendaylight.controller.cluster.access.client.AccessClientUtil; +import org.opendaylight.controller.cluster.access.client.ClientActorContext; +import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier; +import org.opendaylight.controller.cluster.datastore.utils.ActorContext; + +public class ClientLocalHistoryTest extends AbstractClientHistoryTest { + private ActorSystem system; + private AbstractDataStoreClientBehavior behavior; + private ClientActorContext clientActorContext; + private ClientLocalHistory object; + + @Mock + private AbstractTransactionCommitCohort cohort; + @Mock + private ClientTransaction transaction; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + system = ActorSystem.apply(); + + final TestProbe clientContextProbe = new TestProbe(system, "client"); + final TestProbe actorContextProbe = new TestProbe(system, "actor-context"); + clientActorContext = AccessClientUtil.createClientActorContext( + system, clientContextProbe.ref(), CLIENT_ID, PERSISTENCE_ID); + final ActorContext actorContextMock = createActorContextMock(system, actorContextProbe.ref()); + behavior = new SimpleDataStoreClientBehavior(clientActorContext, actorContextMock, SHARD_NAME); + + object = new ClientLocalHistory(behavior, HISTORY_ID); + } + + @After + public void tearDown() throws Exception { + JavaTestKit.shutdownActorSystem(system); + } + + @Override + protected ClientLocalHistory object() { + return object; + } + + @Override + protected ClientActorContext clientActorContext() { + return clientActorContext; + } + + @Test + public void testClose() throws Exception { + object().close(); + Assert.assertEquals(AbstractClientHistory.State.CLOSED, object().state()); + } + + @Override + @Test + public void testDoCreateTransaction() throws Exception { + final ClientTransaction clientTransaction = object().doCreateTransaction(); + Assert.assertEquals(object().getIdentifier(), clientTransaction.getIdentifier().getHistoryId()); + } + + @Override + @Test + public void testOnTransactionAbort() throws Exception { + final ClientSnapshot clientSnapshot = object().doCreateSnapshot(); + Assert.assertTrue(clientSnapshot.abort()); + } + + @Override + @Test + public void testCreateHistoryProxy() throws Exception { + final AbstractClientConnection clientConnection = behavior.getConnection(0L); + final ProxyHistory historyProxy = object().createHistoryProxy(HISTORY_ID, clientConnection); + Assert.assertEquals(object().getIdentifier(), historyProxy.getIdentifier()); + } + + @Override + @Test + public void testDoCreateSnapshot() throws Exception { + final ClientSnapshot clientSnapshot = object().doCreateSnapshot(); + Assert.assertEquals(new TransactionIdentifier(object().getIdentifier(), object().nextTx()).getHistoryId(), + clientSnapshot.getIdentifier().getHistoryId()); + } + + @Override + @Test + public void testOnTransactionComplete() throws Exception { + final ClientTransaction transaction = object().createTransaction(); + + // make transaction ready + object().onTransactionReady(transaction, cohort); + // state should be set to IDLE + Assert.assertEquals(AbstractClientHistory.State.IDLE, object.state()); + + // complete transaction + object().onTransactionComplete(transaction.getIdentifier()); + // state is still IDLE + Assert.assertEquals(AbstractClientHistory.State.IDLE, object.state()); + } + + @Override + @Test + public void testOnTransactionReady() throws Exception { + final AbstractTransactionCommitCohort result = object().onTransactionReady( + object().createTransaction(), cohort); + Assert.assertEquals(result, cohort); + } + + @Override + @Test(expected = IllegalStateException.class) + public void testOnTransactionReadyDuplicate() throws Exception { + final ClientTransaction transaction = object().createTransaction(); + object().onTransactionReady(transaction, cohort); + object().onTransactionReady(transaction, cohort); + } + + @Test + public void testOnTransactionReadyAndComplete() throws Exception { + object().updateState(AbstractClientHistory.State.IDLE, AbstractClientHistory.State.TX_OPEN); + final AbstractTransactionCommitCohort transactionCommitCohort = + object().onTransactionReady(transaction, cohort); + Assert.assertEquals(cohort, transactionCommitCohort); + } + + @Test + public void testOnTransactionReadyAndCompleteStateClosed() throws Exception { + object().updateState(AbstractClientHistory.State.IDLE, AbstractClientHistory.State.CLOSED); + final AbstractTransactionCommitCohort transactionCommitCohort = + object().onTransactionReady(transaction, cohort); + Assert.assertEquals(cohort, transactionCommitCohort); + } + + @Test(expected = IllegalStateException.class) + public void testOnTransactionReadyAndCompleteIdleFail() throws Exception { + object().onTransactionReady(transaction, cohort); + } +} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/actors/dds/SingleClientHistoryTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/actors/dds/SingleClientHistoryTest.java new file mode 100644 index 0000000000..72e434ad51 --- /dev/null +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/actors/dds/SingleClientHistoryTest.java @@ -0,0 +1,125 @@ +/* + * 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.databroker.actors.dds; + +import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.CLIENT_ID; + +import akka.actor.ActorSystem; +import akka.testkit.JavaTestKit; +import akka.testkit.TestProbe; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.controller.cluster.access.client.AbstractClientConnection; +import org.opendaylight.controller.cluster.access.client.AccessClientUtil; +import org.opendaylight.controller.cluster.access.client.ClientActorContext; +import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier; +import org.opendaylight.controller.cluster.datastore.utils.ActorContext; + +public class SingleClientHistoryTest extends AbstractClientHistoryTest { + private ActorSystem system; + private AbstractDataStoreClientBehavior behavior; + private ClientActorContext clientActorContext; + private SingleClientHistory object; + + @Mock + private AbstractTransactionCommitCohort cohort; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + system = ActorSystem.apply(); + + final TestProbe clientContextProbe = new TestProbe(system, "client"); + final TestProbe actorContextProbe = new TestProbe(system, "actor-context"); + clientActorContext = AccessClientUtil.createClientActorContext( + system, clientContextProbe.ref(), CLIENT_ID, PERSISTENCE_ID); + final ActorContext actorContextMock = createActorContextMock(system, actorContextProbe.ref()); + behavior = new SimpleDataStoreClientBehavior(clientActorContext, actorContextMock, SHARD_NAME); + + object = new SingleClientHistory(behavior, HISTORY_ID); + } + + @After + public void tearDown() throws Exception { + JavaTestKit.shutdownActorSystem(system); + } + + @Override + protected SingleClientHistory object() { + return object; + } + + @Override + protected ClientActorContext clientActorContext() { + return clientActorContext; + } + + @Override + @Test + public void testDoCreateTransaction() throws Exception { + final ClientTransaction clientTransaction = object().doCreateTransaction(); + Assert.assertEquals(object().getIdentifier(), clientTransaction.getIdentifier().getHistoryId()); + } + + @Override + @Test + public void testCreateHistoryProxy() throws Exception { + final AbstractClientConnection clientConnection = behavior.getConnection(0L); + final ProxyHistory historyProxy = object().createHistoryProxy(HISTORY_ID, clientConnection); + Assert.assertEquals(object().getIdentifier(), historyProxy.getIdentifier()); + } + + @Override + @Test + public void testDoCreateSnapshot() throws Exception { + final ClientSnapshot clientSnapshot = object().doCreateSnapshot(); + Assert.assertEquals(new TransactionIdentifier(object().getIdentifier(), object().nextTx()).getHistoryId(), + clientSnapshot.getIdentifier().getHistoryId()); + } + + @Override + @Test + public void testOnTransactionComplete() throws Exception { + final ClientTransaction transaction = object().createTransaction(); + // make transaction ready + object().onTransactionReady(transaction, cohort); + // complete transaction + object().onTransactionComplete(transaction.getIdentifier()); + // it is possible to make transaction ready again + final AbstractTransactionCommitCohort result = object().onTransactionReady(transaction, cohort); + Assert.assertEquals(result, cohort); + } + + @Override + @Test + public void testOnTransactionAbort() throws Exception { + final ClientSnapshot clientSnapshot = object().doCreateSnapshot(); + Assert.assertTrue(clientSnapshot.abort()); + } + + @Override + @Test + public void testOnTransactionReady() throws Exception { + final AbstractTransactionCommitCohort result = object().onTransactionReady( + object().createTransaction(), cohort); + Assert.assertEquals(result, cohort); + } + + @Override + @Test(expected = IllegalStateException.class) + public void testOnTransactionReadyDuplicate() throws Exception { + final ClientTransaction transaction = object().createTransaction(); + object().onTransactionReady(transaction, cohort); + object().onTransactionReady(transaction, cohort); + } +} \ No newline at end of file