bcd5bb69c1f6dac4e0d0bc811280a3a11fbe2161
[ovsdb.git] / openstack / net-virt-providers / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / services / InboundNatServiceTest.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.WriteTransaction;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
31 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
32 import org.opendaylight.ovsdb.openstack.netvirt.api.Status;
33 import org.opendaylight.ovsdb.openstack.netvirt.api.StatusCode;
34 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.PipelineOrchestrator;
35 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38
39 import com.google.common.util.concurrent.CheckedFuture;
40
41 /**
42  * Unit test for {@link InboundNatService}
43  */
44 @RunWith(MockitoJUnitRunner.class)
45 @SuppressWarnings("unchecked")
46 public class InboundNatServiceTest {
47
48     @InjectMocks private InboundNatService inboundNatService = new InboundNatService(Service.ARP_RESPONDER);
49
50     @Mock private DataBroker dataBroker;
51     @Mock private PipelineOrchestrator orchestrator;
52
53     @Mock private WriteTransaction writeTransaction;
54     @Mock private CheckedFuture<Void, TransactionCommitFailedException> commitFuture;
55
56     private static final String HOST_ADDRESS = "127.0.0.1";
57     private static final String HOST_ADDRESS_PREFIX = "127.0.0.1/32";
58
59     @Before
60     public void setUp() {
61         when(writeTransaction.submit()).thenReturn(commitFuture);
62
63         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
64
65         when(orchestrator.getNextServiceInPipeline(any(Service.class))).thenReturn(Service.ARP_RESPONDER);
66     }
67
68     /**
69      * Test method {@link InboundNatService#programIpRewriteRule(Long, Long, InetAddress, InetAddress, Action)}
70      */
71     @Test
72     public void testProgramIpRewriteRule() throws Exception {
73         InetAddress rewriteAddress = mock(InetAddress.class);
74         when(rewriteAddress.getHostAddress()).thenReturn(HOST_ADDRESS);
75         InetAddress matchAddress = mock(InetAddress.class);
76         when(matchAddress.getHostAddress()).thenReturn(HOST_ADDRESS);
77
78         assertEquals("Error, did not return the expected StatusCode",
79                 new Status(StatusCode.SUCCESS),
80                 inboundNatService.programIpRewriteRule(Long.valueOf(123), Long.valueOf(2), "2",  // FIXME: describe params
81                         matchAddress, rewriteAddress, Action.ADD));
82         verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
83         verify(writeTransaction, times(1)).submit();
84         verify(commitFuture, times(1)).get();
85
86         assertEquals("Error, did not return the expected StatusCode",
87                 new Status(StatusCode.SUCCESS),
88                 inboundNatService.programIpRewriteRule(Long.valueOf(123), Long.valueOf(2), "2",  // FIXME: describe params
89                         matchAddress, rewriteAddress, Action.DELETE));
90         verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
91         verify(writeTransaction, times(2)).submit();
92         verify(commitFuture, times(2)).get(); // 1 + 1 above
93     }
94
95     /**
96      * Test method {@link InboundNatService#programIpRewriteExclusion(Long, String, String, Action)}
97      */
98     @Test
99     public void testProgramIpRewriteExclusion() throws Exception {
100         assertEquals("Error, did not return the expected StatusCode",
101                 new Status(StatusCode.SUCCESS),
102                 inboundNatService.programIpRewriteExclusion(Long.valueOf(123), "2", HOST_ADDRESS_PREFIX, Action.ADD));
103         verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class),
104                 any(InstanceIdentifier.class), any(Node.class), anyBoolean());
105         verify(writeTransaction, times(1)).submit();
106         verify(commitFuture, times(1)).get();
107
108         assertEquals("Error, did not return the expected StatusCode",
109                 new Status(StatusCode.SUCCESS),
110                 inboundNatService.programIpRewriteExclusion(Long.valueOf(123), "2",
111                         HOST_ADDRESS_PREFIX, Action.DELETE));
112         verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
113         verify(writeTransaction, times(2)).submit();
114         verify(commitFuture, times(2)).get(); // 1 + 1 above
115     }
116 }