Switch to MD-SAL APIs
[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.TransactionChainListener;
25 import org.opendaylight.mdsal.binding.api.WriteTransaction;
26 import org.opendaylight.mdsal.common.api.CommitInfo;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Test for {@link FlowWriterTxChain}.
35  */
36 @RunWith(MockitoJUnitRunner.class)
37 public class FlowWriterTxChainTest {
38
39     private static final Logger LOG = LoggerFactory.getLogger(FlowWriterTxChainTest.class);
40     private static final int FLOWS_PER_DPN = 100;
41
42     @Mock
43     private DataBroker mockDataBroker;
44     @Mock
45     private ExecutorService mockFlowPusher;
46     @Mock
47     private WriteTransaction writeTransaction;
48
49     private FlowWriterTxChain flowWriterTxChain;
50
51     @Before
52     public void setUp() throws Exception {
53
54         Mockito.doAnswer(invocation -> {
55             ((Runnable) invocation.getArguments()[0]).run();
56             return null;
57         }).when(mockFlowPusher).execute(ArgumentMatchers.<Runnable>any());
58
59         final TransactionChain mockedTxChain = mock(TransactionChain.class);
60         when(mockedTxChain.newWriteOnlyTransaction()).thenReturn(writeTransaction);
61         doReturn(mockedTxChain).when(mockDataBroker)
62                 .createTransactionChain(ArgumentMatchers.<TransactionChainListener>any());
63
64         doReturn(CommitInfo.emptyFluentFuture()).when(writeTransaction).commit();
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(ArgumentMatchers.<LogicalDatastoreType>any(),
73                 ArgumentMatchers.<InstanceIdentifier<DataObject>>any(), ArgumentMatchers.<DataObject>any(),
74                 ArgumentMatchers.anyBoolean());
75     }
76
77     @Test
78     public void testDeleteFlows() throws Exception {
79         flowWriterTxChain.deleteFlows(1, FLOWS_PER_DPN, 10, (short) 0, (short) 1);
80         Mockito.verify(writeTransaction, Mockito.times(FLOWS_PER_DPN))
81                 .delete(ArgumentMatchers.<LogicalDatastoreType>any(),
82                         ArgumentMatchers.<InstanceIdentifier<DataObject>>any());
83     }
84 }