Clean up use of MockitoAnnotations.initMocks
[controller.git] / opendaylight / md-sal / cds-access-client / src / test / java / org / opendaylight / controller / cluster / access / client / ConnectionEntryTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.client;
9
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.doNothing;
12 import static org.mockito.Mockito.verify;
13
14 import akka.actor.ActorRef;
15 import akka.actor.ActorSystem;
16 import akka.testkit.TestProbe;
17 import com.google.common.testing.FakeTicker;
18 import java.util.concurrent.ThreadLocalRandom;
19 import java.util.function.Consumer;
20 import org.junit.After;
21 import org.junit.AfterClass;
22 import org.junit.Before;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.opendaylight.controller.cluster.access.ABIVersion;
29 import org.opendaylight.controller.cluster.access.concepts.AbstractRequestFailureProxy;
30 import org.opendaylight.controller.cluster.access.concepts.AbstractRequestProxy;
31 import org.opendaylight.controller.cluster.access.concepts.Request;
32 import org.opendaylight.controller.cluster.access.concepts.RequestException;
33 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
34 import org.opendaylight.controller.cluster.access.concepts.Response;
35 import org.opendaylight.yangtools.concepts.WritableIdentifier;
36
37 /**
38  * Test suite covering logic contained in {@link ConnectionEntry}.
39  */
40 @RunWith(MockitoJUnitRunner.StrictStubs.class)
41 public class ConnectionEntryTest {
42     private static class MockFailure extends RequestFailure<WritableIdentifier, MockFailure> {
43         private static final long serialVersionUID = 1L;
44
45         MockFailure(final WritableIdentifier target, final RequestException cause) {
46             super(target, 0, cause);
47         }
48
49         @Override
50         protected AbstractRequestFailureProxy<WritableIdentifier, MockFailure> externalizableProxy(
51                 final ABIVersion version) {
52             return null;
53         }
54
55         @Override
56         protected MockFailure cloneAsVersion(final ABIVersion version) {
57             return this;
58         }
59     }
60
61     private static class MockRequest extends Request<WritableIdentifier, MockRequest> {
62         private static final long serialVersionUID = 1L;
63
64         MockRequest(final WritableIdentifier target, final ActorRef replyTo) {
65             super(target, 0, replyTo);
66         }
67
68         @Override
69         public RequestFailure<WritableIdentifier, ?> toRequestFailure(final RequestException cause) {
70             return new MockFailure(getTarget(), cause);
71         }
72
73         @Override
74         protected AbstractRequestProxy<WritableIdentifier, MockRequest> externalizableProxy(final ABIVersion version) {
75             return null;
76         }
77
78         @Override
79         protected MockRequest cloneAsVersion(final ABIVersion version) {
80             return this;
81         }
82     }
83
84     @Mock
85     private ActorRef mockReplyTo;
86     @Mock
87     private WritableIdentifier mockIdentifier;
88     @Mock
89     private RequestException mockCause;
90     @Mock
91     private Consumer<Response<?, ?>> mockCallback;
92     @Mock
93     private ClientActorBehavior<?> mockBehavior;
94
95     private FakeTicker ticker;
96     private Request<WritableIdentifier, ?> mockRequest;
97     private Response<WritableIdentifier, ?> mockResponse;
98
99     private static ActorSystem actorSystem;
100     private TestProbe mockActor;
101
102     private ConnectionEntry entry;
103
104     @BeforeClass
105     public static void setupClass() {
106         actorSystem = ActorSystem.apply();
107     }
108
109     @AfterClass
110     public static void teardownClass() {
111         actorSystem.terminate();
112     }
113
114     @Before
115     public void setup() {
116         doNothing().when(mockCallback).accept(any(MockFailure.class));
117
118         ticker = new FakeTicker();
119         ticker.advance(ThreadLocalRandom.current().nextLong());
120
121         mockActor = TestProbe.apply(actorSystem);
122         mockRequest = new MockRequest(mockIdentifier, mockReplyTo);
123         mockResponse = mockRequest.toRequestFailure(mockCause);
124
125         entry = new ConnectionEntry(mockRequest, mockCallback, ticker.read());
126     }
127
128     @After
129     public void teardown() {
130         actorSystem.stop(mockActor.ref());
131     }
132
133     @Test
134     public void testComplete() {
135         entry.complete(mockResponse);
136         verify(mockCallback).accept(mockResponse);
137     }
138 }

©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.