| 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 | |
| 3 | import static org.junit.Assert.assertEquals; |
| margaretha | ad1300b | 2021-12-13 17:36:29 +0100 | [diff] [blame] | 4 | import static org.junit.Assert.assertFalse; |
| margaretha | 7c1f428 | 2021-11-29 17:27:53 +0100 | [diff] [blame] | 5 | import static org.junit.Assert.assertNull; |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 6 | import static org.junit.Assert.assertTrue; |
| 7 | |
| 8 | import java.io.IOException; |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 9 | |
| 10 | import org.junit.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 | |
| 13 | import com.fasterxml.jackson.databind.JsonNode; |
| 14 | import com.google.common.net.HttpHeaders; |
| 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 |
| 30 | * |
| 31 | */ |
| 32 | public class VirtualCorpusRewriteTest extends SpringJerseyTest { |
| 33 | |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 34 | @Autowired |
| 35 | private NamedVCLoader vcLoader; |
| 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 |
| margaretha | 7c1f428 | 2021-11-29 17:27:53 +0100 | [diff] [blame] | 40 | public void testRefCachedVC () |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 41 | throws KustvaktException, IOException, QueryException { |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 42 | vcLoader.loadVCToCache("named-vc1", "/vc/named-vc1.jsonld"); |
| margaretha | 7c1f428 | 2021-11-29 17:27:53 +0100 | [diff] [blame] | 43 | assertTrue(VirtualCorpusCache.contains("named-vc1")); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 44 | |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 45 | Response response = target().path(API_VERSION).path("search") |
| margaretha | 1bc9cca | 2018-12-11 15:09:44 +0100 | [diff] [blame] | 46 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| 47 | .queryParam("cq", "referTo named-vc1") |
| abcpro1 | 241bc4f | 2022-11-07 20:13:57 +0000 | [diff] [blame] | 48 | .request() |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 49 | .get(); |
| margaretha | 488e04b | 2018-09-19 18:17:45 +0200 | [diff] [blame] | 50 | |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 51 | String ent = response.readEntity(String.class); |
| margaretha | 1bc9cca | 2018-12-11 15:09:44 +0100 | [diff] [blame] | 52 | JsonNode node = JsonUtils.readTree(ent); |
| 53 | node = node.at("/collection"); |
| 54 | |
| 55 | assertEquals("koral:docGroup", node.at("/@type").asText()); |
| 56 | assertTrue(node.at("/operands/1/rewrites").isMissingNode()); |
| 57 | |
| margaretha | 7c1f428 | 2021-11-29 17:27:53 +0100 | [diff] [blame] | 58 | testRefCachedVCWithUsername(); |
| margaretha | 488e04b | 2018-09-19 18:17:45 +0200 | [diff] [blame] | 59 | |
| margaretha | b097fb0 | 2021-02-22 19:28:33 +0100 | [diff] [blame] | 60 | QueryDO vc = dao.retrieveQueryByName("named-vc1", "system"); |
| 61 | dao.deleteQuery(vc); |
| margaretha | 7c1f428 | 2021-11-29 17:27:53 +0100 | [diff] [blame] | 62 | vc = dao.retrieveQueryByName("named-vc1", "system"); |
| 63 | assertNull(vc); |
| margaretha | ad1300b | 2021-12-13 17:36:29 +0100 | [diff] [blame] | 64 | VirtualCorpusCache.delete("named-vc1"); |
| 65 | assertFalse(VirtualCorpusCache.contains("named-vc1")); |
| margaretha | 488e04b | 2018-09-19 18:17:45 +0200 | [diff] [blame] | 66 | } |
| 67 | |
| margaretha | 7c1f428 | 2021-11-29 17:27:53 +0100 | [diff] [blame] | 68 | private void testRefCachedVCWithUsername () |
| margaretha | 488e04b | 2018-09-19 18:17:45 +0200 | [diff] [blame] | 69 | throws KustvaktException, IOException, QueryException { |
| 70 | |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 71 | Response response = target().path(API_VERSION).path("search") |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 72 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| margaretha | 488e04b | 2018-09-19 18:17:45 +0200 | [diff] [blame] | 73 | .queryParam("cq", "referTo \"system/named-vc1\"") |
| abcpro1 | 241bc4f | 2022-11-07 20:13:57 +0000 | [diff] [blame] | 74 | .request() |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 75 | .get(); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 76 | |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 77 | String ent = response.readEntity(String.class); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 78 | JsonNode node = JsonUtils.readTree(ent); |
| 79 | node = node.at("/collection"); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 80 | assertEquals("koral:docGroup", node.at("/@type").asText()); |
| margaretha | 488e04b | 2018-09-19 18:17:45 +0200 | [diff] [blame] | 81 | |
| 82 | node = node.at("/operands/1/rewrites"); |
| 83 | assertEquals(2, node.size()); |
| 84 | assertEquals("operation:deletion", node.at("/0/operation").asText()); |
| 85 | assertEquals("operation:insertion", node.at("/1/operation").asText()); |
| 86 | |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 87 | } |
| margaretha | 488e04b | 2018-09-19 18:17:45 +0200 | [diff] [blame] | 88 | |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 89 | @Test |
| margaretha | 488e04b | 2018-09-19 18:17:45 +0200 | [diff] [blame] | 90 | public void testRewriteFreeAndSystemVCRef () |
| 91 | throws KustvaktException, Exception { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 92 | Response response = target().path(API_VERSION).path("search") |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 93 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| margaretha | 3a57940 | 2019-07-04 15:40:46 +0200 | [diff] [blame] | 94 | .queryParam("cq", "referTo \"system-vc\"") |
| abcpro1 | 241bc4f | 2022-11-07 20:13:57 +0000 | [diff] [blame] | 95 | .request() |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 96 | .get(); |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 97 | |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 98 | String ent = response.readEntity(String.class); |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 99 | JsonNode node = JsonUtils.readTree(ent); |
| 100 | node = node.at("/collection"); |
| 101 | |
| 102 | assertEquals("koral:docGroup", node.at("/@type").asText()); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 103 | assertEquals("koral:doc", node.at("/operands/0/@type").asText()); |
| margaretha | 488e04b | 2018-09-19 18:17:45 +0200 | [diff] [blame] | 104 | |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 105 | assertEquals("koral:doc", node.at("/operands/1/@type").asText()); |
| 106 | assertEquals("GOE", node.at("/operands/1/value").asText()); |
| 107 | assertEquals("corpusSigle", node.at("/operands/1/key").asText()); |
| 108 | |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 109 | node = node.at("/operands/1/rewrites"); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 110 | assertEquals(3, node.size()); |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 111 | assertEquals("operation:deletion", node.at("/0/operation").asText()); |
| 112 | assertEquals("operation:deletion", node.at("/1/operation").asText()); |
| 113 | assertEquals("operation:insertion", node.at("/2/operation").asText()); |
| 114 | } |
| 115 | |
| 116 | @Test |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 117 | public void testRewritePubAndSystemVCRef () throws KustvaktException { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 118 | Response response = target().path(API_VERSION).path("search") |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 119 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| margaretha | 3a57940 | 2019-07-04 15:40:46 +0200 | [diff] [blame] | 120 | .queryParam("cq", "referTo \"system/system-vc\"") |
| abcpro1 | 241bc4f | 2022-11-07 20:13:57 +0000 | [diff] [blame] | 121 | .request() |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 122 | .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler |
| 123 | .createBasicAuthorizationHeaderValue("user", "pass")) |
| 124 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 125 | .get(); |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 126 | |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 127 | String ent = response.readEntity(String.class); |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 128 | JsonNode node = JsonUtils.readTree(ent); |
| 129 | node = node.at("/collection"); |
| 130 | assertEquals("koral:docGroup", node.at("/@type").asText()); |
| 131 | assertEquals("koral:docGroup", node.at("/operands/0/@type").asText()); |
| margaretha | 488e04b | 2018-09-19 18:17:45 +0200 | [diff] [blame] | 132 | |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 133 | node = node.at("/operands/1/rewrites"); |
| 134 | assertEquals(3, node.size()); |
| 135 | assertEquals("operation:deletion", node.at("/0/operation").asText()); |
| 136 | assertEquals("operation:deletion", node.at("/1/operation").asText()); |
| 137 | assertEquals("operation:insertion", node.at("/2/operation").asText()); |
| 138 | } |
| 139 | |
| 140 | @Test |
| 141 | public void testRewriteWithDoryVCRef () |
| 142 | throws KustvaktException, IOException, QueryException { |
| 143 | |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 144 | Response response = target().path(API_VERSION).path("search") |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 145 | .queryParam("q", "Fisch").queryParam("ql", "poliqarp") |
| margaretha | 3a57940 | 2019-07-04 15:40:46 +0200 | [diff] [blame] | 146 | .queryParam("cq", "referTo \"dory/dory-vc\"") |
| abcpro1 | 241bc4f | 2022-11-07 20:13:57 +0000 | [diff] [blame] | 147 | .request() |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 148 | .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler |
| 149 | .createBasicAuthorizationHeaderValue("dory", "pass")) |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 150 | .get(); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 151 | |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 152 | String ent = response.readEntity(String.class); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 153 | JsonNode node = JsonUtils.readTree(ent); |
| 154 | node = node.at("/collection"); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 155 | assertEquals("koral:docGroup", node.at("/@type").asText()); |
| 156 | node = node.at("/operands/1/rewrites"); |
| 157 | assertEquals(3, node.size()); |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 158 | } |
| 159 | } |