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