| margaretha | 92ad2ec | 2023-05-15 14:10:00 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.lite; |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 2 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 4 | import static org.mockserver.integration.ClientAndServer.startClientAndServer; |
| 5 | import static org.mockserver.model.HttpRequest.request; |
| 6 | import static org.mockserver.model.HttpResponse.response; |
| 7 | |
| 8 | import java.io.IOException; |
| 9 | import java.net.URISyntaxException; |
| 10 | import java.nio.charset.StandardCharsets; |
| 11 | |
| 12 | import org.apache.commons.io.IOUtils; |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 13 | import org.junit.jupiter.api.AfterEach; |
| 14 | import org.junit.jupiter.api.BeforeEach; |
| 15 | import org.junit.jupiter.api.Test; |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 16 | import org.mockserver.client.MockServerClient; |
| 17 | import org.mockserver.integration.ClientAndServer; |
| 18 | import org.mockserver.model.Header; |
| 19 | import org.springframework.beans.factory.annotation.Autowired; |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.JsonNode; |
| margaretha | 96c309d | 2023-08-16 12:24:12 +0200 | [diff] [blame] | 21 | import jakarta.ws.rs.core.Response; |
| 22 | import jakarta.ws.rs.core.Response.Status; |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 23 | |
| 24 | import de.ids_mannheim.korap.config.KustvaktConfiguration; |
| 25 | import de.ids_mannheim.korap.config.LiteJerseyTest; |
| 26 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 27 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 28 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 29 | |
| 30 | public class SearchNetworkEndpointTest extends LiteJerseyTest { |
| 31 | |
| 32 | @Autowired |
| 33 | private KustvaktConfiguration config; |
| 34 | |
| 35 | private ClientAndServer mockServer; |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 36 | |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 37 | private MockServerClient mockClient; |
| 38 | |
| 39 | private int port = 6080; |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 40 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 41 | private String searchResult; |
| 42 | |
| 43 | private String endpointURL = "http://localhost:" + port + "/searchEndpoint"; |
| 44 | |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 45 | public SearchNetworkEndpointTest () throws IOException { |
| 46 | searchResult = IOUtils.toString( |
| 47 | ClassLoader.getSystemResourceAsStream( |
| 48 | "network-output/search-result.jsonld"), |
| 49 | StandardCharsets.UTF_8); |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 52 | @BeforeEach |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 53 | public void startMockServer () { |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 54 | mockServer = startClientAndServer(port); |
| 55 | mockClient = new MockServerClient("localhost", mockServer.getPort()); |
| 56 | } |
| 57 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 58 | @AfterEach |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 59 | public void stopMockServer () { |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 60 | mockServer.stop(); |
| 61 | } |
| 62 | |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 63 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 64 | public void testSearchNetwork () |
| 65 | throws IOException, KustvaktException, URISyntaxException { |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 66 | config.setNetworkEndpointURL(endpointURL); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 67 | mockClient.reset() |
| 68 | .when(request().withMethod("POST").withPath("/searchEndpoint") |
| 69 | .withHeaders( |
| 70 | new Header("Content-Type", |
| 71 | "application/json; charset=utf-8"), |
| 72 | new Header("Accept", "application/json"))) |
| 73 | .respond(response() |
| 74 | .withHeader(new Header("Content-Type", |
| 75 | "application/json; charset=utf-8")) |
| 76 | .withBody(searchResult).withStatusCode(200)); |
| 77 | Response response = target().path(API_VERSION).path("search") |
| 78 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| 79 | .queryParam("engine", "network").request().get(); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 80 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 81 | String entity = response.readEntity(String.class); |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 82 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 83 | assertEquals(2, node.at("/matches").size()); |
| 84 | } |
| 85 | |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 86 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 87 | public void testSearchWithUnknownURL () |
| 88 | throws IOException, KustvaktException { |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 89 | config.setNetworkEndpointURL("http://localhost:1040/search"); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 90 | Response response = target().path(API_VERSION).path("search") |
| 91 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| 92 | .queryParam("engine", "network").request().get(); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 93 | String entity = response.readEntity(String.class); |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 94 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 95 | assertEquals(StatusCodes.SEARCH_NETWORK_ENDPOINT_FAILED, |
| 96 | node.at("/errors/0/0").asInt()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 97 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 98 | } |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 99 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 100 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 101 | public void testSearchWithUnknownHost () throws KustvaktException { |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 102 | config.setNetworkEndpointURL("http://search.com"); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 103 | Response response = target().path(API_VERSION).path("search") |
| 104 | .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp") |
| 105 | .queryParam("engine", "network").request().get(); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 106 | String entity = response.readEntity(String.class); |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 107 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 108 | assertEquals(StatusCodes.SEARCH_NETWORK_ENDPOINT_FAILED, |
| 109 | node.at("/errors/0/0").asInt()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 110 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 111 | } |
| 112 | } |