refactoring

Change-Id: I631ff6bfc2884287a8e629196b3810086bbd21cf
diff --git a/src/main/java/de/ids_mannheim/korap/config/BeanConfiguration.java b/src/main/java/de/ids_mannheim/korap/config/BeanConfiguration.java
index 8081e5d..4d777b0 100644
--- a/src/main/java/de/ids_mannheim/korap/config/BeanConfiguration.java
+++ b/src/main/java/de/ids_mannheim/korap/config/BeanConfiguration.java
@@ -1,6 +1,5 @@
 package de.ids_mannheim.korap.config;
 
-import de.ids_mannheim.korap.interfaces.AuthenticationIface;
 import de.ids_mannheim.korap.interfaces.AuthenticationManagerIface;
 import de.ids_mannheim.korap.interfaces.EncryptionIface;
 import de.ids_mannheim.korap.interfaces.db.*;
diff --git a/src/main/java/de/ids_mannheim/korap/exceptions/KustvaktException.java b/src/main/java/de/ids_mannheim/korap/exceptions/KustvaktException.java
index 68a3018..8e4148c 100644
--- a/src/main/java/de/ids_mannheim/korap/exceptions/KustvaktException.java
+++ b/src/main/java/de/ids_mannheim/korap/exceptions/KustvaktException.java
@@ -67,8 +67,8 @@
     }
 
 
