Search in sources :

Example 1 with Log

use of com.amazon.dataprepper.model.log.Log in project data-prepper by opensearch-project.

the class HTTPSourceTest method testHTTPJsonResponse400.

@Test
public void testHTTPJsonResponse400() {
    // Prepare
    final String testBadData = "}";
    final int testPayloadSize = testBadData.getBytes().length;
    HTTPSourceUnderTest.start(testBuffer);
    refreshMeasurements();
    // When
    WebClient.of().execute(RequestHeaders.builder().scheme(SessionProtocol.HTTP).authority("127.0.0.1:2021").method(HttpMethod.POST).path("/log/ingest").contentType(MediaType.JSON_UTF_8).build(), HttpData.ofUtf8(testBadData)).aggregate().whenComplete((i, ex) -> assertSecureResponseWithStatusCode(i, HttpStatus.BAD_REQUEST)).join();
    // Then
    Assertions.assertTrue(testBuffer.isEmpty());
    // Verify metrics
    final Measurement requestReceivedCount = MetricsTestUtil.getMeasurementFromList(requestsReceivedMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, requestReceivedCount.getValue());
    final Measurement badRequestsCount = MetricsTestUtil.getMeasurementFromList(badRequestsMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, badRequestsCount.getValue());
}
Also used : Statistic(io.micrometer.core.instrument.Statistic) BeforeEach(org.junit.jupiter.api.BeforeEach) RequestHeaders(com.linecorp.armeria.common.RequestHeaders) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) MetricsTestUtil(com.amazon.dataprepper.metrics.MetricsTestUtil) Matchers.not(org.hamcrest.Matchers.not) ArmeriaHttpAuthenticationProvider(com.amazon.dataprepper.armeria.authentication.ArmeriaHttpAuthenticationProvider) AsciiString(io.netty.util.AsciiString) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Server(com.linecorp.armeria.server.Server) WebClient(com.linecorp.armeria.client.WebClient) Map(java.util.Map) Record(com.amazon.dataprepper.model.record.Record) PluginFactory(com.amazon.dataprepper.model.plugin.PluginFactory) Path(java.nio.file.Path) ClientFactory(com.linecorp.armeria.client.ClientFactory) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) CheckpointState(com.amazon.dataprepper.model.CheckpointState) Collection(java.util.Collection) CompletionException(java.util.concurrent.CompletionException) HttpMethod(com.linecorp.armeria.common.HttpMethod) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) IOUtils(org.apache.commons.io.IOUtils) MockedStatic(org.mockito.MockedStatic) List(java.util.List) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) ServerBuilder(com.linecorp.armeria.server.ServerBuilder) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SessionProtocol(com.linecorp.armeria.common.SessionProtocol) Mock(org.mockito.Mock) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) MetricNames(com.amazon.dataprepper.metrics.MetricNames) ResponseTimeoutException(com.linecorp.armeria.client.ResponseTimeoutException) Mockito.lenient(org.mockito.Mockito.lenient) MediaType(com.linecorp.armeria.common.MediaType) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) HttpStatus(com.linecorp.armeria.common.HttpStatus) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) BlockingBuffer(com.amazon.dataprepper.plugins.buffer.blockingbuffer.BlockingBuffer) PluginMetrics(com.amazon.dataprepper.metrics.PluginMetrics) HttpData(com.linecorp.armeria.common.HttpData) PluginSetting(com.amazon.dataprepper.model.configuration.PluginSetting) Log(com.amazon.dataprepper.model.log.Log) Files(java.nio.file.Files) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Measurement(io.micrometer.core.instrument.Measurement) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) StringJoiner(java.util.StringJoiner) Assertions(org.junit.jupiter.api.Assertions) Mockito.reset(org.mockito.Mockito.reset) InputStream(java.io.InputStream) Measurement(io.micrometer.core.instrument.Measurement) AsciiString(io.netty.util.AsciiString) Test(org.junit.jupiter.api.Test)

Example 2 with Log

