| margaretha | 2544cdf | 2019-07-08 11:39:43 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.controller; |
| 2 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| margaretha | 8489f86 | 2025-02-05 11:32:16 +0100 | [diff] [blame] | 4 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 5 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| margaretha | 2544cdf | 2019-07-08 11:39:43 +0200 | [diff] [blame] | 6 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 7 | import org.junit.jupiter.api.Disabled; |
| 8 | import org.junit.jupiter.api.Test; |
| margaretha | 8489f86 | 2025-02-05 11:32:16 +0100 | [diff] [blame] | 9 | |
| margaretha | 2544cdf | 2019-07-08 11:39:43 +0200 | [diff] [blame] | 10 | import com.fasterxml.jackson.databind.JsonNode; |
| 11 | import com.google.common.net.HttpHeaders; |
| margaretha | 2544cdf | 2019-07-08 11:39:43 +0200 | [diff] [blame] | 12 | |
| 13 | import de.ids_mannheim.korap.config.SpringJerseyTest; |
| 14 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 15 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 16 | import de.ids_mannheim.korap.query.serialize.MetaQueryBuilder; |
| 17 | import de.ids_mannheim.korap.query.serialize.QuerySerializer; |
| 18 | import de.ids_mannheim.korap.utils.JsonUtils; |
| margaretha | 8489f86 | 2025-02-05 11:32:16 +0100 | [diff] [blame] | 19 | import jakarta.ws.rs.client.Entity; |
| 20 | import jakarta.ws.rs.core.Response; |
| 21 | import jakarta.ws.rs.core.Response.Status; |
| margaretha | 2544cdf | 2019-07-08 11:39:43 +0200 | [diff] [blame] | 22 | |
| margaretha | 9c6493e | 2022-01-25 11:54:29 +0100 | [diff] [blame] | 23 | public class SearchPublicMetadataTest extends SpringJerseyTest { |
| margaretha | 2544cdf | 2019-07-08 11:39:43 +0200 | [diff] [blame] | 24 | |
| 25 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 26 | public void testSearchPublicMetadata () throws KustvaktException { |
| 27 | Response response = target().path(API_VERSION).path("search") |
| 28 | .queryParam("q", "Sonne").queryParam("ql", "poliqarp") |
| 29 | .queryParam("access-rewrite-disabled", "true").request().get(); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 30 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 31 | String entity = response.readEntity(String.class); |
| margaretha | 2544cdf | 2019-07-08 11:39:43 +0200 | [diff] [blame] | 32 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 8489f86 | 2025-02-05 11:32:16 +0100 | [diff] [blame] | 33 | assertTrue(node.at("/meta/snippet").isMissingNode()); |
| 34 | assertEquals(allCorpusAccess, |
| 35 | node.at("/collection/rewrites/0/_comment").asText()); |
| 36 | assertFalse(node.at("/matches/0/snippet").asBoolean()); |
| 37 | assertFalse(node.at("/matches/0/tokens").asBoolean()); |
| margaretha | 2544cdf | 2019-07-08 11:39:43 +0200 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 41 | public void testSearchPublicMetadataExtern () throws KustvaktException { |
| 42 | Response response = target().path(API_VERSION).path("search") |
| 43 | .queryParam("q", "Sonne").queryParam("ql", "poliqarp") |
| 44 | .queryParam("access-rewrite-disabled", "true").request() |
| 45 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32").get(); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 46 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 47 | String entity = response.readEntity(String.class); |
| margaretha | 2544cdf | 2019-07-08 11:39:43 +0200 | [diff] [blame] | 48 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 8489f86 | 2025-02-05 11:32:16 +0100 | [diff] [blame] | 49 | assertEquals("All corpus access policy has been added.", |
| 50 | node.at("/collection/rewrites/0/_comment").asText()); |
| margaretha | 2544cdf | 2019-07-08 11:39:43 +0200 | [diff] [blame] | 51 | assertTrue(node.at("/matches/0/snippet").isMissingNode()); |
| 52 | } |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 53 | |
| margaretha | 8596764 | 2019-11-13 13:35:33 +0100 | [diff] [blame] | 54 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 55 | public void testSearchPublicMetadataWithCustomFields () |
| 56 | throws KustvaktException { |
| 57 | Response response = target().path(API_VERSION).path("search") |
| 58 | .queryParam("q", "Sonne").queryParam("ql", "poliqarp") |
| 59 | .queryParam("fields", "author,title") |
| 60 | .queryParam("access-rewrite-disabled", "true").request().get(); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 61 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 62 | String entity = response.readEntity(String.class); |
| margaretha | 8596764 | 2019-11-13 13:35:33 +0100 | [diff] [blame] | 63 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 8489f86 | 2025-02-05 11:32:16 +0100 | [diff] [blame] | 64 | assertEquals(allCorpusAccess, |
| 65 | node.at("/collection/rewrites/0/_comment").asText()); |
| margaretha | 8596764 | 2019-11-13 13:35:33 +0100 | [diff] [blame] | 66 | assertTrue(node.at("/matches/0/snippet").isMissingNode()); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 67 | assertEquals(node.at("/matches/0/author").asText(), |
| 68 | "Goethe, Johann Wolfgang von"); |
| 69 | assertEquals(node.at("/matches/0/title").asText(), |
| 70 | "Italienische Reise"); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 71 | // assertEquals(3, node.at("/matches/0").size()); |
| margaretha | 8596764 | 2019-11-13 13:35:33 +0100 | [diff] [blame] | 72 | } |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 73 | |
| margaretha | 8596764 | 2019-11-13 13:35:33 +0100 | [diff] [blame] | 74 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 75 | public void testSearchPublicMetadataWithNonPublicField () |
| 76 | throws KustvaktException { |
| 77 | Response response = target().path(API_VERSION).path("search") |
| 78 | .queryParam("q", "Sonne").queryParam("ql", "poliqarp") |
| 79 | .queryParam("fields", "author,title,snippet") |
| 80 | .queryParam("access-rewrite-disabled", "true").request().get(); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 81 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 82 | String entity = response.readEntity(String.class); |
| margaretha | 8596764 | 2019-11-13 13:35:33 +0100 | [diff] [blame] | 83 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 84 | assertEquals(StatusCodes.NON_PUBLIC_FIELD_IGNORED, |
| 85 | node.at("/warnings/0/0").asInt()); |
| 86 | assertEquals(node.at("/warnings/0/1").asText(), |
| 87 | "The requested non public fields are ignored"); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 88 | assertEquals(node.at("/warnings/0/2").asText(), "snippet"); |
| margaretha | 8596764 | 2019-11-13 13:35:33 +0100 | [diff] [blame] | 89 | } |
| margaretha | 2544cdf | 2019-07-08 11:39:43 +0200 | [diff] [blame] | 90 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 91 | // EM: The API is disabled |
| 92 | @Disabled |
| margaretha | 2544cdf | 2019-07-08 11:39:43 +0200 | [diff] [blame] | 93 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 94 | public void testSearchPostPublicMetadata () throws KustvaktException { |
| margaretha | 2544cdf | 2019-07-08 11:39:43 +0200 | [diff] [blame] | 95 | QuerySerializer s = new QuerySerializer(); |
| 96 | s.setQuery("[orth=der]", "poliqarp"); |
| 97 | s.setCollection("corpusSigle=GOE"); |
| 98 | s.setQuery("Wasser", "poliqarp"); |
| margaretha | 2544cdf | 2019-07-08 11:39:43 +0200 | [diff] [blame] | 99 | MetaQueryBuilder meta = new MetaQueryBuilder(); |
| 100 | meta.addEntry("snippets", "true"); |
| 101 | s.setMeta(meta); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 102 | Response response = target().path(API_VERSION).path("search").request() |
| 103 | .post(Entity.json(s.toJSON())); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 104 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 105 | String ent = response.readEntity(String.class); |
| margaretha | 2544cdf | 2019-07-08 11:39:43 +0200 | [diff] [blame] | 106 | JsonNode node = JsonUtils.readTree(ent); |
| margaretha | 8489f86 | 2025-02-05 11:32:16 +0100 | [diff] [blame] | 107 | assertEquals(allCorpusAccess, |
| 108 | node.at("/collection/rewrites/0/_comment").asText()); |
| margaretha | 2544cdf | 2019-07-08 11:39:43 +0200 | [diff] [blame] | 109 | assertTrue(node.at("/matches/0/snippet").isMissingNode()); |
| 110 | } |
| margaretha | 2544cdf | 2019-07-08 11:39:43 +0200 | [diff] [blame] | 111 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 112 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 113 | public void testSearchPublicMetadataWithSystemVC () |
| 114 | throws KustvaktException { |
| 115 | Response response = target().path(API_VERSION).path("search") |
| 116 | .queryParam("q", "Sonne").queryParam("ql", "poliqarp") |
| 117 | .queryParam("cq", "referTo system-vc") |
| 118 | .queryParam("access-rewrite-disabled", "true").request().get(); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 119 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 120 | String entity = response.readEntity(String.class); |
| margaretha | 45a6509 | 2025-03-24 09:20:02 +0100 | [diff] [blame^] | 121 | JsonNode node = JsonUtils.readTree(entity).at("/collection"); |
| 122 | assertEquals("operation:and",node.at("/operation").asText()); |
| 123 | |
| 124 | assertEquals("operation:override", node.at("/rewrites/0/operation").asText()); |
| margaretha | d59b34d | 2024-12-03 10:31:30 +0100 | [diff] [blame] | 125 | assertEquals("koral:docGroupRef", |
| margaretha | 4d06c12 | 2025-03-05 10:43:16 +0100 | [diff] [blame] | 126 | node.at("/rewrites/0/original/@type").asText()); |
| 127 | assertEquals("system-vc", node.at("/rewrites/0/original/ref").asText()); |
| margaretha | 45a6509 | 2025-03-24 09:20:02 +0100 | [diff] [blame^] | 128 | |
| 129 | node = node.at("/operands/1"); |
| 130 | assertEquals("koral:docGroupRef", node.at("/@type").asText()); |
| 131 | assertEquals("system-vc", node.at("/ref").asText()); |
| 132 | // assertEquals(node.at("/value").asText(), "GOE"); |
| 133 | // assertEquals(node.at("/match").asText(), "match:eq"); |
| 134 | // assertEquals(node.at("/key").asText(), "corpusSigle"); |
| 135 | |
| margaretha | 2544cdf | 2019-07-08 11:39:43 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 139 | public void testSearchPublicMetadataWithPrivateVC () |
| 140 | throws KustvaktException { |
| 141 | Response response = target().path(API_VERSION).path("search") |
| 142 | .queryParam("q", "Sonne").queryParam("ql", "poliqarp") |
| 143 | .queryParam("cq", "referTo \"dory/dory-vc\"") |
| 144 | .queryParam("access-rewrite-disabled", "true").request().get(); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 145 | String entity = response.readEntity(String.class); |
| margaretha | 2544cdf | 2019-07-08 11:39:43 +0200 | [diff] [blame] | 146 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 147 | assertEquals(StatusCodes.AUTHORIZATION_FAILED, |
| 148 | node.at("/errors/0/0").asInt()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 149 | assertEquals(node.at("/errors/0/2").asText(), "guest"); |
| margaretha | 2544cdf | 2019-07-08 11:39:43 +0200 | [diff] [blame] | 150 | } |
| 151 | } |