| margaretha | 92ad2ec | 2023-05-15 14:10:00 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.lite; |
| margaretha | 1d1c73e | 2019-12-03 18:07:28 +0100 | [diff] [blame] | 2 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 5 | import static org.mockserver.integration.ClientAndServer.startClientAndServer; |
| 6 | import static org.mockserver.model.HttpRequest.request; |
| 7 | import static org.mockserver.model.HttpResponse.response; |
| margaretha | 1d1c73e | 2019-12-03 18:07:28 +0100 | [diff] [blame] | 8 | |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 9 | import java.io.BufferedReader; |
| margaretha | 1d1c73e | 2019-12-03 18:07:28 +0100 | [diff] [blame] | 10 | import java.io.IOException; |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 11 | import java.io.InputStreamReader; |
| 12 | import java.io.OutputStream; |
| 13 | import java.net.HttpURLConnection; |
| 14 | import java.net.URISyntaxException; |
| 15 | import java.net.URL; |
| 16 | import java.net.URLEncoder; |
| 17 | import java.nio.charset.StandardCharsets; |
| margaretha | 1d1c73e | 2019-12-03 18:07:28 +0100 | [diff] [blame] | 18 | |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 19 | import org.apache.commons.io.IOUtils; |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 20 | import org.junit.jupiter.api.AfterEach; |
| 21 | import org.junit.jupiter.api.BeforeEach; |
| 22 | import org.junit.jupiter.api.Test; |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 23 | import org.mockserver.client.MockServerClient; |
| 24 | import org.mockserver.integration.ClientAndServer; |
| 25 | import org.mockserver.model.Header; |
| margaretha | 1d1c73e | 2019-12-03 18:07:28 +0100 | [diff] [blame] | 26 | import com.fasterxml.jackson.databind.JsonNode; |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 27 | |
| margaretha | 96c309d | 2023-08-16 12:24:12 +0200 | [diff] [blame] | 28 | import jakarta.ws.rs.core.Response; |
| 29 | import jakarta.ws.rs.core.Response.Status; |
| margaretha | 1d1c73e | 2019-12-03 18:07:28 +0100 | [diff] [blame] | 30 | |
| margaretha | 1d1c73e | 2019-12-03 18:07:28 +0100 | [diff] [blame] | 31 | import de.ids_mannheim.korap.config.LiteJerseyTest; |
| 32 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 33 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| margaretha | 1d1c73e | 2019-12-03 18:07:28 +0100 | [diff] [blame] | 34 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 35 | |
| 36 | public class LiteSearchPipeTest extends LiteJerseyTest { |
| 37 | |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 38 | private ClientAndServer mockServer; |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 39 | |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 40 | private MockServerClient mockClient; |
| 41 | |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 42 | private int port = 6070; |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 43 | |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 44 | private String pipeJson, pipeWithParamJson; |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 45 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 46 | private String glemmUri = "http://localhost:" + port + "/glemm"; |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 47 | |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 48 | public LiteSearchPipeTest () throws IOException { |
| 49 | pipeJson = IOUtils.toString( |
| 50 | ClassLoader.getSystemResourceAsStream( |
| 51 | "pipe-output/test-pipes.jsonld"), |
| 52 | StandardCharsets.UTF_8); |
| 53 | pipeWithParamJson = IOUtils.toString( |
| 54 | ClassLoader.getSystemResourceAsStream( |
| 55 | "pipe-output/with-param.jsonld"), |
| 56 | StandardCharsets.UTF_8); |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 59 | @BeforeEach |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 60 | public void startMockServer () { |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 61 | mockServer = startClientAndServer(port); |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 62 | mockClient = new MockServerClient("localhost", mockServer.getPort()); |
| 63 | } |
| 64 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 65 | @AfterEach |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 66 | public void stopMockServer () { |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 67 | mockServer.stop(); |
| 68 | } |
| 69 | |
| margaretha | 1d1c73e | 2019-12-03 18:07:28 +0100 | [diff] [blame] | 70 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 71 | public void testMockServer () throws IOException { |
| 72 | mockClient.reset() |
| 73 | .when(request().withMethod("POST").withPath("/test") |
| 74 | .withHeader(new Header("Content-Type", |
| 75 | "application/json; charset=utf-8"))) |
| 76 | .respond(response() |
| 77 | .withHeader(new Header("Content-Type", |
| 78 | "application/json; charset=utf-8")) |
| 79 | .withBody("{test}").withStatusCode(200)); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 80 | URL url = new URL("http://localhost:" + port + "/test"); |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 81 | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 82 | connection.setRequestMethod("POST"); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 83 | connection.setRequestProperty("Content-Type", |
| 84 | "application/json; charset=UTF-8"); |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 85 | connection.setRequestProperty("Accept", "application/json"); |
| 86 | connection.setDoOutput(true); |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 87 | String json = "{\"name\" : \"dory\"}"; |
| 88 | try (OutputStream os = connection.getOutputStream()) { |
| 89 | byte[] input = json.getBytes("utf-8"); |
| 90 | os.write(input, 0, input.length); |
| 91 | } |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 92 | assertEquals(200, connection.getResponseCode()); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 93 | BufferedReader br = new BufferedReader( |
| 94 | new InputStreamReader(connection.getInputStream(), "utf-8")); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 95 | assertEquals(br.readLine(), "{test}"); |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 99 | public void testSearchWithPipes () |
| 100 | throws IOException, KustvaktException, URISyntaxException { |
| 101 | mockClient.reset() |
| 102 | .when(request().withMethod("POST").withPath("/glemm") |
| 103 | .withHeaders( |
| 104 | new Header("Content-Type", |
| 105 | "application/json; charset=utf-8"), |
| 106 | new Header("Accept", "application/json"))) |
| 107 | .respond(response() |
| 108 | .withHeader(new Header("Content-Type", |
| 109 | "application/json; charset=utf-8")) |
| 110 | .withBody(pipeJson).withStatusCode(200)); |
| 111 | Response response = target().path(API_VERSION).path("search") |
| 112 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| 113 | .queryParam("pipes", glemmUri).request().get(); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 114 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 115 | String entity = response.readEntity(String.class); |
| margaretha | 1d1c73e | 2019-12-03 18:07:28 +0100 | [diff] [blame] | 116 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 117 | assertEquals(2, node.at("/query/wrap/key").size()); |
| margaretha | 1d1c73e | 2019-12-03 18:07:28 +0100 | [diff] [blame] | 118 | node = node.at("/query/wrap/rewrites"); |
| 119 | assertEquals(2, node.size()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 120 | assertEquals(node.at("/0/src").asText(), "Glemm"); |
| 121 | assertEquals(node.at("/0/operation").asText(), "operation:override"); |
| 122 | assertEquals(node.at("/0/scope").asText(), "key"); |
| 123 | assertEquals(node.at("/1/src").asText(), "Kustvakt"); |
| 124 | assertEquals(node.at("/1/operation").asText(), "operation:injection"); |
| 125 | assertEquals(node.at("/1/scope").asText(), "foundry"); |
| margaretha | 1d1c73e | 2019-12-03 18:07:28 +0100 | [diff] [blame] | 126 | } |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 127 | |
| 128 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 129 | public void testSearchWithUrlEncodedPipes () |
| 130 | throws IOException, KustvaktException { |
| 131 | mockClient.reset() |
| 132 | .when(request().withMethod("POST").withPath("/glemm") |
| 133 | .withHeaders( |
| 134 | new Header("Content-Type", |
| 135 | "application/json; charset=utf-8"), |
| 136 | new Header("Accept", "application/json"))) |
| 137 | .respond(response() |
| 138 | .withHeader(new Header("Content-Type", |
| 139 | "application/json; charset=utf-8")) |
| 140 | .withBody(pipeJson).withStatusCode(200)); |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 141 | glemmUri = URLEncoder.encode(glemmUri, "utf-8"); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 142 | Response response = target().path(API_VERSION).path("search") |
| 143 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| 144 | .queryParam("pipes", glemmUri).request().get(); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 145 | String entity = response.readEntity(String.class); |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 146 | JsonNode node = JsonUtils.readTree(entity); |
| 147 | assertEquals(2, node.at("/query/wrap/key").size()); |
| 148 | } |
| 149 | |
| 150 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 151 | public void testSearchWithMultiplePipes () throws KustvaktException { |
| 152 | mockClient.reset() |
| 153 | .when(request().withMethod("POST").withPath("/glemm") |
| 154 | .withQueryStringParameter("param").withHeaders( |
| 155 | new Header("Content-Type", |
| 156 | "application/json; charset=utf-8"), |
| 157 | new Header("Accept", "application/json"))) |
| 158 | .respond(response() |
| 159 | .withHeader(new Header("Content-Type", |
| 160 | "application/json; charset=utf-8")) |
| 161 | .withBody(pipeWithParamJson).withStatusCode(200)); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 162 | String glemmUri2 = glemmUri + "?param=blah"; |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 163 | Response response = target().path(API_VERSION).path("search") |
| 164 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| 165 | .queryParam("pipes", glemmUri + "," + glemmUri2).request() |
| 166 | .get(); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 167 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 168 | String entity = response.readEntity(String.class); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 169 | JsonNode node = JsonUtils.readTree(entity); |
| 170 | assertEquals(3, node.at("/query/wrap/key").size()); |
| 171 | } |
| 172 | |
| 173 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 174 | public void testSearchWithUnknownURL () |
| 175 | throws IOException, KustvaktException { |
| 176 | String url = target().getUri().toString() + API_VERSION |
| 177 | + "/test/tralala"; |
| 178 | Response response = target().path(API_VERSION).path("search") |
| 179 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| 180 | .queryParam("pipes", url).request().get(); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 181 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 182 | String entity = response.readEntity(String.class); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 183 | JsonNode node = JsonUtils.readTree(entity); |
| 184 | assertEquals(StatusCodes.PIPE_FAILED, node.at("/warnings/0/0").asInt()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 185 | assertEquals(node.at("/warnings/0/3").asText(), "404 Not Found"); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 189 | public void testSearchWithUnknownHost () throws KustvaktException { |
| 190 | Response response = target().path(API_VERSION).path("search") |
| 191 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| 192 | .queryParam("pipes", "http://glemm").request().get(); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 193 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 194 | String entity = response.readEntity(String.class); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 195 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 196 | assertEquals(StatusCodes.PIPE_FAILED, node.at("/warnings/0/0").asInt()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 197 | assertEquals(node.at("/warnings/0/3").asText(), "glemm"); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 201 | public void testSearchUnsupportedMediaType () throws KustvaktException { |
| 202 | mockClient.reset() |
| 203 | .when(request().withMethod("POST").withPath("/non-json-pipe")) |
| 204 | .respond(response().withStatusCode(415)); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 205 | String pipeUri = "http://localhost:" + port + "/non-json-pipe"; |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 206 | Response response = target().path(API_VERSION).path("search") |
| 207 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| 208 | .queryParam("pipes", pipeUri).request().get(); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 209 | String entity = response.readEntity(String.class); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 210 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 211 | JsonNode node = JsonUtils.readTree(entity); |
| 212 | assertEquals(StatusCodes.PIPE_FAILED, node.at("/warnings/0/0").asInt()); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 213 | assertEquals(node.at("/warnings/0/3").asText(), |
| 214 | "415 Unsupported Media Type"); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 218 | public void testSearchWithMultiplePipeWarnings () throws KustvaktException { |
| 219 | String url = target().getUri().toString() + API_VERSION |
| 220 | + "/test/tralala"; |
| 221 | Response response = target().path(API_VERSION).path("search") |
| 222 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| 223 | .queryParam("pipes", url + "," + "http://glemm").request() |
| 224 | .get(); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 225 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 226 | String entity = response.readEntity(String.class); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 227 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 228 | assertEquals(2, node.at("/warnings").size()); |
| 229 | assertEquals(StatusCodes.PIPE_FAILED, node.at("/warnings/0/0").asInt()); |
| 230 | assertEquals(url, node.at("/warnings/0/2").asText()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 231 | assertEquals(node.at("/warnings/0/3").asText(), "404 Not Found"); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 232 | assertEquals(StatusCodes.PIPE_FAILED, node.at("/warnings/1/0").asInt()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 233 | assertEquals(node.at("/warnings/1/2").asText(), "http://glemm"); |
| 234 | assertEquals(node.at("/warnings/1/3").asText(), "glemm"); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 238 | public void testSearchWithInvalidJsonResponse () throws KustvaktException { |
| 239 | mockClient.reset() |
| 240 | .when(request().withMethod("POST").withPath("/invalid-response") |
| 241 | .withHeaders( |
| 242 | new Header("Content-Type", |
| 243 | "application/json; charset=utf-8"), |
| 244 | new Header("Accept", "application/json"))) |
| 245 | .respond(response().withBody("{blah:}").withStatusCode(200) |
| 246 | .withHeaders(new Header("Content-Type", |
| 247 | "application/json; charset=utf-8"))); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 248 | String pipeUri = "http://localhost:" + port + "/invalid-response"; |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 249 | Response response = target().path(API_VERSION).path("search") |
| 250 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| 251 | .queryParam("pipes", pipeUri).request().get(); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 252 | String entity = response.readEntity(String.class); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 253 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 254 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 255 | assertEquals(StatusCodes.DESERIALIZATION_FAILED, |
| 256 | node.at("/errors/0/0").asInt()); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 260 | public void testSearchWithPlainTextResponse () throws KustvaktException { |
| 261 | mockClient.reset() |
| 262 | .when(request().withMethod("POST").withPath("/plain-text") |
| 263 | .withHeaders( |
| 264 | new Header("Content-Type", |
| 265 | "application/json; charset=utf-8"), |
| 266 | new Header("Accept", "application/json"))) |
| 267 | .respond(response().withBody("blah").withStatusCode(200)); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 268 | String pipeUri = "http://localhost:" + port + "/plain-text"; |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 269 | Response response = target().path(API_VERSION).path("search") |
| 270 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| 271 | .queryParam("pipes", pipeUri).request().get(); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 272 | String entity = response.readEntity(String.class); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 273 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 274 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 275 | assertEquals(StatusCodes.DESERIALIZATION_FAILED, |
| 276 | node.at("/errors/0/0").asInt()); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 280 | public void testSearchWithMultipleAndUnknownPipes () |
| 281 | throws KustvaktException { |
| 282 | mockClient.reset() |
| 283 | .when(request().withMethod("POST").withPath("/glemm") |
| 284 | .withHeaders( |
| 285 | new Header("Content-Type", |
| 286 | "application/json; charset=utf-8"), |
| 287 | new Header("Accept", "application/json"))) |
| 288 | .respond(response() |
| 289 | .withHeader(new Header("Content-Type", |
| 290 | "application/json; charset=utf-8")) |
| 291 | .withBody(pipeJson).withStatusCode(200)); |
| 292 | Response response = target().path(API_VERSION).path("search") |
| 293 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| 294 | .queryParam("pipes", "http://unknown" + "," + glemmUri) |
| 295 | .request().get(); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 296 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 297 | String entity = response.readEntity(String.class); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 298 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 299 | assertEquals(2, node.at("/query/wrap/key").size()); |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 300 | assertTrue(node.at("/warnings").isMissingNode()); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 301 | response = target().path(API_VERSION).path("search") |
| 302 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| 303 | .queryParam("pipes", glemmUri + ",http://unknown").request() |
| 304 | .get(); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 305 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 306 | entity = response.readEntity(String.class); |
| margaretha | 0ddaed6 | 2021-01-21 17:10:33 +0100 | [diff] [blame] | 307 | node = JsonUtils.readTree(entity); |
| 308 | assertEquals(StatusCodes.PIPE_FAILED, node.at("/warnings/0/0").asInt()); |
| margaretha | 137d4ee | 2019-12-13 14:49:15 +0100 | [diff] [blame] | 309 | } |
| margaretha | 1d1c73e | 2019-12-03 18:07:28 +0100 | [diff] [blame] | 310 | } |