2 * Copyright (c) 2015 Inocybe and others. All rights reserved.
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
9 package org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.services;
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;
19 import java.net.InetAddress;
20 import java.util.concurrent.atomic.AtomicBoolean;
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;
42 import com.google.common.util.concurrent.CheckedFuture;
45 * Unit test for {@link InboundNatService}
47 @RunWith(MockitoJUnitRunner.class)
48 @SuppressWarnings("unchecked")
49 public class InboundNatServiceTest {
51 @InjectMocks private InboundNatService inboundNatService = new InboundNatService(Service.ARP_RESPONDER);
53 @Mock private DataBroker dataBroker;
54 @Mock private PipelineOrchestrator orchestrator;
56 @Mock private WriteTransaction writeTransaction;
57 @Mock private CheckedFuture<Void, TransactionCommitFailedException> commitFuture;
59 private static final String HOST_ADDRESS = "127.0.0.1";
60 private static final String HOST_ADDRESS_PREFIX = "127.0.0.1/32";
63 public void setUp() throws IllegalArgumentException, IllegalAccessException {
64 when(writeTransaction.submit()).thenReturn(commitFuture);
66 when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
68 when(orchestrator.getNextServiceInPipeline(any(Service.class))).thenReturn(Service.ARP_RESPONDER);
70 NetvirtProvidersProvider netvirtProvider = mock(NetvirtProvidersProvider.class);
71 MemberModifier.field(NetvirtProvidersProvider.class, "hasProviderEntityOwnership").set(netvirtProvider, new AtomicBoolean(true));
76 * Test method {@link InboundNatService#programIpRewriteRule(Long, Long, String, InetAddress, InetAddress, Action)}
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);
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();
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
103 * Test method {@link InboundNatService#programIpRewriteExclusion(Long, String, String, Action)}
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();
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