| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.controller; |
| 2 | |
| margaretha | c7196d2 | 2018-08-27 14:20:03 +0200 | [diff] [blame] | 3 | import static org.junit.Assert.assertEquals; |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 4 | import static org.junit.Assert.assertTrue; |
| 5 | |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 6 | import java.io.IOException; |
| 7 | |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 8 | import org.junit.Test; |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 9 | import org.springframework.beans.factory.annotation.Autowired; |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 10 | |
| 11 | import com.fasterxml.jackson.databind.JsonNode; |
| margaretha | 0e1fc55 | 2019-08-08 15:31:01 +0200 | [diff] [blame] | 12 | import com.google.common.net.HttpHeaders; |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 13 | import com.sun.jersey.api.client.ClientResponse; |
| margaretha | 0e1fc55 | 2019-08-08 15:31:01 +0200 | [diff] [blame] | 14 | import com.sun.jersey.api.client.ClientResponse.Status; |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 15 | |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 16 | import de.ids_mannheim.korap.KrillCollection; |
| margaretha | 0e1fc55 | 2019-08-08 15:31:01 +0200 | [diff] [blame] | 17 | import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler; |
| 18 | import de.ids_mannheim.korap.config.Attributes; |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 19 | import de.ids_mannheim.korap.config.NamedVCLoader; |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 20 | import de.ids_mannheim.korap.config.SpringJerseyTest; |
| margaretha | b097fb0 | 2021-02-22 19:28:33 +0100 | [diff] [blame] | 21 | import de.ids_mannheim.korap.dao.QueryDao; |
| 22 | import de.ids_mannheim.korap.entity.QueryDO; |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 23 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| margaretha | 4a33812 | 2019-01-25 16:02:18 +0100 | [diff] [blame] | 24 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 25 | import de.ids_mannheim.korap.util.QueryException; |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 26 | import de.ids_mannheim.korap.utils.JsonUtils; |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 27 | import net.sf.ehcache.CacheManager; |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 28 | |
| 29 | public class VCReferenceTest extends SpringJerseyTest { |
| 30 | |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 31 | @Autowired |
| 32 | private NamedVCLoader vcLoader; |
| 33 | @Autowired |
| margaretha | b097fb0 | 2021-02-22 19:28:33 +0100 | [diff] [blame] | 34 | private QueryDao dao; |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 35 | |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 36 | @Test |
| margaretha | 4a33812 | 2019-01-25 16:02:18 +0100 | [diff] [blame] | 37 | public void testRefPredefinedVC () |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 38 | throws KustvaktException, IOException, QueryException { |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 39 | testSearchWithoutVCRefOr(); |
| 40 | testSearchWithoutVCRefAnd(); |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 41 | |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 42 | KrillCollection.cache = CacheManager.newInstance().getCache("named_vc"); |
| 43 | vcLoader.loadVCToCache("named-vc1", "/vc/named-vc1.jsonld"); |
| margaretha | d4c7ff3 | 2018-08-27 16:46:22 +0200 | [diff] [blame] | 44 | testStatisticsWithVCReference(); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 45 | |
| 46 | // TODO: test auto-caching (disabled in krill) |
| 47 | vcLoader.loadVCToCache("named-vc2", "/vc/named-vc2.jsonld"); |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 48 | testSearchWithVCRefNotEqual(); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 49 | |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 50 | // retrieve from cache |
| 51 | testSearchWithVCRefEqual(); |
| 52 | testSearchWithVCRefNotEqual(); |
| margaretha | 9e53bb2 | 2018-09-14 19:39:15 +0200 | [diff] [blame] | 53 | |
| 54 | KrillCollection.cache.removeAll(); |
| margaretha | b097fb0 | 2021-02-22 19:28:33 +0100 | [diff] [blame] | 55 | QueryDO vc = dao.retrieveQueryByName("named-vc1", "system"); |
| 56 | dao.deleteQuery(vc); |
| 57 | vc = dao.retrieveQueryByName("named-vc2", "system"); |
| 58 | dao.deleteQuery(vc); |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 59 | } |
| margaretha | c7196d2 | 2018-08-27 14:20:03 +0200 | [diff] [blame] | 60 | |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 61 | private void testSearchWithoutVCRefOr () throws KustvaktException { |
| margaretha | ee0cbfe | 2018-08-28 17:47:14 +0200 | [diff] [blame] | 62 | ClientResponse response = resource().path(API_VERSION).path("search") |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 63 | .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 | } |
| margaretha | c7196d2 | 2018-08-27 14:20:03 +0200 | [diff] [blame] | 72 | |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 73 | private void testSearchWithoutVCRefAnd () throws KustvaktException { |
| margaretha | ee0cbfe | 2018-08-28 17:47:14 +0200 | [diff] [blame] | 74 | ClientResponse response = resource().path(API_VERSION).path("search") |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 75 | .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 { |
| margaretha | ee0cbfe | 2018-08-28 17:47:14 +0200 | [diff] [blame] | 86 | ClientResponse response = resource().path(API_VERSION).path("search") |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 87 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| margaretha | 488e04b | 2018-09-19 18:17:45 +0200 | [diff] [blame] | 88 | .queryParam("cq", "referTo \"system/named-vc1\"") |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 89 | .get(ClientResponse.class); |
| 90 | |
| 91 | String ent = response.getEntity(String.class); |
| 92 | JsonNode node = JsonUtils.readTree(ent); |
| 93 | assertTrue(node.at("/matches").size() > 0); |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 94 | } |
| margaretha | c7196d2 | 2018-08-27 14:20:03 +0200 | [diff] [blame] | 95 | |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 96 | public void testSearchWithVCRefNotEqual () throws KustvaktException { |
| margaretha | ee0cbfe | 2018-08-28 17:47:14 +0200 | [diff] [blame] | 97 | ClientResponse response = resource().path(API_VERSION).path("search") |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 98 | .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 | |
| margaretha | c7196d2 | 2018-08-27 14:20:03 +0200 | [diff] [blame] | 107 | public void testStatisticsWithVCReference () throws KustvaktException { |
| 108 | String corpusQuery = "availability = /CC-BY.*/ & referTo named-vc1"; |
| margaretha | 563aabe | 2018-09-13 20:39:45 +0200 | [diff] [blame] | 109 | ClientResponse response = resource().path(API_VERSION) |
| 110 | .path("statistics").queryParam("corpusQuery", corpusQuery) |
| margaretha | c7196d2 | 2018-08-27 14:20:03 +0200 | [diff] [blame] | 111 | .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 | } |
| margaretha | 4a33812 | 2019-01-25 16:02:18 +0100 | [diff] [blame] | 117 | |
| 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); |
| margaretha | b5e1e0a | 2019-01-29 22:11:57 +0100 | [diff] [blame] | 127 | assertEquals(StatusCodes.NO_RESOURCE_FOUND, |
| margaretha | 4a33812 | 2019-01-25 16:02:18 +0100 | [diff] [blame] | 128 | 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") |
| margaretha | 3a57940 | 2019-07-04 15:40:46 +0200 | [diff] [blame] | 136 | .queryParam("cq", "referTo \"dory/dory-vc\"") |
| margaretha | 4a33812 | 2019-01-25 16:02:18 +0100 | [diff] [blame] | 137 | .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 | } |
| margaretha | 0e1fc55 | 2019-08-08 15:31:01 +0200 | [diff] [blame] | 145 | |
| 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 | } |
| margaretha | 1b32045 | 2018-08-02 16:56:25 +0200 | [diff] [blame] | 201 | } |