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