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