246b2db4982f2b140ced7194694059891a46a6a0
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / sal / SalAsyncConfigServiceImplTest.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
9 package org.opendaylight.openflowplugin.impl.services.sal;
10
11 import static org.mockito.Mockito.verify;
12
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.concurrent.Future;
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.Matchers;
21 import org.mockito.Mockito;
22 import org.mockito.runners.MockitoJUnitRunner;
23 import org.opendaylight.openflowplugin.impl.services.ServiceMocking;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.async.config.service.rev170619.GetAsyncInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.async.config.service.rev170619.GetAsyncOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.async.config.service.rev170619.GetAsyncOutputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.async.config.service.rev170619.SetAsyncInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
32
33 /**
34  * Test for {@link SalAsyncConfigServiceImpl}.
35  */
36 @RunWith(MockitoJUnitRunner.class)
37 public class SalAsyncConfigServiceImplTest extends ServiceMocking {
38
39     private SalAsyncConfigServiceImpl salAsyncConfigService;
40
41     @Override
42     public void setup() throws Exception {
43         salAsyncConfigService = new SalAsyncConfigServiceImpl(
44                 mockedRequestContextStack, mockedDeviceContext);
45     }
46
47     @Test
48     public void testSetAsync() throws Exception {
49         final SetAsyncInput setAsyncInput = new org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol
50                 .rev130731.SetAsyncInputBuilder().build();
51         final RpcResult<SetAsyncInput> replyRpcResult = RpcResultBuilder.success(setAsyncInput).build();
52         final ListenableFuture<RpcResult<SetAsyncInput>> replyFuture = Futures.immediateFuture(replyRpcResult);
53         Mockito.when(mockedRequestContext.getFuture()).thenReturn(replyFuture);
54
55         final Future<RpcResult<Void>> setAsyncResult =
56                 salAsyncConfigService.setAsync(new SetAsyncInputBuilder().build());
57
58         Assert.assertNotNull(setAsyncResult);
59         Assert.assertTrue(setAsyncResult.isDone());
60         Assert.assertTrue(setAsyncResult.get().isSuccessful());
61         verify(mockedRequestContextStack).createRequestContext();
62     }
63
64     @Test
65     public void testGetAsyncTest() throws Exception {
66         final GetAsyncOutput getAsyncOutput = new GetAsyncOutputBuilder().build();
67         final RpcResult<GetAsyncOutput> replyRpcResult = RpcResultBuilder.success(getAsyncOutput).build();
68         final ListenableFuture<RpcResult<GetAsyncOutput>> replyFuture = Futures.immediateFuture(replyRpcResult);
69         Mockito.when(mockedRequestContext.getFuture()).thenReturn(replyFuture);
70
71         final Future<RpcResult<GetAsyncOutput>> getAsyncResult =
72                 salAsyncConfigService.getAsync(new GetAsyncInputBuilder().build());
73
74         Assert.assertNotNull(getAsyncResult);
75         Assert.assertTrue(getAsyncResult.isDone());
76         Assert.assertTrue(getAsyncResult.get().isSuccessful());
77         verify(mockedRequestContextStack).createRequestContext();
78         verify(mockedOutboundQueue).commitEntry(Matchers.eq(ServiceMocking.DUMMY_XID_VALUE),
79                 Matchers.<OfHeader>any(), Matchers.<FutureCallback<OfHeader>>any());
80     }
81 }