XMLRouterConfigService test added.
authorErik Brakkee <erik@brakkee.org>
Mon, 8 Aug 2011 20:57:25 +0000 (22:57 +0200)
committerErik Brakkee <erik@brakkee.org>
Mon, 8 Aug 2011 20:57:25 +0000 (22:57 +0200)
impl/src/main/java/org/wamblee/xmlrouter/impl/SingleRouterConfig.java
impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouterConfigService.java
impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterConfigServiceTest.java [new file with mode: 0644]
impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterFunctionTest.java

index 1e9dd86afb2afdf417db64e3d12063e7a2a76677..f0bf090d2fa1b9f49afe7352e5ce7f77e5db0ef0 100644 (file)
@@ -40,27 +40,28 @@ public class SingleRouterConfig implements ExtendedRouterConfig {
     /**
      * Constructs a router configuration.
      * 
-     * @param aSequenceNumberGenerator
-     *            Sequence number generator to use.
+     * @param aId
+     *            Unique id for this configuration.
      */
     public SingleRouterConfig(Id<RouterConfig> aId) {
         id = aId;
         documentTypes = new ConfigImpl<DocumentType>(new Id<Config>(
-            "documentTypes")) {
+            aId.getId() + ".documenttypes")) {
             @Override
             public DocumentType wrap(String aPrefix, DocumentType aT) {
                 return new RobustDocumentType(aPrefix, aT);
             }
         };
         transformations = new ConfigImpl<Transformation>(new Id<Config>(
-            "transformations")) {
+            aId.getId() + ".transformations")) {
             @Override
             public Transformation wrap(String aPrefix,
                 Transformation aTransformation) {
                 return new RobustTransformation(aPrefix, aTransformation);
             }
         };