use of com.amazon.dataprepper.model.log.Log in project data-prepper by opensearch-project.

the class HTTPSourceTest method testHTTPJsonResponse200.

@Test
public void testHTTPJsonResponse200() {
    // Prepare
    final String testData = "[{\"log\": \"somelog\"}]";
    final int testPayloadSize = testData.getBytes().length;
    HTTPSourceUnderTest.start(testBuffer);
    refreshMeasurements();
    // When
    WebClient.of().execute(RequestHeaders.builder().scheme(SessionProtocol.HTTP).authority("127.0.0.1:2021").method(HttpMethod.POST).path("/log/ingest").contentType(MediaType.JSON_UTF_8).build(), HttpData.ofUtf8(testData)).aggregate().whenComplete((i, ex) -> assertSecureResponseWithStatusCode(i, HttpStatus.OK)).join();
    // Then
    Assertions.assertFalse(testBuffer.isEmpty());
    final Map.Entry<Collection<Record<Log>>, CheckpointState> result = testBuffer.read(100);
    List<Record<Log>> records = new ArrayList<>(result.getKey());
    Assertions.assertEquals(1, records.size());
    final Record<Log> record = records.get(0);
    Assertions.assertEquals("somelog", record.getData().get("log", String.class));
    // Verify metrics
    final Measurement requestReceivedCount = MetricsTestUtil.getMeasurementFromList(requestsReceivedMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, requestReceivedCount.getValue());
    final Measurement successRequestsCount = MetricsTestUtil.getMeasurementFromList(successRequestsMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, successRequestsCount.getValue());
    final Measurement requestProcessDurationCount = MetricsTestUtil.getMeasurementFromList(requestProcessDurationMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, requestProcessDurationCount.getValue());
    final Measurement requestProcessDurationMax = MetricsTestUtil.getMeasurementFromList(requestProcessDurationMeasurements, Statistic.MAX);
    Assertions.assertTrue(requestProcessDurationMax.getValue() > 0);
    final Measurement payloadSizeMax = MetricsTestUtil.getMeasurementFromList(payloadSizeSummaryMeasurements, Statistic.MAX);
    Assertions.assertEquals(testPayloadSize, payloadSizeMax.getValue());
}
Also used : Statistic(io.micrometer.core.instrument.Statistic) BeforeEach(org.junit.jupiter.api.BeforeEach) RequestHeaders(com.linecorp.armeria.common.RequestHeaders) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) MetricsTestUtil(com.amazon.dataprepper.metrics.MetricsTestUtil) Matchers.not(org.hamcrest.Matchers.not) ArmeriaHttpAuthenticationProvider(com.amazon.dataprepper.armeria.authentication.ArmeriaHttpAuthenticationProvider) AsciiString(io.netty.util.AsciiString) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Server(com.linecorp.armeria.server.Server) WebClient(com.linecorp.armeria.client.WebClient) Map(java.util.Map) Record(com.amazon.dataprepper.model.record.Record) PluginFactory(com.amazon.dataprepper.model.plugin.PluginFactory) Path(java.nio.file.Path) ClientFactory(com.linecorp.armeria.client.ClientFactory) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) CheckpointState(com.amazon.dataprepper.model.CheckpointState) Collection(java.util.Collection) CompletionException(java.util.concurrent.CompletionException) HttpMethod(com.linecorp.armeria.common.HttpMethod) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) IOUtils(org.apache.commons.io.IOUtils) MockedStatic(org.mockito.MockedStatic) List(java.util.List) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) ServerBuilder(com.linecorp.armeria.server.ServerBuilder) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SessionProtocol(com.linecorp.armeria.common.SessionProtocol) Mock(org.mockito.Mock) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) MetricNames(com.amazon.dataprepper.metrics.MetricNames) ResponseTimeoutException(com.linecorp.armeria.client.ResponseTimeoutException) Mockito.lenient(org.mockito.Mockito.lenient) MediaType(com.linecorp.armeria.common.MediaType) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) HttpStatus(com.linecorp.armeria.common.HttpStatus) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) BlockingBuffer(com.amazon.dataprepper.plugins.buffer.blockingbuffer.BlockingBuffer) PluginMetrics(com.amazon.dataprepper.metrics.PluginMetrics) HttpData(com.linecorp.armeria.common.HttpData) PluginSetting(com.amazon.dataprepper.model.configuration.PluginSetting) Log(com.amazon.dataprepper.model.log.Log) Files(java.nio.file.Files) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Measurement(io.micrometer.core.instrument.Measurement) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) StringJoiner(java.util.StringJoiner) Assertions(org.junit.jupiter.api.Assertions) Mockito.reset(org.mockito.Mockito.reset) InputStream(java.io.InputStream) Measurement(io.micrometer.core.instrument.Measurement) Log(com.amazon.dataprepper.model.log.Log) ArrayList(java.util.ArrayList) AsciiString(io.netty.util.AsciiString) Collection(java.util.Collection) Record(com.amazon.dataprepper.model.record.Record) CheckpointState(com.amazon.dataprepper.model.CheckpointState) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 3 with Log

