netconf-notifications-impl unit tests added 34/45734/3
authorRudolf Brisuda <rudolf.brisuda@pantheon.tech>
Fri, 16 Sep 2016 12:54:21 +0000 (14:54 +0200)
committerRudolf Brisuda <rbrisuda@cisco.com>
Fri, 23 Sep 2016 09:51:31 +0000 (09:51 +0000)
Change-Id: I2a73ca3962cfa663d4dfa7dd2035a878f090d54f
Signed-off-by: Rudolf Brisuda <rudolf.brisuda@pantheon.tech>
netconf/netconf-notifications-impl/src/main/java/org/opendaylight/netconf/notifications/impl/osgi/Activator.java
netconf/netconf-notifications-impl/src/test/java/org/opendaylight/netconf/notifications/impl/osgi/ActivatorTest.java [new file with mode: 0644]

index 89aee08e87f22476577928136c5f628647e9c3b1..1cbd5237389dad5900da775ce0458399ecb20721 100644 (file)
@@ -8,6 +8,7 @@
 
 package org.opendaylight.netconf.notifications.impl.osgi;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.Sets;
 import java.util.Collections;
 import java.util.Dictionary;
@@ -107,4 +108,9 @@ public class Activator implements BundleActivator {
             operationaServiceRegistration = null;
         }
     }
+
+    @VisibleForTesting
+    NetconfNotificationManager getNetconfNotificationManager() {
+        return netconfNotificationManager;
+    }
 }
diff --git a/netconf/netconf-notifications-impl/src/test/java/org/opendaylight/netconf/notifications/impl/osgi/ActivatorTest.java b/netconf/netconf-notifications-impl/src/test/java/org/opendaylight/netconf/notifications/impl/osgi/ActivatorTest.java
new file mode 100644 (file)
index 0000000..24e8652
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.netconf.notifications.impl.osgi;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Set;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.opendaylight.controller.config.util.capability.BasicCapability;
+import org.opendaylight.controller.config.util.capability.Capability;
+import org.opendaylight.netconf.api.monitoring.CapabilityListener;
+import org.opendaylight.netconf.api.util.NetconfConstants;
+import org.opendaylight.netconf.mapping.api.NetconfOperation;
+import org.opendaylight.netconf.mapping.api.NetconfOperationService;
+import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
+import org.opendaylight.netconf.notifications.NetconfNotification;
+import org.opendaylight.netconf.notifications.NetconfNotificationCollector;
+import org.opendaylight.netconf.notifications.impl.NetconfNotificationManager;
+import org.opendaylight.netconf.notifications.impl.ops.CreateSubscription;
+import org.opendaylight.netconf.notifications.impl.ops.Get;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+
+public class ActivatorTest {
+
+    @Test
+    public void testActivator() throws Exception {
+        final Activator activator = new Activator();
+        final BundleContext context = mock(BundleContext.class);
+
+
+        final ServiceRegistration netconfNotificationCollectorServiceRegistration  = mock(ServiceRegistration.class);
+        final ServiceRegistration operationaServiceRegistration = mock(ServiceRegistration.class);
+
+        // test registering services
+        doReturn(netconfNotificationCollectorServiceRegistration).when(context).
+                registerService(eq(NetconfNotificationCollector.class), any(NetconfNotificationManager.class), any());
+        doReturn(operationaServiceRegistration).when(context).
+                registerService(eq(NetconfOperationServiceFactory.class), any(NetconfOperationServiceFactory.class), any());
+
+        activator.start(context);
+
+        verify(context, times(1)).registerService(eq(NetconfNotificationCollector.class),
+                any(NetconfNotificationManager.class), eq(new Hashtable<>()));
+
+        final ArgumentCaptor<NetconfOperationServiceFactory> serviceFactoryArgumentCaptor =
+                ArgumentCaptor.forClass(NetconfOperationServiceFactory.class);
+
+        final Dictionary<String, String> properties = new Hashtable<>();
+        properties.put(NetconfConstants.SERVICE_NAME, NetconfConstants.NETCONF_MONITORING);
+
+        verify(context, times(1)).registerService(eq(NetconfOperationServiceFactory.class),
+                serviceFactoryArgumentCaptor.capture(), eq(properties));
+
+        // test service factory argument requisites
+        final NetconfOperationServiceFactory serviceFactory = serviceFactoryArgumentCaptor.getValue();
+
+        final Set<Capability> capabilities = Collections.singleton(new BasicCapability(NetconfNotification.NOTIFICATION_NAMESPACE));
+
+        assertEquals(capabilities.iterator().next().getCapabilityUri(), serviceFactory.getCapabilities().iterator().next().getCapabilityUri());
+        assertEquals(capabilities.iterator().next().getCapabilitySchema(), serviceFactory.getCapabilities().iterator().next().getCapabilitySchema());
+        assertEquals(capabilities.iterator().next().getModuleNamespace(), serviceFactory.getCapabilities().iterator().next().getModuleNamespace());
+        assertEquals(capabilities.iterator().next().getModuleName(), serviceFactory.getCapabilities().iterator().next().getModuleName());
+
+        final CapabilityListener listener = mock(CapabilityListener.class);
+
+        doNothing().when(listener).onCapabilitiesChanged(any(), any());
+
+        serviceFactory.registerCapabilityListener(listener);
+
+        verify(listener).onCapabilitiesChanged(serviceFactory.getCapabilities(), Collections.emptySet());
+
+        final NetconfOperationService netconfOperationService = serviceFactory.createService("id");
+        final Set<NetconfOperation> netconfOperations = netconfOperationService.getNetconfOperations();
+
+        final CreateSubscription createSubscription = new CreateSubscription("id", activator.getNetconfNotificationManager());
+
+        netconfOperations.forEach(
+                operation -> {
+                    if (operation instanceof CreateSubscription) {
+                        assertEquals(createSubscription.toString(), operation.toString());
+                    }
+                    if (operation instanceof Get) {
+                        assertEquals("id", ((Get) operation).getNetconfSessionIdForReporting());
+                    }
+                }
+        );
+
+        // test unregister after stop
+        doNothing().when(netconfNotificationCollectorServiceRegistration).unregister();
+        doNothing().when(operationaServiceRegistration).unregister();
+
+        activator.stop(context);
+
+        verify(netconfNotificationCollectorServiceRegistration, times(1)).unregister();
+        verify(operationaServiceRegistration, times(1)).unregister();
+
+    }
+
+
+}