initial versions.
[xmlrouter] / impl / src / test / java / org / wamblee / xmlrouter / impl / XMLRouterTest.java
diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java
new file mode 100644 (file)
index 0000000..c4a73ba
--- /dev/null
@@ -0,0 +1,318 @@
+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 javax.xml.transform.dom.DOMSource;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.wamblee.xmlrouter.common.Id;
+import org.wamblee.xmlrouter.config.DocumentType;
+import org.wamblee.xmlrouter.config.Transformation;
+import org.wamblee.xmlrouter.subscribe.Destination;
+
+public class XMLRouterTest {
+
+    public static class MyDestination implements Destination {
+
+        private boolean receiveResult;
+        private Collection<String> types;
+
+        public MyDestination(boolean aReceiveResult, Collection<String> aTypes) {
+            receiveResult = aReceiveResult;
+            types = aTypes;
+        }
+
+        @Override
+        public Collection<String> chooseFromTargetTypes(
+            Collection<String> aPossibleTargetTypes) {
+            return types;
+        }
+
+        @Override
+        public String getName() {
+            return "xxx";
+        }
+
+        @Override
+        public boolean receive(DOMSource aEvent) {
+            return receiveResult;
+        }
+    }
+
+    private XMLRouter router;
+    private DOMSource source1;
+    private DOMSource source2;
+    private DOMSource source3;
+
+    private Destination destinationSpy;
+    private Id<Destination> destinationId;
+
+    @Before
+    public void setUp() {
+        router = new XMLRouter();
+        source1 = mock(DOMSource.class);
+        source2 = mock(DOMSource.class);
+        source3 = mock(DOMSource.class);
+    }
+
+    @Test
+    public void testNoInputDocumentsRegistered() {
+        Destination destination = new MyDestination(true, Arrays.asList("any"));
+        destinationSpy = spy(destination);
+
+        destinationId = router.registerDestination(destinationSpy);
+        assertFalse(router.publish("any", source1));
+    }
+
+    @Test
+    public void testOneDestinationNoTransformationSuccess() {
+        destinationSpy = registerDestination(true, "any");
+        registerDocumentType("any");
+
+        assertTrue(router.publish("any", source1));
+        verify(destinationSpy).receive(same(source1));
+
+        // Unregister the destination.
+        router.unregisterDestination(destinationId);
+        reset(destinationSpy);
+        assertFalse(router.publish("any", source2));
+        verifyNoMoreInteractions(destinationSpy);
+    }
+
+    private void registerDocumentType(String aType) {
+        DocumentType type = mock(DocumentType.class);
+        when(type.isInstance(any(DOMSource.class))).thenReturn(true);
+        when(type.getName()).thenReturn(aType);
+        Id<DocumentType> typeId = router.addDocumentType(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<DocumentType> typeId = router.addDocumentType(type);
+    }
+
+    private Destination registerDestination(boolean aResult, String... types) {
+        Destination destination = new MyDestination(aResult,
+            Arrays.asList(types));
+        Destination myspy = spy(destination);
+        destinationId = router.registerDestination(myspy);
+        return myspy;
+    }
+
+    @Test
+    public void testOneDestinationNotMatches() {
+        destinationSpy = registerDestination(true);
+        registerDocumentType("any");
+
+        assertFalse(router.publish("any", source1));
+        verify(destinationSpy, never()).receive(any(DOMSource.class));
+    }
+
+    @Test
+    public void testOneDestinationThrowsException() {
+        destinationSpy = registerDestination(true, "any");
+        registerDocumentType("any");
+
+        doThrow(new RuntimeException()).when(destinationSpy).receive(
+            any(DOMSource.class));
+
+        assertFalse(router.publish("any", source1));
+        verify(destinationSpy).receive(same(source1));
+    }
+
+    @Test
+    public void testOneDestinationThrowsExceptionSecondDestinationStillHandled() {
+        testOneDestinationThrowsException();
+        Destination destination2 = new MyDestination(true, Arrays.asList("any"));
+        Destination destinationSpy2 = spy(destination2);
+        Id<Destination> destinationId2 = router
+            .registerDestination(destinationSpy2);
+
+        assertTrue(router.publish("any", source1));
+        verify(destinationSpy2).receive(same(source1));
+    }
+
+    @Test
+    public void testDestinationChooseFromTargetTypesThrowsException() {
+        destinationSpy = registerDestination(true, "any");
+        registerDocumentType("any");
+
+        doThrow(new RuntimeException()).when(destinationSpy)
+            .chooseFromTargetTypes((Collection<String>) anyObject());
+
+        assertFalse(router.publish("any", source1));
+        verify(destinationSpy, never()).receive(same(source1));
+    }
+
+    @Test
+    public void testDestinationChooseFromTargetTypesThrowsExceptionSecondDestinationStillOk() {
+        testDestinationChooseFromTargetTypesThrowsException();
+
+        Destination destination2 = new MyDestination(true, Arrays.asList("any"));
+        Destination destinationSpy2 = spy(destination2);
+        Id<Destination> destinationId2 = router
+            .registerDestination(destinationSpy2);
+        assertTrue(router.publish("any", source1));
+        verify(destinationSpy, never()).receive(same(source1));
+        verify(destinationSpy2).receive(same(source1));
+    }
+
+    @Test
+    public void testDestinationChooseFromTargetTypesReturnsNull() {
+        destinationSpy = registerDestination(true, "any");
+        registerDocumentType("any");
+
+        when(
+            destinationSpy
+                .chooseFromTargetTypes((Collection<String>) anyObject()))
+            .thenReturn(null);
+
+        assertFalse(router.publish("any", source1));
+        verify(destinationSpy, never()).receive(same(source1));
+    }
+
+    @Test
+    public void testDestinationChooseFromTargetTypesReturnsNullSecondDestinationStillOk() {
+        testDestinationChooseFromTargetTypesReturnsNull();
+
+        Destination destination2 = new MyDestination(true, Arrays.asList("any"));
+        Destination destinationSpy2 = spy(destination2);
+        Id<Destination> destinationId2 = router
+            .registerDestination(destinationSpy2);
+        assertTrue(router.publish("any", source1));
+        verify(destinationSpy, never()).receive(same(source1));
+        verify(destinationSpy2).receive(same(source1));
+    }
+
+    @Test
+    public void testOneTransformationOneDestination() {
+        registerDocumentType("any");
+        Transformation transformation = mock(Transformation.class);
+        when(transformation.getFromType()).thenReturn("any");
+        when(transformation.getToType()).thenReturn("bla");
+        when(transformation.transform(same(source1))).thenReturn(source2);
+        router.addTransformation(transformation);
+
+        Destination destination = mock(Destination.class);
+        when(
+            destination.chooseFromTargetTypes((Collection<String>) anyObject()))
+            .thenReturn(Arrays.asList("bla"));
+
+        router.registerDestination(destination);
+
+        when(destination.receive(any(DOMSource.class))).thenReturn(true);
+        assertTrue(router.publish("bla", source1));
+
+        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));
+    }
+
+    private Transformation createTransformation(String aFrom, String aTo,
+        DOMSource aSource, DOMSource aTarget) {
+        Transformation transformation = mock(Transformation.class);
+        when(transformation.getFromType()).thenReturn(aFrom);
+        when(transformation.getToType()).thenReturn(aTo);
+        when(transformation.transform(same(aSource))).thenReturn(aTarget);
+        return transformation;
+    }
+
+    @Test
+    public void testOneTransformationReturnsNull() {
+        registerDocumentType("any");
+        Transformation transformation = createTransformation("any", "bla",
+            source1, null);
+
+        router.addTransformation(transformation);
+
+        Destination destination = mock(Destination.class);
+        when(
+            destination.chooseFromTargetTypes((Collection<String>) anyObject()))
+            .thenReturn(Arrays.asList("bla"));
+        router.registerDestination(destination);
+
+        assertFalse(router.publish("bla", source1));
+
+        verify(transformation).transform(source1);
+        verify(destination, never()).receive(any(DOMSource.class));
+
+        // add second transformation that behaves normally
+        Transformation transformation2 = createTransformation("any", "bla2",
+            source1, source2);
+
+        router.addTransformation(transformation2);
+        when(
+            destination.chooseFromTargetTypes((Collection<String>) anyObject()))
+            .thenReturn(Arrays.asList("bla", "bla2"));
+
+        reset(transformation);
+        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));
+
+        verify(transformation).transform(source1);
+        verify(transformation2).transform(source1);
+
+        verify(destination).receive(same(source2));
+
+    }
+
+    @Test
+    public void testChooseMultipleDestinationsOneType() {
+        Destination dest1 = registerDestination(true, "any");
+        Destination dest2 = registerDestination(true, "any");
+        registerDocumentType("any");
+
+        assertTrue(router.publish("source", source1));
+
+        verify(dest1).receive(same(source1));
+        verify(dest2).receive(same(source1));
+    }
+
+    @Test
+    public void testMultipleDeliveryToOneDestination() {
+        Destination dest = registerDestination(true, "any", "other");
+        registerDocumentType("any", source1);
+        registerDocumentType("other", source2);
+        Transformation transformation = createTransformation("any", "other",
+            source1, source2);
+        router.addTransformation(transformation);
+
+        assertTrue(router.publish("source", source1));
+
+        verify(dest).receive(same(source1));
+        verify(dest).receive(same(source2));
+    }
+
+    @Test
+    public void testMultipleTransformations() {
+        Destination dest = registerDestination(true, "any", "other");
+        registerDocumentType("any", source1);
+        registerDocumentType("other", source3);
+
+        Transformation t1 = createTransformation("any", "intermediate",
+            source1, source2);
+        router.addTransformation(t1);
+        Transformation t2 = createTransformation("intermediate", "other",
+            source2, source3);
+        router.addTransformation(t2);
+
+        assertTrue(router.publish("source", source1));
+
+        verify(dest).receive(same(source3));
+    }
+}