XMLRouterConfigService test added.
[xmlrouter] / impl / src / test / java / org / wamblee / xmlrouter / impl / XMLRouterConfigServiceTest.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 junit.framework.Assert.*;
19 import static org.mockito.Mockito.*;
20
21 import java.util.List;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.ArgumentCaptor;
26 import org.wamblee.xmlrouter.common.Id;
27 import org.wamblee.xmlrouter.config.ConfigException;
28 import org.wamblee.xmlrouter.config.DocumentType;
29 import org.wamblee.xmlrouter.config.Filter;
30 import org.wamblee.xmlrouter.config.RouterConfig;
31 import org.wamblee.xmlrouter.config.Transformation;
32
33 public class XMLRouterConfigServiceTest {
34
35     private XMLRouterConfiguration config;
36     private ArgumentCaptor<ExtendedRouterConfig> captor;
37     private XMLRouterConfigService svc;
38
39     @Before
40     public void setUp() {
41         config = mock(XMLRouterConfiguration.class);
42         captor = ArgumentCaptor.forClass(ExtendedRouterConfig.class);
43         svc = new XMLRouterConfigService("myapp", config);
44     }
45
46     @Test
47     public void testPrefixingOfConfigItems() {
48         RouterConfig routerConfig = createRouterConfig("id", "");
49
50         svc.apply(routerConfig, null);
51
52         verifyResults("");
53     }
54
55     private void verifyResults(String aSuffix) {
56         verify(config).setRouterConfig(captor.capture());
57         RouterConfig received = captor.getValue();
58
59         List<DocumentType> docTypes = received.documentTypeConfig().values();
60         assertEquals(1, docTypes.size());
61         assertEquals("myapp.id.documenttypes.doctype" + aSuffix, docTypes
62             .get(0).getId().toString());
63
64         List<Transformation> transformations = received.transformationConfig()
65             .values();
66         assertEquals(1, transformations.size());
67         assertEquals("myapp.id.transformations.t1" + aSuffix, transformations
68             .get(0).getId().toString());
69
70         List<Filter> filters = received.filterConfig().values();
71         assertEquals(1, filters.size());
72         assertEquals("myapp.id.filters.f1" + aSuffix, filters.get(0).getId()
73             .toString());
74     }
75
76     private RouterConfig createRouterConfig(String routerConfigId,
77         String aSuffix) {
78         RouterConfig routerConfig = svc.emptyConfig(routerConfigId);
79
80         DocumentType type = mock(DocumentType.class);
81         when(type.getName()).thenReturn("doctype" + aSuffix);
82         when(type.getId())
83             .thenReturn(new Id<DocumentType>("doctype" + aSuffix));
84
85         Transformation trans = mock(Transformation.class);
86         when(trans.getId()).thenReturn(new Id<Transformation>("t1" + aSuffix));
87
88         Filter filter = mock(Filter.class);
89         when(filter.getId()).thenReturn(new Id<Filter>("f1" + aSuffix));
90
91         routerConfig.documentTypeConfig().add(type);
92         routerConfig.transformationConfig().add(trans);
93         routerConfig.filterConfig().add(filter);
94         return routerConfig;
95     }
96
97     @Test(expected = ConfigException.class)
98     public void testReplaceWithoutIndicatingOldConfig() {
99         RouterConfig routerConfig1 = createRouterConfig("id", "");
100
101         svc.apply(routerConfig1, null);
102         RouterConfig routerConfig2 = createRouterConfig("id", "");
103
104         svc.apply(routerConfig2, null);
105     }
106
107     @Test
108     public void testReplaceCorrectly() {
109         RouterConfig routerConfig1 = createRouterConfig("id", "");
110
111         svc.apply(routerConfig1, null);
112         RouterConfig routerConfig2 = createRouterConfig("id", "suffix");
113
114         reset(config);
115         svc.apply(routerConfig2, new Id<RouterConfig>("id"));
116
117         verifyResults("suffix");
118     }
119
120 }