-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #726 from commercetools/datadog-middleware-test
add test for Datadog middleware
- Loading branch information
Showing
5 changed files
with
280 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...etools/commercetools-monitoring-datadog/src/test/java/example/ResponseSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
|
||
package example; | ||
|
||
import com.commercetools.api.models.common.Reference; | ||
import com.commercetools.api.models.product.ProductReference; | ||
import com.commercetools.monitoring.datadog.DatadogResponseSerializer; | ||
import com.datadog.api.client.ApiException; | ||
import com.datadog.api.client.v2.api.MetricsApi; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
|
||
import io.vrap.rmf.base.client.ApiHttpHeaders; | ||
import io.vrap.rmf.base.client.ApiHttpResponse; | ||
import io.vrap.rmf.base.client.ResponseSerializer; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
public class ResponseSerializerTest { | ||
|
||
@Test | ||
public void testSerialize() throws ApiException, JsonProcessingException { | ||
MetricsApi metricsApi = Mockito.mock(MetricsApi.class); | ||
Mockito.when(metricsApi.submitMetrics(Mockito.any())).thenReturn(null); | ||
DatadogResponseSerializer serializer = new DatadogResponseSerializer(ResponseSerializer.of(), metricsApi); | ||
|
||
Reference reference = ProductReference.builder().id("abc").build(); | ||
|
||
serializer.toJsonByteArray(reference); | ||
|
||
Mockito.verify(metricsApi).submitMetrics(Mockito.argThat(metricPayload -> { | ||
Assertions.assertThat(metricPayload).isNotNull(); | ||
Assertions.assertThat(metricPayload.getSeries().get(0).getMetric()) | ||
.isEqualTo("commercetools.json.serialization"); | ||
return true; | ||
})); | ||
} | ||
|
||
@Test | ||
public void testDeserialize() throws ApiException, JsonProcessingException { | ||
MetricsApi metricsApi = Mockito.mock(MetricsApi.class); | ||
Mockito.when(metricsApi.submitMetrics(Mockito.any())).thenReturn(null); | ||
DatadogResponseSerializer serializer = new DatadogResponseSerializer(ResponseSerializer.of(), metricsApi); | ||
|
||
String responseBody = "{ \"typeId\": \"product\", \"id\": \"abc\" }"; | ||
ApiHttpResponse<byte[]> response = new ApiHttpResponse<>(200, new ApiHttpHeaders(), responseBody.getBytes()); | ||
|
||
ApiHttpResponse<Reference> reference = serializer.convertResponse(response, Reference.class); | ||
|
||
Assertions.assertThat(reference.getBody()).isInstanceOf(ProductReference.class); | ||
Mockito.verify(metricsApi).submitMetrics(Mockito.argThat(metricPayload -> { | ||
Assertions.assertThat(metricPayload).isNotNull(); | ||
Assertions.assertThat(metricPayload.getSeries().get(0).getMetric()) | ||
.isEqualTo("commercetools.json.deserialization"); | ||
return true; | ||
})); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
...tools/commercetools-monitoring-datadog/src/test/java/example/TelemetryMiddlewareTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
|
||
package example; | ||
|
||
import static io.vrap.rmf.base.client.utils.ClientUtils.blockingWait; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.time.Duration; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.Function; | ||
|
||
import com.commercetools.monitoring.datadog.DatadogMiddleware; | ||
import com.datadog.api.client.ApiException; | ||
import com.datadog.api.client.v2.api.MetricsApi; | ||
import com.tngtech.junit.dataprovider.DataProvider; | ||
import com.tngtech.junit.dataprovider.DataProviderExtension; | ||
import com.tngtech.junit.dataprovider.UseDataProvider; | ||
import com.tngtech.junit.dataprovider.UseDataProviderExtension; | ||
|
||
import io.vrap.rmf.base.client.*; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.TestTemplate; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mockito; | ||
|
||
@ExtendWith(DataProviderExtension.class) | ||
@ExtendWith(UseDataProviderExtension.class) | ||
public class TelemetryMiddlewareTest { | ||
|
||
@DataProvider | ||
public static Object[][] responses() { | ||
return new Object[][] { { 200, 2 }, { 201, 2 }, { 400, 3 }, { 401, 3 }, { 403, 3 }, { 404, 3 }, { 409, 3 }, | ||
{ 499, 3 }, { 500, 3 }, { 502, 3 }, { 503, 3 }, { 504, 3 }, { 599, 3 }, }; | ||
} | ||
|
||
@TestTemplate | ||
@UseDataProvider("responses") | ||
public void testCounts(int statusCode, int count) throws ApiException { | ||
MetricsApi metricsApi = Mockito.mock(MetricsApi.class); | ||
Mockito.when(metricsApi.submitMetrics(Mockito.any())).thenReturn(null); | ||
DatadogMiddleware middleware = new DatadogMiddleware(metricsApi); | ||
|
||
final ApiHttpRequest request = new ApiHttpRequest(ApiHttpMethod.GET, URI.create("/"), new ApiHttpHeaders(), | ||
"".getBytes()); | ||
|
||
blockingWait( | ||
middleware.invoke(request, | ||
request1 -> CompletableFuture | ||
.completedFuture(new ApiHttpResponse<>(statusCode, new ApiHttpHeaders(), "".getBytes()))), | ||
Duration.ofSeconds(1)); | ||
|
||
Mockito.verify(metricsApi, Mockito.times(count)).submitMetrics(Mockito.argThat(metricPayload -> { | ||
Assertions.assertThat(metricPayload).isNotNull(); | ||
Assertions.assertThat(metricPayload.getSeries().get(0).getMetric()) | ||
.isIn("commercetools.client.duration", "commercetools.client.request.total", | ||
"commercetools.client.request.error"); | ||
return true; | ||
})); | ||
} | ||
|
||
@TestTemplate | ||
@UseDataProvider("responses") | ||
public void testHttpCounts(int statusCode, int count) throws URISyntaxException, ApiException { | ||
MetricsApi metricsApi = Mockito.mock(MetricsApi.class); | ||
Mockito.when(metricsApi.submitMetrics(Mockito.any())).thenReturn(null); | ||
DatadogMiddleware middleware = new DatadogMiddleware(metricsApi); | ||
|
||
ApiHttpClient client = ClientBuilder | ||
.of(new TestHttpClient(request -> CompletableFuture | ||
.completedFuture(new ApiHttpResponse<>(statusCode, new ApiHttpHeaders(), "".getBytes())))) | ||
.withApiBaseUrl(new URI("")) | ||
.withTelemetryMiddleware(middleware) | ||
.withErrorMiddleware() | ||
.build(); | ||
|
||
final ApiHttpRequest request = new ApiHttpRequest(ApiHttpMethod.GET, URI.create("/"), new ApiHttpHeaders(), | ||
"".getBytes()); | ||
try { | ||
blockingWait(client.execute(request), Duration.ofSeconds(1)); | ||
} | ||
catch (ApiHttpException ignored) { | ||
} | ||
|
||
Mockito.verify(metricsApi, Mockito.times(count)).submitMetrics(Mockito.argThat(metricPayload -> { | ||
Assertions.assertThat(metricPayload).isNotNull(); | ||
Assertions.assertThat(metricPayload.getSeries().get(0).getMetric()) | ||
.isIn("commercetools.client.duration", "commercetools.client.request.total", | ||
"commercetools.client.request.error"); | ||
return true; | ||
})); | ||
} | ||
|
||
static class TestHttpClient implements VrapHttpClient { | ||
private final Function<ApiHttpRequest, CompletableFuture<ApiHttpResponse<byte[]>>> next; | ||
|
||
public TestHttpClient(Function<ApiHttpRequest, CompletableFuture<ApiHttpResponse<byte[]>>> next) { | ||
this.next = next; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<ApiHttpResponse<byte[]>> execute(ApiHttpRequest request) { | ||
return next.apply(request); | ||
} | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
rmf/rmf-java-base/src/test/java/io/vrap/rmf/base/client/http/TelemetryMiddlewareTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
|
||
package io.vrap.rmf.base.client.http; | ||
|
||
import static io.vrap.rmf.base.client.utils.ClientUtils.blockingWait; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.time.Duration; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.Function; | ||
|
||
import com.tngtech.junit.dataprovider.DataProvider; | ||
import com.tngtech.junit.dataprovider.DataProviderExtension; | ||
import com.tngtech.junit.dataprovider.UseDataProvider; | ||
import com.tngtech.junit.dataprovider.UseDataProviderExtension; | ||
|
||
import io.vrap.rmf.base.client.*; | ||
import io.vrap.rmf.base.client.error.*; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.TestTemplate; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
@ExtendWith(DataProviderExtension.class) | ||
@ExtendWith(UseDataProviderExtension.class) | ||
public class TelemetryMiddlewareTest { | ||
|
||
@DataProvider | ||
public static Object[][] responses() { | ||
return new Object[][] { { 200, 1, 0 }, { 201, 1, 0 }, { 400, 1, 1 }, { 401, 1, 1 }, { 403, 1, 1 }, | ||
{ 404, 1, 1 }, { 409, 1, 1 }, { 499, 1, 1 }, { 500, 1, 1 }, { 502, 1, 1 }, { 503, 1, 1 }, { 504, 1, 1 }, | ||
{ 599, 1, 1 }, }; | ||
} | ||
|
||
@TestTemplate | ||
@UseDataProvider("responses") | ||
public void testCounts(int statusCode, int count, int errorCount) { | ||
TestTelemetryMiddleware middleware = new TestTelemetryMiddleware(); | ||
|
||
final ApiHttpRequest request = new ApiHttpRequest(ApiHttpMethod.GET, URI.create("/"), new ApiHttpHeaders(), | ||
"".getBytes()); | ||
|
||
blockingWait( | ||
middleware.invoke(request, | ||
request1 -> CompletableFuture | ||
.completedFuture(new ApiHttpResponse<>(statusCode, new ApiHttpHeaders(), "".getBytes()))), | ||
Duration.ofSeconds(1)); | ||
|
||
Assertions.assertThat(middleware.count).isEqualTo(count); | ||
Assertions.assertThat(middleware.errorCount).isEqualTo(errorCount); | ||
} | ||
|
||
@TestTemplate | ||
@UseDataProvider("responses") | ||
public void testHttpCounts(int statusCode, int count, int errorCount) throws URISyntaxException { | ||
TestTelemetryMiddleware middleware = new TestTelemetryMiddleware(); | ||
|
||
ApiHttpClient client = ClientBuilder | ||
.of(new TestHttpClient(request -> CompletableFuture | ||
.completedFuture(new ApiHttpResponse<>(statusCode, new ApiHttpHeaders(), "".getBytes())))) | ||
.withApiBaseUrl(new URI("")) | ||
.withTelemetryMiddleware(middleware) | ||
.withErrorMiddleware() | ||
.build(); | ||
|
||
final ApiHttpRequest request = new ApiHttpRequest(ApiHttpMethod.GET, URI.create("/"), new ApiHttpHeaders(), | ||
"".getBytes()); | ||
try { | ||
blockingWait(client.execute(request), Duration.ofSeconds(1)); | ||
} | ||
catch (ApiHttpException ignored) { | ||
} | ||
|
||
Assertions.assertThat(middleware.count).isEqualTo(count); | ||
Assertions.assertThat(middleware.errorCount).isEqualTo(errorCount); | ||
} | ||
|
||
static class TestHttpClient implements VrapHttpClient { | ||
private final Function<ApiHttpRequest, CompletableFuture<ApiHttpResponse<byte[]>>> next; | ||
|
||
public TestHttpClient(Function<ApiHttpRequest, CompletableFuture<ApiHttpResponse<byte[]>>> next) { | ||
this.next = next; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<ApiHttpResponse<byte[]>> execute(ApiHttpRequest request) { | ||
return next.apply(request); | ||
} | ||
} | ||
|
||
static class TestTelemetryMiddleware implements TelemetryMiddleware { | ||
|
||
public long count = 0; | ||
public long errorCount = 0; | ||
@Override | ||
public CompletableFuture<ApiHttpResponse<byte[]>> invoke(ApiHttpRequest request, | ||
Function<ApiHttpRequest, CompletableFuture<ApiHttpResponse<byte[]>>> next) { | ||
return next.apply(request).thenApply(response -> { | ||
count++; | ||
if (response.getStatusCode() >= 400) { | ||
errorCount++; | ||
} | ||
return response; | ||
}); | ||
} | ||
} | ||
} |