2 * Copyright © 2015 Red Hat, Inc. 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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.rev141210;
11 import java.util.Dictionary;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 import org.opendaylight.controller.config.api.DependencyResolver;
15 import org.opendaylight.controller.config.api.JmxAttribute;
16 import org.opendaylight.controller.config.api.ModuleIdentifier;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
20 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
21 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.PipelineOrchestrator;
22 import org.opendaylight.ovsdb.openstack.netvirt.sfc.NetvirtSfcProvider;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
25 import javax.management.ObjectName;
26 import org.opendaylight.ovsdb.openstack.netvirt.sfc.standalone.openflow13.services.SfcClassifierService;
27 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
28 import org.osgi.framework.BundleContext;
29 import org.osgi.framework.ServiceReference;
30 import org.osgi.framework.ServiceRegistration;
31 import org.powermock.api.mockito.PowerMockito;
32 import org.powermock.core.classloader.annotations.PrepareForTest;
33 import org.powermock.modules.junit4.PowerMockRunner;
35 import static org.mockito.Matchers.any;
36 import static org.mockito.Matchers.eq;
37 import static org.mockito.Mockito.doNothing;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.verify;
40 import static org.mockito.Mockito.when;
42 @PrepareForTest(ServiceHelper.class)
43 @RunWith(PowerMockRunner.class)
44 public class NetvirtSfcModuleTest {
46 public void testCustomValidation() {
47 NetvirtSfcModule module = new NetvirtSfcModule(mock(ModuleIdentifier.class), mock(DependencyResolver.class));
48 // ensure no exceptions on validation
49 // currently this method is empty
50 module.customValidation();
53 @SuppressWarnings("unchecked")
55 public void testCreateInstance() throws Exception {
57 DependencyResolver dependencyResolver = mock(DependencyResolver.class);
58 BindingAwareBroker broker = mock(BindingAwareBroker.class);
59 ProviderContext session = mock(ProviderContext.class);
60 DataBroker dataBroker = mock(DataBroker.class);
61 when(dependencyResolver.resolveInstance(eq(BindingAwareBroker.class),
62 any(ObjectName.class), any(JmxAttribute.class))).thenReturn(broker);
63 when(session.getSALService(eq(DataBroker.class))).thenReturn(dataBroker);
65 // create instance of module with injected mocks
66 NetvirtSfcModule module = new NetvirtSfcModule(mock(ModuleIdentifier.class), dependencyResolver);
67 // getInstance calls resolveInstance to get the broker dependency and then calls createInstance
68 BundleContext bundleContext = mock(BundleContext.class);
69 PowerMockito.mockStatic(ServiceHelper.class);
70 PipelineOrchestrator pipelineOrchestrator = mock(PipelineOrchestrator.class);
71 PowerMockito.when(ServiceHelper.getGlobalInstance(eq(PipelineOrchestrator.class), any(AbstractServiceInstance.class)))
72 .thenReturn(pipelineOrchestrator);
73 PowerMockito.when(ServiceHelper.getGlobalInstance(eq(Southbound.class), any(AbstractServiceInstance.class)))
74 .thenReturn(mock(Southbound.class));
76 doNothing().when(pipelineOrchestrator).registerService(any(ServiceReference.class),
77 any(AbstractServiceInstance.class));
78 when(bundleContext.registerService(
79 eq(new String[]{AbstractServiceInstance.class.getName(), SfcClassifierService.class.getName()}),
81 any(Dictionary.class)))
82 .thenReturn(mock(ServiceRegistration.class));
83 when(bundleContext.getServiceReference(SfcClassifierService.class.getName()))
84 .thenReturn(mock(ServiceReference.class));
85 AutoCloseable closeable = module.getInstance();
86 ((NetvirtSfcProvider)closeable).setBundleContext(bundleContext);
87 ((NetvirtSfcProvider)closeable).setOf13Provider("standalone");
88 ((NetvirtSfcProvider)closeable).onSessionInitiated(session);
89 // verify that the module registered the returned provider with the broker
90 verify(broker).registerProvider((NetvirtSfcProvider)closeable);
92 // ensure no exceptions on close