|
| 1 | + |
| 2 | +package example; |
| 3 | + |
| 4 | +import com.commercetools.api.models.common.Reference; |
| 5 | +import com.commercetools.api.models.product.ProductReference; |
| 6 | +import com.commercetools.monitoring.datadog.DatadogResponseSerializer; |
| 7 | +import com.datadog.api.client.ApiException; |
| 8 | +import com.datadog.api.client.v2.api.MetricsApi; |
| 9 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 10 | + |
| 11 | +import io.vrap.rmf.base.client.ApiHttpHeaders; |
| 12 | +import io.vrap.rmf.base.client.ApiHttpResponse; |
| 13 | +import io.vrap.rmf.base.client.ResponseSerializer; |
| 14 | + |
| 15 | +import org.assertj.core.api.Assertions; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | +import org.mockito.Mockito; |
| 18 | + |
| 19 | +public class ResponseSerializerTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + public void testSerialize() throws ApiException, JsonProcessingException { |
| 23 | + MetricsApi metricsApi = Mockito.mock(MetricsApi.class); |
| 24 | + Mockito.when(metricsApi.submitMetrics(Mockito.any())).thenReturn(null); |
| 25 | + DatadogResponseSerializer serializer = new DatadogResponseSerializer(ResponseSerializer.of(), metricsApi); |
| 26 | + |
| 27 | + Reference reference = ProductReference.builder().id("abc").build(); |
| 28 | + |
| 29 | + serializer.toJsonByteArray(reference); |
| 30 | + |
| 31 | + Mockito.verify(metricsApi).submitMetrics(Mockito.argThat(metricPayload -> { |
| 32 | + Assertions.assertThat(metricPayload).isNotNull(); |
| 33 | + Assertions.assertThat(metricPayload.getSeries().get(0).getMetric()) |
| 34 | + .isEqualTo("commercetools.json.serialization"); |
| 35 | + return true; |
| 36 | + })); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testDeserialize() throws ApiException, JsonProcessingException { |
| 41 | + MetricsApi metricsApi = Mockito.mock(MetricsApi.class); |
| 42 | + Mockito.when(metricsApi.submitMetrics(Mockito.any())).thenReturn(null); |
| 43 | + DatadogResponseSerializer serializer = new DatadogResponseSerializer(ResponseSerializer.of(), metricsApi); |
| 44 | + |
| 45 | + String responseBody = "{ \"typeId\": \"product\", \"id\": \"abc\" }"; |
| 46 | + ApiHttpResponse<byte[]> response = new ApiHttpResponse<>(200, new ApiHttpHeaders(), responseBody.getBytes()); |
| 47 | + |
| 48 | + ApiHttpResponse<Reference> reference = serializer.convertResponse(response, Reference.class); |
| 49 | + |
| 50 | + Assertions.assertThat(reference.getBody()).isInstanceOf(ProductReference.class); |
| 51 | + Mockito.verify(metricsApi).submitMetrics(Mockito.argThat(metricPayload -> { |
| 52 | + Assertions.assertThat(metricPayload).isNotNull(); |
| 53 | + Assertions.assertThat(metricPayload.getSeries().get(0).getMetric()) |
| 54 | + .isEqualTo("commercetools.json.deserialization"); |
| 55 | + return true; |
| 56 | + })); |
| 57 | + } |
| 58 | +} |
0 commit comments