X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FXMLRouterTest.java;h=c6b1aa0b8f65227b08b40a34442632632052e311;hb=9dbc2844950b55ae552fe74840954ea71b754c7a;hp=2d73fad15694e652e9fae870b5edc45458e48b23;hpb=3994d4a35e7404908fc17beac75479c1a72fa915;p=xmlrouter diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java index 2d73fad..c6b1aa0 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java @@ -15,21 +15,26 @@ */ package org.wamblee.xmlrouter.impl; -import static junit.framework.Assert.*; import static org.mockito.Matchers.*; import static org.mockito.Mockito.*; import java.util.Arrays; import java.util.Collection; +import java.util.UUID; +import java.util.logging.Handler; import java.util.logging.Level; +import java.util.logging.Logger; import javax.xml.transform.dom.DOMSource; import org.junit.Before; import org.junit.Test; +import org.wamblee.general.SystemClock; import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.DocumentType; +import org.wamblee.xmlrouter.config.Filter; import org.wamblee.xmlrouter.config.Transformation; +import org.wamblee.xmlrouter.listener.EventInfo; import org.wamblee.xmlrouter.listener.EventListener; import org.wamblee.xmlrouter.listener.LoggingEventListener; import org.wamblee.xmlrouter.subscribe.Destination; @@ -63,6 +68,8 @@ public class XMLRouterTest { } } + private ExtendedRouterConfig routerConfig; + private XMLRouterConfiguration config; private XMLRouter router; private DOMSource source1; private DOMSource source2; @@ -74,21 +81,74 @@ public class XMLRouterTest { @Before public void setUp() { + + Logger logger = Logger.getLogger(XMLRouter.class.getName()); + setLogLevel(logger, Level.FINEST); + + routerConfig = new SingleRouterConfig("routerconfig"); + config = new XMLRouterConfigurationImpl(routerConfig); EventListener logListener = new LoggingEventListener(Level.INFO); listener = spy(logListener); - router = new XMLRouter(listener); + router = new XMLRouter(new SystemClock(), config, listener); source1 = mock(DOMSource.class); source2 = mock(DOMSource.class); source3 = mock(DOMSource.class); } + private void setLogLevel(Logger aLogger, Level aLevel) { + aLogger.setLevel(aLevel); + for (Handler handler : aLogger.getHandlers()) { + handler.setLevel(Level.FINEST); + } + Logger parent = aLogger.getParent(); + if (parent != null) { + setLogLevel(parent, aLevel); + } + } + @Test public void testNoInputDocumentsRegistered() { Destination destination = new MyDestination(true, Arrays.asList("any")); destinationSpy = spy(destination); destinationId = router.registerDestination(destinationSpy); - assertFalse(router.publish("any", source1)); + router.publish("any", source1); + verify(listener).notDelivered(any(EventInfo.class)); + } + + @Test + public void testMisBehavingDocumentType() { + DocumentType type = mockDocument("docid"); + doThrow(new RuntimeException("x")).when(type).isInstance( + any(DOMSource.class)); + routerConfig.documentTypeConfig().add(type); + router.publish("xx", mock(DOMSource.class)); + verify(listener).notDelivered(any(EventInfo.class)); + // no exception should occur. + } + + private DocumentType mockDocument(String docid) { + DocumentType type = mock(DocumentType.class); + when(type.getId()).thenReturn(new Id(docid)); + return type; + } + + @Test + public void testMisBehavingFilter() { + registerDocumentType("any"); + Filter filter = mockFilter("filterid"); + doThrow(new RuntimeException("x")).when(filter).isAllowed(anyString(), + any(DOMSource.class)); + routerConfig.filterConfig().add(filter); + router.publish("xx", mock(DOMSource.class)); + verify(listener).notDelivered(any(EventInfo.class)); + // no exception should occur. + } + + private Filter mockFilter(String filterId) { + Filter filter = mock(Filter.class); + when(filter.getId()).thenReturn(new Id(filterId)); + return filter; } @Test @@ -96,28 +156,37 @@ public class XMLRouterTest { destinationSpy = registerDestination(true, "any"); registerDocumentType("any"); - assertTrue(router.publish("any", source1)); + router.publish("any", source1); + verify(listener).delivered(any(EventInfo.class), + anyListOf(Transformation.class), anyString(), eq(true)); verify(destinationSpy).receive(same(source1)); // Unregister the destination. router.unregisterDestination(destinationId); - reset(destinationSpy); - assertFalse(router.publish("any", source2)); + resetMocks(); + router.publish("any", source2); + verify(listener).notDelivered(any(EventInfo.class)); verifyNoMoreInteractions(destinationSpy); } + private void resetMocks() { + reset(destinationSpy); + reset(listener); + } + private void registerDocumentType(String aType) { - DocumentType type = mock(DocumentType.class); + DocumentType type = mockDocument(UUID.randomUUID().toString()); when(type.isInstance(any(DOMSource.class))).thenReturn(true); when(type.getName()).thenReturn(aType); - Id typeId = router.addDocumentType(type); + routerConfig.documentTypeConfig().add(type); } private void registerDocumentType(String aType, DOMSource aSource) { DocumentType type = mock(DocumentType.class); when(type.isInstance(same(aSource))).thenReturn(true); when(type.getName()).thenReturn(aType); - Id typeId = router.addDocumentType(type); + when(type.getId()).thenReturn(new Id(aType)); + routerConfig.documentTypeConfig().add(type); } private Destination registerDestination(boolean aResult, String... types) { @@ -133,7 +202,8 @@ public class XMLRouterTest { destinationSpy = registerDestination(true); registerDocumentType("any"); - assertFalse(router.publish("any", source1)); + router.publish("any", source1); + verify(listener).notDelivered(any(EventInfo.class)); verify(destinationSpy, never()).receive(any(DOMSource.class)); } @@ -145,7 +215,8 @@ public class XMLRouterTest { doThrow(new RuntimeException()).when(destinationSpy).receive( any(DOMSource.class)); - assertFalse(router.publish("any", source1)); + router.publish("any", source1); + verify(listener).notDelivered(any(EventInfo.class)); verify(destinationSpy).receive(same(source1)); } @@ -157,8 +228,12 @@ public class XMLRouterTest { Id destinationId2 = router .registerDestination(destinationSpy2); - assertTrue(router.publish("any", source1)); + router.publish("any", source1); + verify(listener).delivered(any(EventInfo.class), + anyListOf(Transformation.class), anyString(), eq(true)); + verify(destinationSpy2).receive(same(source1)); + } @Test @@ -169,7 +244,8 @@ public class XMLRouterTest { doThrow(new RuntimeException()).when(destinationSpy) .chooseFromTargetTypes((Collection) anyObject()); - assertFalse(router.publish("any", source1)); + router.publish("any", source1); + verify(listener).notDelivered(any(EventInfo.class)); verify(destinationSpy, never()).receive(same(source1)); } @@ -181,7 +257,10 @@ public class XMLRouterTest { Destination destinationSpy2 = spy(destination2); Id destinationId2 = router .registerDestination(destinationSpy2); - assertTrue(router.publish("any", source1)); + router.publish("any", source1); + verify(listener).delivered(any(EventInfo.class), + anyListOf(Transformation.class), anyString(), eq(true)); + verify(destinationSpy, never()).receive(same(source1)); verify(destinationSpy2).receive(same(source1)); } @@ -196,7 +275,8 @@ public class XMLRouterTest { .chooseFromTargetTypes((Collection) anyObject())) .thenReturn(null); - assertFalse(router.publish("any", source1)); + router.publish("any", source1); + verify(listener).notDelivered(any(EventInfo.class)); verify(destinationSpy, never()).receive(same(source1)); } @@ -208,7 +288,10 @@ public class XMLRouterTest { Destination destinationSpy2 = spy(destination2); Id destinationId2 = router .registerDestination(destinationSpy2); - assertTrue(router.publish("any", source1)); + router.publish("any", source1); + verify(listener).delivered(any(EventInfo.class), + anyListOf(Transformation.class), anyString(), eq(true)); + verify(destinationSpy, never()).receive(same(source1)); verify(destinationSpy2).receive(same(source1)); } @@ -217,10 +300,13 @@ public class XMLRouterTest { public void testOneTransformationOneDestination() { registerDocumentType("any"); Transformation transformation = mock(Transformation.class); + when(transformation.getId()) + .thenReturn(new Id("trans")); when(transformation.getFromType()).thenReturn("any"); when(transformation.getToType()).thenReturn("bla"); when(transformation.transform(same(source1))).thenReturn(source2); - router.addTransformation(transformation); + routerConfig.transformationConfig().add(transformation); + config.setRouterConfig(routerConfig); Destination destination = mock(Destination.class); when( @@ -230,19 +316,83 @@ public class XMLRouterTest { router.registerDestination(destination); when(destination.receive(any(DOMSource.class))).thenReturn(true); - assertTrue(router.publish("bla", source1)); + router.publish("bla", source1); + verify(listener).delivered(any(EventInfo.class), + anyListOf(Transformation.class), anyString(), eq(true)); verify(transformation).transform(source1); verify(destination).receive(same(source2)); // now the same when the destination rejects the event. when(destination.receive(any(DOMSource.class))).thenReturn(false); - assertFalse(router.publish("bla", source1)); + router.publish("bla", source1); + verify(listener).notDelivered(any(EventInfo.class)); + } + + @Test + public void testOneTransformationOneDestinationFilterRejectsDestinationDocType() { + registerDocumentType("any"); + + Transformation transformation = mock(Transformation.class); + when(transformation.getId()) + .thenReturn(new Id("trans")); + when(transformation.getFromType()).thenReturn("any"); + when(transformation.getToType()).thenReturn("bla"); + when(transformation.transform(same(source1))).thenReturn(source2); + routerConfig.transformationConfig().add(transformation); + + Filter filter = mock(Filter.class); + when(filter.getId()).thenReturn(new Id("f")); + when(filter.isAllowed(anyString(), same(source2))).thenReturn(false); + when(filter.isAllowed(anyString(), same(source1))).thenReturn(true); + routerConfig.filterConfig().add(filter); + + config.setRouterConfig(routerConfig); + + Destination destination = mock(Destination.class); + when( + destination.chooseFromTargetTypes((Collection) anyObject())) + .thenReturn(Arrays.asList("bla")); + + router.registerDestination(destination); + + when(destination.receive(any(DOMSource.class))).thenReturn(true); + router.publish("bla", source1); + verify(listener).notDelivered(any(EventInfo.class)); + + verify(transformation).transform(source1); + verify(destination, never()).receive(any(DOMSource.class)); + } + + @Test + public void testMisbehavingTransformationOneDestination() { + registerDocumentType("any"); + Transformation transformation = mock(Transformation.class); + when(transformation.getId()) + .thenReturn(new Id("trans")); + when(transformation.getFromType()).thenReturn("any"); + when(transformation.getToType()).thenReturn("bla"); + doThrow(new RuntimeException("x")).when(transformation).transform( + same(source1)); + routerConfig.transformationConfig().add(transformation); + + Destination destination = mock(Destination.class); + when( + destination.chooseFromTargetTypes((Collection) anyObject())) + .thenReturn(Arrays.asList("bla")); + + router.registerDestination(destination); + + when(destination.receive(any(DOMSource.class))).thenReturn(true); + router.publish("bla", source1); + verify(listener).notDelivered(any(EventInfo.class)); } private Transformation createTransformation(String aFrom, String aTo, DOMSource aSource, DOMSource aTarget) { Transformation transformation = mock(Transformation.class); + when(transformation.getId()).thenReturn( + new Id(UUID.randomUUID().toString())); when(transformation.getFromType()).thenReturn(aFrom); when(transformation.getToType()).thenReturn(aTo); when(transformation.transform(same(aSource))).thenReturn(aTarget); @@ -255,15 +405,16 @@ public class XMLRouterTest { Transformation transformation = createTransformation("any", "bla", source1, null); - router.addTransformation(transformation); - + routerConfig.transformationConfig().add(transformation); + config.setRouterConfig(routerConfig); Destination destination = mock(Destination.class); when( destination.chooseFromTargetTypes((Collection) anyObject())) .thenReturn(Arrays.asList("bla")); router.registerDestination(destination); - assertFalse(router.publish("bla", source1)); + router.publish("bla", source1); + verify(listener).notDelivered(any(EventInfo.class)); verify(transformation).transform(source1); verify(destination, never()).receive(any(DOMSource.class)); @@ -272,18 +423,23 @@ public class XMLRouterTest { Transformation transformation2 = createTransformation("any", "bla2", source1, source2); - router.addTransformation(transformation2); + routerConfig.transformationConfig().add(transformation2); + config.setRouterConfig(routerConfig); when( destination.chooseFromTargetTypes((Collection) anyObject())) .thenReturn(Arrays.asList("bla", "bla2")); reset(transformation); + when(transformation.getId()) + .thenReturn(new Id("trans")); when(transformation.getFromType()).thenReturn("any"); when(transformation.getToType()).thenReturn("bla"); when(transformation.transform(same(source1))).thenReturn(null); when(destination.receive(any(DOMSource.class))).thenReturn(true); - assertTrue(router.publish("bla", source1)); + router.publish("bla", source1); + verify(listener).delivered(any(EventInfo.class), + anyListOf(Transformation.class), anyString(), eq(true)); verify(transformation).transform(source1); verify(transformation2).transform(source1); @@ -298,7 +454,9 @@ public class XMLRouterTest { Destination dest2 = registerDestination(true, "any"); registerDocumentType("any"); - assertTrue(router.publish("source", source1)); + router.publish("source", source1); + verify(listener, times(2)).delivered(any(EventInfo.class), + anyListOf(Transformation.class), anyString(), eq(true)); verify(dest1).receive(same(source1)); verify(dest2).receive(same(source1)); @@ -311,9 +469,12 @@ public class XMLRouterTest { registerDocumentType("other", source2); Transformation transformation = createTransformation("any", "other", source1, source2); - router.addTransformation(transformation); + routerConfig.transformationConfig().add(transformation); + config.setRouterConfig(routerConfig); - assertTrue(router.publish("source", source1)); + router.publish("source", source1); + verify(listener, times(2)).delivered(any(EventInfo.class), + anyListOf(Transformation.class), anyString(), eq(true)); verify(dest).receive(same(source1)); verify(dest).receive(same(source2)); @@ -321,19 +482,67 @@ public class XMLRouterTest { @Test public void testMultipleTransformations() { - Destination dest = registerDestination(true, "any", "other"); + Destination dest = registerDestination(true, "other"); registerDocumentType("any", source1); registerDocumentType("other", source3); Transformation t1 = createTransformation("any", "intermediate", source1, source2); - router.addTransformation(t1); + routerConfig.transformationConfig().add(t1); Transformation t2 = createTransformation("intermediate", "other", source2, source3); - router.addTransformation(t2); + routerConfig.transformationConfig().add(t2); + config.setRouterConfig(routerConfig); - assertTrue(router.publish("source", source1)); + router.publish("source", source1); + verify(listener).delivered(any(EventInfo.class), + anyListOf(Transformation.class), anyString(), eq(true)); verify(dest).receive(same(source3)); } + + @Test + public void testMultipleTransformationsFilterRejectsIntermediateDocument() { + Destination dest = registerDestination(true, "other"); + registerDocumentType("any", source1); + registerDocumentType("other", source3); + + Transformation t1 = createTransformation("any", "intermediate", + source1, source2); + routerConfig.transformationConfig().add(t1); + Transformation t2 = createTransformation("intermediate", "other", + source2, source3); + routerConfig.transformationConfig().add(t2); + config.setRouterConfig(routerConfig); + + Filter filter = mock(Filter.class); + when(filter.getId()).thenReturn(new Id("f")); + when(filter.isAllowed(anyString(), same(source1))).thenReturn(true); + when(filter.isAllowed(anyString(), same(source2))).thenReturn(false); + when(filter.isAllowed(anyString(), same(source3))).thenReturn(true); + routerConfig.filterConfig().add(filter); + + router.publish("source", source1); + verify(listener).notDelivered(any(EventInfo.class)); + verify(dest, never()).receive(any(DOMSource.class)); + } + + @Test + public void testDestinationGivesError() { + Destination destination = mock(Destination.class); + when(destination.getName()).thenReturn("name"); + when(destination.chooseFromTargetTypes(anyCollectionOf(String.class))) + .thenReturn(Arrays.asList("any")); + doThrow(new RuntimeException("x")).when(destination).receive( + any(DOMSource.class)); + router.registerDestination(destination); + + registerDocumentType("any"); + + router.publish("source", source1); + + verify(listener).delivered(any(EventInfo.class), + anyListOf(Transformation.class), anyString(), eq(false)); + + } }