d7c504e97555e15f98276deb20dcac9db7f9c61a
[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.common.Id;
31 import org.wamblee.xmlrouter.config.DocumentType;
32 import org.wamblee.xmlrouter.config.RouterConfig;
33 import org.wamblee.xmlrouter.config.Transformation;
34 import org.wamblee.xmlrouter.listener.EventInfo;
35 import org.wamblee.xmlrouter.listener.EventListener;
36 import org.wamblee.xmlrouter.listener.LoggingEventListener;
37 import org.wamblee.xmlrouter.subscribe.Destination;
38
39 public class XMLRouterFunctionTest {
40
41     private XMLRouterConfiguration config;
42     private XMLRouter routerService;
43     private XMLRouterConfigService configService;
44
45     private EventListener listener;
46
47     private DOMSource source1;
48     private DOMSource source2;
49
50     @Before
51     public void setUp() {
52         config = new XMLRouterConfigurationImpl();
53         EventListener logListener = new LoggingEventListener(Level.INFO);
54         listener = spy(logListener);
55         routerService = new XMLRouter(new SystemClock(), config, listener);
56         configService = new XMLRouterConfigService("app", config);
57
58         source1 = mock(DOMSource.class);
59         source2 = mock(DOMSource.class);
60     }
61
62     private RouterConfig registerDocumentType(String aType) {
63         DocumentType type = mock(DocumentType.class);
64         when(type.isInstance(any(DOMSource.class))).thenReturn(true);
65         when(type.getName()).thenReturn(aType);
66         when(type.getId()).thenReturn(new Id<DocumentType>(aType));
67         RouterConfig routerConfig = configService.emptyConfig("app");
68         routerConfig.documentTypeConfig().add(type);
69         return routerConfig;
70     }
71
72     @Test
73     public void testOneTransformationOneDestination() {
74         RouterConfig routerConfig = registerDocumentType("any");
75         Transformation transformation = mock(Transformation.class);
76         when(transformation.getId())
77             .thenReturn(new Id<Transformation>("trans"));
78         when(transformation.getFromType()).thenReturn("any");
79         when(transformation.getToType()).thenReturn("bla");
80         when(transformation.transform(same(source1))).thenReturn(source2);
81         routerConfig.transformationConfig().add(transformation);
82
83         configService.apply(routerConfig, null);
84
85         Destination destination = mock(Destination.class);
86         when(
87             destination.chooseFromTargetTypes((Collection<String>) anyObject()))
88             .thenReturn(Arrays.asList("bla"));
89
90         routerService.registerDestination(destination);
91
92         when(destination.receive(any(DOMSource.class))).thenReturn(true);
93         routerService.publish("bla", source1);
94         verify(listener).delivered(any(EventInfo.class),
95             anyListOf(Transformation.class), anyString(), eq(true));
96
97         verify(transformation).transform(source1);
98         verify(destination).receive(same(source2));
99
100         // now the same when the destination rejects the event.
101         when(destination.receive(any(DOMSource.class))).thenReturn(false);
102         routerService.publish("bla", source1);
103         verify(listener).notDelivered(any(EventInfo.class));
104     }
105 }