diff --git a/src/main/java/ResourceApi.java b/src/main/java/ResourceApi.java index 631db49..23cdcca 100644 --- a/src/main/java/ResourceApi.java +++ b/src/main/java/ResourceApi.java @@ -1,4 +1,7 @@ +import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; public class ResourceApi { @@ -13,4 +16,17 @@ public class ResourceApi { return inputStream; } } + + public String getStringFromInputStream(InputStream inputStream) throws IOException { + BufferedReader in = new BufferedReader(new InputStreamReader(inputStream)); + String inputLine; + StringBuffer response = new StringBuffer(); + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine + "\n"); + } + in.close(); + + return response.toString(); + } } diff --git a/src/test/java/ResourceApiTest.java b/src/test/java/ResourceApiTest.java index cecc615..3c985ab 100644 --- a/src/test/java/ResourceApiTest.java +++ b/src/test/java/ResourceApiTest.java @@ -1,13 +1,10 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; +import java.io.*; +import java.nio.charset.StandardCharsets; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; class ResourceApiTest { @@ -36,4 +33,11 @@ class ResourceApiTest { assertTrue(response.toString().contains("Alleinherrschaft")); } + + @Test + void getStringFromInputStream() throws IOException { + String testString = "I am a test string!\nAnother test line.\n"; + InputStream is = new ByteArrayInputStream(testString.getBytes(StandardCharsets.UTF_8)); + assertEquals(resourceApi.getStringFromInputStream(is), testString); + } }