X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FXMLRouterTest.java;h=c6b1aa0b8f65227b08b40a34442632632052e311;hb=3b2c669b25bfcb5a3c3f06ff9180d85143bebb2a;hp=4bcf2a46929b734e060e9b009b548585d86202ca;hpb=19413a6699295b4bbebc1b3bdb9838fd4370e581;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 4bcf2a4..c6b1aa0 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java @@ -20,7 +20,10 @@ 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; @@ -65,6 +68,8 @@ public class XMLRouterTest { } } + private ExtendedRouterConfig routerConfig; + private XMLRouterConfiguration config; private XMLRouter router; private DOMSource source1; private DOMSource source2; @@ -76,14 +81,31 @@ 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(new SystemClock(), 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")); @@ -96,27 +118,39 @@ public class XMLRouterTest { @Test public void testMisBehavingDocumentType() { - DocumentType type = mock(DocumentType.class); + DocumentType type = mockDocument("docid"); doThrow(new RuntimeException("x")).when(type).isInstance( any(DOMSource.class)); - router.documentTypeConfig().add(type); + 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 = mock(Filter.class); + Filter filter = mockFilter("filterid"); doThrow(new RuntimeException("x")).when(filter).isAllowed(anyString(), any(DOMSource.class)); - router.filterConfig().add(filter); + 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 public void testOneDestinationNoTransformationSuccess() { destinationSpy = registerDestination(true, "any"); @@ -124,7 +158,7 @@ public class XMLRouterTest { router.publish("any", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(destinationSpy).receive(same(source1)); // Unregister the destination. @@ -141,17 +175,18 @@ public class XMLRouterTest { } 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.documentTypeConfig().add(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.documentTypeConfig().add(type); + when(type.getId()).thenReturn(new Id(aType)); + routerConfig.documentTypeConfig().add(type); } private Destination registerDestination(boolean aResult, String... types) { @@ -195,7 +230,7 @@ public class XMLRouterTest { router.publish("any", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(destinationSpy2).receive(same(source1)); @@ -224,7 +259,7 @@ public class XMLRouterTest { .registerDestination(destinationSpy2); router.publish("any", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(destinationSpy, never()).receive(same(source1)); verify(destinationSpy2).receive(same(source1)); @@ -255,7 +290,7 @@ public class XMLRouterTest { .registerDestination(destinationSpy2); router.publish("any", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(destinationSpy, never()).receive(same(source1)); verify(destinationSpy2).receive(same(source1)); @@ -265,11 +300,13 @@ public class XMLRouterTest { public void testOneTransformationOneDestination() { registerDocumentType("any"); Transformation transformation = mock(Transformation.class); - when(transformation.getName()).thenReturn("trans"); + 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.transformationConfig().add(transformation); + routerConfig.transformationConfig().add(transformation); + config.setRouterConfig(routerConfig); Destination destination = mock(Destination.class); when( @@ -281,7 +318,7 @@ public class XMLRouterTest { when(destination.receive(any(DOMSource.class))).thenReturn(true); router.publish("bla", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(transformation).transform(source1); verify(destination).receive(same(source2)); @@ -292,16 +329,52 @@ public class XMLRouterTest { 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.getName()).thenReturn("trans"); + 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)); - router.transformationConfig().add(transformation); + routerConfig.transformationConfig().add(transformation); Destination destination = mock(Destination.class); when( @@ -318,7 +391,8 @@ public class XMLRouterTest { private Transformation createTransformation(String aFrom, String aTo, DOMSource aSource, DOMSource aTarget) { Transformation transformation = mock(Transformation.class); - when(transformation.getName()).thenReturn("trans"); + 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); @@ -331,8 +405,8 @@ public class XMLRouterTest { Transformation transformation = createTransformation("any", "bla", source1, null); - router.transformationConfig().add(transformation); - + routerConfig.transformationConfig().add(transformation); + config.setRouterConfig(routerConfig); Destination destination = mock(Destination.class); when( destination.chooseFromTargetTypes((Collection) anyObject())) @@ -349,13 +423,15 @@ public class XMLRouterTest { Transformation transformation2 = createTransformation("any", "bla2", source1, source2); - router.transformationConfig().add(transformation2); + routerConfig.transformationConfig().add(transformation2); + config.setRouterConfig(routerConfig); when( destination.chooseFromTargetTypes((Collection) anyObject())) .thenReturn(Arrays.asList("bla", "bla2")); reset(transformation); - when(transformation.getName()).thenReturn("trans"); + when(transformation.getId()) + .thenReturn(new Id("trans")); when(transformation.getFromType()).thenReturn("any"); when(transformation.getToType()).thenReturn("bla"); when(transformation.transform(same(source1))).thenReturn(null); @@ -363,7 +439,7 @@ public class XMLRouterTest { when(destination.receive(any(DOMSource.class))).thenReturn(true); router.publish("bla", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(transformation).transform(source1); verify(transformation2).transform(source1); @@ -380,7 +456,7 @@ public class XMLRouterTest { router.publish("source", source1); verify(listener, times(2)).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(dest1).receive(same(source1)); verify(dest2).receive(same(source1)); @@ -393,11 +469,12 @@ public class XMLRouterTest { registerDocumentType("other", source2); Transformation transformation = createTransformation("any", "other", source1, source2); - router.transformationConfig().add(transformation); + routerConfig.transformationConfig().add(transformation); + config.setRouterConfig(routerConfig); router.publish("source", source1); verify(listener, times(2)).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(dest).receive(same(source1)); verify(dest).receive(same(source2)); @@ -411,18 +488,45 @@ public class XMLRouterTest { Transformation t1 = createTransformation("any", "intermediate", source1, source2); - router.transformationConfig().add(t1); + routerConfig.transformationConfig().add(t1); Transformation t2 = createTransformation("intermediate", "other", source2, source3); - router.transformationConfig().add(t2); + routerConfig.transformationConfig().add(t2); + config.setRouterConfig(routerConfig); router.publish("source", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + 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); @@ -438,7 +542,7 @@ public class XMLRouterTest { router.publish("source", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(false)); + anyListOf(Transformation.class), anyString(), eq(false)); } }