Migrate deprecated testing components
[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.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.opendaylight.controller.cluster.access.ABIVersion;
28 import org.opendaylight.controller.cluster.access.concepts.AbstractRequestFailureProxy;
29 import org.opendaylight.controller.cluster.access.concepts.AbstractRequestProxy;
30 import org.opendaylight.controller.cluster.access.concepts.Request;
31 import org.opendaylight.controller.cluster.access.concepts.RequestException;
32 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
33 import org.opendaylight.controller.cluster.access.concepts.Response;
34 import org.opendaylight.yangtools.concepts.WritableIdentifier;
35
36 /**
37  * Test suite covering logic contained in {@link ConnectionEntry}.
38  *
39  * @author Robert Varga
40  */
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         MockitoAnnotations.initMocks(this);
117
118         doNothing().when(mockCallback).accept(any(MockFailure.class));
119
120         ticker = new FakeTicker();
121         ticker.advance(ThreadLocalRandom.current().nextLong());
122
123         mockActor = TestProbe.apply(actorSystem);
124         mockRequest = new MockRequest(mockIdentifier, mockReplyTo);
125         mockResponse = mockRequest.toRequestFailure(mockCause);
126
127         entry = new ConnectionEntry(mockRequest, mockCallback, ticker.read());
128     }
129
130     @After
131     public void teardown() {
132         actorSystem.stop(mockActor.ref());
133     }
134
135     @Test
136     public void testComplete() {
137         entry.complete(mockResponse);
138         verify(mockCallback).accept(mockResponse);
139     }
140 }

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