Fix handling non-existent large context group.
Large context group is optional. For instance, it is not required at the
DNB instance.
Change-Id: I7922349cdb8a53fcb95d1e3f01d2b6b9a3c62165
diff --git a/Changes b/Changes
index f32bce1..fdbc55a 100644
--- a/Changes
+++ b/Changes
@@ -2,6 +2,7 @@
- Add a large-context group allowing users to access larger context by request [AI-assisted, #745]
- Apply new kwic and shrinking properties for kustvakt-dnb
+- Fix handling non-existent large context group
# version 1.2-SNAPSHOT
diff --git a/src/main/java/de/ids_mannheim/korap/rewrite/QueryContextRewrite.java b/src/main/java/de/ids_mannheim/korap/rewrite/QueryContextRewrite.java
index 9f5088a..470368d 100644
--- a/src/main/java/de/ids_mannheim/korap/rewrite/QueryContextRewrite.java
+++ b/src/main/java/de/ids_mannheim/korap/rewrite/QueryContextRewrite.java
@@ -10,6 +10,7 @@
import de.ids_mannheim.korap.dao.UserGroupDao;
import de.ids_mannheim.korap.entity.UserGroup;
import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.exceptions.StatusCodes;
import de.ids_mannheim.korap.service.UserGroupService;
import de.ids_mannheim.korap.user.User;
import de.ids_mannheim.korap.utils.JsonUtils;
@@ -45,10 +46,17 @@
private boolean isInLargeContextGroup (User user)
throws KustvaktException {
if (user == null) return false;
- UserGroup group = userGroupDao.retrieveGroupByName(LARGE_CONTEXT_GROUP,
+ try {
+ UserGroup group = userGroupDao.retrieveGroupByName(LARGE_CONTEXT_GROUP,
false);
- if (group == null) return false;
- return userGroupService.isMember(user.getUsername(), group);
+ return userGroupService.isMember(user.getUsername(), group);
+
+ }catch (KustvaktException e) {
+ if (e.getStatusCode() == StatusCodes.NO_RESOURCE_FOUND) {
+ return false;
+ }
+ throw e;
+ }
}
private boolean cutContext (KoralNode context, String position,
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 03bcb5c..3595049 100644
--- a/src/test/java/de/ids_mannheim/korap/scenario/DNBTest.java
+++ b/src/test/java/de/ids_mannheim/korap/scenario/DNBTest.java
@@ -3,7 +3,9 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.parallel.Isolated;
import org.springframework.test.context.ContextConfiguration;
import com.fasterxml.jackson.databind.JsonNode;
@@ -15,13 +17,14 @@
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
+@Isolated
@ContextConfiguration(
locations = "classpath:test-config-dnb.xml",
inheritLocations = false
)
public class DNBTest extends SpringJerseyTest {
- public final static String API_VERSION = "v1.0";
+ public final static String API_VERSION = "v1.1";
private JsonNode sendQuery (String query) throws KustvaktException {
Response r = target().path(API_VERSION).path("search")
@@ -35,24 +38,28 @@
}
+ @BeforeAll
+ public static void loadDnbProperties() {
+ KrillProperties.loadProperties("kustvakt-dnb.conf");
+ }
+
@AfterAll
public static void resetKrillProperties() {
KrillProperties.loadProperties("kustvakt-test.conf");
}
- @Test
- public void testTokenMatchSize () throws KustvaktException {
- assertEquals(1, KrillProperties.maxTokenMatchSize);
- assertEquals(25, KrillProperties.maxTokenContextSize);
+ @Test
+ public void testTokenMatchSize () throws KustvaktException {
+ assertEquals(12, KrillProperties.maxTokenMatchSize);
+ assertEquals(25, KrillProperties.maxTokenContextSize);
- JsonNode node = sendQuery("[orth=das]");
- assertEquals(KrillProperties.maxTokenMatchSize,
- node.at("/matches/0/tokens/match").size());
+ JsonNode node = sendQuery("[orth=das]");
+ assertEquals(1, node.at("/matches/0/tokens/match").size());
- node = sendQuery("[orth=das][orth=Glück]");
- assertEquals(KrillProperties.maxTokenMatchSize,
- node.at("/matches/0/tokens/match").size());
- }
+ node = sendQuery("[orth=das][orth=Glück]");
+ assertEquals(6, node.at("/matches/0/context/left/1").asInt());
+ assertEquals(6, node.at("/matches/0/context/right/1").asInt());
+ }
@Test
public void testTokenContextMatchSize () throws KustvaktException {
@@ -62,6 +69,7 @@
.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,
@@ -69,13 +77,11 @@
assertEquals(KrillProperties.maxTokenContextSize,
node.at("/meta/context/right/1").asInt());
- assertEquals(KrillProperties.maxTokenContextSize,
+ assertEquals(24, // using kwic
node.at("/matches/0/tokens/left").size());
- // There is a bug in Krill (https://github.com/KorAP/Krill/issues/141)
- // So the following test fails
-// assertEquals(KrillProperties.maxTokenContextSize,
-// node.at("/matches/0/tokens/right").size());
+ assertEquals(24,
+ node.at("/matches/0/tokens/right").size());
}
}
diff --git a/src/test/resources/kustvakt-dnb.conf b/src/test/resources/kustvakt-dnb.conf
index c807080..a7b338e 100644
--- a/src/test/resources/kustvakt-dnb.conf
+++ b/src/test/resources/kustvakt-dnb.conf
@@ -27,8 +27,8 @@
# Kustvakt versions
#
# multiple versions comma separated
-current.api.version = v1.0
-supported.api.versions = v1.0
+current.api.version = v1.1
+supported.api.versions = v1.0, v1.1
# Server
#
diff --git a/src/test/resources/test-config-dnb.xml b/src/test/resources/test-config-dnb.xml
index 138d52e..9ef746d 100644
--- a/src/test/resources/test-config-dnb.xml
+++ b/src/test/resources/test-config-dnb.xml
@@ -159,9 +159,8 @@
<!-- <bean id="initializator"
class="de.ids_mannheim.korap.init.Initializator"
- init-method="initTestWithoutAvailability">
- </bean>
- -->
+ init-method="initTest">
+ </bean> -->
<!-- Krill -->
<bean id="search_krill"