use of com.amazon.dataprepper.model.log.Log in project data-prepper by opensearch-project.

the class HTTPSourceTest method testHTTPJsonResponse415.

@Test
public void testHTTPJsonResponse415() {
    // Prepare
    final int testMaxPendingRequests = 1;
    final int testThreadCount = 1;
    final int serverTimeoutInMillis = 500;
    when(sourceConfig.getRequestTimeoutInMillis()).thenReturn(serverTimeoutInMillis);
    when(sourceConfig.getMaxPendingRequests()).thenReturn(testMaxPendingRequests);
    when(sourceConfig.getThreadCount()).thenReturn(testThreadCount);
    HTTPSourceUnderTest = new HTTPSource(sourceConfig, pluginMetrics, pluginFactory);
    // Start the source
    HTTPSourceUnderTest.start(testBuffer);
    refreshMeasurements();
    final RequestHeaders testRequestHeaders = RequestHeaders.builder().scheme(SessionProtocol.HTTP).authority("127.0.0.1:2021").method(HttpMethod.POST).path("/log/ingest").contentType(MediaType.JSON_UTF_8).build();
    final HttpData testHttpData = HttpData.ofUtf8("[{\"log\": \"somelog\"}]");
    // Fill in the buffer
    WebClient.of().execute(testRequestHeaders, testHttpData).aggregate().whenComplete((i, ex) -> assertSecureResponseWithStatusCode(i, HttpStatus.OK)).join();
    // Disable client timeout
    WebClient testWebClient = WebClient.builder().responseTimeoutMillis(0).build();
    // When/Then
    testWebClient.execute(testRequestHeaders, testHttpData).aggregate().whenComplete((i, ex) -> assertSecureResponseWithStatusCode(i, HttpStatus.REQUEST_TIMEOUT)).join();
    // verify metrics
    final Measurement requestReceivedCount = MetricsTestUtil.getMeasurementFromList(requestsReceivedMeasurements, Statistic.COUNT);
    Assertions.assertEquals(2.0, requestReceivedCount.getValue());
    final Measurement successRequestsCount = MetricsTestUtil.getMeasurementFromList(successRequestsMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, successRequestsCount.getValue());
    final Measurement requestTimeoutsCount = MetricsTestUtil.getMeasurementFromList(requestTimeoutsMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, requestTimeoutsCount.getValue());
    final Measurement requestProcessDurationMax = MetricsTestUtil.getMeasurementFromList(requestProcessDurationMeasurements, Statistic.MAX);
    final double maxDurationInMillis = 1000 * requestProcessDurationMax.getValue();
    Assertions.assertTrue(maxDurationInMillis > serverTimeoutInMillis);
}
Also used : Statistic(io.micrometer.core.instrument.Statistic) BeforeEach(org.junit.jupiter.api.BeforeEach) RequestHeaders(com.linecorp.armeria.common.RequestHeaders) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) MetricsTestUtil(com.amazon.dataprepper.metrics.MetricsTestUtil) Matchers.not(org.hamcrest.Matchers.not) ArmeriaHttpAuthenticationProvider(com.amazon.dataprepper.armeria.authentication.ArmeriaHttpAuthenticationProvider) AsciiString(io.netty.util.AsciiString) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Server(com.linecorp.armeria.server.Server) WebClient(com.linecorp.armeria.client.WebClient) Map(java.util.Map) Record(com.amazon.dataprepper.model.record.Record) PluginFactory(com.amazon.dataprepper.model.plugin.PluginFactory) Path(java.nio.file.Path) ClientFactory(com.linecorp.armeria.client.ClientFactory) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) CheckpointState(com.amazon.dataprepper.model.CheckpointState) Collection(java.util.Collection) CompletionException(java.util.concurrent.CompletionException) HttpMethod(com.linecorp.armeria.common.HttpMethod) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) IOUtils(org.apache.commons.io.IOUtils) MockedStatic(org.mockito.MockedStatic) List(java.util.List) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) ServerBuilder(com.linecorp.armeria.server.ServerBuilder) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SessionProtocol(com.linecorp.armeria.common.SessionProtocol) Mock(org.mockito.Mock) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) MetricNames(com.amazon.dataprepper.metrics.MetricNames) ResponseTimeoutException(com.linecorp.armeria.client.ResponseTimeoutException) Mockito.lenient(org.mockito.Mockito.lenient) MediaType(com.linecorp.armeria.common.MediaType) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) HttpStatus(com.linecorp.armeria.common.HttpStatus) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) BlockingBuffer(com.amazon.dataprepper.plugins.buffer.blockingbuffer.BlockingBuffer) PluginMetrics(com.amazon.dataprepper.metrics.PluginMetrics) HttpData(com.linecorp.armeria.common.HttpData) PluginSetting(com.amazon.dataprepper.model.configuration.PluginSetting) Log(com.amazon.dataprepper.model.log.Log) Files(java.nio.file.Files) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Measurement(io.micrometer.core.instrument.Measurement) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) StringJoiner(java.util.StringJoiner) Assertions(org.junit.jupiter.api.Assertions) Mockito.reset(org.mockito.Mockito.reset) InputStream(java.io.InputStream) Measurement(io.micrometer.core.instrument.Measurement) HttpData(com.linecorp.armeria.common.HttpData) RequestHeaders(com.linecorp.armeria.common.RequestHeaders) WebClient(com.linecorp.armeria.client.WebClient) Test(org.junit.jupiter.api.Test)

