Skip to content

Commit

Permalink
refactor: fix prom sink tests by mocking response body
Browse files Browse the repository at this point in the history
  • Loading branch information
mayur.gubrele committed Aug 23, 2021
1 parent b75a29b commit 4b60fb1
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/test/java/io/odpf/firehose/sink/prometheus/PromSinkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public void shouldPrepareRequestDuringPreparationAndCallItDuringExecution() thro
when(statusLine.getStatusCode()).thenReturn(200);
when(request.build(messages)).thenReturn(httpPostList);
when(httpClient.execute(httpPost)).thenReturn(response);
when(response.getEntity()).thenReturn(httpEntity);
when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(Snappy.compress(writeRequest.toByteArray())));

PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient, retryStatusCodeRange, requestLogStatusCodeRanges);
promSink.prepare(messages);
Expand All @@ -108,6 +110,8 @@ public void shouldThrowNeedToRetryExceptionWhenResponseCodeIsGivenRange() throws
when(httpPost.getURI()).thenReturn(new URI("http://dummy.com"));
when(request.build(messages)).thenReturn(httpPostList);
when(httpClient.execute(httpPost)).thenReturn(response);
when(response.getEntity()).thenReturn(httpEntity);
when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(Snappy.compress(writeRequest.toByteArray())));

PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient,
new RangeToHashMapConverter().convert(null, "400-505"), requestLogStatusCodeRanges);
Expand Down Expand Up @@ -177,6 +181,7 @@ public void shouldLogEntireRequestIfInStatusCodeRangeAndCaptureDroppedMessages()
when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(Snappy.compress(writeRequest.toByteArray())));
when(request.build(messages)).thenReturn(httpPostList);
when(httpClient.execute(httpPost)).thenReturn(response);
when(response.getEntity()).thenReturn(httpEntity);

PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient,
retryStatusCodeRange, new RangeToHashMapConverter().convert(null, "400-505"));
Expand All @@ -202,6 +207,7 @@ public void shouldNotLogEntireRequestIfNotInStatusCodeRange() throws Exception {
when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(Snappy.compress(writeRequest.toByteArray())));
when(request.build(messages)).thenReturn(httpPostList);
when(httpClient.execute(httpPost)).thenReturn(response);
when(response.getEntity()).thenReturn(httpEntity);

PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient,
retryStatusCodeRange, new RangeToHashMapConverter().convert(null, "400-499"));
Expand All @@ -223,6 +229,7 @@ public void shouldCaptureDroppedMessagesMetricsIfNotInStatusCodeRange() throws E
when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(Snappy.compress(writeRequest.toByteArray())));
when(request.build(messages)).thenReturn(httpPostList);
when(httpClient.execute(httpPost)).thenReturn(response);
when(response.getEntity()).thenReturn(httpEntity);

PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient,
new RangeToHashMapConverter().convert(null, "400-499"), requestLogStatusCodeRanges);
Expand All @@ -239,6 +246,8 @@ public void shouldNotCaptureDroppedMessagesMetricsIfInStatusCodeRange() throws E
when(httpPost.getURI()).thenReturn(new URI("http://dummy.com"));
when(request.build(messages)).thenReturn(httpPostList);
when(httpClient.execute(httpPost)).thenReturn(response);
when(response.getEntity()).thenReturn(httpEntity);
when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(Snappy.compress(writeRequest.toByteArray())));

PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient,
new RangeToHashMapConverter().convert(null, "400-600"), requestLogStatusCodeRanges);
Expand All @@ -254,6 +263,8 @@ public void shouldNotCaptureDroppedMessagesMetricsIfStatusCodeIs200() throws Exc
when(httpPost.getURI()).thenReturn(new URI("http://dummy.com"));
when(request.build(messages)).thenReturn(httpPostList);
when(httpClient.execute(httpPost)).thenReturn(response);
when(response.getEntity()).thenReturn(httpEntity);
when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(Snappy.compress(writeRequest.toByteArray())));

PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient,
retryStatusCodeRange, requestLogStatusCodeRanges);
Expand All @@ -270,6 +281,8 @@ public void shouldNotCaptureDroppedMessagesMetricsIfStatusCodeIs201() throws Exc
when(httpPost.getURI()).thenReturn(new URI("http://dummy.com"));
when(request.build(messages)).thenReturn(httpPostList);
when(httpClient.execute(httpPost)).thenReturn(response);
when(response.getEntity()).thenReturn(httpEntity);
when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(Snappy.compress(writeRequest.toByteArray())));

PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient,
retryStatusCodeRange, requestLogStatusCodeRanges);
Expand All @@ -287,6 +300,8 @@ public void shouldCaptureResponseStatusCount() throws Exception {
when(httpPost.getURI()).thenReturn(uri);
when(request.build(messages)).thenReturn(httpPostList);
when(httpClient.execute(httpPost)).thenReturn(response);
when(response.getEntity()).thenReturn(httpEntity);
when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(Snappy.compress(writeRequest.toByteArray())));

PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient,
retryStatusCodeRange, requestLogStatusCodeRanges);
Expand Down

0 comments on commit 4b60fb1

Please sign in to comment.