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