Example 4 with Log

use of com.amazon.dataprepper.model.log.Log in project data-prepper by opensearch-project.

the class HTTPSourceTest method testHTTPJsonResponse413.

@Test
public void testHTTPJsonResponse413() throws InterruptedException {
    // Prepare
    final String testData = "[{\"log\": \"test log 1\"}, {\"log\": \"test log 2\"}]";
    final int testPayloadSize = testData.getBytes().length;
    HTTPSourceUnderTest.start(testBuffer);
    refreshMeasurements();
    // When
    WebClient.of().execute(RequestHeaders.builder().scheme(SessionProtocol.HTTP).authority("127.0.0.1:2021").method(HttpMethod.POST).path("/log/ingest").contentType(MediaType.JSON_UTF_8).build(), HttpData.ofUtf8(testData)).aggregate().whenComplete((i, ex) -> assertSecureResponseWithStatusCode(i, HttpStatus.REQUEST_ENTITY_TOO_LARGE)).join();
    // Then
    Assertions.assertTrue(testBuffer.isEmpty());
    // Verify metrics
    final Measurement requestReceivedCount = MetricsTestUtil.getMeasurementFromList(requestsReceivedMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, requestReceivedCount.getValue());
    final Measurement successRequestsCount = MetricsTestUtil.getMeasurementFromList(successRequestsMeasurements, Statistic.COUNT);
    Assertions.assertEquals(0.0, successRequestsCount.getValue());
    final Measurement requestsTooLargeCount = MetricsTestUtil.getMeasurementFromList(requestsTooLargeMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, requestsTooLargeCount.getValue());
    final Measurement requestProcessDurationCount = MetricsTestUtil.getMeasurementFromList(requestProcessDurationMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, requestProcessDurationCount.getValue());
    final Measurement requestProcessDurationMax = MetricsTestUtil.getMeasurementFromList(requestProcessDurationMeasurements, Statistic.MAX);
    Assertions.assertTrue(requestProcessDurationMax.getValue() > 0);
    final Measurement payloadSizeMax = MetricsTestUtil.getMeasurementFromList(payloadSizeSummaryMeasurements, Statistic.MAX);
    Assertions.assertEquals(testPayloadSize, payloadSizeMax.getValue());
}
Also used : Statistic(io.micrometer.core.instrument.Statistic) BeforeEach(org.junit.jupiter.api.BeforeEach) RequestHeaders(com.linecorp.armeria.common.RequestHeaders) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) MetricsTestUtil(com.amazon.dataprepper.metrics.MetricsTestUtil) Matchers.not(org.hamcrest.Matchers.not) ArmeriaHttpAuthenticationProvider(com.amazon.dataprepper.armeria.authentication.ArmeriaHttpAuthenticationProvider) AsciiString(io.netty.util.AsciiString) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Server(com.linecorp.armeria.server.Server) WebClient(com.linecorp.armeria.client.WebClient) Map(java.util.Map) Record(com.amazon.dataprepper.model.record.Record) PluginFactory(com.amazon.dataprepper.model.plugin.PluginFactory) Path(java.nio.file.Path) ClientFactory(com.linecorp.armeria.client.ClientFactory) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) CheckpointState(com.amazon.dataprepper.model.CheckpointState) Collection(java.util.Collection) CompletionException(java.util.concurrent.CompletionException) HttpMethod(com.linecorp.armeria.common.HttpMethod) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) IOUtils(org.apache.commons.io.IOUtils) MockedStatic(org.mockito.MockedStatic) List(java.util.List) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) ServerBuilder(com.linecorp.armeria.server.ServerBuilder) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SessionProtocol(com.linecorp.armeria.common.SessionProtocol) Mock(org.mockito.Mock) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) MetricNames(com.amazon.dataprepper.metrics.MetricNames) ResponseTimeoutException(com.linecorp.armeria.client.ResponseTimeoutException) Mockito.lenient(org.mockito.Mockito.lenient) MediaType(com.linecorp.armeria.common.MediaType) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) HttpStatus(com.linecorp.armeria.common.HttpStatus) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) BlockingBuffer(com.amazon.dataprepper.plugins.buffer.blockingbuffer.BlockingBuffer) PluginMetrics(com.amazon.dataprepper.metrics.PluginMetrics) HttpData(com.linecorp.armeria.common.HttpData) PluginSetting(com.amazon.dataprepper.model.configuration.PluginSetting) Log(com.amazon.dataprepper.model.log.Log) Files(java.nio.file.Files) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Measurement(io.micrometer.core.instrument.Measurement) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) StringJoiner(java.util.StringJoiner) Assertions(org.junit.jupiter.api.Assertions) Mockito.reset(org.mockito.Mockito.reset) InputStream(java.io.InputStream) Measurement(io.micrometer.core.instrument.Measurement) AsciiString(io.netty.util.AsciiString) Test(org.junit.jupiter.api.Test)

