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