2 * Copyright 2005-2011 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.xmlrouter.impl;
18 import static org.mockito.Matchers.*;
19 import static org.mockito.Mockito.*;
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.logging.Level;
25 import javax.xml.transform.dom.DOMSource;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.wamblee.general.SystemClock;
30 import org.wamblee.xmlrouter.common.Id;
31 import org.wamblee.xmlrouter.config.DocumentType;
32 import org.wamblee.xmlrouter.config.Transformation;
33 import org.wamblee.xmlrouter.listener.EventInfo;
34 import org.wamblee.xmlrouter.listener.EventListener;
35 import org.wamblee.xmlrouter.listener.LoggingEventListener;
36 import org.wamblee.xmlrouter.subscribe.Destination;
38 public class XMLRouterFunctionTest {
40 private XMLRouterConfiguration config;
41 private XMLRouter routerService;
42 private XMLRouterConfigService configService;
44 private EventListener listener;
46 private DOMSource source1;
47 private DOMSource source2;
51 config = new XMLRouterConfigurationImpl();
52 EventListener logListener = new LoggingEventListener(Level.INFO);
53 listener = spy(logListener);
54 routerService = new XMLRouter(new SystemClock(), config, listener);
55 configService = new XMLRouterConfigService("app", config);
57 source1 = mock(DOMSource.class);
58 source2 = mock(DOMSource.class);
61 private RouterConfig registerDocumentType(String aType) {
62 DocumentType type = mock(DocumentType.class);
63 when(type.isInstance(any(DOMSource.class))).thenReturn(true);
64 when(type.getName()).thenReturn(aType);
65 when(type.getId()).thenReturn(new Id<DocumentType>(aType));
66 RouterConfig routerConfig = configService.emptyConfig("app");
67 routerConfig.documentTypeConfig().add(type);
72 public void testOneTransformationOneDestination() {
73 RouterConfig routerConfig = registerDocumentType("any");
74 Transformation transformation = mock(Transformation.class);
75 when(transformation.getId())
76 .thenReturn(new Id<Transformation>("trans"));
77 when(transformation.getFromType()).thenReturn("any");
78 when(transformation.getToType()).thenReturn("bla");
79 when(transformation.transform(same(source1))).thenReturn(source2);
80 routerConfig.transformationConfig().add(transformation);
82 configService.apply(routerConfig, null);
84 Destination destination = mock(Destination.class);
86 destination.chooseFromTargetTypes((Collection<String>) anyObject()))
87 .thenReturn(Arrays.asList("bla"));
89 routerService.registerDestination(destination);
91 when(destination.receive(any(DOMSource.class))).thenReturn(true);
92 routerService.publish("bla", source1);
93 verify(listener).delivered(any(EventInfo.class),
94 anyListOf(Transformation.class), anyString(), eq(true));
96 verify(transformation).transform(source1);
97 verify(destination).receive(same(source2));
99 // now the same when the destination rejects the event.
100 when(destination.receive(any(DOMSource.class))).thenReturn(false);
101 routerService.publish("bla", source1);
102 verify(listener).notDelivered(any(EventInfo.class));