-        filters = new ConfigImpl<Filter>(new Id<Config>("filters")) {
+        filters = new ConfigImpl<Filter>(new Id<Config>(aId.getId() +
+            ".filters")) {
             @Override
             public Filter wrap(String aPrefix, Filter aFilter) {
                 return new RobustFilter(aPrefix, aFilter);
index 203e56fd5d0e1632cf65ba6b5ec40da1853541fb..cd1997299d5e631f055fbda2060e0a56f9000c99 100644 (file)
@@ -19,8 +19,12 @@ import java.util.concurrent.atomic.AtomicLong;
 
 import org.wamblee.xmlrouter.common.Id;
 import org.wamblee.xmlrouter.config.Config;
+import org.wamblee.xmlrouter.config.ConfigException;
+import org.wamblee.xmlrouter.config.DocumentType;
+import org.wamblee.xmlrouter.config.Filter;
 import org.wamblee.xmlrouter.config.RouterConfig;
 import org.wamblee.xmlrouter.config.RouterConfigService;
+import org.wamblee.xmlrouter.config.Transformation;
 
 // TODO pass unique id of config service in the constructor to allow multiple instances with
 // unique ids (ServiceFactory) 
@@ -33,30 +37,58 @@ import org.wamblee.xmlrouter.config.RouterConfigService;
  */
 public class XMLRouterConfigService implements RouterConfigService {
 
+    private String application;
     private AtomicLong sequence;
     private XMLRouterConfiguration config;
     private Config<RouterConfig> routerConfigs;
 
-    public XMLRouterConfigService(XMLRouterConfiguration aConfig) {
+    public XMLRouterConfigService(String aApplication,
+        XMLRouterConfiguration aConfig) {
+        application = aApplication;
         sequence = new AtomicLong(1L);
         config = aConfig;
-        routerConfigs = new ConfigImpl<RouterConfig>(new Id<Config>("config")) {
-            public RouterConfig wrap(String aPrefix, RouterConfig aT) {
-                return aT;
+        routerConfigs = new ConfigImpl<RouterConfig>(new Id<Config>(
+            aApplication)) {
+            public RouterConfig wrap(final String aPrefix, final RouterConfig aT) {
+                return new RouterConfig() {
+                    @Override
+                    public Id<RouterConfig> getId() {
+                        return new Id<RouterConfig>(aT.getId().toString());
+                    }
+
+                    @Override
+                    public Config<DocumentType> documentTypeConfig() {
+                        return aT.documentTypeConfig();
+                    }
+
+                    @Override
+                    public Config<Filter> filterConfig() {
+                        return aT.filterConfig();
+                    }
+
+                    @Override
+                    public Config<Transformation> transformationConfig() {
+                        return aT.transformationConfig();
+                    }
+                };
             }
         };
     }
 
     @Override
     public RouterConfig emptyConfig(String aId) {
-        // TODO check AP{ impacts.
-        return new SingleRouterConfig(new Id<RouterConfig>(aId));
+        return new SingleRouterConfig(new Id<RouterConfig>(application + "." +
+            aId));
     }
 
     @Override
     public void apply(RouterConfig aConfig, Id<RouterConfig> aOldConfig) {
         if (aOldConfig != null) {
-            routerConfigs.remove(aOldConfig);
+            if (!routerConfigs.remove(new Id<RouterConfig>(application + "." +
+                aOldConfig.getId()))) {
+                throw new ConfigException("Unknown routerconfig '" +
+                    aOldConfig + "'");
+            }
         }
         routerConfigs.add(aConfig);
         update();
@@ -73,5 +105,4 @@ public class XMLRouterConfigService implements RouterConfigService {
             new Id<RouterConfig>("routerconfig"), routerConfigs.values());
         config.setRouterConfig(newconfig);
     }
-
 }
diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterConfigServiceTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterConfigServiceTest.java
new file mode 100644 (file)
index 0000000..4086fa3
--- /dev/null
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2005-2011 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.xmlrouter.impl;
+
+import static junit.framework.Assert.*;
+import static org.mockito.Mockito.*;
+
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.wamblee.xmlrouter.common.Id;
+import org.wamblee.xmlrouter.config.ConfigException;
+import org.wamblee.xmlrouter.config.DocumentType;
+import org.wamblee.xmlrouter.config.Filter;
+import org.wamblee.xmlrouter.config.RouterConfig;
+import org.wamblee.xmlrouter.config.Transformation;
+
+public class XMLRouterConfigServiceTest {
+
+    private XMLRouterConfiguration config;
+    private ArgumentCaptor<ExtendedRouterConfig> captor;
+    private XMLRouterConfigService svc;
+
+    @Before
+    public void setUp() {
+        config = mock(XMLRouterConfiguration.class);
+        captor = ArgumentCaptor.forClass(ExtendedRouterConfig.class);
+        svc = new XMLRouterConfigService("myapp", config);
+    }
+
+    @Test
+    public void testPrefixingOfConfigItems() {
+        RouterConfig routerConfig = createRouterConfig("id", "");
+
+        svc.apply(routerConfig, null);
+
+        verifyResults("");
+    }
+
+    private void verifyResults(String aSuffix) {
+        verify(config).setRouterConfig(captor.capture());
+        RouterConfig received = captor.getValue();
+
+        List<DocumentType> docTypes = received.documentTypeConfig().values();
+        assertEquals(1, docTypes.size());
+        assertEquals("myapp.id.documenttypes.doctype" + aSuffix, docTypes
+            .get(0).getId().toString());
+
+        List<Transformation> transformations = received.transformationConfig()
+            .values();
+        assertEquals(1, transformations.size());
+        assertEquals("myapp.id.transformations.t1" + aSuffix, transformations
+            .get(0).getId().toString());
+
+        List<Filter> filters = received.filterConfig().values();
+        assertEquals(1, filters.size());
+        assertEquals("myapp.id.filters.f1" + aSuffix, filters.get(0).getId()
+            .toString());
+    }
+
+    private RouterConfig createRouterConfig(String routerConfigId,
+        String aSuffix) {
+        RouterConfig routerConfig = svc.emptyConfig(routerConfigId);
+
+        DocumentType type = mock(DocumentType.class);
+        when(type.getName()).thenReturn("doctype" + aSuffix);
+        when(type.getId())
+            .thenReturn(new Id<DocumentType>("doctype" + aSuffix));
+
+        Transformation trans = mock(Transformation.class);
+        when(trans.getId()).thenReturn(new Id<Transformation>("t1" + aSuffix));
+
+        Filter filter = mock(Filter.class);
+        when(filter.getId()).thenReturn(new Id<Filter>("f1" + aSuffix));
+
+        routerConfig.documentTypeConfig().add(type);
+        routerConfig.transformationConfig().add(trans);
+        routerConfig.filterConfig().add(filter);
+        return routerConfig;
+    }
+
+    @Test(expected = ConfigException.class)
+    public void testReplaceWithoutIndicatingOldConfig() {
+        RouterConfig routerConfig1 = createRouterConfig("id", "");
+
+        svc.apply(routerConfig1, null);
+        RouterConfig routerConfig2 = createRouterConfig("id", "");
+
+        svc.apply(routerConfig2, null);
+    }
+
+    @Test
+    public void testReplaceCorrectly() {
+        RouterConfig routerConfig1 = createRouterConfig("id", "");
+
+        svc.apply(routerConfig1, null);
+        RouterConfig routerConfig2 = createRouterConfig("id", "suffix");
+
+        reset(config);
+        svc.apply(routerConfig2, new Id<RouterConfig>("id"));
+
+        verifyResults("suffix");
+    }
+
+}
index bfb2b41129da4d901fb9ecd8cae3c57d079fb9db..d7c504e97555e15f98276deb20dcac9db7f9c61a 100644 (file)
@@ -53,7 +53,7 @@ public class XMLRouterFunctionTest {
         EventListener logListener = new LoggingEventListener(Level.INFO);
         listener = spy(logListener);
         routerService = new XMLRouter(new SystemClock(), config, listener);
-        configService = new XMLRouterConfigService(config);
+        configService = new XMLRouterConfigService("app", config);
 
         source1 = mock(DOMSource.class);
         source2 = mock(DOMSource.class);