Example 5 with Log

use of com.amazon.dataprepper.model.log.Log in project data-prepper by opensearch-project.

the class HTTPSourceTest method testHTTPJsonResponse429.

@Test
public void testHTTPJsonResponse429() throws InterruptedException {
    // Prepare
    final Map<String, Object> settings = new HashMap<>();
    final int testMaxPendingRequests = 1;
    final int testThreadCount = 1;
    final int clientTimeoutInMillis = 100;
    final int serverTimeoutInMillis = (testMaxPendingRequests + testThreadCount + 1) * clientTimeoutInMillis;
    when(sourceConfig.getRequestTimeoutInMillis()).thenReturn(serverTimeoutInMillis);
    when(sourceConfig.getMaxPendingRequests()).thenReturn(testMaxPendingRequests);
    when(sourceConfig.getThreadCount()).thenReturn(testThreadCount);
    HTTPSourceUnderTest = new HTTPSource(sourceConfig, pluginMetrics, pluginFactory);
    // Start the source
    HTTPSourceUnderTest.start(testBuffer);
    refreshMeasurements();
    final RequestHeaders testRequestHeaders = RequestHeaders.builder().scheme(SessionProtocol.HTTP).authority("127.0.0.1:2021").method(HttpMethod.POST).path("/log/ingest").contentType(MediaType.JSON_UTF_8).build();
    final HttpData testHttpData = HttpData.ofUtf8("[{\"log\": \"somelog\"}]");
    // Fill in the buffer
    WebClient.of().execute(testRequestHeaders, testHttpData).aggregate().whenComplete((i, ex) -> assertSecureResponseWithStatusCode(i, HttpStatus.OK)).join();
    // Send requests to throttle the server when buffer is full
    // Set the client timeout to be less than source serverTimeoutInMillis / (testMaxPendingRequests + testThreadCount)
    WebClient testWebClient = WebClient.builder().responseTimeoutMillis(clientTimeoutInMillis).build();
    for (int i = 0; i < testMaxPendingRequests + testThreadCount; i++) {
        CompletionException actualException = Assertions.assertThrows(CompletionException.class, () -> testWebClient.execute(testRequestHeaders, testHttpData).aggregate().join());
        assertThat(actualException.getCause(), instanceOf(ResponseTimeoutException.class));
    }
    // When/Then
    testWebClient.execute(testRequestHeaders, testHttpData).aggregate().whenComplete((i, ex) -> assertSecureResponseWithStatusCode(i, HttpStatus.TOO_MANY_REQUESTS)).join();
    // Wait until source server timeout a request processing thread
    Thread.sleep(serverTimeoutInMillis);
    // New request should timeout instead of being rejected
    CompletionException actualException = Assertions.assertThrows(CompletionException.class, () -> testWebClient.execute(testRequestHeaders, testHttpData).aggregate().join());
    assertThat(actualException.getCause(), instanceOf(ResponseTimeoutException.class));
    // verify metrics
    final Measurement requestReceivedCount = MetricsTestUtil.getMeasurementFromList(requestsReceivedMeasurements, Statistic.COUNT);
    Assertions.assertEquals(testMaxPendingRequests + testThreadCount + 2, requestReceivedCount.getValue());
    final Measurement successRequestsCount = MetricsTestUtil.getMeasurementFromList(successRequestsMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, successRequestsCount.getValue());
    final Measurement rejectedRequestsCount = MetricsTestUtil.getMeasurementFromList(rejectedRequestsMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, rejectedRequestsCount.getValue());
}
Also used : Statistic(io.micrometer.core.instrument.Statistic) BeforeEach(org.junit.jupiter.api.BeforeEach) RequestHeaders(com.linecorp.armeria.common.RequestHeaders) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) MetricsTestUtil(com.amazon.dataprepper.metrics.MetricsTestUtil) Matchers.not(org.hamcrest.Matchers.not) ArmeriaHttpAuthenticationProvider(com.amazon.dataprepper.armeria.authentication.ArmeriaHttpAuthenticationProvider) AsciiString(io.netty.util.AsciiString) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Server(com.linecorp.armeria.server.Server) WebClient(com.linecorp.armeria.client.WebClient) Map(java.util.Map) Record(com.amazon.dataprepper.model.record.Record) PluginFactory(com.amazon.dataprepper.model.plugin.PluginFactory) Path(java.nio.file.Path) ClientFactory(com.linecorp.armeria.client.ClientFactory) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) CheckpointState(com.amazon.dataprepper.model.CheckpointState) Collection(java.util.Collection) CompletionException(java.util.concurrent.CompletionException) HttpMethod(com.linecorp.armeria.common.HttpMethod) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) IOUtils(org.apache.commons.io.IOUtils) MockedStatic(org.mockito.MockedStatic) List(java.util.List) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) ServerBuilder(com.linecorp.armeria.server.ServerBuilder) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SessionProtocol(com.linecorp.armeria.common.SessionProtocol) Mock(org.mockito.Mock) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) MetricNames(com.amazon.dataprepper.metrics.MetricNames) ResponseTimeoutException(com.linecorp.armeria.client.ResponseTimeoutException) Mockito.lenient(org.mockito.Mockito.lenient) MediaType(com.linecorp.armeria.common.MediaType) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) HttpStatus(com.linecorp.armeria.common.HttpStatus) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) BlockingBuffer(com.amazon.dataprepper.plugins.buffer.blockingbuffer.BlockingBuffer) PluginMetrics(com.amazon.dataprepper.metrics.PluginMetrics) HttpData(com.linecorp.armeria.common.HttpData) PluginSetting(com.amazon.dataprepper.model.configuration.PluginSetting) Log(com.amazon.dataprepper.model.log.Log) Files(java.nio.file.Files) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Measurement(io.micrometer.core.instrument.Measurement) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) StringJoiner(java.util.StringJoiner) Assertions(org.junit.jupiter.api.Assertions) Mockito.reset(org.mockito.Mockito.reset) InputStream(java.io.InputStream) ResponseTimeoutException(com.linecorp.armeria.client.ResponseTimeoutException) Measurement(io.micrometer.core.instrument.Measurement) HashMap(java.util.HashMap) AsciiString(io.netty.util.AsciiString) WebClient(com.linecorp.armeria.client.WebClient) HttpData(com.linecorp.armeria.common.HttpData) CompletionException(java.util.concurrent.CompletionException) RequestHeaders(com.linecorp.armeria.common.RequestHeaders) Test(org.junit.jupiter.api.Test)

