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.netconf.api.capability.BasicCapability;
27 import org.opendaylight.netconf.api.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).registerService(eq(NetconfOperationServiceFactory.class),
56 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 =
76 Collections.singleton(new BasicCapability(NetconfNotification.NOTIFICATION_NAMESPACE));
78 assertEquals(capabilities.iterator().next()
79 .getCapabilityUri(), serviceFactory.getCapabilities().iterator().next().getCapabilityUri());
80 assertEquals(capabilities.iterator().next()
81 .getCapabilitySchema(), serviceFactory.getCapabilities().iterator().next().getCapabilitySchema());
82 assertEquals(capabilities.iterator().next()
83 .getModuleNamespace(), serviceFactory.getCapabilities().iterator().next().getModuleNamespace());
84 assertEquals(capabilities.iterator().next()
85 .getModuleName(), serviceFactory.getCapabilities().iterator().next().getModuleName());
87 final CapabilityListener listener = mock(CapabilityListener.class);
89 doNothing().when(listener).onCapabilitiesChanged(any(), any());
91 serviceFactory.registerCapabilityListener(listener);
93 verify(listener).onCapabilitiesChanged(serviceFactory.getCapabilities(), Collections.emptySet());
95 final NetconfOperationService netconfOperationService = serviceFactory.createService("id");
96 final Set<NetconfOperation> netconfOperations = netconfOperationService.getNetconfOperations();
98 final CreateSubscription createSubscription =
99 new CreateSubscription("id", activator.getNetconfNotificationManager());
101 netconfOperations.forEach(
103 if (operation instanceof CreateSubscription) {
104 assertEquals(createSubscription.toString(), operation.toString());
106 if (operation instanceof Get) {
107 assertEquals("id", ((Get) operation).getNetconfSessionIdForReporting());
112 // test unregister after stop
113 doNothing().when(netconfNotificationCollectorServiceRegistration).unregister();
114 doNothing().when(operationaServiceRegistration).unregister();
116 activator.stop(context);
118 verify(netconfNotificationCollectorServiceRegistration, times(1)).unregister();
119 verify(operationaServiceRegistration, times(1)).unregister();