OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / util / BarrierUtilTest.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
9 package org.opendaylight.openflowplugin.impl.util;
10
11 import com.google.common.base.Function;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import org.apache.commons.lang3.tuple.Pair;
14 import org.junit.After;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.ArgumentCaptor;
20 import org.mockito.ArgumentMatchers;
21 import org.mockito.Captor;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.FlowCapableTransactionService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
36
37 /**
38  * Test for {@link BarrierUtil}.
39  */
40 @RunWith(MockitoJUnitRunner.class)
41 public class BarrierUtilTest {
42
43     public static final NodeKey NODE_KEY = new NodeKey(new NodeId("ut-dummy-node"));
44     private static final NodeRef NODE_REF = new NodeRef(InstanceIdentifier.create(Nodes.class)
45             .child(Node.class, NODE_KEY));
46
47     @Mock
48     private FlowCapableTransactionService transactionService;
49     @Mock
50     private Function<Pair<RpcResult<String>, RpcResult<SendBarrierOutput>>, RpcResult<String>> compositeTransform;
51     @Captor
52     private ArgumentCaptor<Pair<RpcResult<String>, RpcResult<SendBarrierOutput>>> pairCpt;
53
54     @Before
55     public void setUp() throws Exception {
56         Mockito.when(transactionService.sendBarrier(ArgumentMatchers.<SendBarrierInput>any()))
57                 .thenReturn(RpcResultBuilder.<SendBarrierOutput>success().buildFuture());
58     }
59
60     @After
61     public void tearDown() throws Exception {
62         Mockito.verifyNoMoreInteractions(transactionService, compositeTransform);
63     }
64
65     @Test
66     public void testChainBarrier() throws Exception {
67         final String data = "ut-data1";
68         final ListenableFuture<RpcResult<String>> input = RpcResultBuilder.success(data).buildFuture();
69         final ListenableFuture<RpcResult<String>> chainResult =
70                 BarrierUtil.chainBarrier(input, NODE_REF, transactionService, compositeTransform);
71
72         Mockito.verify(transactionService).sendBarrier(ArgumentMatchers.<SendBarrierInput>any());
73         Mockito.verify(compositeTransform).apply(pairCpt.capture());
74
75         final Pair<RpcResult<String>, RpcResult<SendBarrierOutput>> value = pairCpt.getValue();
76         Assert.assertTrue(value.getLeft().isSuccessful());
77         Assert.assertEquals(data, value.getLeft().getResult());
78         Assert.assertTrue(value.getRight().isSuccessful());
79         Assert.assertNull(value.getRight().getResult());
80
81     }
82
83     @Test
84     public void testCreateSendBarrierInput() throws Exception {
85         final SendBarrierInput barrierInput = BarrierUtil.createSendBarrierInput(NODE_REF);
86
87         Assert.assertEquals(NODE_REF, barrierInput.getNode());
88         Assert.assertEquals(SendBarrierInput.class, barrierInput.getImplementedInterface());
89     }
90 }