2 * Copyright (c) 2016 Cisco Systems, 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.netconf.notifications.impl.osgi;
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.eq;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
20 import java.util.Collections;
21 import java.util.Dictionary;
22 import java.util.Hashtable;
24 import org.junit.Test;
25 import org.mockito.ArgumentCaptor;
26 import org.opendaylight.controller.config.util.capability.BasicCapability;
27 import org.opendaylight.controller.config.util.capability.Capability;
28 import org.opendaylight.netconf.api.monitoring.CapabilityListener;
29 import org.opendaylight.netconf.api.util.NetconfConstants;
30 import org.opendaylight.netconf.mapping.api.NetconfOperation;
31 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
32 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
33 import org.opendaylight.netconf.notifications.NetconfNotification;
34 import org.opendaylight.netconf.notifications.NetconfNotificationCollector;
35 import org.opendaylight.netconf.notifications.impl.NetconfNotificationManager;
36 import org.opendaylight.netconf.notifications.impl.ops.CreateSubscription;
37 import org.opendaylight.netconf.notifications.impl.ops.Get;
38 import org.osgi.framework.BundleContext;
39 import org.osgi.framework.ServiceRegistration;
41 public class ActivatorTest {
44 public void testActivator() throws Exception {
45 final Activator activator = new Activator();
46 final BundleContext context = mock(BundleContext.class);
49 final ServiceRegistration netconfNotificationCollectorServiceRegistration = mock(ServiceRegistration.class);
50 final ServiceRegistration operationaServiceRegistration = mock(ServiceRegistration.class);
52 // test registering services
53 doReturn(netconfNotificationCollectorServiceRegistration).when(context).
54 registerService(eq(NetconfNotificationCollector.class), any(NetconfNotificationManager.class), any());
55 doReturn(operationaServiceRegistration).when(context).
56 registerService(eq(NetconfOperationServiceFactory.class), any(NetconfOperationServiceFactory.class), any());
58 activator.start(context);
60 verify(context, times(1)).registerService(eq(NetconfNotificationCollector.class),
61 any(NetconfNotificationManager.class), eq(new Hashtable<>()));
63 final ArgumentCaptor<NetconfOperationServiceFactory> serviceFactoryArgumentCaptor =
64 ArgumentCaptor.forClass(NetconfOperationServiceFactory.class);
66 final Dictionary<String, String> properties = new Hashtable<>();
67 properties.put(NetconfConstants.SERVICE_NAME, NetconfConstants.NETCONF_MONITORING);
69 verify(context, times(1)).registerService(eq(NetconfOperationServiceFactory.class),
70 serviceFactoryArgumentCaptor.capture(), eq(properties));
72 // test service factory argument requisites
73 final NetconfOperationServiceFactory serviceFactory = serviceFactoryArgumentCaptor.getValue();
75 final Set<Capability> capabilities = Collections.singleton(new BasicCapability(NetconfNotification.NOTIFICATION_NAMESPACE));
77 assertEquals(capabilities.iterator().next().getCapabilityUri(), serviceFactory.getCapabilities().iterator().next().getCapabilityUri());
78 assertEquals(capabilities.iterator().next().getCapabilitySchema(), serviceFactory.getCapabilities().iterator().next().getCapabilitySchema());
79 assertEquals(capabilities.iterator().next().getModuleNamespace(), serviceFactory.getCapabilities().iterator().next().getModuleNamespace());
80 assertEquals(capabilities.iterator().next().getModuleName(), serviceFactory.getCapabilities().iterator().next().getModuleName());
82 final CapabilityListener listener = mock(CapabilityListener.class);
84 doNothing().when(listener).onCapabilitiesChanged(any(), any());
86 serviceFactory.registerCapabilityListener(listener);
88 verify(listener).onCapabilitiesChanged(serviceFactory.getCapabilities(), Collections.emptySet());
90 final NetconfOperationService netconfOperationService = serviceFactory.createService("id");
91 final Set<NetconfOperation> netconfOperations = netconfOperationService.getNetconfOperations();
93 final CreateSubscription createSubscription = new CreateSubscription("id", activator.getNetconfNotificationManager());
95 netconfOperations.forEach(
97 if (operation instanceof CreateSubscription) {
98 assertEquals(createSubscription.toString(), operation.toString());
100 if (operation instanceof Get) {
101 assertEquals("id", ((Get) operation).getNetconfSessionIdForReporting());
106 // test unregister after stop
107 doNothing().when(netconfNotificationCollectorServiceRegistration).unregister();
108 doNothing().when(operationaServiceRegistration).unregister();
110 activator.stop(context);
112 verify(netconfNotificationCollectorServiceRegistration, times(1)).unregister();
113 verify(operationaServiceRegistration, times(1)).unregister();