blob: 5beb40bc5ba9738e1e4081a391d289caf91083a3 [file] [log] [blame]
margaretha5a5b9952023-06-12 12:46:36 +02001package de.ids_mannheim.korap.cache;
2
Marc Kupietzd43a98d2023-09-22 17:11:46 +02003import static org.junit.jupiter.api.Assertions.assertEquals;
4import static org.junit.jupiter.api.Assertions.assertTrue;
margaretha5a5b9952023-06-12 12:46:36 +02005
margaretha5a5b9952023-06-12 12:46:36 +02006
Marc Kupietzd43a98d2023-09-22 17:11:46 +02007import org.junit.jupiter.api.Test;
margaretha5a5b9952023-06-12 12:46:36 +02008import org.springframework.beans.factory.annotation.Autowired;
margaretha5a5b9952023-06-12 12:46:36 +02009import com.fasterxml.jackson.databind.JsonNode;
margaretha5a5b9952023-06-12 12:46:36 +020010import de.ids_mannheim.korap.config.SpringJerseyTest;
11import de.ids_mannheim.korap.core.service.SearchService;
12import de.ids_mannheim.korap.exceptions.KustvaktException;
13import de.ids_mannheim.korap.utils.JsonUtils;
margaretha96c309d2023-08-16 12:24:12 +020014import jakarta.ws.rs.core.Response;
15import jakarta.ws.rs.core.Response.Status;
margaretha5a5b9952023-06-12 12:46:36 +020016
17public class TotalResultTest extends SpringJerseyTest {
18
19 @Autowired
20 private SearchService searchService;
margarethaaaa9d802023-09-28 09:26:31 +020021
22 @Test
23 public void testClearCache () {
24 for (int i = 1; i < 10; i++) {
25 searchService.getTotalResultCache().storeInCache(i, "10");
26 }
27
28 searchService.getTotalResultCache().clearCache();
29
30 assertEquals(0, searchService.getTotalResultCache()
31 .getAllCacheElements().size());
32 }
margaretha5a5b9952023-06-12 12:46:36 +020033
34 @Test
35 public void testSearchWithPaging () throws KustvaktException {
margarethaaaa9d802023-09-28 09:26:31 +020036 searchService.getTotalResultCache().clearCache();
37
margaretha5a5b9952023-06-12 12:46:36 +020038 assertEquals(0, searchService.getTotalResultCache()
39 .getAllCacheElements().size());
margarethaaaa9d802023-09-28 09:26:31 +020040
margaretha5a5b9952023-06-12 12:46:36 +020041 Response response = target().path(API_VERSION).path("search")
42 .queryParam("q", "[orth=die]").queryParam("ql", "poliqarp")
43 .queryParam("page", "1").request().get();
44 assertEquals(Status.OK.getStatusCode(), response.getStatus());
45 String entity = response.readEntity(String.class);
46 JsonNode node = JsonUtils.readTree(entity);
Marc Kupietzd43a98d2023-09-22 17:11:46 +020047 assertTrue(node.at("/meta/totalResults").isNumber(), "totalResults should be a number");
margaretha5a5b9952023-06-12 12:46:36 +020048 int totalResults = node.at("/meta/totalResults").asInt();
Marc Kupietzd43a98d2023-09-22 17:11:46 +020049 assertEquals(1, searchService.getTotalResultCache().getAllCacheElements().size());
50 response = target().path(API_VERSION).path("search").queryParam("q", "[orth=die]").queryParam("ql", "poliqarp").queryParam("page", "2").request().get();
margaretha5a5b9952023-06-12 12:46:36 +020051 assertEquals(Status.OK.getStatusCode(), response.getStatus());
52 entity = response.readEntity(String.class);
53 node = JsonUtils.readTree(entity);
Marc Kupietzd43a98d2023-09-22 17:11:46 +020054 assertTrue(node.at("/meta/totalResults").isNumber(), "totalResults should be a number");
margaretha5a5b9952023-06-12 12:46:36 +020055 assertEquals(totalResults, node.at("/meta/totalResults").asInt());
Marc Kupietzd43a98d2023-09-22 17:11:46 +020056 assertEquals(1, searchService.getTotalResultCache().getAllCacheElements().size());
margaretha5a5b9952023-06-12 12:46:36 +020057 assertTrue(node.at("/meta/cutOff").isMissingNode());
margaretha5a5b9952023-06-12 12:46:36 +020058 testSearchWithCutOff();
59 }
Marc Kupietzd43a98d2023-09-22 17:11:46 +020060
margaretha5a5b9952023-06-12 12:46:36 +020061 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +020062 public void testSearchWithCutOffTrue() throws KustvaktException {
63 int cacheSize = searchService.getTotalResultCache().getAllCacheElements().size();
64 Response response = target().path(API_VERSION).path("search").queryParam("q", "ich").queryParam("ql", "poliqarp").queryParam("page", "2").queryParam("cutoff", "true").request().get();
margaretha5a5b9952023-06-12 12:46:36 +020065 assertEquals(Status.OK.getStatusCode(), response.getStatus());
Marc Kupietzd43a98d2023-09-22 17:11:46 +020066 String query = "{\"meta\":{\"startPage\":2,\"tokens\":false,\"cutOff\":" + "true,\"snippets\":true,\"timeout\":10000},\"query\":{\"@type\":" + "\"koral:token\",\"wrap\":{\"@type\":\"koral:term\",\"match\":" + "\"match:eq\",\"layer\":\"orth\",\"key\":\"ich\",\"foundry\":" + "\"opennlp\",\"rewrites\":[{\"@type\":\"koral:rewrite\",\"src\":" + "\"Kustvakt\",\"operation\":\"operation:injection\",\"scope\":" + "\"foundry\"}]}},\"@context\":\"http://korap.ids-mannheim.de/ns" + "/koral/0.3/context.jsonld\",\"collection\":{\"@type\":\"koral:" + "doc\",\"match\":\"match:eq\",\"type\":\"type:regex\",\"value\":" + "\"CC-BY.*\",\"key\":\"availability\",\"rewrites\":[{\"@type\":" + "\"koral:rewrite\",\"src\":\"Kustvakt\",\"operation\":\"operation:" + "insertion\",\"scope\":\"availability(FREE)\"}]}}";
margaretha5a5b9952023-06-12 12:46:36 +020067 int cacheKey = searchService.createTotalResultCacheKey(query);
Marc Kupietzd43a98d2023-09-22 17:11:46 +020068 assertEquals(null, searchService.getTotalResultCache().getCacheValue(cacheKey));
69 assertEquals(cacheSize, searchService.getTotalResultCache().getAllCacheElements().size());
margaretha5a5b9952023-06-12 12:46:36 +020070 }
Marc Kupietzd43a98d2023-09-22 17:11:46 +020071
72 private void testSearchWithCutOff() throws KustvaktException {
73 Response response = target().path(API_VERSION).path("search").queryParam("q", "[orth=die]").queryParam("ql", "poliqarp").queryParam("page", "3").queryParam("cutoff", "false").request().get();
margaretha5a5b9952023-06-12 12:46:36 +020074 assertEquals(Status.OK.getStatusCode(), response.getStatus());
75 String entity = response.readEntity(String.class);
76 JsonNode node = JsonUtils.readTree(entity);
margaretha5a5b9952023-06-12 12:46:36 +020077 assertTrue(node.at("/meta/cutOff").isMissingNode());
Marc Kupietzd43a98d2023-09-22 17:11:46 +020078 response = target().path(API_VERSION).path("search").queryParam("q", "[orth=die]").queryParam("ql", "poliqarp").queryParam("page", "4").queryParam("cutoff", "true").request().get();
margaretha5a5b9952023-06-12 12:46:36 +020079 entity = response.readEntity(String.class);
80 node = JsonUtils.readTree(entity);
margaretha5a5b9952023-06-12 12:46:36 +020081 assertTrue(node.at("/meta/cutOff").asBoolean());
82 }
83}