d1cd249c8747925b83d29cda7eea9e8a5b2bc931
[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
9 package org.opendaylight.openflowplugin.applications.bulk.o.matic;
10
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14
15 import com.google.common.util.concurrent.Futures;
16 import java.util.concurrent.ExecutorService;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.ArgumentMatchers;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Test for {@link FlowWriterTxChain}.
36  */
37 @RunWith(MockitoJUnitRunner.class)
38 public class FlowWriterTxChainTest {
39
40     private static final Logger LOG = LoggerFactory.getLogger(FlowWriterTxChainTest.class);
41     private static final int FLOWS_PER_DPN = 100;
42
43     @Mock
44     private DataBroker mockDataBroker;
45     @Mock
46     private ExecutorService mockFlowPusher;
47     @Mock
48     private WriteTransaction writeTransaction;
49
50     private FlowWriterTxChain flowWriterTxChain;
51
52     @Before
53     public void setUp() throws Exception {
54
55         Mockito.doAnswer(invocation -> {
56             ((Runnable) invocation.getArguments()[0]).run();
57             return null;
58         }).when(mockFlowPusher).execute(ArgumentMatchers.<Runnable>any());
59
60         final BindingTransactionChain mockedTxChain = mock(BindingTransactionChain.class);
61         when(mockedTxChain.newWriteOnlyTransaction()).thenReturn(writeTransaction);
62         doReturn(mockedTxChain).when(mockDataBroker)
63                 .createTransactionChain(ArgumentMatchers.<TransactionChainListener>any());
64
65         when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
66
67         flowWriterTxChain = new FlowWriterTxChain(mockDataBroker, mockFlowPusher);
68     }
69
70     @Test
71     public void testAddFlows() throws Exception {
72         flowWriterTxChain.addFlows(1, FLOWS_PER_DPN, 10, 10, 10, (short) 0, (short) 1, true);
73         Mockito.verify(writeTransaction, Mockito.times(FLOWS_PER_DPN)).put(ArgumentMatchers.<LogicalDatastoreType>any(),
74                 ArgumentMatchers.<InstanceIdentifier<DataObject>>any(), ArgumentMatchers.<DataObject>any(),
75                 ArgumentMatchers.anyBoolean());
76     }
77
78     @Test
79     public void testDeleteFlows() throws Exception {
80         flowWriterTxChain.deleteFlows(1, FLOWS_PER_DPN, 10, (short) 0, (short) 1);
81         Mockito.verify(writeTransaction, Mockito.times(FLOWS_PER_DPN))
82                 .delete(ArgumentMatchers.<LogicalDatastoreType>any(),
83                         ArgumentMatchers.<InstanceIdentifier<DataObject>>any());
84     }
85 }