blob: 84a5056c4e17023a197af8afc184148e20048a82 [file] [log] [blame]
margaretha398f4722019-01-09 19:07:20 +01001package de.ids_mannheim.korap.rewrite;
margaretha563aabe2018-09-13 20:39:45 +02002
3import static org.junit.Assert.assertEquals;
margarethaad1300b2021-12-13 17:36:29 +01004import static org.junit.Assert.assertFalse;
margaretha7c1f4282021-11-29 17:27:53 +01005import static org.junit.Assert.assertNull;
margaretha9e53bb22018-09-14 19:39:15 +02006import static org.junit.Assert.assertTrue;
7
8import java.io.IOException;
margaretha563aabe2018-09-13 20:39:45 +02009
10import org.junit.Test;
margaretha9e53bb22018-09-14 19:39:15 +020011import org.springframework.beans.factory.annotation.Autowired;
margaretha563aabe2018-09-13 20:39:45 +020012
13import com.fasterxml.jackson.databind.JsonNode;
14import com.google.common.net.HttpHeaders;
abcpro173fe8f22022-11-08 19:56:52 +000015import javax.ws.rs.core.Response;
margaretha563aabe2018-09-13 20:39:45 +020016
17import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler;
margaretha7c1f4282021-11-29 17:27:53 +010018import de.ids_mannheim.korap.cache.VirtualCorpusCache;
margaretha563aabe2018-09-13 20:39:45 +020019import de.ids_mannheim.korap.config.Attributes;
margaretha9e53bb22018-09-14 19:39:15 +020020import de.ids_mannheim.korap.config.NamedVCLoader;
margaretha563aabe2018-09-13 20:39:45 +020021import de.ids_mannheim.korap.config.SpringJerseyTest;
margarethab097fb02021-02-22 19:28:33 +010022import de.ids_mannheim.korap.dao.QueryDao;
23import de.ids_mannheim.korap.entity.QueryDO;
margaretha563aabe2018-09-13 20:39:45 +020024import de.ids_mannheim.korap.exceptions.KustvaktException;
margaretha9e53bb22018-09-14 19:39:15 +020025import de.ids_mannheim.korap.util.QueryException;
margaretha563aabe2018-09-13 20:39:45 +020026import de.ids_mannheim.korap.utils.JsonUtils;
27
28/**
29 * @author margaretha
30 *
31 */
32public class VirtualCorpusRewriteTest extends SpringJerseyTest {
33
margaretha9e53bb22018-09-14 19:39:15 +020034 @Autowired
35 private NamedVCLoader vcLoader;
36 @Autowired
margarethab097fb02021-02-22 19:28:33 +010037 private QueryDao dao;
margaretha488e04b2018-09-19 18:17:45 +020038
margaretha563aabe2018-09-13 20:39:45 +020039 @Test
margaretha7c1f4282021-11-29 17:27:53 +010040 public void testRefCachedVC ()
margaretha9e53bb22018-09-14 19:39:15 +020041 throws KustvaktException, IOException, QueryException {
margaretha9e53bb22018-09-14 19:39:15 +020042 vcLoader.loadVCToCache("named-vc1", "/vc/named-vc1.jsonld");
margaretha7c1f4282021-11-29 17:27:53 +010043 assertTrue(VirtualCorpusCache.contains("named-vc1"));
margaretha9e53bb22018-09-14 19:39:15 +020044
abcpro173fe8f22022-11-08 19:56:52 +000045 Response response = target().path(API_VERSION).path("search")
margaretha1bc9cca2018-12-11 15:09:44 +010046 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
47 .queryParam("cq", "referTo named-vc1")
abcpro1241bc4f2022-11-07 20:13:57 +000048 .request()
abcpro173fe8f22022-11-08 19:56:52 +000049 .get();
margaretha488e04b2018-09-19 18:17:45 +020050
abcpro173fe8f22022-11-08 19:56:52 +000051 String ent = response.readEntity(String.class);
margaretha1bc9cca2018-12-11 15:09:44 +010052 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
margaretha7c1f4282021-11-29 17:27:53 +010058 testRefCachedVCWithUsername();
margaretha488e04b2018-09-19 18:17:45 +020059
margarethab097fb02021-02-22 19:28:33 +010060 QueryDO vc = dao.retrieveQueryByName("named-vc1", "system");
61 dao.deleteQuery(vc);
margaretha7c1f4282021-11-29 17:27:53 +010062 vc = dao.retrieveQueryByName("named-vc1", "system");
63 assertNull(vc);
margarethaad1300b2021-12-13 17:36:29 +010064 VirtualCorpusCache.delete("named-vc1");
65 assertFalse(VirtualCorpusCache.contains("named-vc1"));
margaretha488e04b2018-09-19 18:17:45 +020066 }
67
margaretha7c1f4282021-11-29 17:27:53 +010068 private void testRefCachedVCWithUsername ()
margaretha488e04b2018-09-19 18:17:45 +020069 throws KustvaktException, IOException, QueryException {
70
abcpro173fe8f22022-11-08 19:56:52 +000071 Response response = target().path(API_VERSION).path("search")
margaretha9e53bb22018-09-14 19:39:15 +020072 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
margaretha488e04b2018-09-19 18:17:45 +020073 .queryParam("cq", "referTo \"system/named-vc1\"")
abcpro1241bc4f2022-11-07 20:13:57 +000074 .request()
abcpro173fe8f22022-11-08 19:56:52 +000075 .get();
margaretha9e53bb22018-09-14 19:39:15 +020076
abcpro173fe8f22022-11-08 19:56:52 +000077 String ent = response.readEntity(String.class);
margaretha9e53bb22018-09-14 19:39:15 +020078 JsonNode node = JsonUtils.readTree(ent);
79 node = node.at("/collection");
margaretha9e53bb22018-09-14 19:39:15 +020080 assertEquals("koral:docGroup", node.at("/@type").asText());
margaretha488e04b2018-09-19 18:17:45 +020081
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
margaretha9e53bb22018-09-14 19:39:15 +020087 }
margaretha488e04b2018-09-19 18:17:45 +020088
margaretha9e53bb22018-09-14 19:39:15 +020089 @Test
margaretha488e04b2018-09-19 18:17:45 +020090 public void testRewriteFreeAndSystemVCRef ()
91 throws KustvaktException, Exception {
abcpro173fe8f22022-11-08 19:56:52 +000092 Response response = target().path(API_VERSION).path("search")
margaretha563aabe2018-09-13 20:39:45 +020093 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
margaretha3a579402019-07-04 15:40:46 +020094 .queryParam("cq", "referTo \"system-vc\"")
abcpro1241bc4f2022-11-07 20:13:57 +000095 .request()
abcpro173fe8f22022-11-08 19:56:52 +000096 .get();
margaretha563aabe2018-09-13 20:39:45 +020097
abcpro173fe8f22022-11-08 19:56:52 +000098 String ent = response.readEntity(String.class);
margaretha563aabe2018-09-13 20:39:45 +020099 JsonNode node = JsonUtils.readTree(ent);
100 node = node.at("/collection");
101
102 assertEquals("koral:docGroup", node.at("/@type").asText());
margaretha9e53bb22018-09-14 19:39:15 +0200103 assertEquals("koral:doc", node.at("/operands/0/@type").asText());
margaretha488e04b2018-09-19 18:17:45 +0200104
margaretha9e53bb22018-09-14 19:39:15 +0200105 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
margaretha563aabe2018-09-13 20:39:45 +0200109 node = node.at("/operands/1/rewrites");
margaretha9e53bb22018-09-14 19:39:15 +0200110 assertEquals(3, node.size());
margaretha563aabe2018-09-13 20:39:45 +0200111 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
margaretha9e53bb22018-09-14 19:39:15 +0200117 public void testRewritePubAndSystemVCRef () throws KustvaktException {
abcpro173fe8f22022-11-08 19:56:52 +0000118 Response response = target().path(API_VERSION).path("search")
margaretha563aabe2018-09-13 20:39:45 +0200119 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
margaretha3a579402019-07-04 15:40:46 +0200120 .queryParam("cq", "referTo \"system/system-vc\"")
abcpro1241bc4f2022-11-07 20:13:57 +0000121 .request()
margaretha563aabe2018-09-13 20:39:45 +0200122 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
123 .createBasicAuthorizationHeaderValue("user", "pass"))
124 .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
abcpro173fe8f22022-11-08 19:56:52 +0000125 .get();
margaretha563aabe2018-09-13 20:39:45 +0200126
abcpro173fe8f22022-11-08 19:56:52 +0000127 String ent = response.readEntity(String.class);
margaretha563aabe2018-09-13 20:39:45 +0200128 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());
margaretha488e04b2018-09-19 18:17:45 +0200132
margaretha9e53bb22018-09-14 19:39:15 +0200133 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
abcpro173fe8f22022-11-08 19:56:52 +0000144 Response response = target().path(API_VERSION).path("search")
margaretha9e53bb22018-09-14 19:39:15 +0200145 .queryParam("q", "Fisch").queryParam("ql", "poliqarp")
margaretha3a579402019-07-04 15:40:46 +0200146 .queryParam("cq", "referTo \"dory/dory-vc\"")
abcpro1241bc4f2022-11-07 20:13:57 +0000147 .request()
margaretha9e53bb22018-09-14 19:39:15 +0200148 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
149 .createBasicAuthorizationHeaderValue("dory", "pass"))
abcpro173fe8f22022-11-08 19:56:52 +0000150 .get();
margaretha9e53bb22018-09-14 19:39:15 +0200151
abcpro173fe8f22022-11-08 19:56:52 +0000152 String ent = response.readEntity(String.class);
margaretha9e53bb22018-09-14 19:39:15 +0200153 JsonNode node = JsonUtils.readTree(ent);
154 node = node.at("/collection");
margaretha9e53bb22018-09-14 19:39:15 +0200155 assertEquals("koral:docGroup", node.at("/@type").asText());
156 node = node.at("/operands/1/rewrites");
157 assertEquals(3, node.size());
margaretha563aabe2018-09-13 20:39:45 +0200158 }
159}