cc663438a2b5853727b6af25dab2c7a3607a383e
[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.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("app", 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         when(type.getId()).thenReturn(new Id<DocumentType>(aType));
66         RouterConfig routerConfig = configService.emptyConfig("app");
67         routerConfig.documentTypeConfig().add(type);
68         return routerConfig;
69     }
70
71     @Test
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);
81
82         configService.apply(routerConfig, null);
83
84         Destination destination = mock(Destination.class);
85         when(
86             destination.chooseFromTargetTypes((Collection<String>) anyObject()))
87             .thenReturn(Arrays.asList("bla"));
88
89         routerService.registerDestination(destination);
90
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));
95
96         verify(transformation).transform(source1);
97         verify(destination).receive(same(source2));
98
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));
103     }
104 }