Merge "Use ${project.version} for internal dependencies"
[ovsdb.git] / openstack / net-virt-providers / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / services / RoutingServiceTest.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 fort {@link RoutingService}
43  */
44 @RunWith(MockitoJUnitRunner.class)
45 @SuppressWarnings("unchecked")
46 public class RoutingServiceTest {
47
48     @InjectMocks private RoutingService routingService = new RoutingService();
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 SEGMENTATION_ID = "2";
57     private static final String HOST_ADDRESS = "127.0.0.1";
58     private static final String MAC_ADDRESS = "87:1D:5E:02:40:B8";
59
60     @Before
61     public void setUp() throws Exception {
62         when(writeTransaction.submit()).thenReturn(commitFuture);
63
64         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
65
66         when(orchestrator.getNextServiceInPipeline(any(Service.class))).thenReturn(Service.ARP_RESPONDER);
67     }
68
69     /**
70      * Test method {@link RoutingService#programRouterInterface(Long, String, String, String, InetAddress, int, Action)}
71      */
72     @Test
73     public void testProgramRouterInterface() throws Exception {
74         InetAddress address = mock(InetAddress.class);
75         when(address.getHostAddress()).thenReturn(HOST_ADDRESS);
76
77         assertEquals("Error, did not return the expected StatusCode",
78                 new Status(StatusCode.SUCCESS),
79                 routingService.programRouterInterface(Long.valueOf(123),
80                         SEGMENTATION_ID, SEGMENTATION_ID, MAC_ADDRESS, address, 1, Action.ADD));
81         verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
82         verify(writeTransaction, times(1)).submit();
83         verify(commitFuture, times(1)).get();
84
85         assertEquals("Error, did not return the expected StatusCode",
86                 new Status(StatusCode.SUCCESS),
87                 routingService.programRouterInterface(Long.valueOf(123),
88                         SEGMENTATION_ID, SEGMENTATION_ID, MAC_ADDRESS, address, 1, Action.DELETE));
89         verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
90         verify(writeTransaction, times(2)).submit();
91         verify(commitFuture, times(2)).get(); // 1 + 1 above
92     }
93
94     /**
95      * Test method {@link RoutingService#programDefaultRouteEntry(Long, String, String, InetAddress, Action)}
96      */
97     @Test
98     public void testProgramDefaultRouteEntry() throws Exception {
99         InetAddress address = mock(InetAddress.class);
100         when(address.getHostAddress()).thenReturn(HOST_ADDRESS);
101
102         assertEquals("Error, did not return the expected StatusCode",
103                 new Status(StatusCode.SUCCESS),
104                 routingService.programDefaultRouteEntry(Long.valueOf(123),
105                         SEGMENTATION_ID, MAC_ADDRESS, address, Action.ADD));
106         verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class),
107                 any(InstanceIdentifier.class), any(Node.class), anyBoolean());
108         verify(writeTransaction, times(1)).submit();
109         verify(commitFuture, times(1)).get();
110
111         assertEquals("Error, did not return the expected StatusCode",
112                 new Status(StatusCode.SUCCESS),
113                 routingService.programDefaultRouteEntry(Long.valueOf(123),
114                         SEGMENTATION_ID, MAC_ADDRESS, address, Action.DELETE));
115         verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
116         verify(writeTransaction, times(2)).submit();
117         verify(commitFuture, times(2)).get(); // 1 + 1 above
118     }
119 }