2 * Copyright © 2015, 2016 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.impl.rev141210;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
18 import java.util.Dictionary;
20 import javax.management.ObjectName;
22 import org.junit.Test;
23 import org.opendaylight.controller.config.api.DependencyResolver;
24 import org.opendaylight.controller.config.api.JmxAttribute;
25 import org.opendaylight.controller.config.api.ModuleIdentifier;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
28 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
29 import org.opendaylight.netvirt.openstack.netvirt.api.Southbound;
30 import org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
31 import org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.PipelineOrchestrator;
32 import org.opendaylight.netvirt.openstack.netvirt.sfc.NetvirtSfcProvider;
33 import org.opendaylight.netvirt.openstack.netvirt.sfc.standalone.openflow13.services.SfcClassifierService;
34 import org.opendaylight.netvirt.utils.servicehelper.ServiceHelper;
35 import org.osgi.framework.BundleContext;
36 import org.osgi.framework.ServiceReference;
37 import org.osgi.framework.ServiceRegistration;
39 public class NetvirtSfcImplModuleTest {
41 public void testCustomValidation() {
42 NetvirtSfcImplModule module = new NetvirtSfcImplModule(mock(ModuleIdentifier.class), mock(DependencyResolver.class));
43 // ensure no exceptions on validation
44 // currently this method is empty
45 module.customValidation();
48 @SuppressWarnings("unchecked")
50 public void testCreateInstance() throws Exception {
52 DependencyResolver dependencyResolver = mock(DependencyResolver.class);
53 BindingAwareBroker broker = mock(BindingAwareBroker.class);
54 ProviderContext session = mock(ProviderContext.class);
55 DataBroker dataBroker = mock(DataBroker.class);
56 when(dependencyResolver.resolveInstance(eq(BindingAwareBroker.class),
57 any(ObjectName.class), any(JmxAttribute.class))).thenReturn(broker);
58 when(session.getSALService(eq(DataBroker.class))).thenReturn(dataBroker);
60 // create instance of module with injected mocks
61 NetvirtSfcImplModule module = new NetvirtSfcImplModule(mock(ModuleIdentifier.class), dependencyResolver);
62 // getInstance calls resolveInstance to get the broker dependency and then calls createInstance
63 BundleContext bundleContext = mock(BundleContext.class);
64 PipelineOrchestrator pipelineOrchestrator = mock(PipelineOrchestrator.class);
65 ServiceHelper.overrideGlobalInstance(PipelineOrchestrator.class, pipelineOrchestrator);
66 ServiceHelper.overrideGlobalInstance(Southbound.class, mock(Southbound.class));
68 doNothing().when(pipelineOrchestrator).registerService(any(ServiceReference.class),
69 any(AbstractServiceInstance.class));
70 when(bundleContext.registerService(
71 eq(new String[]{AbstractServiceInstance.class.getName(), SfcClassifierService.class.getName()}),
73 any(Dictionary.class)))
74 .thenReturn(mock(ServiceRegistration.class));
75 when(bundleContext.getServiceReference(SfcClassifierService.class.getName()))
76 .thenReturn(mock(ServiceReference.class));
77 AutoCloseable closeable = module.getInstance();
78 ((NetvirtSfcProvider)closeable).setBundleContext(bundleContext);
79 ((NetvirtSfcProvider)closeable).setOf13Provider("standalone");
80 ((NetvirtSfcProvider)closeable).setAddSfFlows(false);
81 ((NetvirtSfcProvider)closeable).onSessionInitiated(session);
82 // verify that the module registered the returned provider with the broker
83 verify(broker).registerProvider((NetvirtSfcProvider)closeable);
85 // ensure no exceptions on close