Add AbstractTransactionCommitCohort unit tests
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / databroker / actors / dds / TranasactionTester.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.databroker.actors.dds;
9
10 import akka.testkit.TestProbe;
11 import javax.annotation.Nonnull;
12 import org.junit.Assert;
13 import org.opendaylight.controller.cluster.access.ABIVersion;
14 import org.opendaylight.controller.cluster.access.client.AbstractClientConnection;
15 import org.opendaylight.controller.cluster.access.client.AccessClientUtil;
16 import org.opendaylight.controller.cluster.access.commands.TransactionFailure;
17 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
18 import org.opendaylight.controller.cluster.access.concepts.AbstractRequestFailureProxy;
19 import org.opendaylight.controller.cluster.access.concepts.FailureEnvelope;
20 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
21 import org.opendaylight.controller.cluster.access.concepts.RequestException;
22 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
23 import org.opendaylight.controller.cluster.access.concepts.RequestSuccess;
24 import org.opendaylight.controller.cluster.access.concepts.SuccessEnvelope;
25 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
26
27 /**
28  * Helper class. Allows checking messages received by backend and respond to them.
29  */
30 class TranasactionTester {
31
32     private final RemoteProxyTransaction transaction;
33     private final AbstractClientConnection<ShardBackendInfo> connection;
34     private final TestProbe backendProbe;
35     private RequestEnvelope envelope;
36
37     TranasactionTester(final RemoteProxyTransaction transaction,
38                        final AbstractClientConnection<ShardBackendInfo> connection,
39                        final TestProbe backendProbe) {
40         this.transaction = transaction;
41         this.connection = connection;
42         this.backendProbe = backendProbe;
43     }
44
45     RemoteProxyTransaction getTransaction() {
46         return transaction;
47     }
48
49     TransactionRequest getLastReceivedMessage() {
50         return (TransactionRequest) envelope.getMessage();
51     }
52
53     <T extends TransactionRequest> T expectTransactionRequest(final Class<T> expected) {
54         envelope = backendProbe.expectMsgClass(RequestEnvelope.class);
55         Assert.assertTrue(expected.isAssignableFrom(envelope.getMessage().getClass()));
56         return (T) envelope.getMessage();
57     }
58
59     void replySuccess(final RequestSuccess<?, ?> success) {
60         final long sessionId = envelope.getSessionId();
61         final long txSequence = envelope.getTxSequence();
62         final long executionTime = 0L;
63         final SuccessEnvelope responseEnvelope = new SuccessEnvelope(success, sessionId, txSequence, executionTime);
64         AccessClientUtil.completeRequest(connection, responseEnvelope);
65     }
66
67     void replyFailure(final RequestException cause) {
68         final long sessionId = envelope.getSessionId();
69         final long txSequence = envelope.getTxSequence();
70         final long executionTime = 0L;
71         final RequestFailure<?, ?> fail =
72                 new MockFailure(transaction.getIdentifier(), envelope.getMessage().getSequence(), cause);
73         final FailureEnvelope responseEnvelope = new FailureEnvelope(fail, sessionId, txSequence, executionTime);
74         AccessClientUtil.completeRequest(connection, responseEnvelope);
75     }
76
77     private static class MockFailure extends RequestFailure<TransactionIdentifier, TransactionFailure> {
78         private MockFailure(@Nonnull final TransactionIdentifier target, final long sequence,
79                             @Nonnull final RequestException cause) {
80             super(target, sequence, cause);
81         }
82
83         @Nonnull
84         @Override
85         protected TransactionFailure cloneAsVersion(@Nonnull final ABIVersion targetVersion) {
86             throw new UnsupportedOperationException("Not implemented");
87         }
88
89         @Override
90         protected AbstractRequestFailureProxy<TransactionIdentifier, TransactionFailure> externalizableProxy(
91                 @Nonnull final ABIVersion version) {
92             throw new UnsupportedOperationException("Not implemented");
93         }
94     }
95 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.