+/*
+ * Copyright 2005-2011 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.xmlrouter.impl;
+
+import static org.mockito.Matchers.*;
+import static org.mockito.Mockito.*;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.logging.Level;
+
+import javax.xml.transform.dom.DOMSource;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.wamblee.general.SystemClock;
+import org.wamblee.xmlrouter.config.DocumentType;
+import org.wamblee.xmlrouter.config.RouterConfig;
+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;
+
+public class XMLRouterFunctionTest {
+
+ private XMLRouterConfiguration config;
+ private XMLRouter routerService;
+ private XMLRouterConfigService configService;
+
+ private EventListener listener;
+
+ private DOMSource source1;
+ private DOMSource source2;
+
+ @Before
+ public void setUp() {
+ config = new XMLRouterConfigurationImpl();
+ EventListener logListener = new LoggingEventListener(Level.INFO);
+ listener = spy(logListener);
+ routerService = new XMLRouter(new SystemClock(), config, listener);
+ configService = new XMLRouterConfigService(config);
+
+ source1 = mock(DOMSource.class);
+ source2 = mock(DOMSource.class);
+ }
+
+ private RouterConfig registerDocumentType(String aType) {
+ DocumentType type = mock(DocumentType.class);
+ when(type.isInstance(any(DOMSource.class))).thenReturn(true);
+ when(type.getName()).thenReturn(aType);
+ RouterConfig routerConfig = configService.emptyConfig();
+ routerConfig.documentTypeConfig().add(type);
+ return routerConfig;
+ }
+
+ @Test
+ public void testOneTransformationOneDestination() {
+ RouterConfig routerConfig = registerDocumentType("any");
+ Transformation transformation = mock(Transformation.class);
+ when(transformation.getName()).thenReturn("trans");
+ when(transformation.getFromType()).thenReturn("any");
+ when(transformation.getToType()).thenReturn("bla");
+ when(transformation.transform(same(source1))).thenReturn(source2);
+ routerConfig.transformationConfig().add(transformation);
+
+ configService.apply(routerConfig, null);
+
+ Destination destination = mock(Destination.class);
+ when(
+ destination.chooseFromTargetTypes((Collection<String>) anyObject()))
+ .thenReturn(Arrays.asList("bla"));
+
+ routerService.registerDestination(destination);
+
+ when(destination.receive(any(DOMSource.class))).thenReturn(true);
+ routerService.publish("bla", source1);
+ verify(listener).delivered(any(EventInfo.class),
+ anyListOf(Transformation.class), anyLong(), 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);
+ routerService.publish("bla", source1);
+ verify(listener).notDelivered(any(EventInfo.class));
+ }
+}