46e36c0c4bdb4c86a67f07c067e512b889a35151
[xmlrouter] / impl / src / test / java / org / wamblee / xmlrouter / impl / XMLRouterFunctionTest.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.xmlrouter.impl;
17
18 import static org.mockito.Matchers.*;
19 import static org.mockito.Mockito.*;
20
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.logging.Level;
24
25 import javax.xml.transform.dom.DOMSource;
26
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.wamblee.general.SystemClock;
30 import org.wamblee.xmlrouter.config.DocumentType;
31 import org.wamblee.xmlrouter.config.RouterConfig;
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;
37
38 public class XMLRouterFunctionTest {
39
40     private XMLRouterConfiguration config;
41     private XMLRouter routerService;
42     private XMLRouterConfigService configService;
43
44     private EventListener listener;
45
46     private DOMSource source1;
47     private DOMSource source2;
48
49     @Before
50     public void setUp() {
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(config);
56
57         source1 = mock(DOMSource.class);
58         source2 = mock(DOMSource.class);
59     }
60
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         RouterConfig routerConfig = configService.emptyConfig();
66         routerConfig.documentTypeConfig().add(type);
67         return routerConfig;
68     }
69
70     @Test
71     public void testOneTransformationOneDestination() {
72         RouterConfig routerConfig = registerDocumentType("any");
73         Transformation transformation = mock(Transformation.class);
74         when(transformation.getName()).thenReturn("trans");
75         when(transformation.getFromType()).thenReturn("any");
76         when(transformation.getToType()).thenReturn("bla");
77         when(transformation.transform(same(source1))).thenReturn(source2);
78         routerConfig.transformationConfig().add(transformation);
79
80         configService.apply(routerConfig, null);
81
82         Destination destination = mock(Destination.class);
83         when(
84             destination.chooseFromTargetTypes((Collection<String>) anyObject()))
85             .thenReturn(Arrays.asList("bla"));
86
87         routerService.registerDestination(destination);
88
89         when(destination.receive(any(DOMSource.class))).thenReturn(true);
90         routerService.publish("bla", source1);
91         verify(listener).delivered(any(EventInfo.class),
92             anyListOf(Transformation.class), anyLong(), anyString(), eq(true));
93
94         verify(transformation).transform(source1);
95         verify(destination).receive(same(source2));
96
97         // now the same when the destination rejects the event.
98         when(destination.receive(any(DOMSource.class))).thenReturn(false);
99         routerService.publish("bla", source1);
100         verify(listener).notDelivered(any(EventInfo.class));
101     }
102 }