| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.cache; |
| 2 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 5 | |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 6 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 7 | import org.junit.jupiter.api.Test; |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 8 | import org.springframework.beans.factory.annotation.Autowired; |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 9 | import com.fasterxml.jackson.databind.JsonNode; |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 10 | import de.ids_mannheim.korap.config.SpringJerseyTest; |
| 11 | import de.ids_mannheim.korap.core.service.SearchService; |
| 12 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 13 | import de.ids_mannheim.korap.utils.JsonUtils; |
| margaretha | 96c309d | 2023-08-16 12:24:12 +0200 | [diff] [blame] | 14 | import jakarta.ws.rs.core.Response; |
| 15 | import jakarta.ws.rs.core.Response.Status; |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 16 | |
| 17 | public class TotalResultTest extends SpringJerseyTest { |
| 18 | |
| 19 | @Autowired |
| 20 | private SearchService searchService; |
| margaretha | aaa9d80 | 2023-09-28 09:26:31 +0200 | [diff] [blame] | 21 | |
| 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 | } |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 33 | |
| 34 | @Test |
| 35 | public void testSearchWithPaging () throws KustvaktException { |
| margaretha | aaa9d80 | 2023-09-28 09:26:31 +0200 | [diff] [blame] | 36 | searchService.getTotalResultCache().clearCache(); |
| 37 | |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 38 | assertEquals(0, searchService.getTotalResultCache() |
| 39 | .getAllCacheElements().size()); |
| margaretha | aaa9d80 | 2023-09-28 09:26:31 +0200 | [diff] [blame] | 40 | |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 41 | 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 Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 47 | assertTrue(node.at("/meta/totalResults").isNumber(), "totalResults should be a number"); |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 48 | int totalResults = node.at("/meta/totalResults").asInt(); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 49 | 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(); |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 51 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 52 | entity = response.readEntity(String.class); |
| 53 | node = JsonUtils.readTree(entity); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 54 | assertTrue(node.at("/meta/totalResults").isNumber(), "totalResults should be a number"); |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 55 | assertEquals(totalResults, node.at("/meta/totalResults").asInt()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 56 | assertEquals(1, searchService.getTotalResultCache().getAllCacheElements().size()); |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 57 | assertTrue(node.at("/meta/cutOff").isMissingNode()); |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 58 | testSearchWithCutOff(); |
| 59 | } |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 60 | |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 61 | @Test |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 62 | 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(); |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 65 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 66 | 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)\"}]}}"; |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 67 | int cacheKey = searchService.createTotalResultCacheKey(query); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 68 | assertEquals(null, searchService.getTotalResultCache().getCacheValue(cacheKey)); |
| 69 | assertEquals(cacheSize, searchService.getTotalResultCache().getAllCacheElements().size()); |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 70 | } |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 71 | |
| 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(); |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 74 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 75 | String entity = response.readEntity(String.class); |
| 76 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 77 | assertTrue(node.at("/meta/cutOff").isMissingNode()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 78 | response = target().path(API_VERSION).path("search").queryParam("q", "[orth=die]").queryParam("ql", "poliqarp").queryParam("page", "4").queryParam("cutoff", "true").request().get(); |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 79 | entity = response.readEntity(String.class); |
| 80 | node = JsonUtils.readTree(entity); |
| margaretha | 5a5b995 | 2023-06-12 12:46:36 +0200 | [diff] [blame] | 81 | assertTrue(node.at("/meta/cutOff").asBoolean()); |
| 82 | } |
| 83 | } |