| 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 | |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 15 | import javax.ws.rs.core.Response; |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 16 | |
| 17 | import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler; |
| margaretha | 7c1f428 | 2021-11-29 17:27:53 +0100 | [diff] [blame] | 18 | import de.ids_mannheim.korap.cache.VirtualCorpusCache; |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 19 | import de.ids_mannheim.korap.config.Attributes; |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 20 | import de.ids_mannheim.korap.config.NamedVCLoader; |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 21 | import de.ids_mannheim.korap.config.SpringJerseyTest; |
| margaretha | b097fb0 | 2021-02-22 19:28:33 +0100 | [diff] [blame] | 22 | import de.ids_mannheim.korap.dao.QueryDao; |
| 23 | import de.ids_mannheim.korap.entity.QueryDO; |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 24 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 25 | import de.ids_mannheim.korap.util.QueryException; |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 26 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 27 | |
| 28 | /** |
| 29 | * @author margaretha |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 30 | */ |
| 31 | public class VirtualCorpusRewriteTest extends SpringJerseyTest { |
| 32 | |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 33 | @Autowired |
| 34 | private NamedVCLoader vcLoader; |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 35 | |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 36 | @Autowired |
| margaretha | b097fb0 | 2021-02-22 19:28:33 +0100 | [diff] [blame] | 37 | private QueryDao dao; |
| margaretha | 488e04b | 2018-09-19 18:17:45 +0200 | [diff] [blame] | 38 | |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 39 | @Test |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 40 | public void testRefCachedVC() 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")); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 43 | Response response = target().path(API_VERSION).path("search").queryParam("q", "[orth=der]").queryParam("ql", "poliqarp").queryParam("cq", "referTo named-vc1").request().get(); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 44 | String ent = response.readEntity(String.class); |
| margaretha | 1bc9cca | 2018-12-11 15:09:44 +0100 | [diff] [blame] | 45 | JsonNode node = JsonUtils.readTree(ent); |
| 46 | node = node.at("/collection"); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 47 | assertEquals(node.at("/@type").asText(), "koral:docGroup"); |
| margaretha | 1bc9cca | 2018-12-11 15:09:44 +0100 | [diff] [blame] | 48 | assertTrue(node.at("/operands/1/rewrites").isMissingNode()); |
| margaretha | 7c1f428 | 2021-11-29 17:27:53 +0100 | [diff] [blame] | 49 | testRefCachedVCWithUsername(); |
| margaretha | b097fb0 | 2021-02-22 19:28:33 +0100 | [diff] [blame] | 50 | QueryDO vc = dao.retrieveQueryByName("named-vc1", "system"); |
| 51 | dao.deleteQuery(vc); |
| margaretha | 7c1f428 | 2021-11-29 17:27:53 +0100 | [diff] [blame] | 52 | vc = dao.retrieveQueryByName("named-vc1", "system"); |
| 53 | assertNull(vc); |
| margaretha | ad1300b | 2021-12-13 17:36:29 +0100 | [diff] [blame] | 54 | VirtualCorpusCache.delete("named-vc1"); |
| 55 | assertFalse(VirtualCorpusCache.contains("named-vc1")); |
| margaretha | 488e04b | 2018-09-19 18:17:45 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 58 | private void testRefCachedVCWithUsername() throws KustvaktException, IOException, QueryException { |
| 59 | Response response = target().path(API_VERSION).path("search").queryParam("q", "[orth=der]").queryParam("ql", "poliqarp").queryParam("cq", "referTo \"system/named-vc1\"").request().get(); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 60 | String ent = response.readEntity(String.class); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 61 | JsonNode node = JsonUtils.readTree(ent); |
| 62 | node = node.at("/collection"); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 63 | assertEquals(node.at("/@type").asText(), "koral:docGroup"); |
| margaretha | 488e04b | 2018-09-19 18:17:45 +0200 | [diff] [blame] | 64 | node = node.at("/operands/1/rewrites"); |
| 65 | assertEquals(2, node.size()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 66 | assertEquals(node.at("/0/operation").asText(), "operation:deletion"); |
| 67 | assertEquals(node.at("/1/operation").asText(), "operation:insertion"); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 68 | } |
| margaretha | 488e04b | 2018-09-19 18:17:45 +0200 | [diff] [blame] | 69 | |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 70 | @Test |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 71 | public void testRewriteFreeAndSystemVCRef() throws KustvaktException, Exception { |
| 72 | Response response = target().path(API_VERSION).path("search").queryParam("q", "[orth=der]").queryParam("ql", "poliqarp").queryParam("cq", "referTo \"system-vc\"").request().get(); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 73 | String ent = response.readEntity(String.class); |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 74 | JsonNode node = JsonUtils.readTree(ent); |
| 75 | node = node.at("/collection"); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 76 | assertEquals(node.at("/@type").asText(), "koral:docGroup"); |
| 77 | assertEquals(node.at("/operands/0/@type").asText(), "koral:doc"); |
| 78 | assertEquals(node.at("/operands/1/@type").asText(), "koral:doc"); |
| 79 | assertEquals(node.at("/operands/1/value").asText(), "GOE"); |
| 80 | assertEquals(node.at("/operands/1/key").asText(), "corpusSigle"); |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 81 | node = node.at("/operands/1/rewrites"); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 82 | assertEquals(3, node.size()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 83 | assertEquals(node.at("/0/operation").asText(), "operation:deletion"); |
| 84 | assertEquals(node.at("/1/operation").asText(), "operation:deletion"); |
| 85 | assertEquals(node.at("/2/operation").asText(), "operation:insertion"); |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Test |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 89 | public void testRewritePubAndSystemVCRef() throws KustvaktException { |
| 90 | Response response = target().path(API_VERSION).path("search").queryParam("q", "[orth=der]").queryParam("ql", "poliqarp").queryParam("cq", "referTo \"system/system-vc\"").request().header(Attributes.AUTHORIZATION, HttpAuthorizationHandler.createBasicAuthorizationHeaderValue("user", "pass")).header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32").get(); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 91 | String ent = response.readEntity(String.class); |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 92 | JsonNode node = JsonUtils.readTree(ent); |
| 93 | node = node.at("/collection"); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 94 | assertEquals(node.at("/@type").asText(), "koral:docGroup"); |
| 95 | assertEquals(node.at("/operands/0/@type").asText(), "koral:docGroup"); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 96 | node = node.at("/operands/1/rewrites"); |
| 97 | assertEquals(3, node.size()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 98 | assertEquals(node.at("/0/operation").asText(), "operation:deletion"); |
| 99 | assertEquals(node.at("/1/operation").asText(), "operation:deletion"); |
| 100 | assertEquals(node.at("/2/operation").asText(), "operation:insertion"); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Test |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 104 | public void testRewriteWithDoryVCRef() throws KustvaktException, IOException, QueryException { |
| 105 | Response response = target().path(API_VERSION).path("search").queryParam("q", "Fisch").queryParam("ql", "poliqarp").queryParam("cq", "referTo \"dory/dory-vc\"").request().header(Attributes.AUTHORIZATION, HttpAuthorizationHandler.createBasicAuthorizationHeaderValue("dory", "pass")).get(); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 106 | String ent = response.readEntity(String.class); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 107 | JsonNode node = JsonUtils.readTree(ent); |
| 108 | node = node.at("/collection"); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 109 | assertEquals(node.at("/@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()); |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 112 | } |
| 113 | } |