Fix Bug 3663: Update netvirt.provider UT
[ovsdb.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.mockito.Matchers.anyString;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16
17 import java.lang.reflect.Field;
18 import java.util.concurrent.ExecutorService;
19
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.InjectMocks;
23 import org.mockito.Mock;
24 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
25 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
26 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
27 import org.osgi.framework.BundleContext;
28 import org.osgi.framework.ServiceReference;
29 import org.powermock.api.mockito.PowerMockito;
30 import org.powermock.core.classloader.annotations.PrepareForTest;
31 import org.powermock.modules.junit4.PowerMockRunner;
32
33 /**
34  * Unit test for {@link PipelineOrchestratorImplTest}
35  */
36 @PrepareForTest(ServiceHelper.class)
37 @RunWith(PowerMockRunner.class)
38 public class PipelineOrchestratorImplTest {
39     @InjectMocks private PipelineOrchestratorImpl orchestrator;
40
41     @Mock private ExecutorService eventHandler;
42     @Mock private Southbound southbound;
43
44     /***
45      * Registers a mock service and verifies the registration by asking the
46      * pipeline orchestrator to return the associated service from its internal
47      * registry
48      */
49     @Test
50     public void testRegisterAndUnregisterService() {
51         Service service = Service.CLASSIFIER;
52         ServiceReference<?> serviceReference = mock(ServiceReference.class);
53         when(serviceReference.getProperty(anyString())).thenReturn(service);
54
55         AbstractServiceInstance abstractServiceInstance = mock(AbstractServiceInstance.class);
56
57         orchestrator.registerService(serviceReference, abstractServiceInstance);
58         assertEquals("Error, registerService() service registration fails",
59                 abstractServiceInstance,
60                 orchestrator.getServiceInstance(service));
61
62         orchestrator.unregisterService(serviceReference);
63         assertNull("Error, unregisterService() didn't delete the service", orchestrator.getServiceInstance(service));
64     }
65
66     /**
67      * Test method
68      * {@link PipelineOrchestratorImplr#getNextServiceInPipeline(Service)}
69      */
70     @Test
71     public void testGetNextServiceInPipeline() {
72
73         assertEquals(orchestrator.getNextServiceInPipeline(Service.CLASSIFIER),
74                 Service.ARP_RESPONDER);
75         assertEquals(
76                 orchestrator.getNextServiceInPipeline(Service.ARP_RESPONDER),
77                 Service.INBOUND_NAT);
78         assertEquals(
79                 orchestrator.getNextServiceInPipeline(Service.INBOUND_NAT),
80                 Service.EGRESS_ACL);
81         assertEquals(orchestrator.getNextServiceInPipeline(Service.EGRESS_ACL),
82                 Service.LOAD_BALANCER);
83         assertEquals(
84                 orchestrator.getNextServiceInPipeline(Service.LOAD_BALANCER),
85                 Service.ROUTING);
86         assertEquals(orchestrator.getNextServiceInPipeline(Service.ROUTING),
87                 Service.L3_FORWARDING);
88         assertEquals(
89                 orchestrator.getNextServiceInPipeline(Service.L3_FORWARDING),
90                 Service.L2_REWRITE);
91         assertEquals(orchestrator.getNextServiceInPipeline(Service.L2_REWRITE),
92                 Service.INGRESS_ACL);
93         assertEquals(
94                 orchestrator.getNextServiceInPipeline(Service.INGRESS_ACL),
95                 Service.OUTBOUND_NAT);
96         assertEquals(
97                 orchestrator.getNextServiceInPipeline(Service.OUTBOUND_NAT),
98                 Service.L2_FORWARDING);
99         assertNull(orchestrator.getNextServiceInPipeline(Service.L2_FORWARDING));
100     }
101
102   @Test
103   public void testSetDependencies() throws Exception {
104       NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
105       Southbound southbound = mock(Southbound.class);
106
107       PowerMockito.mockStatic(ServiceHelper.class);
108       PowerMockito.when(ServiceHelper.getGlobalInstance(NodeCacheManager.class, orchestrator)).thenReturn(nodeCacheManager);
109       PowerMockito.when(ServiceHelper.getGlobalInstance(Southbound.class, orchestrator)).thenReturn(southbound);
110
111       orchestrator.setDependencies(mock(BundleContext.class), mock(ServiceReference.class));
112
113 //      assertEquals("Error, did not return the correct object", getField("nodeCacheManager"), nodeCacheManager);
114       assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
115   }
116
117   private Object getField(String fieldName) throws Exception {
118       Field field = PipelineOrchestratorImpl.class.getDeclaredField(fieldName);
119       field.setAccessible(true);
120       return field.get(orchestrator);
121   }
122 }