6a1ec183f2bedf0f63bab364bec75aa4380ff8af
[openflowplugin.git] / applications / bulk-o-matic / src / test / java / org / opendaylight / openflowplugin / applications / bulk / o / matic / FlowWriterTxChainTest.java
1 /*
2  * Copyright (c) 2016, 2017 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.openflowplugin.applications.bulk.o.matic;
9
10 import static org.mockito.Mockito.doReturn;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
13
14 import java.util.concurrent.ExecutorService;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.ArgumentMatchers;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.junit.MockitoJUnitRunner;
22 import org.opendaylight.mdsal.binding.api.DataBroker;
23 import org.opendaylight.mdsal.binding.api.TransactionChain;
24 import org.opendaylight.mdsal.binding.api.WriteTransaction;
25 import org.opendaylight.mdsal.common.api.CommitInfo;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28
29 /**
30  * Test for {@link FlowWriterTxChain}.
31  */
32 @RunWith(MockitoJUnitRunner.class)
33 public class FlowWriterTxChainTest {
34
35     private static final int FLOWS_PER_DPN = 100;
36
37     @Mock
38     private DataBroker mockDataBroker;
39     @Mock
40     private ExecutorService mockFlowPusher;
41     @Mock
42     private WriteTransaction writeTransaction;
43
44     private FlowWriterTxChain flowWriterTxChain;
45
46     @Before
47     public void setUp() {
48
49         Mockito.doAnswer(invocation -> {
50             ((Runnable) invocation.getArguments()[0]).run();
51             return null;
52         }).when(mockFlowPusher).execute(ArgumentMatchers.any());
53
54         final TransactionChain mockedTxChain = mock(TransactionChain.class);
55         when(mockedTxChain.newWriteOnlyTransaction()).thenReturn(writeTransaction);
56         doReturn(mockedTxChain).when(mockDataBroker)
57                 .createMergingTransactionChain(ArgumentMatchers.any());
58
59         doReturn(CommitInfo.emptyFluentFuture()).when(writeTransaction).commit();
60
61         flowWriterTxChain = new FlowWriterTxChain(mockDataBroker, mockFlowPusher);
62     }
63
64     @Test
65     public void testAddFlows() {
66         flowWriterTxChain.addFlows(1, FLOWS_PER_DPN, 10, 10, 10, (short) 0, (short) 1, true);
67         Mockito.verify(writeTransaction, Mockito.times(FLOWS_PER_DPN)).put(ArgumentMatchers.any(),
68                 ArgumentMatchers.any(), ArgumentMatchers.any(),
69                 ArgumentMatchers.anyBoolean());
70     }
71
72     @Test
73     public void testDeleteFlows() {
74         flowWriterTxChain.deleteFlows(1, FLOWS_PER_DPN, 10, (short) 0, (short) 1);
75         Mockito.verify(writeTransaction, Mockito.times(FLOWS_PER_DPN))
76                 .delete(ArgumentMatchers.any(),
77                         ArgumentMatchers.<InstanceIdentifier<DataObject>>any());
78     }
79 }