-    @Override
-    public String toString () {
+
+    public String string () {
         return "Excpt{" + "status=" + getStatusCode() + ", message="
                 + getMessage() + ", args=" + getEntity() + ", userid=" + userid
                 + '}';
diff --git a/src/main/java/de/ids_mannheim/korap/exceptions/WrappedException.java b/src/main/java/de/ids_mannheim/korap/exceptions/WrappedException.java
index 60c0715..3e067a7 100644
--- a/src/main/java/de/ids_mannheim/korap/exceptions/WrappedException.java
+++ b/src/main/java/de/ids_mannheim/korap/exceptions/WrappedException.java
@@ -30,7 +30,7 @@
         this(e.getUserid(), e.getStatusCode(), e.getMessage(), e.getEntity());
         AuditRecord record = AuditRecord.serviceRecord(e.getUserid(), status,
                 args);
-        record.setField_1(e.toString());
+        record.setField_1(e.string());
         this.records.addAll(e.getRecords());
         this.records.add(record);
     }
diff --git a/src/main/java/de/ids_mannheim/korap/exceptions/dbException.java b/src/main/java/de/ids_mannheim/korap/exceptions/dbException.java
index f6fdf23..076797b 100644
--- a/src/main/java/de/ids_mannheim/korap/exceptions/dbException.java
+++ b/src/main/java/de/ids_mannheim/korap/exceptions/dbException.java
@@ -31,14 +31,14 @@
     public dbException (KustvaktException e, Integer status, String ... args) {
         this(e.getUserid(), e.getStatusCode(), e.getMessage(), e.getEntity());
         AuditRecord record = AuditRecord.dbRecord(e.getUserid(), status, args);
-        record.setField_1(e.toString());
+        record.setField_1(e.string());
         this.records.addAll(e.getRecords());
         this.records.add(record);
     }
 
 
     @Override
-    public String toString () {
+    public String string () {
         return "DBExcpt{" + "status=" + getStatusCode() + ", message="
                 + getMessage() + ", args=" + getEntity() + ", userid="
                 + this.getUserid() + '}';
diff --git a/src/main/java/de/ids_mannheim/korap/security/ac/ConditionManagement.java b/src/main/java/de/ids_mannheim/korap/security/ac/ConditionManagement.java
index d0db4fa..bda8260 100644
--- a/src/main/java/de/ids_mannheim/korap/security/ac/ConditionManagement.java
+++ b/src/main/java/de/ids_mannheim/korap/security/ac/ConditionManagement.java
@@ -1,6 +1,6 @@
 package de.ids_mannheim.korap.security.ac;
 
-import de.ids_mannheim.korap.config.BeanConfiguration;
+import de.ids_mannheim.korap.config.BeansFactory;
 import de.ids_mannheim.korap.exceptions.EmptyResultException;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.exceptions.NotAuthorizedException;
@@ -28,7 +28,8 @@
 
     public ConditionManagement (User user) {
         this.user = user;
-        this.policydao = BeanConfiguration.getBeans().getPolicyDbProvider();
+        this.policydao = BeansFactory.getKustvaktContext()
+                .getPolicyDbProvider();
 
     }
 
diff --git a/src/main/java/de/ids_mannheim/korap/security/ac/SecurityManager.java b/src/main/java/de/ids_mannheim/korap/security/ac/SecurityManager.java
index 059d33b..0fc0863 100644
--- a/src/main/java/de/ids_mannheim/korap/security/ac/SecurityManager.java
+++ b/src/main/java/de/ids_mannheim/korap/security/ac/SecurityManager.java
@@ -58,13 +58,11 @@
         this.policies[0] = new ArrayList<>();
         this.silent = true;
         this.user = user;
-        overrideProviders(null);
+        overrideProviders(BeansFactory.getKustvaktContext());
     }
 
 
     public static void overrideProviders (ContextHolder beans) {
-        if (beans == null)
-            beans = BeansFactory.getKustvaktContext();
         if (policydao == null | crypto == null) {
             SecurityManager.policydao = beans.getPolicyDbProvider();
             SecurityManager.crypto = beans.getEncryption();
diff --git a/src/main/java/de/ids_mannheim/korap/security/auth/KustvaktAuthenticationManager.java b/src/main/java/de/ids_mannheim/korap/security/auth/KustvaktAuthenticationManager.java
index 918650a..8decf1d 100644
--- a/src/main/java/de/ids_mannheim/korap/security/auth/KustvaktAuthenticationManager.java
+++ b/src/main/java/de/ids_mannheim/korap/security/auth/KustvaktAuthenticationManager.java
@@ -761,7 +761,7 @@
             return data;
         }
         catch (KustvaktException e) {
-            jlog.error("Error during user data retrieval!", e.getEntity());
+            jlog.error("Error during user data retrieval: {}", e.getEntity());
             throw new WrappedException(e, StatusCodes.GET_ACCOUNT_FAILED);
         }
     }
diff --git a/src/main/java/de/ids_mannheim/korap/utils/KoralCollectionQueryBuilder.java b/src/main/java/de/ids_mannheim/korap/utils/KoralCollectionQueryBuilder.java
index a559415..811c227 100644
--- a/src/main/java/de/ids_mannheim/korap/utils/KoralCollectionQueryBuilder.java
+++ b/src/main/java/de/ids_mannheim/korap/utils/KoralCollectionQueryBuilder.java
@@ -191,8 +191,6 @@
 
 
         public static ObjectNode buildDocGroup (String op, JsonNode ... groups) {
-            System.out.println("GROUPS " + Arrays.asList(groups));
-
             ObjectNode node = JsonUtils.createObjectNode();
             node.put("@type", "koral:docGroup");
             node.put("operation", "operation:" + op);
diff --git a/src/main/java/de/ids_mannheim/korap/web/KustvaktBaseServer.java b/src/main/java/de/ids_mannheim/korap/web/KustvaktBaseServer.java
index 8dd27e3..48d5030 100644
--- a/src/main/java/de/ids_mannheim/korap/web/KustvaktBaseServer.java
+++ b/src/main/java/de/ids_mannheim/korap/web/KustvaktBaseServer.java
@@ -31,8 +31,7 @@
         KustvaktConfiguration.loadLog4jLogger();
         KustvaktBaseServer server = new KustvaktBaseServer() {
             @Override
-            protected void setup() {
-            }
+            protected void setup () {}
         };
         KustvaktArgs kargs = server.readAttributes(args);
 
@@ -78,7 +77,9 @@
         return kargs;
     }
 
-    protected abstract void setup();
+
+    protected abstract void setup ();
+
 
     protected void startServer (KustvaktArgs kargs) {
         if (kargs.init)
@@ -125,7 +126,7 @@
             server.join();
         }
         catch (Exception e) {
-            e.printStackTrace();
+            System.out.println("Server could not be started!");
             System.exit(-1);
         }
 
diff --git a/src/main/java/de/ids_mannheim/korap/web/service/full/AuthService.java b/src/main/java/de/ids_mannheim/korap/web/service/full/AuthService.java
index 6d2e309..6f4afe0 100644
--- a/src/main/java/de/ids_mannheim/korap/web/service/full/AuthService.java
+++ b/src/main/java/de/ids_mannheim/korap/web/service/full/AuthService.java
@@ -245,7 +245,7 @@
             controller.logout(context);
         }
         catch (KustvaktException e) {
-            jlog.error("Logout Exception", e);
+            jlog.error("Logout Exception: {}", e.string());
             throw KustvaktResponseHandler.throwit(e);
         }
         return Response.ok().build();
diff --git a/src/main/java/de/ids_mannheim/korap/web/service/full/ResourceService.java b/src/main/java/de/ids_mannheim/korap/web/service/full/ResourceService.java
index c77d409..2bced4f 100644
--- a/src/main/java/de/ids_mannheim/korap/web/service/full/ResourceService.java
+++ b/src/main/java/de/ids_mannheim/korap/web/service/full/ResourceService.java
@@ -379,7 +379,7 @@
         }
         catch (KustvaktException e) {
             //todo: instead of throwing exception, build notification and rewrites into result query
-            jlog.error("Exception encountered!");
+            jlog.error("Exception encountered: {}", e.string());
             throw KustvaktResponseHandler.throwit(e);
         }
 