Aggregations

ArmeriaHttpAuthenticationProvider (com.amazon.dataprepper.armeria.authentication.ArmeriaHttpAuthenticationProvider)5 MetricNames (com.amazon.dataprepper.metrics.MetricNames)5 MetricsTestUtil (com.amazon.dataprepper.metrics.MetricsTestUtil)5 PluginMetrics (com.amazon.dataprepper.metrics.PluginMetrics)5 CheckpointState (com.amazon.dataprepper.model.CheckpointState)5 PluginSetting (com.amazon.dataprepper.model.configuration.PluginSetting)5 Log (com.amazon.dataprepper.model.log.Log)5 PluginFactory (com.amazon.dataprepper.model.plugin.PluginFactory)5 Record (com.amazon.dataprepper.model.record.Record)5 BlockingBuffer (com.amazon.dataprepper.plugins.buffer.blockingbuffer.BlockingBuffer)5 ClientFactory (com.linecorp.armeria.client.ClientFactory)5 ResponseTimeoutException (com.linecorp.armeria.client.ResponseTimeoutException)5 WebClient (com.linecorp.armeria.client.WebClient)5 AggregatedHttpResponse (com.linecorp.armeria.common.AggregatedHttpResponse)5 HttpData (com.linecorp.armeria.common.HttpData)5 HttpMethod (com.linecorp.armeria.common.HttpMethod)5 HttpStatus (com.linecorp.armeria.common.HttpStatus)5 MediaType (com.linecorp.armeria.common.MediaType)5 RequestHeaders (com.linecorp.armeria.common.RequestHeaders)5 SessionProtocol (com.linecorp.armeria.common.SessionProtocol)5