X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fconfig-persister-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fpersist%2Fimpl%2Fosgi%2FConfigPersisterTest.java;h=7b79e41f380af45f8a41a1347b8816516fe8bc32;hp=b722496142e4f2014b82e776317488e02ddb4527;hb=3927509ec3ecfa32a51b725d2b7155d425f5b877;hpb=12204729ecaa1d1862a580cc6282eceacd21efe2 diff --git a/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterTest.java b/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterTest.java index b722496142..7b79e41f38 100644 --- a/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterTest.java +++ b/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterTest.java @@ -7,13 +7,21 @@ */ package org.opendaylight.controller.netconf.persist.impl.osgi; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; + import com.google.common.collect.Sets; +import java.io.IOException; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.opendaylight.controller.config.api.ConflictingVersionException; +import org.opendaylight.controller.netconf.api.Capability; import org.opendaylight.controller.netconf.api.NetconfDocumentedException; -import org.opendaylight.controller.netconf.mapping.api.Capability; import org.opendaylight.controller.netconf.mapping.api.HandlingPriority; import org.opendaylight.controller.netconf.mapping.api.NetconfOperation; import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution; @@ -25,28 +33,18 @@ import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.xml.sax.SAXException; -import javax.management.MBeanServer; -import java.io.IOException; -import java.lang.management.ManagementFactory; - -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; - public class ConfigPersisterTest { - private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterTest.class); + private static final Logger LOG = LoggerFactory.getLogger(ConfigPersisterTest.class); private MockedBundleContext ctx; private ConfigPersisterActivator configPersisterActivator; - private static final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); private TestingExceptionHandler handler; - private void setUpContextAndStartPersister(String requiredCapability) throws Exception { + private void setUpContextAndStartPersister(String requiredCapability, final NetconfOperationService conflictingService) throws Exception { DummyAdapterWithInitialSnapshot.expectedCapability = requiredCapability; ctx = new MockedBundleContext(1000, 1000); + doReturn(getConflictingService()).when(ctx.serviceFactory).createService(anyString()); configPersisterActivator = new ConfigPersisterActivator(); configPersisterActivator.start(ctx.getBundleContext()); } @@ -65,7 +63,7 @@ public class ConfigPersisterTest { @Test public void testPersisterNotAllCapabilitiesProvided() throws Exception { - setUpContextAndStartPersister("required-cap"); + setUpContextAndStartPersister("required-cap", getConflictingService()); Thread.sleep(2000); handler.assertException(IllegalStateException.class, "Max wait for capabilities reached.Not enough capabilities " + "for . Expected but not found: [required-cap]"); @@ -74,7 +72,7 @@ public class ConfigPersisterTest { @Test public void testPersisterSuccessfulPush() throws Exception { - setUpContextAndStartPersister("cap1"); + setUpContextAndStartPersister("cap1", getConflictingService()); NetconfOperationService service = getWorkingService(getOKDocument()); doReturn(service).when(ctx.serviceFactory).createService(anyString()); Thread.sleep(2000); @@ -83,13 +81,13 @@ public class ConfigPersisterTest { // this means pushing of config was successful public void assertCannotRegisterAsJMXListener_pushWasSuccessful() { - handler.assertException(RuntimeException.class, "Cannot register as JMX listener to netconf"); + handler.assertException(IllegalStateException.class, "Cannot register as JMX listener to netconf"); } public NetconfOperationService getWorkingService(Document document) throws SAXException, IOException, NetconfDocumentedException { NetconfOperationService service = mock(NetconfOperationService.class); Capability capability = mock(Capability.class); - doReturn(Sets.newHashSet(capability)).when(service).getCapabilities(); +// doReturn(Sets.newHashSet(capability)).when(service).getCapabilities(); doReturn("cap1").when(capability).getCapabilityUri(); @@ -112,30 +110,32 @@ public class ConfigPersisterTest { @Test public void testPersisterConflictingVersionException() throws Exception { - setUpContextAndStartPersister("cap1"); - NetconfOperationService service = getWorkingService(getConflictVersionDocument()); - doReturn(service).when(ctx.serviceFactory).createService(anyString()); + setUpContextAndStartPersister("cap1", getConflictingService()); + Thread.sleep(2000); handler.assertException(IllegalStateException.class, "Max wait for conflicting version stabilization timeout"); } - private Document getConflictVersionDocument() throws SAXException, IOException { - return XmlUtil.readXmlToDocument( - "\n" + - "" + - ConflictingVersionException.class.getCanonicalName() + - "\n" + - "" - ); + private NetconfOperationService getConflictingService() throws Exception { + NetconfOperationService service = getWorkingService(getOKDocument()); + ConflictingVersionException cve = new ConflictingVersionException(""); + try { + NetconfDocumentedException.wrap(cve); + throw new AssertionError("Should throw an exception"); + }catch(NetconfDocumentedException e) { + NetconfOperation mockedOperation = service.getNetconfOperations().iterator().next(); + doThrow(e).when(mockedOperation).handle(any(Document.class), any(NetconfOperationChainedExecution.class)); + return service; + } } @Test public void testSuccessConflictingVersionException() throws Exception { - setUpContextAndStartPersister("cap1"); - doReturn(getWorkingService(getConflictVersionDocument())).when(ctx.serviceFactory).createService(anyString()); + setUpContextAndStartPersister("cap1", getConflictingService()); + doReturn(getConflictingService()).when(ctx.serviceFactory).createService(anyString()); Thread.sleep(500); // working service: - logger.info("Switching to working service **"); + LOG.info("Switching to working service **"); doReturn(getWorkingService(getOKDocument())).when(ctx.serviceFactory).createService(anyString()); Thread.sleep(1000); assertCannotRegisterAsJMXListener_pushWasSuccessful();