Merge "BUG-8607: Fix issues in checkstyle enforcement for module bulk-o-matic"
[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.Matchers;
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(Matchers.<Runnable>any());
59
60         final BindingTransactionChain mockedTxChain = mock(BindingTransactionChain.class);
61         when(mockedTxChain.newWriteOnlyTransaction()).thenReturn(writeTransaction);
62         doReturn(mockedTxChain).when(mockDataBroker).createTransactionChain(Matchers.<TransactionChainListener>any());
63
64         when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
65
66         flowWriterTxChain = new FlowWriterTxChain(mockDataBroker, mockFlowPusher);
67     }
68
69     @Test
70     public void testAddFlows() throws Exception {
71         flowWriterTxChain.addFlows(1, FLOWS_PER_DPN, 10, 10, 10, (short) 0, (short) 1, true);
72         Mockito.verify(writeTransaction, Mockito.times(FLOWS_PER_DPN)).put(Matchers.<LogicalDatastoreType>any(),
73                 Matchers.<InstanceIdentifier<DataObject>>any(), Matchers.<DataObject>any(), Matchers.anyBoolean());
74     }
75
76     @Test
77     public void testDeleteFlows() throws Exception {
78         flowWriterTxChain.deleteFlows(1, FLOWS_PER_DPN, 10, (short) 0, (short) 1);
79         Mockito.verify(writeTransaction, Mockito.times(FLOWS_PER_DPN)).delete(Matchers.<LogicalDatastoreType>any(),
80                 Matchers.<InstanceIdentifier<DataObject>>any());
81     }
82 }