| margaretha | 398f472 | 2019-01-09 19:07:20 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap.rewrite; |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 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.assertFalse; |
| 5 | import static org.junit.jupiter.api.Assertions.assertNull; |
| 6 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 7 | |
| 8 | import java.io.IOException; |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 9 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 10 | import org.junit.jupiter.api.Test; |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 11 | import org.springframework.beans.factory.annotation.Autowired; |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 12 | import com.fasterxml.jackson.databind.JsonNode; |
| 13 | import com.google.common.net.HttpHeaders; |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 14 | |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 15 | import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler; |
| margaretha | 7c1f428 | 2021-11-29 17:27:53 +0100 | [diff] [blame] | 16 | import de.ids_mannheim.korap.cache.VirtualCorpusCache; |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 17 | import de.ids_mannheim.korap.config.Attributes; |
| 18 | import de.ids_mannheim.korap.config.SpringJerseyTest; |
| margaretha | b097fb0 | 2021-02-22 19:28:33 +0100 | [diff] [blame] | 19 | import de.ids_mannheim.korap.dao.QueryDao; |
| 20 | import de.ids_mannheim.korap.entity.QueryDO; |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 21 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| margaretha | c858890 | 2024-06-13 09:36:53 +0200 | [diff] [blame] | 22 | import de.ids_mannheim.korap.init.NamedVCLoader; |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 23 | import de.ids_mannheim.korap.util.QueryException; |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 24 | import de.ids_mannheim.korap.utils.JsonUtils; |
| margaretha | 96c309d | 2023-08-16 12:24:12 +0200 | [diff] [blame] | 25 | import jakarta.ws.rs.core.Response; |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * @author margaretha |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 29 | */ |
| 30 | public class VirtualCorpusRewriteTest extends SpringJerseyTest { |
| 31 | |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 32 | @Autowired |
| 33 | private NamedVCLoader vcLoader; |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 34 | |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 35 | @Autowired |
| margaretha | b097fb0 | 2021-02-22 19:28:33 +0100 | [diff] [blame] | 36 | private QueryDao dao; |
| margaretha | 488e04b | 2018-09-19 18:17:45 +0200 | [diff] [blame] | 37 | |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 38 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 39 | public void testRefCachedVC () |
| 40 | throws KustvaktException, IOException, QueryException { |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 41 | vcLoader.loadVCToCache("named-vc1", "/vc/named-vc1.jsonld"); |
| margaretha | 7c1f428 | 2021-11-29 17:27:53 +0100 | [diff] [blame] | 42 | assertTrue(VirtualCorpusCache.contains("named-vc1")); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 43 | Response response = target().path(API_VERSION).path("search") |
| 44 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| 45 | .queryParam("cq", "referTo named-vc1").request().get(); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 46 | String ent = response.readEntity(String.class); |
| margaretha | 1bc9cca | 2018-12-11 15:09:44 +0100 | [diff] [blame] | 47 | JsonNode node = JsonUtils.readTree(ent); |
| 48 | node = node.at("/collection"); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 49 | assertEquals(node.at("/@type").asText(), "koral:docGroup"); |
| margaretha | 1bc9cca | 2018-12-11 15:09:44 +0100 | [diff] [blame] | 50 | assertTrue(node.at("/operands/1/rewrites").isMissingNode()); |
| margaretha | 7c1f428 | 2021-11-29 17:27:53 +0100 | [diff] [blame] | 51 | testRefCachedVCWithUsername(); |
| margaretha | b097fb0 | 2021-02-22 19:28:33 +0100 | [diff] [blame] | 52 | QueryDO vc = dao.retrieveQueryByName("named-vc1", "system"); |
| 53 | dao.deleteQuery(vc); |
| margaretha | 7c1f428 | 2021-11-29 17:27:53 +0100 | [diff] [blame] | 54 | vc = dao.retrieveQueryByName("named-vc1", "system"); |
| 55 | assertNull(vc); |
| margaretha | ad1300b | 2021-12-13 17:36:29 +0100 | [diff] [blame] | 56 | VirtualCorpusCache.delete("named-vc1"); |
| 57 | assertFalse(VirtualCorpusCache.contains("named-vc1")); |
| margaretha | 488e04b | 2018-09-19 18:17:45 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 60 | private void testRefCachedVCWithUsername () |
| 61 | throws KustvaktException, IOException, QueryException { |
| 62 | Response response = target().path(API_VERSION).path("search") |
| 63 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| 64 | .queryParam("cq", "referTo \"system/named-vc1\"").request() |
| 65 | .get(); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 66 | String ent = response.readEntity(String.class); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 67 | JsonNode node = JsonUtils.readTree(ent); |
| 68 | node = node.at("/collection"); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 69 | assertEquals(node.at("/@type").asText(), "koral:docGroup"); |
| margaretha | 488e04b | 2018-09-19 18:17:45 +0200 | [diff] [blame] | 70 | node = node.at("/operands/1/rewrites"); |
| 71 | assertEquals(2, node.size()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 72 | assertEquals(node.at("/0/operation").asText(), "operation:deletion"); |
| 73 | assertEquals(node.at("/1/operation").asText(), "operation:insertion"); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 74 | } |
| margaretha | 488e04b | 2018-09-19 18:17:45 +0200 | [diff] [blame] | 75 | |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 76 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 77 | public void testRewriteFreeAndSystemVCRef () |
| 78 | throws KustvaktException, Exception { |
| 79 | Response response = target().path(API_VERSION).path("search") |
| 80 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| 81 | .queryParam("cq", "referTo \"system-vc\"").request().get(); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 82 | String ent = response.readEntity(String.class); |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 83 | JsonNode node = JsonUtils.readTree(ent); |
| 84 | node = node.at("/collection"); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 85 | assertEquals(node.at("/@type").asText(), "koral:docGroup"); |
| 86 | assertEquals(node.at("/operands/0/@type").asText(), "koral:doc"); |
| 87 | assertEquals(node.at("/operands/1/@type").asText(), "koral:doc"); |
| 88 | assertEquals(node.at("/operands/1/value").asText(), "GOE"); |
| 89 | assertEquals(node.at("/operands/1/key").asText(), "corpusSigle"); |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 90 | node = node.at("/operands/1/rewrites"); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 91 | assertEquals(3, node.size()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 92 | assertEquals(node.at("/0/operation").asText(), "operation:deletion"); |
| 93 | assertEquals(node.at("/1/operation").asText(), "operation:deletion"); |
| 94 | assertEquals(node.at("/2/operation").asText(), "operation:insertion"); |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 98 | public void testRewritePubAndSystemVCRef () throws KustvaktException { |
| 99 | Response response = target().path(API_VERSION).path("search") |
| 100 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| 101 | .queryParam("cq", "referTo \"system/system-vc\"").request() |
| 102 | .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler |
| 103 | .createBasicAuthorizationHeaderValue("user", "pass")) |
| 104 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32").get(); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 105 | String ent = response.readEntity(String.class); |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 106 | JsonNode node = JsonUtils.readTree(ent); |
| 107 | node = node.at("/collection"); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 108 | assertEquals(node.at("/@type").asText(), "koral:docGroup"); |
| 109 | assertEquals(node.at("/operands/0/@type").asText(), "koral:docGroup"); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 110 | node = node.at("/operands/1/rewrites"); |
| 111 | assertEquals(3, node.size()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 112 | assertEquals(node.at("/0/operation").asText(), "operation:deletion"); |
| 113 | assertEquals(node.at("/1/operation").asText(), "operation:deletion"); |
| 114 | assertEquals(node.at("/2/operation").asText(), "operation:insertion"); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 118 | public void testRewriteWithDoryVCRef () |
| 119 | throws KustvaktException, IOException, QueryException { |
| 120 | Response response = target().path(API_VERSION).path("search") |
| 121 | .queryParam("q", "Fisch").queryParam("ql", "poliqarp") |
| 122 | .queryParam("cq", "referTo \"dory/dory-vc\"").request() |
| 123 | .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler |
| 124 | .createBasicAuthorizationHeaderValue("dory", "pass")) |
| 125 | .get(); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 126 | String ent = response.readEntity(String.class); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 127 | JsonNode node = JsonUtils.readTree(ent); |
| 128 | node = node.at("/collection"); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 129 | assertEquals(node.at("/@type").asText(), "koral:docGroup"); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 130 | node = node.at("/operands/1/rewrites"); |
| 131 | assertEquals(3, node.size()); |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 132 | } |
| 133 | } |