Adding Unit Tests for netvirt/providers/openflow13/PipelineOrchestratorImpl
[netvirt.git] / openstack / net-virt-providers / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / PipelineOrchestratorImplTest.java
1 /*
2  * Copyright (c) 2015 Inocybe Technologies.  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;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Matchers.anyString;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.when;
20
21 import java.lang.reflect.Field;
22 import java.util.Random;
23 import java.util.concurrent.BlockingQueue;
24
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mock;
30 import org.mockito.Mockito;
31 import org.mockito.runners.MockitoJUnitRunner;
32 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
33 import org.opendaylight.neutron.spi.INeutronPortCRUD;
34 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
35 import org.opendaylight.neutron.spi.NeutronLoadBalancerPool;
36 import org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent;
37 import org.opendaylight.ovsdb.openstack.netvirt.AbstractHandler;
38 import org.opendaylight.ovsdb.openstack.netvirt.LBaaSHandler;
39 import org.opendaylight.ovsdb.openstack.netvirt.LBaaSPoolHandler;
40 import org.opendaylight.ovsdb.openstack.netvirt.NeutronCacheUtils;
41 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
42 import org.opendaylight.ovsdb.openstack.netvirt.impl.EventDispatcherImpl;
43 import org.osgi.framework.ServiceReference;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
47
48 /**
49  * Unit test for {@link PipelineOrchestratorImplTest}
50  */
51 @PrepareForTest(PipelineOrchestratorImpl.class)
52 @RunWith(PowerMockRunner.class)
53 public class PipelineOrchestratorImplTest {
54
55     @Mock
56     private ServiceReference ref;
57     @Mock
58     private ServiceReference ref2;
59     @Mock
60     private AbstractServiceInstance serviceInstance;
61     @Mock
62     private AbstractServiceInstance serviceInstance2;
63
64     @InjectMocks
65     private PipelineOrchestratorImpl orchestrator;
66
67     private AbstractEvent.HandlerType handlerTypeObject = AbstractEvent.HandlerType.NEUTRON_FLOATING_IP;
68
69     @Before
70     public void setUp() {
71         Random r = new Random();
72
73         orchestrator = new PipelineOrchestratorImpl();
74         orchestrator.init();
75         orchestrator.start();
76
77         when(ref.getProperty(org.osgi.framework.Constants.SERVICE_ID))
78                 .thenReturn(r.nextLong());
79         when(ref.getProperty(Constants.EVENT_HANDLER_TYPE_PROPERTY))
80                 .thenReturn(handlerTypeObject);
81         when(ref.getProperty(AbstractServiceInstance.SERVICE_PROPERTY))
82                 .thenReturn(Service.CLASSIFIER);
83
84         when(ref2.getProperty(org.osgi.framework.Constants.SERVICE_ID))
85                 .thenReturn(r.nextLong());
86         when(ref2.getProperty(Constants.EVENT_HANDLER_TYPE_PROPERTY))
87                 .thenReturn(handlerTypeObject);
88         when(ref2.getProperty(AbstractServiceInstance.SERVICE_PROPERTY))
89                 .thenReturn(Service.INBOUND_NAT);
90
91         when(serviceInstance.getService()).thenReturn(Service.CLASSIFIER);
92         when(serviceInstance2.getService()).thenReturn(Service.INBOUND_NAT);
93     }
94
95     /***
96      * Registers a mock service and verifies the registration by asking the
97      * pipeline orchestrator to return the associated service from its internal
98      * registry
99      */
100     @Test
101     public void testRegisterService() {
102         orchestrator.registerService(ref, serviceInstance);
103         assertEquals("Error, registerService() service registration fails",
104                 serviceInstance,
105                 orchestrator.getServiceInstance(Service.CLASSIFIER));
106     }
107
108     /***
109      * Test method {@link PipelineOrchestratorImplr#registerService(Service)}
110      *
111      * Unregisters a mock service and verifies the process by asking the
112      * pipeline orchestrator to return the associated service from its internal
113      * registry
114      */
115     @Test
116     public void testUnRegisterService() {
117
118         orchestrator = new PipelineOrchestratorImpl();
119         orchestrator.init();
120         orchestrator.start();
121         orchestrator.registerService(ref, serviceInstance);
122         orchestrator.unregisterService(ref);
123
124         assertEquals("Error, unregisterService() service registration fails",
125                 null, orchestrator.getServiceInstance(Service.CLASSIFIER));
126
127     }
128
129     /**
130      * Test method
131      * {@link PipelineOrchestratorImplr#getNextServiceInPipeline(Service)}
132      */
133     @Test
134     public void testGetNextServiceInPipeline() {
135
136         assertEquals(orchestrator.getNextServiceInPipeline(Service.CLASSIFIER),
137                 Service.ARP_RESPONDER);
138         assertEquals(
139                 orchestrator.getNextServiceInPipeline(Service.ARP_RESPONDER),
140                 Service.INBOUND_NAT);
141         assertEquals(
142                 orchestrator.getNextServiceInPipeline(Service.INBOUND_NAT),
143                 Service.EGRESS_ACL);
144         assertEquals(orchestrator.getNextServiceInPipeline(Service.EGRESS_ACL),
145                 Service.LOAD_BALANCER);
146         assertEquals(
147                 orchestrator.getNextServiceInPipeline(Service.LOAD_BALANCER),
148                 Service.ROUTING);
149         assertEquals(orchestrator.getNextServiceInPipeline(Service.ROUTING),
150                 Service.L3_FORWARDING);
151         assertEquals(
152                 orchestrator.getNextServiceInPipeline(Service.L3_FORWARDING),
153                 Service.L2_REWRITE);
154         assertEquals(orchestrator.getNextServiceInPipeline(Service.L2_REWRITE),
155                 Service.INGRESS_ACL);
156         assertEquals(
157                 orchestrator.getNextServiceInPipeline(Service.INGRESS_ACL),
158                 Service.OUTBOUND_NAT);
159         assertEquals(
160                 orchestrator.getNextServiceInPipeline(Service.OUTBOUND_NAT),
161                 Service.L2_FORWARDING);
162         assertNull(orchestrator.getNextServiceInPipeline(Service.L2_FORWARDING));
163
164     }
165
166     /**
167      * Test method {@link PipelineOrchestratorImpl#getServiceInstance(Service)}
168      */
169     @Test
170     public void testGetServiceInstance() {
171
172         orchestrator = new PipelineOrchestratorImpl();
173         orchestrator.init();
174         orchestrator.start();
175         orchestrator.registerService(ref, serviceInstance);
176         orchestrator.registerService(ref2, serviceInstance2);
177
178         assertEquals(
179                 "Error, getServiceInstance() fails to return an instance of a registered service",
180                 serviceInstance,
181                 orchestrator.getServiceInstance(Service.CLASSIFIER));
182
183         assertEquals(
184                 "Error, getServiceInstance() returned an instance of a service that wasn't registered.",
185                 null, orchestrator.getServiceInstance(Service.DIRECTOR));
186     }
187
188 }