import java.util.Arrays;
import java.util.Collection;
+import java.util.UUID;
import java.util.logging.Level;
import javax.xml.transform.dom.DOMSource;
import org.wamblee.xmlrouter.common.Id;
import org.wamblee.xmlrouter.config.DocumentType;
import org.wamblee.xmlrouter.config.Filter;
+import org.wamblee.xmlrouter.config.RouterConfig;
import org.wamblee.xmlrouter.config.Transformation;
import org.wamblee.xmlrouter.listener.EventInfo;
import org.wamblee.xmlrouter.listener.EventListener;
}
}
+ private ExtendedRouterConfig routerConfig;
+ private XMLRouterConfiguration config;
private XMLRouter router;
private DOMSource source1;
private DOMSource source2;
@Before
public void setUp() {
+ routerConfig = new SingleRouterConfig(new Id<RouterConfig>(
+ "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);
@Test
public void testMisBehavingDocumentType() {
- DocumentType type = mock(DocumentType.class);
+ DocumentType type = mockDocument("docid");
doThrow(new RuntimeException("x")).when(type).isInstance(
any(DOMSource.class));
- router.getDocumentTypeConfig().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<DocumentType>(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.getFilterConfig().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<Filter>(filterId));
+ return filter;
+ }
+
@Test
public void testOneDestinationNoTransformationSuccess() {
destinationSpy = registerDestination(true, "any");
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.
}
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<DocumentType> typeId = router.getDocumentTypeConfig().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<DocumentType> typeId = router.getDocumentTypeConfig().add(type);
+ when(type.getId()).thenReturn(new Id<DocumentType>(aType));
+ routerConfig.documentTypeConfig().add(type);
}
private Destination registerDestination(boolean aResult, String... types) {
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));
.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));
.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));
public void testOneTransformationOneDestination() {
registerDocumentType("any");
Transformation transformation = mock(Transformation.class);
- when(transformation.getName()).thenReturn("trans");
+ when(transformation.getId())
+ .thenReturn(new Id<Transformation>("trans"));
when(transformation.getFromType()).thenReturn("any");
when(transformation.getToType()).thenReturn("bla");
when(transformation.transform(same(source1))).thenReturn(source2);
- router.getTransformationConfig().add(transformation);
+ routerConfig.transformationConfig().add(transformation);
+ config.setRouterConfig(routerConfig);
Destination destination = mock(Destination.class);
when(
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));
public void testMisbehavingTransformationOneDestination() {
registerDocumentType("any");
Transformation transformation = mock(Transformation.class);
- when(transformation.getName()).thenReturn("trans");
+ when(transformation.getId())
+ .thenReturn(new Id<Transformation>("trans"));
when(transformation.getFromType()).thenReturn("any");
when(transformation.getToType()).thenReturn("bla");
doThrow(new RuntimeException("x")).when(transformation).transform(
same(source1));
- router.getTransformationConfig().add(transformation);
+ routerConfig.transformationConfig().add(transformation);
Destination destination = mock(Destination.class);
when(
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<Transformation>("trans"));
when(transformation.getFromType()).thenReturn(aFrom);
when(transformation.getToType()).thenReturn(aTo);
when(transformation.transform(same(aSource))).thenReturn(aTarget);
Transformation transformation = createTransformation("any", "bla",
source1, null);
- router.getTransformationConfig().add(transformation);
-
+ routerConfig.transformationConfig().add(transformation);
+ config.setRouterConfig(routerConfig);
Destination destination = mock(Destination.class);
when(
destination.chooseFromTargetTypes((Collection<String>) anyObject()))
Transformation transformation2 = createTransformation("any", "bla2",
source1, source2);
- router.getTransformationConfig().add(transformation2);
+ routerConfig.transformationConfig().add(transformation2);
+ config.setRouterConfig(routerConfig);
when(
destination.chooseFromTargetTypes((Collection<String>) anyObject()))
.thenReturn(Arrays.asList("bla", "bla2"));
reset(transformation);
- when(transformation.getName()).thenReturn("trans");
+ when(transformation.getId())
+ .thenReturn(new Id<Transformation>("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);
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);
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));
registerDocumentType("other", source2);
Transformation transformation = createTransformation("any", "other",
source1, source2);
- router.getTransformationConfig().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));
Transformation t1 = createTransformation("any", "intermediate",
source1, source2);
- router.getTransformationConfig().add(t1);
+ routerConfig.transformationConfig().add(t1);
Transformation t2 = createTransformation("intermediate", "other",
source2, source3);
- router.getTransformationConfig().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));
}
router.publish("source", source1);
verify(listener).delivered(any(EventInfo.class),
- anyListOf(Transformation.class), anyLong(), anyString(), eq(false));
+ anyListOf(Transformation.class), anyString(), eq(false));
}
}