@@ -440,7 +440,7 @@
             user = controller.getUser(context.getUsername());
         }
         catch (KustvaktException e) {
-            jlog.error("Exception encountered!", e);
+            jlog.error("Exception encountered: {}", e.string());
             throw KustvaktResponseHandler.throwit(e);
         }
         String result;
@@ -483,6 +483,7 @@
                 result = this.graphDBhandler.getResponse(map, "distKwic");
             }
             catch (KustvaktException e) {
+                jlog.error("Exception encountered: {}", e.string());
                 throw KustvaktResponseHandler.throwit(e);
             }
         }
@@ -582,8 +583,8 @@
                     MultivaluedMap map = new MultivaluedMapImpl();
                     map.add("q", query);
                     map.add("count", String.valueOf(pageLength));
-                    map.add("lctxs", String.valueOf(meta.getSpanContext()
-                            .getLeftSize()));
+                    map.add("lctxs",
+                            String.valueOf(meta.getSpanContext().getLeftSize()));
                     map.add("rctxs", String.valueOf(meta.getSpanContext()
                             .getRightSize()));
                     result = this.graphDBhandler.getResponse(map, "distKwic");
@@ -602,7 +603,7 @@
             return Response.ok(result).build();
         }
         catch (KustvaktException e) {
-            jlog.error("Exception encountered!", e);
+            jlog.error("Exception encountered: {}", e.string());
             throw KustvaktResponseHandler.throwit(e);
         }
 
@@ -671,6 +672,7 @@
             return Response.ok(searchKrill.getStatistics(qstr)).build();
         }
         catch (KustvaktException e) {
+            jlog.error("Exception encountered: {}", e.string());
             throw KustvaktResponseHandler.throwit(e);
         }
     }
@@ -694,6 +696,7 @@
             user = controller.getUser(c.getUsername());
         }
         catch (KustvaktException e) {
+            jlog.error("Exception encountered: {}", e.string());
             throw KustvaktResponseHandler.throwit(e);
         }
 
@@ -748,7 +751,7 @@
             this.resourceHandler.updateResources(user, resource);
         }
         catch (KustvaktException e) {
-            jlog.error("Exception encountered!", e);
+            jlog.error("Exception encountered: {}", e.string());
             throw KustvaktResponseHandler.throwit(e);
         }
         return Response.ok().build();
