Bump versions to 7.0.1-SNAPSHOT
[controller.git] / opendaylight / md-sal / cds-access-client / src / test / java / org / opendaylight / controller / cluster / access / client / ConnectedClientConnectionTest.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.client;
9
10 import static org.junit.Assert.assertNull;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.same;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.verify;
16
17 import java.util.function.Consumer;
18 import org.junit.Test;
19 import org.opendaylight.controller.cluster.access.ABIVersion;
20 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequestBuilder;
21 import org.opendaylight.controller.cluster.access.commands.TransactionWrite;
22 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
23 import org.opendaylight.controller.cluster.access.concepts.Request;
24 import org.opendaylight.controller.cluster.access.concepts.RequestException;
25 import org.opendaylight.controller.cluster.access.concepts.Response;
26 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
27 import org.opendaylight.controller.cluster.messaging.MessageSlice;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
31
32 public class ConnectedClientConnectionTest
33         extends AbstractClientConnectionTest<ConnectedClientConnection<BackendInfo>, BackendInfo> {
34
35     @Test
36     public void testCheckTimeoutConnectionTimedout() {
37         final Consumer<Response<?, ?>> callback = mock(Consumer.class);
38         connection.sendRequest(createRequest(replyToProbe.ref()), callback);
39         final long now = context.ticker().read() + ConnectedClientConnection.DEFAULT_BACKEND_ALIVE_TIMEOUT_NANOS;
40         assertNull(connection.checkTimeout(now));
41     }
42
43     @Override
44     protected ConnectedClientConnection<BackendInfo> createConnection() {
45         final BackendInfo backend = new BackendInfo(backendProbe.ref(), "test", 0L, ABIVersion.current(), 10);
46         final ConnectingClientConnection<BackendInfo> connectingConn = new ConnectingClientConnection<>(context, 0L,
47                 backend.getName());
48         return new ConnectedClientConnection<>(connectingConn, backend);
49     }
50
51     @Override
52     @Test
53     public void testReconnectConnection() {
54         final ClientActorBehavior<BackendInfo> behavior = mock(ClientActorBehavior.class);
55         connection.lockedReconnect(behavior, mock(RequestException.class));
56         verify(behavior).reconnectConnection(same(connection), any(ReconnectingClientConnection.class));
57     }
58
59     @SuppressWarnings("unchecked")
60     @Test
61     public void testSendSliceableMessageRequest() {
62         final ClientActorConfig config = AccessClientUtil.newMockClientActorConfig();
63         doReturn(5).when(config).getMaximumMessageSliceSize();
64         context = new ClientActorContext(contextProbe.ref(), PERSISTENCE_ID, system, CLIENT_ID, config);
65         connection = createConnection();
66
67         final Consumer<Response<?, ?>> callback = mock(Consumer.class);
68
69         final TransactionIdentifier identifier =
70                 new TransactionIdentifier(new LocalHistoryIdentifier(CLIENT_ID, 0L), 0L);
71         ModifyTransactionRequestBuilder reqBuilder =
72                 new ModifyTransactionRequestBuilder(identifier, replyToProbe.ref());
73         reqBuilder.addModification(new TransactionWrite(YangInstanceIdentifier.empty(), Builders.containerBuilder()
74                 .withNodeIdentifier(YangInstanceIdentifier.NodeIdentifier.create(
75                         QName.create("namespace", "localName"))).build()));
76         reqBuilder.setSequence(0L);
77         final Request<?, ?> request = reqBuilder.build();
78         connection.sendRequest(request, callback);
79
80         backendProbe.expectMsgClass(MessageSlice.class);
81     }
82 }