blob: dbeecf16667099b6b6a7b3e4a938a0b70bfaa754 [file] [log] [blame]
margaretha1b320452018-08-02 16:56:25 +02001package de.ids_mannheim.korap.web.controller;
2
margarethac7196d22018-08-27 14:20:03 +02003import static org.junit.Assert.assertEquals;
margaretha1b320452018-08-02 16:56:25 +02004import static org.junit.Assert.assertTrue;
5
margaretha563aabe2018-09-13 20:39:45 +02006import java.io.IOException;
7
margaretha1b320452018-08-02 16:56:25 +02008import org.junit.Test;
margaretha9e53bb22018-09-14 19:39:15 +02009import org.springframework.beans.factory.annotation.Autowired;
margaretha1b320452018-08-02 16:56:25 +020010
11import com.fasterxml.jackson.databind.JsonNode;
margaretha0e1fc552019-08-08 15:31:01 +020012import com.google.common.net.HttpHeaders;
margaretha1b320452018-08-02 16:56:25 +020013import com.sun.jersey.api.client.ClientResponse;
margaretha0e1fc552019-08-08 15:31:01 +020014import com.sun.jersey.api.client.ClientResponse.Status;
margaretha1b320452018-08-02 16:56:25 +020015
margaretha9e53bb22018-09-14 19:39:15 +020016import de.ids_mannheim.korap.KrillCollection;
margaretha0e1fc552019-08-08 15:31:01 +020017import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler;
18import de.ids_mannheim.korap.config.Attributes;
margaretha9e53bb22018-09-14 19:39:15 +020019import de.ids_mannheim.korap.config.NamedVCLoader;
margaretha1b320452018-08-02 16:56:25 +020020import de.ids_mannheim.korap.config.SpringJerseyTest;
margarethab097fb02021-02-22 19:28:33 +010021import de.ids_mannheim.korap.dao.QueryDao;
22import de.ids_mannheim.korap.entity.QueryDO;
margaretha1b320452018-08-02 16:56:25 +020023import de.ids_mannheim.korap.exceptions.KustvaktException;
margaretha4a338122019-01-25 16:02:18 +010024import de.ids_mannheim.korap.exceptions.StatusCodes;
margaretha563aabe2018-09-13 20:39:45 +020025import de.ids_mannheim.korap.util.QueryException;
margaretha1b320452018-08-02 16:56:25 +020026import de.ids_mannheim.korap.utils.JsonUtils;
margaretha9e53bb22018-09-14 19:39:15 +020027import net.sf.ehcache.CacheManager;
margaretha1b320452018-08-02 16:56:25 +020028
29public class VCReferenceTest extends SpringJerseyTest {
30
margaretha9e53bb22018-09-14 19:39:15 +020031 @Autowired
32 private NamedVCLoader vcLoader;
33 @Autowired
margarethab097fb02021-02-22 19:28:33 +010034 private QueryDao dao;
margaretha9e53bb22018-09-14 19:39:15 +020035
margaretha1b320452018-08-02 16:56:25 +020036 @Test
margaretha4a338122019-01-25 16:02:18 +010037 public void testRefPredefinedVC ()
margaretha563aabe2018-09-13 20:39:45 +020038 throws KustvaktException, IOException, QueryException {
margaretha1b320452018-08-02 16:56:25 +020039 testSearchWithoutVCRefOr();
40 testSearchWithoutVCRefAnd();
margaretha563aabe2018-09-13 20:39:45 +020041
margaretha9e53bb22018-09-14 19:39:15 +020042 KrillCollection.cache = CacheManager.newInstance().getCache("named_vc");
43 vcLoader.loadVCToCache("named-vc1", "/vc/named-vc1.jsonld");
margarethad4c7ff32018-08-27 16:46:22 +020044 testStatisticsWithVCReference();
margaretha9e53bb22018-09-14 19:39:15 +020045
46 // TODO: test auto-caching (disabled in krill)
47 vcLoader.loadVCToCache("named-vc2", "/vc/named-vc2.jsonld");
margaretha1b320452018-08-02 16:56:25 +020048 testSearchWithVCRefNotEqual();
margaretha9e53bb22018-09-14 19:39:15 +020049
margaretha1b320452018-08-02 16:56:25 +020050 // retrieve from cache
51 testSearchWithVCRefEqual();
52 testSearchWithVCRefNotEqual();
margaretha9e53bb22018-09-14 19:39:15 +020053
54 KrillCollection.cache.removeAll();
margarethab097fb02021-02-22 19:28:33 +010055 QueryDO vc = dao.retrieveQueryByName("named-vc1", "system");
56 dao.deleteQuery(vc);
57 vc = dao.retrieveQueryByName("named-vc2", "system");
58 dao.deleteQuery(vc);
margaretha1b320452018-08-02 16:56:25 +020059 }
margarethac7196d22018-08-27 14:20:03 +020060
margaretha1b320452018-08-02 16:56:25 +020061 private void testSearchWithoutVCRefOr () throws KustvaktException {
margarethaee0cbfe2018-08-28 17:47:14 +020062 ClientResponse response = resource().path(API_VERSION).path("search")
margaretha1b320452018-08-02 16:56:25 +020063 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
64 .queryParam("cq",
65 "textSigle=\"GOE/AGF/00000\" | textSigle=\"GOE/AGA/01784\"")
66 .get(ClientResponse.class);
67
68 String ent = response.getEntity(String.class);
69 JsonNode node = JsonUtils.readTree(ent);
70 assertTrue(node.at("/matches").size() > 0);
71 }
margarethac7196d22018-08-27 14:20:03 +020072
margaretha1b320452018-08-02 16:56:25 +020073 private void testSearchWithoutVCRefAnd () throws KustvaktException {
margarethaee0cbfe2018-08-28 17:47:14 +020074 ClientResponse response = resource().path(API_VERSION).path("search")
margaretha1b320452018-08-02 16:56:25 +020075 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
76 .queryParam("cq",
77 "textSigle!=\"GOE/AGI/04846\" & textSigle!=\"GOE/AGA/01784\"")
78 .get(ClientResponse.class);
79
80 String ent = response.getEntity(String.class);
81 JsonNode node = JsonUtils.readTree(ent);
82 assertTrue(node.at("/matches").size() > 0);
83 }
84
85 public void testSearchWithVCRefEqual () throws KustvaktException {
margarethaee0cbfe2018-08-28 17:47:14 +020086 ClientResponse response = resource().path(API_VERSION).path("search")
margaretha1b320452018-08-02 16:56:25 +020087 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
margaretha488e04b2018-09-19 18:17:45 +020088 .queryParam("cq", "referTo \"system/named-vc1\"")
margaretha1b320452018-08-02 16:56:25 +020089 .get(ClientResponse.class);
90
91 String ent = response.getEntity(String.class);
92 JsonNode node = JsonUtils.readTree(ent);
93 assertTrue(node.at("/matches").size() > 0);
margaretha1b320452018-08-02 16:56:25 +020094 }
margarethac7196d22018-08-27 14:20:03 +020095
margaretha1b320452018-08-02 16:56:25 +020096 public void testSearchWithVCRefNotEqual () throws KustvaktException {
margarethaee0cbfe2018-08-28 17:47:14 +020097 ClientResponse response = resource().path(API_VERSION).path("search")
margaretha1b320452018-08-02 16:56:25 +020098 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
99 .queryParam("cq", "referTo named-vc2")
100 .get(ClientResponse.class);
101
102 String ent = response.getEntity(String.class);
103 JsonNode node = JsonUtils.readTree(ent);
104 assertTrue(node.at("/matches").size() > 0);
105 }
106
margarethac7196d22018-08-27 14:20:03 +0200107 public void testStatisticsWithVCReference () throws KustvaktException {
108 String corpusQuery = "availability = /CC-BY.*/ & referTo named-vc1";
margaretha563aabe2018-09-13 20:39:45 +0200109 ClientResponse response = resource().path(API_VERSION)
110 .path("statistics").queryParam("corpusQuery", corpusQuery)
margarethac7196d22018-08-27 14:20:03 +0200111 .get(ClientResponse.class);
112
113 String ent = response.getEntity(String.class);
114 JsonNode node = JsonUtils.readTree(ent);
115 assertEquals(2, node.at("/documents").asInt());
116 }
margaretha4a338122019-01-25 16:02:18 +0100117
118 @Test
119 public void testRefVCNotExist () throws KustvaktException {
120 ClientResponse response = resource().path(API_VERSION).path("search")
121 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
122 .queryParam("cq", "referTo \"username/vc1\"")
123 .get(ClientResponse.class);
124
125 String ent = response.getEntity(String.class);
126 JsonNode node = JsonUtils.readTree(ent);
margarethab5e1e0a2019-01-29 22:11:57 +0100127 assertEquals(StatusCodes.NO_RESOURCE_FOUND,
margaretha4a338122019-01-25 16:02:18 +0100128 node.at("/errors/0/0").asInt());
129 assertEquals("username/vc1", node.at("/errors/0/2").asText());
130 }
131
132 @Test
133 public void testRefNotAuthorized() throws KustvaktException {
134 ClientResponse response = resource().path(API_VERSION).path("search")
135 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
margaretha3a579402019-07-04 15:40:46 +0200136 .queryParam("cq", "referTo \"dory/dory-vc\"")
margaretha4a338122019-01-25 16:02:18 +0100137 .get(ClientResponse.class);
138
139 String ent = response.getEntity(String.class);
140 JsonNode node = JsonUtils.readTree(ent);
141 assertEquals(StatusCodes.AUTHORIZATION_FAILED,
142 node.at("/errors/0/0").asInt());
143 assertEquals("guest", node.at("/errors/0/2").asText());
144 }
margaretha0e1fc552019-08-08 15:31:01 +0200145
146 @Test
147 public void testSearchWithPublishedVCRefGuest () throws KustvaktException {
148 ClientResponse response = resource().path(API_VERSION).path("search")
149 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
150 .queryParam("cq", "referTo \"marlin/published-vc\"")
151 .get(ClientResponse.class);
152
153 String ent = response.getEntity(String.class);
154 JsonNode node = JsonUtils.readTree(ent);
155 assertTrue(node.at("/matches").size() > 0);
156
157 assertEquals("CC-BY.*", node.at("/collection/operands/0/value").asText());
158 assertEquals("koral:doc", node.at("/collection/operands/1/@type").asText());
159 assertEquals("GOE", node.at("/collection/operands/1/value").asText());
160 assertEquals("corpusSigle", node.at("/collection/operands/1/key").asText());
161
162 node = node.at("/collection/operands/1/rewrites");
163 assertEquals(3, node.size());
164 assertEquals("operation:deletion", node.at("/0/operation").asText());
165 assertEquals("@type(koral:docGroupRef)", node.at("/0/scope").asText());
166 assertEquals("operation:deletion", node.at("/1/operation").asText());
167 assertEquals("ref(marlin/published-vc)", node.at("/1/scope").asText());
168 assertEquals("operation:insertion", node.at("/2/operation").asText());
169 }
170
171 @Test
172 public void testSearchWithPublishedVCRef () throws KustvaktException {
173 ClientResponse response = resource().path(API_VERSION).path("search")
174 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
175 .queryParam("cq", "referTo \"marlin/published-vc\"")
176 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
177 .createBasicAuthorizationHeaderValue("squirt", "pass"))
178 .get(ClientResponse.class);
179
180 String ent = response.getEntity(String.class);
181 JsonNode node = JsonUtils.readTree(ent);
182 assertTrue(node.at("/matches").size() > 0);
183
184 // check dory in the hidden group of the vc
185 response = resource().path(API_VERSION).path("group")
186 .path("list").path("system-admin")
187 .queryParam("status", "HIDDEN")
188 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
189 .createBasicAuthorizationHeaderValue("admin", "pass"))
190 .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
191 .get(ClientResponse.class);
192
193 assertEquals(Status.OK.getStatusCode(), response.getStatus());
194 String entity = response.getEntity(String.class);
195 node = JsonUtils.readTree(entity);
196 assertEquals(3, node.at("/0/id").asInt());
197
198 String members = node.at("/0/members").toString();
199 assertTrue(members.contains("\"userId\":\"squirt\""));
200 }
margaretha1b320452018-08-02 16:56:25 +0200201}