@@ -777,7 +780,7 @@
             user = controller.getUser(ctx.getUsername());
         }
         catch (KustvaktException e) {
-            jlog.error("Exception encountered!", e);
+            jlog.error("Exception encountered: {}", e.string());
             throw KustvaktResponseHandler.throwit(e);
         }
         if (VirtualCollection.class.equals(ResourceFactory
@@ -823,7 +826,7 @@
                     resourceHandler.storeResources(user, collection);
                 }
                 catch (KustvaktException e) {
-                    jlog.error("Exception encountered!", e);
+                    jlog.error("Exception encountered: {}", e.string());
                     throw KustvaktResponseHandler.throwit(e);
                 }
             }
@@ -866,7 +869,7 @@
             user = controller.getUser(ctx.getUsername());
         }
         catch (KustvaktException e) {
-            jlog.error("Exception encountered!", e);
+            jlog.error("Exception encountered: {}", e.string());
             throw KustvaktResponseHandler.throwit(e);
         }
         if (VirtualCollection.class.equals(ResourceFactory
@@ -908,7 +911,7 @@
                     resourceHandler.storeResources(user, collection);
                 }
                 catch (KustvaktException e) {
-                    jlog.error("Exception encountered!", e);
+                    jlog.error("Exception encountered: {}", e.string());
                     throw KustvaktResponseHandler.throwit(e);
                 }
             }
@@ -947,7 +950,7 @@
             resourceHandler.deleteResources(user, r);
         }
         catch (KustvaktException e) {
-            jlog.error("Exception encountered!", e);
+            jlog.error("Exception encountered: {}", e.string());
             throw KustvaktResponseHandler.throwit(e);
         }
 
@@ -978,7 +981,7 @@
             user = controller.getUser(tokenContext.getUsername());
         }
         catch (KustvaktException e) {
-            jlog.error("Exception encountered!", e);
+            jlog.error("Exception encountered: {}", e.string());
             throw KustvaktResponseHandler.throwit(e);
         }
 
@@ -1026,7 +1029,7 @@
                 resources = ResourceFinder.search(user, Layer.class);
             }
             catch (KustvaktException e) {
-                jlog.error("Exception encountered!", e);
+                jlog.error("Exception encountered: {}", e.string());
                 throw KustvaktResponseHandler.throwit(e);
             }
             // returns foundries and layers.
@@ -1104,6 +1107,7 @@
                     this.controller.getUser(ctx.getUsername()), id);
         }
         catch (KustvaktException e) {
+            jlog.error("Exception encountered: {}", e.string());
             throw KustvaktResponseHandler.throwit(e);
         }
 
diff --git a/src/main/java/de/ids_mannheim/korap/web/service/full/UserService.java b/src/main/java/de/ids_mannheim/korap/web/service/full/UserService.java
index 5aa25c9..a0b2846 100644
--- a/src/main/java/de/ids_mannheim/korap/web/service/full/UserService.java
+++ b/src/main/java/de/ids_mannheim/korap/web/service/full/UserService.java
@@ -311,7 +311,7 @@
             result = data.data();
         }
         catch (KustvaktException e) {
-            jlog.error("Exception encountered!", e);
+            jlog.error("Exception encountered: {}", e.string());
             throw KustvaktResponseHandler.throwit(e);
         }
         return Response.ok(result).build();
diff --git a/src/test/java/de/ids_mannheim/korap/config/TestHelper.java b/src/test/java/de/ids_mannheim/korap/config/TestHelper.java
index 521a049..53404b6 100644
--- a/src/test/java/de/ids_mannheim/korap/config/TestHelper.java
+++ b/src/test/java/de/ids_mannheim/korap/config/TestHelper.java
@@ -1,5 +1,6 @@
 package de.ids_mannheim.korap.config;
 
+import de.ids_mannheim.korap.exceptions.EmptyResultException;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.handlers.*;
 import de.ids_mannheim.korap.interfaces.AuthenticationIface;
