From 32d911a99c75d88ece24e21b00d6c9f7dba5254c Mon Sep 17 00:00:00 2001 From: Stig Norland Date: Wed, 8 Jan 2025 12:38:33 +0100 Subject: [PATCH] renamed files --- .../nva/search/SearchResourceAuthHandler.java | 26 +- .../nva/search/SearchResourceHandler.java | 70 ++- .../SearchResourceLegacyHandlerTest.java | 464 +++++++++--------- 3 files changed, 273 insertions(+), 287 deletions(-) diff --git a/search-handlers/src/main/java/no/unit/nva/search/SearchResourceAuthHandler.java b/search-handlers/src/main/java/no/unit/nva/search/SearchResourceAuthHandler.java index 61711f54a..1d1ff4842 100644 --- a/search-handlers/src/main/java/no/unit/nva/search/SearchResourceAuthHandler.java +++ b/search-handlers/src/main/java/no/unit/nva/search/SearchResourceAuthHandler.java @@ -14,14 +14,14 @@ import com.amazonaws.services.lambda.runtime.Context; import com.google.common.net.MediaType; - +import java.net.HttpURLConnection; +import java.util.List; import no.unit.nva.search.common.ContentTypeUtils; import no.unit.nva.search.common.records.JsonNodeMutator; import no.unit.nva.search.resource.LegacyMutator; import no.unit.nva.search.resource.ResourceClient; import no.unit.nva.search.resource.ResourceSearchQuery; import no.unit.nva.search.resource.SimplifiedMutator; - import nva.commons.apigateway.AccessRight; import nva.commons.apigateway.ApiGatewayHandler; import nva.commons.apigateway.RequestInfo; @@ -31,9 +31,6 @@ import nva.commons.core.Environment; import nva.commons.core.JacocoGenerated; -import java.net.HttpURLConnection; -import java.util.List; - /** * Handler for searching resources. * @@ -58,15 +55,15 @@ protected List listSupportedMediaTypes() { return DEFAULT_RESPONSE_MEDIA_TYPES; } - @Override - protected void validateRequest(Void unused, RequestInfo requestInfo, Context context) - throws ApiGatewayException { + @Override + protected void validateRequest(Void unused, RequestInfo requestInfo, Context context) + throws ApiGatewayException { validateAccessRight(requestInfo.getAccessRights()); } - @Override - protected String processInput(Void input, RequestInfo requestInfo, Context context) - throws BadRequestException, UnauthorizedException { + @Override + protected String processInput(Void input, RequestInfo requestInfo, Context context) + throws BadRequestException, UnauthorizedException { var version = ContentTypeUtils.extractVersionFromRequestInfo(requestInfo); return ResourceSearchQuery.builder() @@ -90,11 +87,8 @@ protected Integer getSuccessStatusCode(Void input, String output) { } private void validateAccessRight(List accessRights) throws UnauthorizedException { - if (accessRights.contains(AccessRight.MANAGE_RESOURCES_ALL) - || accessRights.contains(AccessRight.MANAGE_CUSTOMERS) - // || accessRights.contains(AccessRight.MANAGE_OWN_AFFILIATION) - // || accessRights.contains(AccessRight.MANAGE_RESOURCES_STANDARD) - ) { + if (accessRights.contains(AccessRight.MANAGE_RESOURCES_ALL) + || accessRights.contains(AccessRight.MANAGE_CUSTOMERS)) { return; } throw new UnauthorizedException(); diff --git a/search-handlers/src/main/java/no/unit/nva/search/SearchResourceHandler.java b/search-handlers/src/main/java/no/unit/nva/search/SearchResourceHandler.java index e0d2456ad..2469c5558 100644 --- a/search-handlers/src/main/java/no/unit/nva/search/SearchResourceHandler.java +++ b/search-handlers/src/main/java/no/unit/nva/search/SearchResourceHandler.java @@ -12,27 +12,23 @@ import com.amazonaws.services.lambda.runtime.Context; import com.google.common.net.MediaType; - +import java.net.HttpURLConnection; +import java.util.List; +import java.util.Map; +import java.util.function.Supplier; import no.unit.nva.search.common.ContentTypeUtils; import no.unit.nva.search.common.records.JsonNodeMutator; import no.unit.nva.search.resource.LegacyMutator; import no.unit.nva.search.resource.ResourceClient; import no.unit.nva.search.resource.ResourceSearchQuery; import no.unit.nva.search.resource.SimplifiedMutator; - import nva.commons.apigateway.ApiGatewayHandler; import nva.commons.apigateway.RequestInfo; import nva.commons.apigateway.exceptions.BadRequestException; import nva.commons.core.Environment; import nva.commons.core.JacocoGenerated; - import org.apache.http.HttpHeaders; -import java.net.HttpURLConnection; -import java.util.List; -import java.util.Map; -import java.util.function.Supplier; - /** * Handler for searching resources. * @@ -40,31 +36,31 @@ */ public class SearchResourceHandler extends ApiGatewayHandler { - private final ResourceClient opensearchClient; + private final ResourceClient opensearchClient; - @JacocoGenerated - public SearchResourceHandler() { - this(new Environment(), defaultClient()); - } + @JacocoGenerated + public SearchResourceHandler() { + this(new Environment(), defaultClient()); + } - public SearchResourceHandler(Environment environment, ResourceClient resourceClient) { - super(Void.class, environment); - this.opensearchClient = resourceClient; - } + public SearchResourceHandler(Environment environment, ResourceClient resourceClient) { + super(Void.class, environment); + this.opensearchClient = resourceClient; + } - @Override - protected List listSupportedMediaTypes() { - return DEFAULT_RESPONSE_MEDIA_TYPES; - } + @Override + protected List listSupportedMediaTypes() { + return DEFAULT_RESPONSE_MEDIA_TYPES; + } - @Override - protected void validateRequest(Void unused, RequestInfo requestInfo, Context context) { - // Do nothing - } + @Override + protected void validateRequest(Void unused, RequestInfo requestInfo, Context context) { + // Do nothing + } - @Override - protected String processInput(Void input, RequestInfo requestInfo, Context context) - throws BadRequestException { + @Override + protected String processInput(Void input, RequestInfo requestInfo, Context context) + throws BadRequestException { var version = ContentTypeUtils.extractVersionFromRequestInfo(requestInfo); return ResourceSearchQuery.builder() @@ -79,12 +75,12 @@ protected String processInput(Void input, RequestInfo requestInfo, Context conte .doSearch(opensearchClient) .withMutator(getMutator(version)) .toString(); - } + } - @Override - protected void addAdditionalHeaders(Supplier> additionalHeaders) { - super.addAdditionalHeaders(() -> Map.of(HttpHeaders.VARY, HttpHeaders.ACCEPT)); - } + @Override + protected void addAdditionalHeaders(Supplier> additionalHeaders) { + super.addAdditionalHeaders(() -> Map.of(HttpHeaders.VARY, HttpHeaders.ACCEPT)); + } private List getIncludedFields(String version) { return V_2024_12_01_SIMPLER_MODEL.equals(version) @@ -98,8 +94,8 @@ private JsonNodeMutator getMutator(String version) { : new LegacyMutator(); } - @Override - protected Integer getSuccessStatusCode(Void input, String output) { - return HttpURLConnection.HTTP_OK; - } + @Override + protected Integer getSuccessStatusCode(Void input, String output) { + return HttpURLConnection.HTTP_OK; + } } diff --git a/search-handlers/src/test/java/no/unit/nva/search/SearchResourceLegacyHandlerTest.java b/search-handlers/src/test/java/no/unit/nva/search/SearchResourceLegacyHandlerTest.java index 548970657..0bdcaeb19 100644 --- a/search-handlers/src/test/java/no/unit/nva/search/SearchResourceLegacyHandlerTest.java +++ b/search-handlers/src/test/java/no/unit/nva/search/SearchResourceLegacyHandlerTest.java @@ -57,245 +57,241 @@ class SearchResourceLegacyHandlerTest { - public static final String SAMPLE_PATH = "search"; - public static final String SAMPLE_DOMAIN_NAME = "localhost"; - public static final String SAMPLE_SEARCH_TERM = "searchTerm"; - public static final String SAMPLE_OPENSEARCH_RESPONSE_WITH_AGGREGATION_JSON = - "sample_opensearch_response.json"; - public static final String EMPTY_OPENSEARCH_RESPONSE_JSON = "empty_opensearch_response.json"; + public static final String SAMPLE_PATH = "search"; + public static final String SAMPLE_DOMAIN_NAME = "localhost"; + public static final String SAMPLE_SEARCH_TERM = "searchTerm"; + public static final String SAMPLE_OPENSEARCH_RESPONSE_WITH_AGGREGATION_JSON = + "sample_opensearch_response.json"; + public static final String EMPTY_OPENSEARCH_RESPONSE_JSON = "empty_opensearch_response.json"; private SearchResourceHandler handler; - private Context contextMock; - private ByteArrayOutputStream outputStream; - private ResourceClient mockedSearchClient; - - private static ExportCsv csvWithFullDate() { - var id = randomUri().toString(); - var title = randomString(); - var type = "AcademicArticle"; - var contributors = List.of(randomString(), randomString(), randomString()); - var date = "2022-01-22"; - - return new ExportCsv() - .withId(id) - .withMainTitle(title) - .withPublicationInstance(type) - .withPublicationDate(date) - .withContributors(String.join(COMMA, contributors)); - } - - public static Stream acceptHeaderValuesProducingTextCsvProvider() { - return Stream.of("text/*", Words.TEXT_CSV); - } - - public static Stream acceptHeaderValuesProducingApplicationJsonProvider() { - return Stream.of(null, "application/json", "application/json; charset=utf-8"); - } - - @BeforeEach - void setUp() { - - mockedSearchClient = mock(ResourceClient.class); + private Context contextMock; + private ByteArrayOutputStream outputStream; + private ResourceClient mockedSearchClient; + + private static ExportCsv csvWithFullDate() { + var id = randomUri().toString(); + var title = randomString(); + var type = "AcademicArticle"; + var contributors = List.of(randomString(), randomString(), randomString()); + var date = "2022-01-22"; + + return new ExportCsv() + .withId(id) + .withMainTitle(title) + .withPublicationInstance(type) + .withPublicationDate(date) + .withContributors(String.join(COMMA, contributors)); + } + + public static Stream acceptHeaderValuesProducingTextCsvProvider() { + return Stream.of("text/*", Words.TEXT_CSV); + } + + public static Stream acceptHeaderValuesProducingApplicationJsonProvider() { + return Stream.of(null, "application/json", "application/json; charset=utf-8"); + } + + @BeforeEach + void setUp() { + + mockedSearchClient = mock(ResourceClient.class); handler = new SearchResourceHandler(new Environment(), mockedSearchClient); - contextMock = mock(Context.class); - outputStream = new ByteArrayOutputStream(); - } - - @ParameterizedTest(name = "should return text/csv for accept header {0}") - @MethodSource("acceptHeaderValuesProducingTextCsvProvider") - void shouldReturnTextCsvWithGivenAcceptHeader(String acceptHeaderValue) throws IOException { - prepareRestHighLevelClientOkResponse(List.of(csvWithFullDate(), csvWithYearOnly())); - handler.handleRequest( - getRequestInputStreamAccepting(acceptHeaderValue), - outputStream, - mock(Context.class)); - - GatewayResponse gatewayResponse = - GatewayResponse.fromOutputStream(outputStream, String.class); - assertThat( - gatewayResponse.getHeaders().get("Content-Type"), - is(equalTo("text/csv; charset=utf-8"))); - } - - private ExportCsv csvWithYearOnly() { - var id = randomUri().toString(); - var title = randomString(); - var type = "AcademicArticle"; - var contributors = List.of(randomString(), randomString(), randomString()); - var date = "2022"; - - return new ExportCsv() - .withId(id) - .withMainTitle(title) - .withPublicationInstance(type) - .withPublicationDate(date) - .withContributors(String.join(COMMA, contributors)); - } - - @Test - void shouldReturnSortedSearchResultsWhenSendingContributorId() throws IOException { - prepareRestHighLevelClientOkResponse(); - handler.handleRequest(getInputStreamWithContributorId(), outputStream, contextMock); - - var gatewayResponse = FakeGatewayResponse.of(outputStream); - - assertNotNull(gatewayResponse.headers()); - assertEquals(HTTP_OK, gatewayResponse.statusCode()); - } - - @Test - void shouldNotReturnSortedSearchResultsWhenSendingMultipleContributorId() throws IOException { - prepareRestHighLevelClientOkResponse(); - - handler.handleRequest(getInputStreamWithMultipleContributorId(), outputStream, contextMock); - - var gatewayResponse = FakeGatewayResponse.of(outputStream); - - assertNotNull(gatewayResponse.headers()); - assertEquals(HTTP_OK, gatewayResponse.statusCode()); - } - - @Test - void shouldReturnResultWithEqualContributorAndContributorPreview() throws IOException { - prepareRestHighLevelClientOkResponse(); - - handler.handleRequest(getInputStreamWithMultipleContributorId(), outputStream, contextMock); - - var gatewayResponse = FakeGatewayResponse.of(outputStream); - var actualBody = gatewayResponse.body(); - var firstHit = actualBody.hits().getFirst(); - - assertNotNull(gatewayResponse.headers()); - assertEquals(HTTP_OK, gatewayResponse.statusCode()); + contextMock = mock(Context.class); + outputStream = new ByteArrayOutputStream(); + } + + @ParameterizedTest(name = "should return text/csv for accept header {0}") + @MethodSource("acceptHeaderValuesProducingTextCsvProvider") + void shouldReturnTextCsvWithGivenAcceptHeader(String acceptHeaderValue) throws IOException { + prepareRestHighLevelClientOkResponse(List.of(csvWithFullDate(), csvWithYearOnly())); + handler.handleRequest( + getRequestInputStreamAccepting(acceptHeaderValue), outputStream, mock(Context.class)); + + GatewayResponse gatewayResponse = + GatewayResponse.fromOutputStream(outputStream, String.class); + assertThat( + gatewayResponse.getHeaders().get("Content-Type"), is(equalTo("text/csv; charset=utf-8"))); + } + + private ExportCsv csvWithYearOnly() { + var id = randomUri().toString(); + var title = randomString(); + var type = "AcademicArticle"; + var contributors = List.of(randomString(), randomString(), randomString()); + var date = "2022"; + + return new ExportCsv() + .withId(id) + .withMainTitle(title) + .withPublicationInstance(type) + .withPublicationDate(date) + .withContributors(String.join(COMMA, contributors)); + } + + @Test + void shouldReturnSortedSearchResultsWhenSendingContributorId() throws IOException { + prepareRestHighLevelClientOkResponse(); + handler.handleRequest(getInputStreamWithContributorId(), outputStream, contextMock); + + var gatewayResponse = FakeGatewayResponse.of(outputStream); + + assertNotNull(gatewayResponse.headers()); + assertEquals(HTTP_OK, gatewayResponse.statusCode()); + } + + @Test + void shouldNotReturnSortedSearchResultsWhenSendingMultipleContributorId() throws IOException { + prepareRestHighLevelClientOkResponse(); + + handler.handleRequest(getInputStreamWithMultipleContributorId(), outputStream, contextMock); + + var gatewayResponse = FakeGatewayResponse.of(outputStream); + + assertNotNull(gatewayResponse.headers()); + assertEquals(HTTP_OK, gatewayResponse.statusCode()); + } + + @Test + void shouldReturnResultWithEqualContributorAndContributorPreview() throws IOException { + prepareRestHighLevelClientOkResponse(); + + handler.handleRequest(getInputStreamWithMultipleContributorId(), outputStream, contextMock); + + var gatewayResponse = FakeGatewayResponse.of(outputStream); + var actualBody = gatewayResponse.body(); + var firstHit = actualBody.hits().getFirst(); + + assertNotNull(gatewayResponse.headers()); + assertEquals(HTTP_OK, gatewayResponse.statusCode()); assertFalse(firstHit.path(ENTITY_DESCRIPTION).path(CONTRIBUTORS).isMissingNode()); assertNotNull(firstHit.path(ENTITY_DESCRIPTION).path(CONTRIBUTORS).get(0)); assertEquals( firstHit.path(ENTITY_DESCRIPTION).path(CONTRIBUTORS), firstHit.path(ENTITY_DESCRIPTION).path("contributorsPreview")); - } - - @Test - void shouldReturnSearchResultsWithEmptyHitsWhenQueryResultIsEmpty() throws IOException { - prepareRestHighLevelClientEmptyResponse(); - - handler.handleRequest(getInputStream(), outputStream, mock(Context.class)); - - var gatewayResponse = FakeGatewayResponse.of(outputStream); - var actualBody = gatewayResponse.body(); - - assertNotNull(gatewayResponse.headers()); - assertEquals(HTTP_OK, gatewayResponse.statusCode()); - assertThat(actualBody.totalHits(), is(equalTo(0))); - assertThat(actualBody.hits(), is(empty())); - assertDoesNotThrow(() -> gatewayResponse.body().id().normalize()); - } - - @Test - void shouldReturn200WhenSortOrderIsNotSpecified() throws IOException { - prepareRestHighLevelClientEmptyResponseForSortOrder(); - - handler.handleRequest(getInputStream(), outputStream, mock(Context.class)); - - var gatewayResponse = FakeGatewayResponse.of(outputStream); - var actualBody = gatewayResponse.body(); - - assertNotNull(gatewayResponse.headers()); - assertEquals(HTTP_OK, gatewayResponse.statusCode()); - assertThat(actualBody.totalHits(), is(equalTo(0))); - assertThat(actualBody.hits(), is(empty())); - assertDoesNotThrow(() -> gatewayResponse.body().id().normalize()); - } - - @ParameterizedTest(name = "Should return application/json for accept header {0}") - @MethodSource("acceptHeaderValuesProducingApplicationJsonProvider") - void shouldProduceWithHeader(String acceptHeaderValue) throws IOException { - prepareRestHighLevelClientOkResponse(); - var requestInput = - nonNull(acceptHeaderValue) - ? getRequestInputStreamAccepting(acceptHeaderValue) - : getInputStream(); - handler.handleRequest(requestInput, outputStream, mock(Context.class)); - - var gatewayResponse = FakeGatewayResponse.of(outputStream); - assertThat( - gatewayResponse.headers().get("Content-Type"), - is(equalTo("application/json; charset=utf-8"))); - } - - private InputStream getInputStream() throws JsonProcessingException { - return new HandlerRequestBuilder(objectMapperWithEmpty) - .withQueryParameters(Map.of(SEARCH_ALL.name(), SAMPLE_SEARCH_TERM)) - .withRequestContext(getRequestContext()) - .build(); - } - - private InputStream getInputStreamWithContributorId() throws JsonProcessingException { - return new HandlerRequestBuilder(objectMapperWithEmpty) - .withQueryParameters( - Map.of( - SEARCH_ALL.name(), - "entityDescription.contributors.identity.id:12345", - "results", - "10", - "from", - "0")) - .withHeaders(Map.of(ACCEPT, "application/json")) - .withRequestContext(getRequestContext()) - .withUserName(randomString()) - .build(); - } - - private InputStream getInputStreamWithMultipleContributorId() throws JsonProcessingException { - return new HandlerRequestBuilder(objectMapperWithEmpty) - .withMultiValueQueryParameters(Map.of(CONTRIBUTOR, List.of("12345", "54321"))) - .withRequestContext(getRequestContext()) - .withHeaders(Map.of(ACCEPT, "application/json")) - .withUserName(randomString()) - .build(); - } - - private InputStream getRequestInputStreamAccepting(String contentType) - throws JsonProcessingException { - return new HandlerRequestBuilder(objectMapperWithEmpty) - .withQueryParameters(Map.of(SEARCH_ALL.asCamelCase(), SAMPLE_SEARCH_TERM)) - .withHeaders(Map.of(ACCEPT, contentType)) - .withRequestContext(getRequestContext()) - .build(); - } - - private ObjectNode getRequestContext() { - return objectMapperWithEmpty.convertValue( - Map.of("path", SAMPLE_PATH, "domainName", SAMPLE_DOMAIN_NAME), ObjectNode.class); - } - - private void prepareRestHighLevelClientOkResponse(List exportCsvs) - throws IOException { - var jsonResponse = FakeSearchResponse.generateSearchResponseString(exportCsvs, null); - var body = objectMapperWithEmpty.readValue(jsonResponse, SwsResponse.class); - - when(mockedSearchClient.doSearch(any())).thenReturn(body); - // var searchResponse = createSearchResponseWithHits(json); - // when(restHighLevelClientMock.search(any(), any())).thenReturn(searchResponse); - } - - private void prepareRestHighLevelClientOkResponse() throws IOException { - var jsonResponse = - stringFromResources(Path.of(SAMPLE_OPENSEARCH_RESPONSE_WITH_AGGREGATION_JSON)); - var body = objectMapperWithEmpty.readValue(jsonResponse, SwsResponse.class); - - when(mockedSearchClient.doSearch(any())).thenReturn(body); - } - - private void prepareRestHighLevelClientEmptyResponse() throws IOException { - var jsonResponse = stringFromResources(Path.of(EMPTY_OPENSEARCH_RESPONSE_JSON)); - var body = objectMapperWithEmpty.readValue(jsonResponse, SwsResponse.class); - - when(mockedSearchClient.doSearch(any())).thenReturn(body); - } - - private void prepareRestHighLevelClientEmptyResponseForSortOrder() throws IOException { - var jsonResponse = stringFromResources(Path.of(EMPTY_OPENSEARCH_RESPONSE_JSON)); - var body = objectMapperWithEmpty.readValue(jsonResponse, SwsResponse.class); - - when(mockedSearchClient.doSearch(any())).thenReturn(body); - } + } + + @Test + void shouldReturnSearchResultsWithEmptyHitsWhenQueryResultIsEmpty() throws IOException { + prepareRestHighLevelClientEmptyResponse(); + + handler.handleRequest(getInputStream(), outputStream, mock(Context.class)); + + var gatewayResponse = FakeGatewayResponse.of(outputStream); + var actualBody = gatewayResponse.body(); + + assertNotNull(gatewayResponse.headers()); + assertEquals(HTTP_OK, gatewayResponse.statusCode()); + assertThat(actualBody.totalHits(), is(equalTo(0))); + assertThat(actualBody.hits(), is(empty())); + assertDoesNotThrow(() -> gatewayResponse.body().id().normalize()); + } + + @Test + void shouldReturn200WhenSortOrderIsNotSpecified() throws IOException { + prepareRestHighLevelClientEmptyResponseForSortOrder(); + + handler.handleRequest(getInputStream(), outputStream, mock(Context.class)); + + var gatewayResponse = FakeGatewayResponse.of(outputStream); + var actualBody = gatewayResponse.body(); + + assertNotNull(gatewayResponse.headers()); + assertEquals(HTTP_OK, gatewayResponse.statusCode()); + assertThat(actualBody.totalHits(), is(equalTo(0))); + assertThat(actualBody.hits(), is(empty())); + assertDoesNotThrow(() -> gatewayResponse.body().id().normalize()); + } + + @ParameterizedTest(name = "Should return application/json for accept header {0}") + @MethodSource("acceptHeaderValuesProducingApplicationJsonProvider") + void shouldProduceWithHeader(String acceptHeaderValue) throws IOException { + prepareRestHighLevelClientOkResponse(); + var requestInput = + nonNull(acceptHeaderValue) + ? getRequestInputStreamAccepting(acceptHeaderValue) + : getInputStream(); + handler.handleRequest(requestInput, outputStream, mock(Context.class)); + + var gatewayResponse = FakeGatewayResponse.of(outputStream); + assertThat( + gatewayResponse.headers().get("Content-Type"), + is(equalTo("application/json; charset=utf-8"))); + } + + private InputStream getInputStream() throws JsonProcessingException { + return new HandlerRequestBuilder(objectMapperWithEmpty) + .withQueryParameters(Map.of(SEARCH_ALL.name(), SAMPLE_SEARCH_TERM)) + .withRequestContext(getRequestContext()) + .build(); + } + + private InputStream getInputStreamWithContributorId() throws JsonProcessingException { + return new HandlerRequestBuilder(objectMapperWithEmpty) + .withQueryParameters( + Map.of( + SEARCH_ALL.name(), + "entityDescription.contributors.identity.id:12345", + "results", + "10", + "from", + "0")) + .withHeaders(Map.of(ACCEPT, "application/json")) + .withRequestContext(getRequestContext()) + .withUserName(randomString()) + .build(); + } + + private InputStream getInputStreamWithMultipleContributorId() throws JsonProcessingException { + return new HandlerRequestBuilder(objectMapperWithEmpty) + .withMultiValueQueryParameters(Map.of(CONTRIBUTOR, List.of("12345", "54321"))) + .withRequestContext(getRequestContext()) + .withHeaders(Map.of(ACCEPT, "application/json")) + .withUserName(randomString()) + .build(); + } + + private InputStream getRequestInputStreamAccepting(String contentType) + throws JsonProcessingException { + return new HandlerRequestBuilder(objectMapperWithEmpty) + .withQueryParameters(Map.of(SEARCH_ALL.asCamelCase(), SAMPLE_SEARCH_TERM)) + .withHeaders(Map.of(ACCEPT, contentType)) + .withRequestContext(getRequestContext()) + .build(); + } + + private ObjectNode getRequestContext() { + return objectMapperWithEmpty.convertValue( + Map.of("path", SAMPLE_PATH, "domainName", SAMPLE_DOMAIN_NAME), ObjectNode.class); + } + + private void prepareRestHighLevelClientOkResponse(List exportCsvs) throws IOException { + var jsonResponse = FakeSearchResponse.generateSearchResponseString(exportCsvs, null); + var body = objectMapperWithEmpty.readValue(jsonResponse, SwsResponse.class); + + when(mockedSearchClient.doSearch(any())).thenReturn(body); + // var searchResponse = createSearchResponseWithHits(json); + // when(restHighLevelClientMock.search(any(), any())).thenReturn(searchResponse); + } + + private void prepareRestHighLevelClientOkResponse() throws IOException { + var jsonResponse = + stringFromResources(Path.of(SAMPLE_OPENSEARCH_RESPONSE_WITH_AGGREGATION_JSON)); + var body = objectMapperWithEmpty.readValue(jsonResponse, SwsResponse.class); + + when(mockedSearchClient.doSearch(any())).thenReturn(body); + } + + private void prepareRestHighLevelClientEmptyResponse() throws IOException { + var jsonResponse = stringFromResources(Path.of(EMPTY_OPENSEARCH_RESPONSE_JSON)); + var body = objectMapperWithEmpty.readValue(jsonResponse, SwsResponse.class); + + when(mockedSearchClient.doSearch(any())).thenReturn(body); + } + + private void prepareRestHighLevelClientEmptyResponseForSortOrder() throws IOException { + var jsonResponse = stringFromResources(Path.of(EMPTY_OPENSEARCH_RESPONSE_JSON)); + var body = objectMapperWithEmpty.readValue(jsonResponse, SwsResponse.class); + + when(mockedSearchClient.doSearch(any())).thenReturn(body); + } }