| 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; |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 21 | |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 22 | import javax.ws.rs.core.Response; |
| 23 | import javax.ws.rs.core.Response.Status; |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 24 | |
| 25 | import de.ids_mannheim.korap.config.KustvaktConfiguration; |
| 26 | import de.ids_mannheim.korap.config.LiteJerseyTest; |
| 27 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 28 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 29 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 30 | |
| 31 | public class SearchNetworkEndpointTest extends LiteJerseyTest { |
| 32 | |
| 33 | @Autowired |
| 34 | private KustvaktConfiguration config; |
| 35 | |
| 36 | private ClientAndServer mockServer; |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 37 | |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 38 | private MockServerClient mockClient; |
| 39 | |
| 40 | private int port = 6080; |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 41 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 42 | private String searchResult; |
| 43 | |
| 44 | private String endpointURL = "http://localhost:" + port + "/searchEndpoint"; |
| 45 | |
| 46 | public SearchNetworkEndpointTest() throws IOException { |
| 47 | searchResult = IOUtils.toString(ClassLoader.getSystemResourceAsStream("network-output/search-result.jsonld"), StandardCharsets.UTF_8); |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 50 | @BeforeEach |
| 51 | public void startMockServer() { |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 52 | mockServer = startClientAndServer(port); |
| 53 | mockClient = new MockServerClient("localhost", mockServer.getPort()); |
| 54 | } |
| 55 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 56 | @AfterEach |
| 57 | public void stopMockServer() { |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 58 | mockServer.stop(); |
| 59 | } |
| 60 | |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 61 | @Test |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 62 | public void testSearchNetwork() throws IOException, KustvaktException, URISyntaxException { |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 63 | config.setNetworkEndpointURL(endpointURL); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 64 | mockClient.reset().when(request().withMethod("POST").withPath("/searchEndpoint").withHeaders(new Header("Content-Type", "application/json; charset=utf-8"), new Header("Accept", "application/json"))).respond(response().withHeader(new Header("Content-Type", "application/json; charset=utf-8")).withBody(searchResult).withStatusCode(200)); |
| 65 | Response response = target().path(API_VERSION).path("search").queryParam("q", "[orth=der]").queryParam("ql", "poliqarp").queryParam("engine", "network").request().get(); |
| 66 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 67 | String entity = response.readEntity(String.class); |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 68 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 69 | assertEquals(2, node.at("/matches").size()); |
| 70 | } |
| 71 | |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 72 | @Test |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 73 | public void testSearchWithUnknownURL() throws IOException, KustvaktException { |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 74 | config.setNetworkEndpointURL("http://localhost:1040/search"); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 75 | Response response = target().path(API_VERSION).path("search").queryParam("q", "[orth=der]").queryParam("ql", "poliqarp").queryParam("engine", "network").request().get(); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 76 | String entity = response.readEntity(String.class); |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 77 | JsonNode node = JsonUtils.readTree(entity); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 78 | assertEquals(StatusCodes.SEARCH_NETWORK_ENDPOINT_FAILED, node.at("/errors/0/0").asInt()); |
| 79 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 80 | } |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 81 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 82 | @Test |
| 83 | public void testSearchWithUnknownHost() throws KustvaktException { |
| 84 | config.setNetworkEndpointURL("http://search.com"); |
| 85 | Response response = target().path(API_VERSION).path("search").queryParam("q", "[orth=der]").queryParam("ql", "poliqarp").queryParam("engine", "network").request().get(); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 86 | String entity = response.readEntity(String.class); |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 87 | JsonNode node = JsonUtils.readTree(entity); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame^] | 88 | assertEquals(StatusCodes.SEARCH_NETWORK_ENDPOINT_FAILED, node.at("/errors/0/0").asInt()); |
| 89 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| margaretha | 4dee07a | 2022-05-27 11:45:28 +0200 | [diff] [blame] | 90 | } |
| 91 | } |