| margaretha | 923c207 | 2022-01-31 14:45:47 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.controller; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | |
| 5 | import java.io.IOException; |
| 6 | |
| 7 | import org.apache.http.entity.ContentType; |
| 8 | import org.junit.Test; |
| 9 | import org.springframework.beans.factory.annotation.Autowired; |
| 10 | |
| 11 | import com.fasterxml.jackson.databind.JsonNode; |
| 12 | import com.google.common.net.HttpHeaders; |
| 13 | import com.sun.jersey.api.client.ClientHandlerException; |
| 14 | import com.sun.jersey.api.client.ClientResponse; |
| 15 | import com.sun.jersey.api.client.ClientResponse.Status; |
| 16 | import com.sun.jersey.api.client.UniformInterfaceException; |
| 17 | |
| 18 | import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler; |
| 19 | import de.ids_mannheim.korap.cache.VirtualCorpusCache; |
| 20 | import de.ids_mannheim.korap.config.Attributes; |
| 21 | import de.ids_mannheim.korap.config.NamedVCLoader; |
| 22 | import de.ids_mannheim.korap.dao.QueryDao; |
| 23 | import de.ids_mannheim.korap.entity.QueryDO; |
| 24 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 25 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 26 | import de.ids_mannheim.korap.util.QueryException; |
| 27 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 28 | |
| 29 | public class VirtualCorpusFieldTest extends VirtualCorpusTestBase { |
| 30 | |
| 31 | @Autowired |
| 32 | private NamedVCLoader vcLoader; |
| 33 | @Autowired |
| 34 | private QueryDao dao; |
| 35 | |
| 36 | private JsonNode testRetrieveField (String username, String vcName, |
| 37 | String field) throws UniformInterfaceException, |
| 38 | ClientHandlerException, KustvaktException { |
| 39 | ClientResponse response = resource().path(API_VERSION).path("vc") |
| 40 | .path("field").path("~" + username).path(vcName) |
| 41 | .queryParam("fieldName", field) |
| 42 | .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler |
| margaretha | ad7de72 | 2022-03-01 09:23:14 +0100 | [diff] [blame] | 43 | .createBasicAuthorizationHeaderValue("admin", "pass")) |
| margaretha | 923c207 | 2022-01-31 14:45:47 +0100 | [diff] [blame] | 44 | .header(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON) |
| 45 | .get(ClientResponse.class); |
| 46 | |
| 47 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 48 | String entity = response.getEntity(String.class); |
| 49 | JsonNode node = JsonUtils.readTree(entity); |
| 50 | return node; |
| 51 | } |
| 52 | |
| 53 | private void testRetrieveProhibitedField (String username, String vcName, |
| 54 | String field) throws UniformInterfaceException, |
| 55 | ClientHandlerException, KustvaktException { |
| 56 | ClientResponse response = resource().path(API_VERSION).path("vc") |
| 57 | .path("field").path("~" + username).path(vcName) |
| 58 | .queryParam("fieldName", field) |
| 59 | .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler |
| margaretha | ad7de72 | 2022-03-01 09:23:14 +0100 | [diff] [blame] | 60 | .createBasicAuthorizationHeaderValue("admin", "pass")) |
| margaretha | 923c207 | 2022-01-31 14:45:47 +0100 | [diff] [blame] | 61 | .header(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON) |
| 62 | .get(ClientResponse.class); |
| 63 | |
| 64 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| 65 | String entity = response.getEntity(String.class); |
| 66 | JsonNode node = JsonUtils.readTree(entity); |
| 67 | assertEquals(StatusCodes.NOT_ALLOWED, node.at("/errors/0/0").asInt()); |
| 68 | } |
| 69 | |
| 70 | private void deleteVcFromDB (String vcName) throws KustvaktException { |
| 71 | QueryDO vc = dao.retrieveQueryByName(vcName, "system"); |
| 72 | dao.deleteQuery(vc); |
| 73 | vc = dao.retrieveQueryByName(vcName, "system"); |
| 74 | assertEquals(null, vc); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void testRetrieveFieldsNamedVC1 () |
| 79 | throws IOException, QueryException, KustvaktException { |
| 80 | |
| 81 | vcLoader.loadVCToCache("named-vc1", "/vc/named-vc1.jsonld"); |
| 82 | |
| 83 | JsonNode n = testRetrieveField("system", "named-vc1", "textSigle"); |
| 84 | assertEquals( |
| 85 | "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld", |
| 86 | n.at("/@context").asText()); |
| 87 | assertEquals("textSigle", n.at("/corpus/key").asText()); |
| 88 | assertEquals(2, n.at("/corpus/value").size()); |
| 89 | |
| 90 | n = testRetrieveField("system", "named-vc1", "author"); |
| 91 | assertEquals(2, n.at("/corpus/value").size()); |
| 92 | assertEquals("Goethe, Johann Wolfgang von", |
| 93 | n.at("/corpus/value/0").asText()); |
| 94 | |
| 95 | testRetrieveUnknownTokens(); |
| 96 | testRetrieveProhibitedField("system", "named-vc1", "tokens"); |
| 97 | testRetrieveProhibitedField("system", "named-vc1", "base"); |
| 98 | |
| 99 | VirtualCorpusCache.delete("named-vc1"); |
| 100 | |
| 101 | deleteVcFromDB("named-vc1"); |
| 102 | } |
| 103 | |
| 104 | private void testRetrieveUnknownTokens () throws UniformInterfaceException, |
| 105 | ClientHandlerException, KustvaktException { |
| 106 | JsonNode n = testRetrieveField("system", "named-vc1", "unknown"); |
| 107 | assertEquals("unknown", n.at("/corpus/key").asText()); |
| 108 | assertEquals(0, n.at("/corpus/value").size()); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void testRetrieveTextSigleNamedVC2 () |
| 113 | throws IOException, QueryException, KustvaktException { |
| 114 | vcLoader.loadVCToCache("named-vc2", "/vc/named-vc2.jsonld"); |
| 115 | |
| 116 | JsonNode n = testRetrieveField("system", "named-vc2", "textSigle"); |
| 117 | assertEquals(2, n.at("/corpus/value").size()); |
| 118 | VirtualCorpusCache.delete("named-vc2"); |
| 119 | deleteVcFromDB("named-vc2"); |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | public void testRetrieveTextSigleNamedVC3 () |
| 124 | throws IOException, QueryException, KustvaktException { |
| 125 | vcLoader.loadVCToCache("named-vc3", "/vc/named-vc3.jsonld"); |
| 126 | |
| 127 | JsonNode n = testRetrieveField("system", "named-vc3", "textSigle"); |
| 128 | n = n.at("/corpus/value"); |
| 129 | assertEquals(1, n.size()); |
| 130 | assertEquals("GOE/AGI/00000", n.get(0).asText()); |
| 131 | |
| 132 | VirtualCorpusCache.delete("named-vc3"); |
| 133 | deleteVcFromDB("named-vc3"); |
| 134 | } |
| margaretha | ad7de72 | 2022-03-01 09:23:14 +0100 | [diff] [blame] | 135 | |
| 136 | @Test |
| 137 | public void testRetrieveFieldUnauthorized () throws KustvaktException, IOException, QueryException { |
| 138 | vcLoader.loadVCToCache("named-vc3", "/vc/named-vc3.jsonld"); |
| 139 | |
| 140 | ClientResponse response = resource().path(API_VERSION).path("vc") |
| 141 | .path("field").path("~system").path("named-vc3") |
| 142 | .queryParam("fieldName", "textSigle") |
| 143 | .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler |
| 144 | .createBasicAuthorizationHeaderValue("dory", "pass")) |
| 145 | .header(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON) |
| 146 | .get(ClientResponse.class); |
| 147 | |
| 148 | assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus()); |
| 149 | String entity = response.getEntity(String.class); |
| 150 | JsonNode node = JsonUtils.readTree(entity); |
| 151 | assertEquals(StatusCodes.AUTHORIZATION_FAILED, node.at("/errors/0/0").asInt()); |
| 152 | assertEquals("Unauthorized operation for user: dory", node.at("/errors/0/1").asText()); |
| 153 | |
| 154 | |
| 155 | VirtualCorpusCache.delete("named-vc3"); |
| 156 | deleteVcFromDB("named-vc3"); |
| 157 | } |
| margaretha | 923c207 | 2022-01-31 14:45:47 +0100 | [diff] [blame] | 158 | } |