ddacdd957c81a013c484d14ead87b0870802719a
[netconf.git] / netconf / netconf-notifications-impl / src / test / java / org / opendaylight / netconf / notifications / impl / osgi / ActivatorTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  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.netconf.notifications.impl.osgi;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.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;
19
20 import java.util.Collections;
21 import java.util.Dictionary;
22 import java.util.Hashtable;
23 import java.util.Set;
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;
40
41 public class ActivatorTest {
42
43     @Test
44     public void testActivator() throws Exception {
45         final Activator activator = new Activator();
46         final BundleContext context = mock(BundleContext.class);
47
48
49         final ServiceRegistration netconfNotificationCollectorServiceRegistration  = mock(ServiceRegistration.class);
50         final ServiceRegistration operationaServiceRegistration = mock(ServiceRegistration.class);
51
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());
57
58         activator.start(context);
59
60         verify(context, times(1)).registerService(eq(NetconfNotificationCollector.class),
61                 any(NetconfNotificationManager.class), eq(new Hashtable<>()));
62
63         final ArgumentCaptor<NetconfOperationServiceFactory> serviceFactoryArgumentCaptor =
64                 ArgumentCaptor.forClass(NetconfOperationServiceFactory.class);
65
66         final Dictionary<String, String> properties = new Hashtable<>();
67         properties.put(NetconfConstants.SERVICE_NAME, NetconfConstants.NETCONF_MONITORING);
68
69         verify(context, times(1)).registerService(eq(NetconfOperationServiceFactory.class),
70                 serviceFactoryArgumentCaptor.capture(), eq(properties));
71
72         // test service factory argument requisites
73         final NetconfOperationServiceFactory serviceFactory = serviceFactoryArgumentCaptor.getValue();
74
75         final Set<Capability> capabilities =
76                 Collections.singleton(new BasicCapability(NetconfNotification.NOTIFICATION_NAMESPACE));
77
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());
86
87         final CapabilityListener listener = mock(CapabilityListener.class);
88
89         doNothing().when(listener).onCapabilitiesChanged(any(), any());
90
91         serviceFactory.registerCapabilityListener(listener);
92
93         verify(listener).onCapabilitiesChanged(serviceFactory.getCapabilities(), Collections.emptySet());
94
95         final NetconfOperationService netconfOperationService = serviceFactory.createService("id");
96         final Set<NetconfOperation> netconfOperations = netconfOperationService.getNetconfOperations();
97
98         final CreateSubscription createSubscription =
99                 new CreateSubscription("id", activator.getNetconfNotificationManager());
100
101         netconfOperations.forEach(
102             operation -> {
103                 if (operation instanceof CreateSubscription) {
104                     assertEquals(createSubscription.toString(), operation.toString());
105                 }
106                 if (operation instanceof Get) {
107                     assertEquals("id", ((Get) operation).getNetconfSessionIdForReporting());
108                 }
109             }
110         );
111
112         // test unregister after stop
113         doNothing().when(netconfNotificationCollectorServiceRegistration).unregister();
114         doNothing().when(operationaServiceRegistration).unregister();
115
116         activator.stop(context);
117
118         verify(netconfNotificationCollectorServiceRegistration, times(1)).unregister();
119         verify(operationaServiceRegistration, times(1)).unregister();
120
121     }
122
123
124 }