Add unit tests for Envelope classes
[controller.git] / opendaylight / md-sal / cds-access-api / src / test / java / org / opendaylight / controller / cluster / access / concepts / FailureEnvelopeTest.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.access.concepts;
9
10 import java.io.DataInput;
11 import java.io.IOException;
12 import javax.annotation.Nonnull;
13 import org.junit.Assert;
14 import org.opendaylight.controller.cluster.access.ABIVersion;
15 import org.opendaylight.yangtools.concepts.WritableIdentifier;
16
17 public class FailureEnvelopeTest extends AbstractEnvelopeTest<FailureEnvelope> {
18
19     @Override
20     protected FailureEnvelope createEnvelope() {
21         final RequestFailure<?, ?> message =
22                 new MockFailure(OBJECT, new RuntimeRequestException("msg", new RuntimeException()), 42);
23         return new FailureEnvelope(message, 1L, 2L, 11L);
24     }
25
26     @Override
27     protected void doAdditionalAssertions(final FailureEnvelope envelope, final FailureEnvelope resolvedObject) {
28         Assert.assertEquals(envelope.getExecutionTimeNanos(), resolvedObject.getExecutionTimeNanos());
29         final RequestException expectedCause = envelope.getMessage().getCause();
30         final RequestException actualCause = resolvedObject.getMessage().getCause();
31         Assert.assertEquals(expectedCause.getMessage(), actualCause.getMessage());
32         Assert.assertEquals(expectedCause.isRetriable(), actualCause.isRetriable());
33     }
34
35     private static class MockRequestFailureProxy extends AbstractRequestFailureProxy<WritableIdentifier, MockFailure> {
36
37         @SuppressWarnings("checkstyle:RedundantModifier")
38         public MockRequestFailureProxy() {
39             //For Externalizable
40         }
41
42         private MockRequestFailureProxy(final MockFailure mockFailure) {
43             super(mockFailure);
44         }
45
46         @Nonnull
47         @Override
48         protected MockFailure createFailure(@Nonnull final WritableIdentifier target, final long sequence,
49                                             @Nonnull final RequestException failureCause) {
50             return new MockFailure(target, failureCause, sequence);
51         }
52
53         @Nonnull
54         @Override
55         protected WritableIdentifier readTarget(@Nonnull final DataInput in) throws IOException {
56             return TransactionIdentifier.readFrom(in);
57         }
58
59     }
60
61     private static class MockFailure extends RequestFailure<WritableIdentifier, MockFailure> {
62         private static final long serialVersionUID = 1L;
63
64         MockFailure(final WritableIdentifier target, final RequestException cause, final long sequence) {
65             super(target, sequence, cause);
66         }
67
68         @Override
69         protected AbstractRequestFailureProxy<WritableIdentifier, MockFailure> externalizableProxy(
70                 final ABIVersion version) {
71             return new MockRequestFailureProxy(this);
72         }
73
74         @Override
75         protected MockFailure cloneAsVersion(final ABIVersion version) {
76             return this;
77         }
78
79     }
80
81 }