Add tests for large context group config. [AI-assisted]

large.context.group.enabled = true for kustvakt.conf
large.context.group.enabled = false for kustvakt-dnb.conf

Large context group is generated during initialization and can be
disabled through KustvaktConfiguration. For the DNB instance, the
initialization is omitted so, the group should not be generated. In the
shared test database, the group exists though.

Change-Id: I8b7f76830918b5adb0442b869729cffe2dbce01b
diff --git a/Changes b/Changes
index 7d7df63..48eda79 100644
--- a/Changes
+++ b/Changes
@@ -4,6 +4,7 @@
 - Apply new kwic and shrinking properties for kustvakt-dnb
 - Fix handling non-existent large context group
 - Make large context group optional and disabled by default [AI-assisted]
+- Add tests for large context group config [AI-assisted]
 
 # version 1.2-SNAPSHOT
 
diff --git a/src/test/java/de/ids_mannheim/korap/rewrite/QueryContextRewriteTest.java b/src/test/java/de/ids_mannheim/korap/rewrite/QueryContextRewriteTest.java
index f4f47a6..74cea3f 100644
--- a/src/test/java/de/ids_mannheim/korap/rewrite/QueryContextRewriteTest.java
+++ b/src/test/java/de/ids_mannheim/korap/rewrite/QueryContextRewriteTest.java
@@ -55,10 +55,13 @@
     }
     
     /** AI generated
-     * @throws KustvaktException
+     * 
+     * When large.context.group.enabled = true, a member of the
+     * LargeContextGroup must receive the larger maxTokenContextLarge limit.
+     * @throws KustvaktExceptiona
      */
     @Test
-    public void testCutTokenContextLarge ()
+    public void testLargeContextEnabledForGroupMember ()
             throws KustvaktException {
         // add test user to LargeContextGroup via web service
         Form form = new Form();
@@ -110,6 +113,123 @@
         }
     }
 
+
+
+    /**
+     * When large.context.group.enabled = true, a user who is NOT a
+     * member of the LargeContextGroup must still be capped at the
+     * regular maxTokenContext limit.
+     */
+    @Test
+    public void testLargeContextEnabledForNonMember ()
+            throws KustvaktException {
+        config.setLargeContextGroupEnabled(true);
+        // TEST_USER is not added to the group here
+        Response searchResponse = target().path(API_VERSION).path("search")
+                .queryParam("q", "Sonne")
+                .queryParam("ql", "poliqarp")
+                .queryParam("context", "60-token,60-token")
+                .request()
+                .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
+                        .createBasicAuthorizationHeaderValue(
+                                TEST_USER, "pass"))
+                .get();
+        String ent = searchResponse.readEntity(String.class);
+        JsonNode node = JsonUtils.readTree(ent);
+
+        // non-member must receive the regular limit
+        JsonNode context = node.at("/meta/context");
+        assertEquals(config.getMaxTokenContext(),
+                context.at("/left/1").asInt());
+        assertEquals(config.getMaxTokenContext(),
+                context.at("/right/1").asInt());
+    }
+
+    /**
+     * When large.context.group.enabled = false, a member of the
+     * LargeContextGroup must still be capped at the regular
+     * maxTokenContext limit.
+     */
+    @Test
+    public void testLargeContextDisabledForGroupMember ()
+            throws KustvaktException {
+        // add test user to LargeContextGroup
+        Form form = new Form();
+        form.param("members", TEST_USER);
+        Response addResponse = target().path(API_VERSION).path("group")
+                .path("@" + LARGE_CONTEXT_GROUP).path("member").request()
+                .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
+                        .createBasicAuthorizationHeaderValue(
+                                LARGE_CONTEXT_GROUP_ADMIN, "pass"))
+                .put(Entity.form(form));
+        assertEquals(Status.OK.getStatusCode(), addResponse.getStatus());
+
+        // disable the feature flag
+        config.setLargeContextGroupEnabled(false);
+        try {
+            Response searchResponse = target().path(API_VERSION).path("search")
+                    .queryParam("q", "Sonne")
+                    .queryParam("ql", "poliqarp")
+                    .queryParam("context", "60-token,60-token")
+                    .request()
+                    .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
+                            .createBasicAuthorizationHeaderValue(
+                                    TEST_USER, "pass"))
+                    .get();
+            String ent = searchResponse.readEntity(String.class);
+            JsonNode node = JsonUtils.readTree(ent);
+
+            // must be capped at regular limit, NOT the large limit
+            JsonNode context = node.at("/meta/context");
+            assertEquals(config.getMaxTokenContext(),
+                    context.at("/left/1").asInt());
+            assertEquals(config.getMaxTokenContext(),
+                    context.at("/right/1").asInt());
+        }
+        finally {
+            // restore feature flag
+            config.setLargeContextGroupEnabled(true);
+            // remove test user from group
+            Response deleteResponse = target().path(API_VERSION).path("group")
+                    .path("@" + LARGE_CONTEXT_GROUP)
+                    .path("~" + TEST_USER).request()
+                    .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
+                            .createBasicAuthorizationHeaderValue(
+                                    LARGE_CONTEXT_GROUP_ADMIN, "pass"))
+                    .delete();
+            assertEquals(Status.OK.getStatusCode(), deleteResponse.getStatus());
+        }
+    }
+
+    /**
+     * When large.context.group.enabled = false, an anonymous (null)
+     * user must also be capped at the regular maxTokenContext.
+     */
+    @Test
+    public void testLargeContextDisabledForAnonymousUser ()
+            throws KustvaktException {
+        config.setLargeContextGroupEnabled(false);
+        try {
+            Response response = target().path(API_VERSION).path("search")
+                    .queryParam("q", "Sonne")
+                    .queryParam("ql", "poliqarp")
+                    .queryParam("context", "60-token,60-token")
+                    .request()
+                    .get();
+            String ent = response.readEntity(String.class);
+            JsonNode node = JsonUtils.readTree(ent);
+
+            JsonNode context = node.at("/meta/context");
+            assertEquals(config.getMaxTokenContext(),
+                    context.at("/left/1").asInt());
+            assertEquals(config.getMaxTokenContext(),
+                    context.at("/right/1").asInt());
+        }
+        finally {
+            config.setLargeContextGroupEnabled(true);
+        }
+    }
+
     @Test
     public void testMetaRewrite () throws KustvaktException {
         QuerySerializer s = new QuerySerializer(API_VERSION_DOUBLE);
diff --git a/src/test/java/de/ids_mannheim/korap/scenario/DNBTest.java b/src/test/java/de/ids_mannheim/korap/scenario/DNBTest.java
index 3595049..43517ba 100644
--- a/src/test/java/de/ids_mannheim/korap/scenario/DNBTest.java
+++ b/src/test/java/de/ids_mannheim/korap/scenario/DNBTest.java
@@ -69,7 +69,6 @@
                 .queryParam("context", "30-token,30-token").request().get();
         assertEquals(Status.OK.getStatusCode(), r.getStatus());
         String entity = r.readEntity(String.class);
-        System.out.println(entity);
         JsonNode node = JsonUtils.readTree(entity);
 
         assertEquals(KrillProperties.maxTokenContextSize,