Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: print response body via toString method
- Loading branch information
mayur.gubrele
committed
Aug 26, 2021
1 parent
4b60fb1
commit 017d027
Showing
5 changed files
with
79 additions
and
28 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
36 changes: 36 additions & 0 deletions
36
src/main/java/io/odpf/firehose/sink/http/SerializableHttpResponse.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,36 @@ | ||
package io.odpf.firehose.sink.http; | ||
|
||
import org.apache.http.HttpResponse; | ||
import org.apache.logging.log4j.util.Strings; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.Serializable; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class SerializableHttpResponse implements Serializable { | ||
private HttpResponse httpResponse; | ||
|
||
public SerializableHttpResponse(HttpResponse httpResponse) { | ||
this.httpResponse = httpResponse; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
InputStream inputStream = null; | ||
try { | ||
inputStream = httpResponse.getEntity().getContent(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return Strings.join(readContent(inputStream), '\n'); | ||
} | ||
|
||
private List<String> readContent(InputStream inputStream) { | ||
return new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines().collect(Collectors.toList()); | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
src/test/java/io/odpf/firehose/sink/http/SerializableHttpResponseTest.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,39 @@ | ||
package io.odpf.firehose.sink.http; | ||
|
||
import org.apache.http.HttpEntity; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.tools.ant.filters.StringInputStream; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.mockito.Mockito.when; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class SerializableHttpResponseTest { | ||
|
||
@Mock | ||
private HttpResponse httpResponse; | ||
@Mock | ||
private HttpEntity httpEntity; | ||
private SerializableHttpResponse serializableHttpResponse; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
serializableHttpResponse = new SerializableHttpResponse(httpResponse); | ||
} | ||
|
||
@Test | ||
public void shouldReturnTheResponseBodyStringInCaseOfNonNullResponse() throws IOException { | ||
when(httpResponse.getEntity()).thenReturn(httpEntity); | ||
when(httpEntity.getContent()).thenReturn(new StringInputStream("[{\"key\":\"value1\"}, {\"key\":\"value2\"}]")); | ||
|
||
String responseBody = serializableHttpResponse.toString(); | ||
Assert.assertEquals("[{\"key\":\"value1\"}, {\"key\":\"value2\"}]", responseBody); | ||
} | ||
} |
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