@@ -35,8 +36,7 @@
 import java.sql.SQLException;
 import java.util.*;
 
-import static org.junit.Assert.assertNotEquals;
-import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.*;
 
 /**
  * creates a test user that can be used to access protected functions
@@ -118,34 +118,42 @@
 
     public TestHelper setupSimpleAccount (String username, String password) {
         KustvaktBaseDaoInterface dao = getBean(ContextHolder.KUSTVAKT_USERDB);
+        EntityHandlerIface edao = (EntityHandlerIface) dao;
+        try {
+            edao.getAccount(username);
+        }
+        catch (EmptyResultException e) {
+            // do nothing
+        }
+        catch (KustvaktException ex) {
+            assertNull("Test user could not be set up", true);
+        }
 
-        if (dao.size() == 0) {
-            Map m = new HashMap<>();
-            m.put(Attributes.USERNAME, username);
+        Map m = new HashMap<>();
+        m.put(Attributes.USERNAME, username);
 
-            try {
-                String hash = ((EncryptionIface) getBean(ContextHolder.KUSTVAKT_ENCRYPTION))
-                        .secureHash(password);
-                m.put(Attributes.PASSWORD, hash);
-            }
-            catch (NoSuchAlgorithmException | UnsupportedEncodingException
-                    | KustvaktException e) {
-                // do nohting
-                assertNotNull("Exception thrown", null);
-            }
-            assertNotNull("userdatabase handler must not be null", dao);
+        try {
+            String hash = ((EncryptionIface) getBean(ContextHolder.KUSTVAKT_ENCRYPTION))
+                    .secureHash(password);
+            m.put(Attributes.PASSWORD, hash);
+        }
+        catch (NoSuchAlgorithmException | UnsupportedEncodingException
+                | KustvaktException e) {
+            // do nohting
+            assertNotNull("Exception thrown", null);
+        }
+        assertNotNull("userdatabase handler must not be null", dao);
 
-            try {
-                EntityHandlerIface edao = (EntityHandlerIface) dao;
-                int i = edao.createAccount(User.UserFactory.toKorAPUser(m));
-                assert BeansFactory.getKustvaktContext().getUserDBHandler()
-                        .getAccount(credentials[0]) != null;
-                assert i == 1;
-            }
-            catch (KustvaktException e) {
-                // do nothing
-                Assert.assertNull("Test user could not be set up", true);
-            }
+        try {
+
+            int i = edao.createAccount(User.UserFactory.toKorAPUser(m));
+            assert BeansFactory.getKustvaktContext().getUserDBHandler()
+                    .getAccount(credentials[0]) != null;
+            assertEquals(1, i);
+        }
+        catch (KustvaktException e) {
+            // do nothing
+            assertNull("Test user could not be set up", true);
         }
         return this;
     }
diff --git a/src/test/java/de/ids_mannheim/korap/web/service/FastJerseyTest.java b/src/test/java/de/ids_mannheim/korap/web/service/FastJerseyTest.java
index b65a73f..3d36b6f 100644
--- a/src/test/java/de/ids_mannheim/korap/web/service/FastJerseyTest.java
+++ b/src/test/java/de/ids_mannheim/korap/web/service/FastJerseyTest.java
@@ -122,7 +122,7 @@
                 testContainer.start();
             }
         }
-        catch (TestContainerException e) {
+        catch (Exception e) {
             initServer(PORT + PORT_IT++);
             startServer();
         }
diff --git a/src/test/java/de/ids_mannheim/korap/web/service/full/AuthServiceTest.java b/src/test/java/de/ids_mannheim/korap/web/service/full/AuthServiceTest.java
index 47771b1..b41ecd3 100644
--- a/src/test/java/de/ids_mannheim/korap/web/service/full/AuthServiceTest.java
+++ b/src/test/java/de/ids_mannheim/korap/web/service/full/AuthServiceTest.java
@@ -31,6 +31,7 @@
     public void testBasicHttp () {
         User user = helper().getUser();
 
+
     }