Merge "mdsal-notification simple unit test added"
[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.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;
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.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;
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).
56                 registerService(eq(NetconfOperationServiceFactory.class), 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 = Collections.singleton(new BasicCapability(NetconfNotification.NOTIFICATION_NAMESPACE));
76
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());
81
82         final CapabilityListener listener = mock(CapabilityListener.class);
83
84         doNothing().when(listener).onCapabilitiesChanged(any(), any());
85
86         serviceFactory.registerCapabilityListener(listener);
87
88         verify(listener).onCapabilitiesChanged(serviceFactory.getCapabilities(), Collections.emptySet());
89
90         final NetconfOperationService netconfOperationService = serviceFactory.createService("id");
91         final Set<NetconfOperation> netconfOperations = netconfOperationService.getNetconfOperations();
92
93         final CreateSubscription createSubscription = new CreateSubscription("id", activator.getNetconfNotificationManager());
94
95         netconfOperations.forEach(
96                 operation -> {
97                     if (operation instanceof CreateSubscription) {
98                         assertEquals(createSubscription.toString(), operation.toString());
99                     }
100                     if (operation instanceof Get) {
101                         assertEquals("id", ((Get) operation).getNetconfSessionIdForReporting());
102                     }
103                 }
104         );
105
106         // test unregister after stop
107         doNothing().when(netconfNotificationCollectorServiceRegistration).unregister();
108         doNothing().when(operationaServiceRegistration).unregister();
109
110         activator.stop(context);
111
112         verify(netconfNotificationCollectorServiceRegistration, times(1)).unregister();
113         verify(operationaServiceRegistration, times(1)).unregister();
114
115     }
116
117
118 }