From a2e47bbc56fc232c8d89568dede6f179bb490bc1 Mon Sep 17 00:00:00 2001 From: Filip Tehlar Date: Tue, 23 Sep 2014 10:24:06 +0200 Subject: [PATCH] BUG-1521 netconf-impl & netconf-monitoring line coverage. Change-Id: Iad7fdaffb3ffeda331f487e2725e2500c6f31c86 Signed-off-by: Filip Tehlar --- .../NetconfMonitoringServiceImplTest.java | 111 ++++++++++++++++++ .../operations/DefaultCloseSessionTest.java | 42 +++++++ .../mapping/operations/DefaultCommitTest.java | 76 ++++++++++++ .../operations/DefaultGetSchemaTest.java | 60 ++++++++++ .../operations/DefaultStopExiTest.java | 40 +++++++ .../impl/osgi/NetconfImplActivatorTest.java | 58 +++++++++ ...onfOperationServiceFactoryTrackerTest.java | 62 ++++++++++ .../DeserializerExceptionHandlerTest.java | 43 +++++++ .../osgi/NetconfMonitoringActivatorTest.java | 51 ++++++++ ...NetconfMonitoringOperationServiceTest.java | 34 ++++++ .../NetconfMonitoringServiceTrackerTest.java | 58 +++++++++ 11 files changed, 635 insertions(+) create mode 100644 opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/NetconfMonitoringServiceImplTest.java create mode 100644 opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/mapping/operations/DefaultCloseSessionTest.java create mode 100644 opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/mapping/operations/DefaultCommitTest.java create mode 100644 opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/mapping/operations/DefaultGetSchemaTest.java create mode 100644 opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/mapping/operations/DefaultStopExiTest.java create mode 100644 opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/osgi/NetconfImplActivatorTest.java create mode 100644 opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/osgi/NetconfOperationServiceFactoryTrackerTest.java create mode 100644 opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/util/DeserializerExceptionHandlerTest.java create mode 100644 opendaylight/netconf/netconf-monitoring/src/test/java/org/opendaylight/controller/netconf/monitoring/osgi/NetconfMonitoringActivatorTest.java create mode 100644 opendaylight/netconf/netconf-monitoring/src/test/java/org/opendaylight/controller/netconf/monitoring/osgi/NetconfMonitoringOperationServiceTest.java create mode 100644 opendaylight/netconf/netconf-monitoring/src/test/java/org/opendaylight/controller/netconf/monitoring/osgi/NetconfMonitoringServiceTrackerTest.java diff --git a/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/NetconfMonitoringServiceImplTest.java b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/NetconfMonitoringServiceImplTest.java new file mode 100644 index 0000000000..1b078be9a4 --- /dev/null +++ b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/NetconfMonitoringServiceImplTest.java @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2014 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.controller.netconf.impl; + +import com.google.common.base.Optional; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import io.netty.channel.Channel; +import java.util.List; +import java.util.Set; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession; +import org.opendaylight.controller.netconf.impl.NetconfServerSession; +import org.opendaylight.controller.netconf.impl.NetconfServerSessionListener; +import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl; +import org.opendaylight.controller.netconf.mapping.api.Capability; +import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider; +import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService; +import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceSnapshot; +import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.*; + +public class NetconfMonitoringServiceImplTest { + + private NetconfMonitoringServiceImpl service; + + @Mock + private NetconfOperationProvider operationProvider; + @Mock + private NetconfManagementSession managementSession; + @Mock + private NetconfOperationServiceSnapshot snapshot; + @Mock + private NetconfOperationService operationService; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + service = new NetconfMonitoringServiceImpl(operationProvider); + } + + @Test + public void testSessions() throws Exception { + doReturn("sessToStr").when(managementSession).toString(); + service.onSessionUp(managementSession); + List list = Lists.newArrayList(managementSession); + } + + @Test(expected = RuntimeException.class) + public void testGetSchemas() throws Exception { + doThrow(RuntimeException.class).when(operationProvider).openSnapshot(anyString()); + service.getSchemas(); + } + + @Test(expected = IllegalStateException.class) + public void testGetSchemas2() throws Exception { + doThrow(Exception.class).when(operationProvider).openSnapshot(anyString()); + service.getSchemas(); + } + + @Test + public void testGetSchemas3() throws Exception { + doReturn("").when(managementSession).toString(); + Capability cap = mock(Capability.class); + Set caps = Sets.newHashSet(cap); + Set services = Sets.newHashSet(operationService); + doReturn(snapshot).when(operationProvider).openSnapshot(anyString()); + doReturn(services).when(snapshot).getServices(); + doReturn(caps).when(operationService).getCapabilities(); + Optional opt = mock(Optional.class); + doReturn(opt).when(cap).getCapabilitySchema(); + doReturn(true).when(opt).isPresent(); + doReturn(opt).when(cap).getModuleNamespace(); + doReturn("namespace").when(opt).get(); + Optional optRev = Optional.of("rev"); + doReturn(optRev).when(cap).getRevision(); + doReturn(Optional.of("modName")).when(cap).getModuleName(); + doReturn(Optional.of(Lists.newArrayList("loc"))).when(cap).getLocation(); + doNothing().when(snapshot).close(); + + assertNotNull(service.getSchemas()); + verify(snapshot, times(1)).close(); + + NetconfServerSessionListener sessionListener = mock(NetconfServerSessionListener.class); + Channel channel = mock(Channel.class); + NetconfHelloMessageAdditionalHeader header = new NetconfHelloMessageAdditionalHeader("name", "addr", "2", "tcp", "id"); + NetconfServerSession sm = new NetconfServerSession(sessionListener, channel, 10, header); + doNothing().when(sessionListener).onSessionUp(any(NetconfServerSession.class)); + sm.sessionUp(); + service.onSessionUp(sm); + assertEquals(1, service.getSessions().getSession().size()); + + assertEquals(Long.valueOf(10), service.getSessions().getSession().get(0).getSessionId()); + + service.onSessionDown(sm); + assertEquals(0, service.getSessions().getSession().size()); + } +} diff --git a/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/mapping/operations/DefaultCloseSessionTest.java b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/mapping/operations/DefaultCloseSessionTest.java new file mode 100644 index 0000000000..ae3d65646f --- /dev/null +++ b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/mapping/operations/DefaultCloseSessionTest.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2014 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.controller.netconf.impl.mapping.operations; + +import org.junit.Test; +import org.opendaylight.controller.netconf.api.NetconfDocumentedException; +import org.opendaylight.controller.netconf.util.xml.XmlElement; +import org.opendaylight.controller.netconf.util.xml.XmlUtil; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; + +public class DefaultCloseSessionTest { + @Test + public void testDefaultCloseSession() throws Exception { + AutoCloseable res = mock(AutoCloseable.class); + doNothing().when(res).close(); + DefaultCloseSession session = new DefaultCloseSession("", res); + Document doc = XmlUtil.newDocument(); + XmlElement elem = XmlElement.fromDomElement(XmlUtil.readXmlToElement("")); + session.handleWithNoSubsequentOperations(doc, elem); + } + + @Test(expected = NetconfDocumentedException.class) + public void testDefaultCloseSession2() throws Exception { + AutoCloseable res = mock(AutoCloseable.class); + doThrow(NetconfDocumentedException.class).when(res).close(); + DefaultCloseSession session = new DefaultCloseSession("", res); + Document doc = XmlUtil.newDocument(); + XmlElement elem = XmlElement.fromDomElement(XmlUtil.readXmlToElement("")); + session.handleWithNoSubsequentOperations(doc, elem); + } +} diff --git a/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/mapping/operations/DefaultCommitTest.java b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/mapping/operations/DefaultCommitTest.java new file mode 100644 index 0000000000..98050de565 --- /dev/null +++ b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/mapping/operations/DefaultCommitTest.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2014 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.controller.netconf.impl.mapping.operations; + +import com.google.common.collect.Sets; +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.controller.netconf.api.NetconfDocumentedException; +import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer; +import org.opendaylight.controller.netconf.impl.NetconfServerSession; +import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider; +import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouter; +import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution; +import org.opendaylight.controller.netconf.util.test.XmlFileLoader; +import org.opendaylight.controller.netconf.util.xml.XmlUtil; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import static org.mockito.Mockito.*; + +public class DefaultCommitTest { + + private NetconfOperationChainedExecution operation; + private Document requestMessage; + private NetconfOperationRouter router; + private DefaultCommitNotificationProducer notifier; + private CapabilityProvider cap; + private DefaultCommit commit; + + @Before + public void setUp() throws Exception { + operation = mock(NetconfOperationChainedExecution.class); + doReturn(XmlUtil.newDocument()).when(operation).execute(any(Document.class)); + router = mock(NetconfOperationRouter.class); + doReturn(false).when(operation).isExecutionTermination(); + notifier = mock(DefaultCommitNotificationProducer.class); + doNothing().when(notifier).sendCommitNotification(anyString(), any(Element.class), anySetOf(String.class)); + cap = mock(CapabilityProvider.class); + doReturn(Sets.newHashSet()).when(cap).getCapabilities(); + Document rpcData = XmlFileLoader.xmlFileToDocument("netconfMessages/editConfig_expectedResult.xml"); + doReturn(rpcData).when(router).onNetconfMessage(any(Document.class), any(NetconfServerSession.class)); + commit = new DefaultCommit(notifier, cap, "", router); + } + + @Test + public void testHandleWithNotification() throws Exception { + requestMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/commit.xml"); + commit.handle(requestMessage, operation); + verify(operation, times(1)).execute(requestMessage); + verify(notifier, times(1)).sendCommitNotification(anyString(), any(Element.class), anySetOf(String.class)); + } + + @Test + public void testHandleWithoutNotification() throws Exception { + requestMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/commit.xml"); + Element elem = requestMessage.getDocumentElement(); + elem.setAttribute("notify", "false"); + commit.handle(requestMessage, operation); + verify(operation, times(1)).execute(requestMessage); + verify(notifier, never()).sendCommitNotification(anyString(), any(Element.class), anySetOf(String.class)); + } + + @Test(expected = NetconfDocumentedException.class) + public void testHandle() throws Exception { + Document rpcData = XmlFileLoader.xmlFileToDocument("netconfMessages/get.xml"); + doReturn(rpcData).when(router).onNetconfMessage(any(Document.class), any(NetconfServerSession.class)); + requestMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/commit.xml"); + commit.handle(requestMessage, operation); + } +} diff --git a/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/mapping/operations/DefaultGetSchemaTest.java b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/mapping/operations/DefaultGetSchemaTest.java new file mode 100644 index 0000000000..b655e90f2b --- /dev/null +++ b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/mapping/operations/DefaultGetSchemaTest.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2014 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.controller.netconf.impl.mapping.operations; + +import com.google.common.base.Optional; +import junit.framework.Assert; +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.controller.netconf.api.NetconfDocumentedException; +import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider; +import org.opendaylight.controller.netconf.util.xml.XmlElement; +import org.opendaylight.controller.netconf.util.xml.XmlUtil; +import org.w3c.dom.Document; + +import static org.junit.Assert.assertNotNull; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; + +public class DefaultGetSchemaTest { + + private CapabilityProvider cap; + private Document doc; + private String getSchema; + + @Before + public void setUp() throws Exception { + cap = mock(CapabilityProvider.class); + doc = XmlUtil.newDocument(); + getSchema = "\n" + + " threadpool-api\n" + + " 2010-09-24\n" + + " ncm:yang\n" + + " \n" + + " "; + } + + @Test(expected = NetconfDocumentedException.class) + public void testDefaultGetSchema() throws Exception { + DefaultGetSchema schema = new DefaultGetSchema(cap, ""); + doThrow(IllegalStateException.class).when(cap).getSchemaForCapability(anyString(), any(Optional.class)); + schema.handleWithNoSubsequentOperations(doc, XmlElement.fromDomElement(XmlUtil.readXmlToElement(getSchema))); + } + + @Test + public void handleWithNoSubsequentOperations() throws Exception { + DefaultGetSchema schema = new DefaultGetSchema(cap, ""); + doReturn("").when(cap).getSchemaForCapability(anyString(), any(Optional.class)); + assertNotNull(schema.handleWithNoSubsequentOperations(doc, XmlElement.fromDomElement(XmlUtil.readXmlToElement(getSchema)))); + } +} diff --git a/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/mapping/operations/DefaultStopExiTest.java b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/mapping/operations/DefaultStopExiTest.java new file mode 100644 index 0000000000..b335165706 --- /dev/null +++ b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/mapping/operations/DefaultStopExiTest.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2014 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.controller.netconf.impl.mapping.operations; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelPipeline; +import org.junit.Test; +import org.opendaylight.controller.netconf.impl.NetconfServerSession; +import org.opendaylight.controller.netconf.util.xml.XmlElement; +import org.opendaylight.controller.netconf.util.xml.XmlUtil; +import org.w3c.dom.Document; + +import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.*; + +public class DefaultStopExiTest { + @Test + public void testHandleWithNoSubsequentOperations() throws Exception { + DefaultStopExi exi = new DefaultStopExi(""); + Document doc = XmlUtil.newDocument(); + Channel channel = mock(Channel.class); + ChannelPipeline pipeline = mock(ChannelPipeline.class); + doReturn(pipeline).when(channel).pipeline(); + ChannelHandler channelHandler = mock(ChannelHandler.class); + doReturn(channelHandler).when(pipeline).replace(anyString(), anyString(), any(ChannelHandler.class)); + + NetconfServerSession serverSession = new NetconfServerSession(null, channel, 2L, null); + exi.setNetconfSession(serverSession); + + assertNotNull(exi.handleWithNoSubsequentOperations(doc, XmlElement.fromDomElement(XmlUtil.readXmlToElement("")))); + verify(pipeline, times(1)).replace(anyString(), anyString(), any(ChannelHandler.class)); + } +} diff --git a/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/osgi/NetconfImplActivatorTest.java b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/osgi/NetconfImplActivatorTest.java new file mode 100644 index 0000000000..b59ea884c5 --- /dev/null +++ b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/osgi/NetconfImplActivatorTest.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2014 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.controller.netconf.impl.osgi; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Dictionary; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService; +import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider; +import org.osgi.framework.*; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.*; + +public class NetconfImplActivatorTest { + + @Mock + private BundleContext bundle; + @Mock + private Filter filter; + @Mock + private ServiceReference reference; + @Mock + private ServiceRegistration registration; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + doReturn(filter).when(bundle).createFilter(anyString()); + doNothing().when(bundle).addServiceListener(any(ServiceListener.class), anyString()); + + ServiceReference[] refs = new ServiceReference[0]; + doReturn(refs).when(bundle).getServiceReferences(anyString(), anyString()); + doReturn(Arrays.asList(refs)).when(bundle).getServiceReferences(any(Class.class), anyString()); + doReturn("").when(bundle).getProperty(anyString()); + doReturn(registration).when(bundle).registerService(any(Class.class), any(NetconfOperationServiceFactoryListenerImpl.class), any(Dictionary.class)); + doNothing().when(registration).unregister(); + doNothing().when(bundle).removeServiceListener(any(ServiceListener.class)); + } + + @Test + public void testStart() throws Exception { + NetconfImplActivator activator = new NetconfImplActivator(); + activator.start(bundle); + verify(bundle, times(2)).registerService(any(Class.class), any(NetconfOperationServiceFactoryListenerImpl.class), any(Dictionary.class)); + activator.stop(bundle); + } +} diff --git a/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/osgi/NetconfOperationServiceFactoryTrackerTest.java b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/osgi/NetconfOperationServiceFactoryTrackerTest.java new file mode 100644 index 0000000000..374e8aeb9f --- /dev/null +++ b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/osgi/NetconfOperationServiceFactoryTrackerTest.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2014 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.controller.netconf.impl.osgi; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Filter; +import org.osgi.framework.ServiceReference; + +import static org.junit.Assert.assertNotNull; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.*; + +public class NetconfOperationServiceFactoryTrackerTest { + + @Mock + private Filter filter; + @Mock + private BundleContext context; + @Mock + private NetconfOperationServiceFactoryListener listener; + @Mock + private NetconfOperationServiceFactory factory; + @Mock + private ServiceReference reference; + + private NetconfOperationServiceFactoryTracker tracker; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + doNothing().when(listener).onRemoveNetconfOperationServiceFactory(any(NetconfOperationServiceFactory.class)); + doReturn(filter).when(context).createFilter(anyString()); + doReturn("").when(reference).toString(); + doReturn(factory).when(context).getService(any(ServiceReference.class)); + doReturn("").when(factory).toString(); + doNothing().when(listener).onAddNetconfOperationServiceFactory(any(NetconfOperationServiceFactory.class)); + tracker = new NetconfOperationServiceFactoryTracker(context, listener); + } + + @Test + public void testNetconfOperationServiceFactoryTracker() throws Exception { + tracker.removedService(null, factory); + verify(listener, times(1)).onRemoveNetconfOperationServiceFactory(any(NetconfOperationServiceFactory.class)); + } + + @Test + public void testAddingService() throws Exception { + assertNotNull(tracker.addingService(reference)); + verify(listener, times(1)).onAddNetconfOperationServiceFactory(any(NetconfOperationServiceFactory.class)); + } +} diff --git a/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/util/DeserializerExceptionHandlerTest.java b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/util/DeserializerExceptionHandlerTest.java new file mode 100644 index 0000000000..6512b4bd33 --- /dev/null +++ b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/util/DeserializerExceptionHandlerTest.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2014 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.controller.netconf.impl.util; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelHandlerContext; +import io.netty.util.concurrent.GenericFutureListener; +import org.junit.Before; +import org.junit.Test; + +import static org.mockito.Mockito.*; + +public class DeserializerExceptionHandlerTest { + + private DeserializerExceptionHandler handler; + private ChannelFuture channelFuture; + private ChannelHandlerContext context; + private Channel channel; + + @Before + public void setUp() throws Exception { + handler = new DeserializerExceptionHandler(); + context = mock(ChannelHandlerContext.class); + channel = mock(Channel.class); + doReturn(channel).when(context).channel(); + channelFuture = mock(ChannelFuture.class); + doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class)); + doReturn(channelFuture).when(channel).writeAndFlush(anyObject()); + } + + @Test + public void testExceptionCaught() throws Exception { + handler.exceptionCaught(context, new Exception()); + verify(context, times(1)).channel(); + } +} diff --git a/opendaylight/netconf/netconf-monitoring/src/test/java/org/opendaylight/controller/netconf/monitoring/osgi/NetconfMonitoringActivatorTest.java b/opendaylight/netconf/netconf-monitoring/src/test/java/org/opendaylight/controller/netconf/monitoring/osgi/NetconfMonitoringActivatorTest.java new file mode 100644 index 0000000000..40493569d6 --- /dev/null +++ b/opendaylight/netconf/netconf-monitoring/src/test/java/org/opendaylight/controller/netconf/monitoring/osgi/NetconfMonitoringActivatorTest.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2014 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.controller.netconf.monitoring.osgi; + +import java.util.Arrays; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Filter; +import org.osgi.framework.ServiceListener; +import org.osgi.framework.ServiceReference; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.*; + +public class NetconfMonitoringActivatorTest { + + @Mock + BundleContext context; + @Mock + Filter filter; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + doReturn(filter).when(context).createFilter(anyString()); + doNothing().when(context).addServiceListener(any(ServiceListener.class), anyString()); + ServiceReference[] refs = new ServiceReference[2]; + doReturn(Arrays.asList(refs)).when(context).getServiceReferences(any(Class.class), anyString()); + doReturn(refs).when(context).getServiceReferences(anyString(), anyString()); + } + + @Test + public void testNetconfMonitoringActivator() throws Exception { + NetconfMonitoringActivator activator = new NetconfMonitoringActivator(); + activator.start(context); + verify(context, times(1)).addServiceListener(any(ServiceListener.class), anyString()); + + activator.stop(context); + verify(context, times(1)).removeServiceListener(any(ServiceListener.class)); + } +} diff --git a/opendaylight/netconf/netconf-monitoring/src/test/java/org/opendaylight/controller/netconf/monitoring/osgi/NetconfMonitoringOperationServiceTest.java b/opendaylight/netconf/netconf-monitoring/src/test/java/org/opendaylight/controller/netconf/monitoring/osgi/NetconfMonitoringOperationServiceTest.java new file mode 100644 index 0000000000..b8e35e934b --- /dev/null +++ b/opendaylight/netconf/netconf-monitoring/src/test/java/org/opendaylight/controller/netconf/monitoring/osgi/NetconfMonitoringOperationServiceTest.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2014 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.controller.netconf.monitoring.osgi; + +import com.google.common.base.Optional; +import org.junit.Test; +import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService; +import org.opendaylight.controller.netconf.monitoring.MonitoringConstants; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; + +public class NetconfMonitoringOperationServiceTest { + @Test + public void testGetters() throws Exception { + NetconfMonitoringService monitor = mock(NetconfMonitoringService.class); + NetconfMonitoringOperationService service = new NetconfMonitoringOperationService(monitor); + + assertEquals(1, service.getNetconfOperations().size()); + + assertEquals(Optional.absent(), service.getCapabilities().iterator().next().getCapabilitySchema()); + assertEquals(Optional.absent(), service.getCapabilities().iterator().next().getLocation()); + assertEquals(Optional.of(MonitoringConstants.MODULE_REVISION), service.getCapabilities().iterator().next().getRevision()); + assertEquals(Optional.of(MonitoringConstants.MODULE_NAME), service.getCapabilities().iterator().next().getModuleName()); + assertEquals(Optional.of(MonitoringConstants.NAMESPACE), service.getCapabilities().iterator().next().getModuleNamespace()); + assertEquals(MonitoringConstants.URI, service.getCapabilities().iterator().next().getCapabilityUri()); + } +} diff --git a/opendaylight/netconf/netconf-monitoring/src/test/java/org/opendaylight/controller/netconf/monitoring/osgi/NetconfMonitoringServiceTrackerTest.java b/opendaylight/netconf/netconf-monitoring/src/test/java/org/opendaylight/controller/netconf/monitoring/osgi/NetconfMonitoringServiceTrackerTest.java new file mode 100644 index 0000000000..2a53a6ce45 --- /dev/null +++ b/opendaylight/netconf/netconf-monitoring/src/test/java/org/opendaylight/controller/netconf/monitoring/osgi/NetconfMonitoringServiceTrackerTest.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2014 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.controller.netconf.monitoring.osgi; + +import java.util.Hashtable; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService; +import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Filter; +import org.osgi.framework.ServiceReference; +import org.osgi.framework.ServiceRegistration; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyCollection; +import static org.mockito.Mockito.*; + +public class NetconfMonitoringServiceTrackerTest { + + @Mock + private ServiceReference reference; + @Mock + private BundleContext context; + @Mock + private ServiceRegistration serviceRegistration; + @Mock + private Filter filter; + @Mock + private NetconfMonitoringService monitoringService; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + doReturn(serviceRegistration).when(context).registerService(any(Class.class), any(NetconfOperationServiceFactory.class), any(Hashtable.class)); + doNothing().when(serviceRegistration).unregister(); + doReturn(filter).when(context).createFilter(anyString()); + doReturn("").when(reference).toString(); + doReturn(monitoringService).when(context).getService(any(ServiceReference.class)); + } + + @Test + public void testAddingService() throws Exception { + NetconfMonitoringServiceTracker tracker = new NetconfMonitoringServiceTracker(context); + tracker.addingService(reference); + verify(context, times(1)).registerService(any(Class.class), any(NetconfOperationServiceFactory.class), any(Hashtable.class)); + tracker.removedService(reference, null); + verify(serviceRegistration, times(1)).unregister(); + } +} -- 2.36.6