Merge "Add JUnit testing for OutboundNatService class."
[ovsdb.git] / openstack / net-virt-providers / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / services / OutboundNatServiceTest.java
1 /*
2  * Copyright (c) 2015 Inocybe 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.ovsdb.openstack.netvirt.providers.openflow13.services;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.anyBoolean;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18
19 import java.net.InetAddress;
20
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.mockito.runners.MockitoJUnitRunner;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
29 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
32 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
33 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.MdsalConsumer;
34 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.PipelineOrchestrator;
35 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
36 import org.opendaylight.ovsdb.plugin.api.Status;
37 import org.opendaylight.ovsdb.plugin.api.StatusCode;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40
41 import com.google.common.util.concurrent.CheckedFuture;
42
43 /**
44  * Unit test fort {@link OutboundNatService}
45  */
46 @RunWith(MockitoJUnitRunner.class)
47 public class OutboundNatServiceTest {
48
49     @InjectMocks private OutboundNatService outboundNatService = new OutboundNatService();
50
51     @Mock private MdsalConsumer mdsalConsumer;
52     @Mock private PipelineOrchestrator orchestrator;
53
54     @Mock private ReadWriteTransaction readWriteTransaction;
55     @Mock private WriteTransaction writeTransaction;
56     @Mock private CheckedFuture<Void, TransactionCommitFailedException> commitFuture;
57
58     private static final String SEGMENTATION_ID = "2";
59     private static final String HOST_ADDRESS = "127.0.0.1";
60     private static final String HOST_ADDRESS_PREFIX = "127.0.0.1/24";
61
62     @Before
63     public void setUp() throws Exception {
64         when(readWriteTransaction.submit()).thenReturn(commitFuture);
65         when(writeTransaction.submit()).thenReturn(commitFuture);
66
67         DataBroker dataBroker = mock(DataBroker.class);
68         when(dataBroker.newReadWriteTransaction()).thenReturn(readWriteTransaction);
69         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
70
71         when(mdsalConsumer.getDataBroker()).thenReturn(dataBroker);
72
73         when(orchestrator.getNextServiceInPipeline(any(Service.class))).thenReturn(Service.ARP_RESPONDER);
74     }
75
76     /**
77      * Test method {@link OutboundNatService#programIpRewriteRule(Node, Long, String, InetAddress, InetAddress, Action)}
78      */
79     @Test
80     public void testProgramIpRewriteRule() throws Exception {
81         InetAddress address = mock(InetAddress.class);
82         when(address.getHostAddress()).thenReturn(HOST_ADDRESS);
83
84         assertEquals("Error, did not return the expected StatusCode", new Status(StatusCode.SUCCESS), outboundNatService.programIpRewriteRule(mock(Node.class), Long.valueOf(123), SEGMENTATION_ID, address, address, Action.ADD));
85         verify(readWriteTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
86         verify(readWriteTransaction, times(1)).submit();
87         verify(commitFuture, times(1)).get();
88
89         assertEquals("Error, did not return the expected StatusCode", new Status(StatusCode.SUCCESS), outboundNatService.programIpRewriteRule(mock(Node.class), Long.valueOf(123), SEGMENTATION_ID, address, address, Action.DELETE));
90         verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
91         verify(readWriteTransaction, times(1)).submit();
92         verify(commitFuture, times(2)).get(); // 1 + 1 above
93     }
94
95     /**
96      * Test method {@link OutboundNatService#programIpRewriteExclusion(Node, Long, String, String, Action)}
97      */
98     @Test
99     public void testProgramIpRewriteExclusion() throws Exception {
100         assertEquals("Error, did not return the expected StatusCode", new Status(StatusCode.SUCCESS), outboundNatService.programIpRewriteExclusion(mock(Node.class), Long.valueOf(123), SEGMENTATION_ID, HOST_ADDRESS_PREFIX, Action.ADD));
101         verify(readWriteTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
102         verify(readWriteTransaction, times(1)).submit();
103         verify(commitFuture, times(1)).get();
104
105         assertEquals("Error, did not return the expected StatusCode", new Status(StatusCode.SUCCESS), outboundNatService.programIpRewriteExclusion(mock(Node.class), Long.valueOf(123), SEGMENTATION_ID, HOST_ADDRESS_PREFIX, Action.DELETE));
106         verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
107         verify(readWriteTransaction, times(1)).submit();
108         verify(commitFuture, times(2)).get(); // 1 + 1 above
109     }
110 }