diff --git a/Drop-In/src/androidTest/java/com/braintreepayments/api/GraphQLQueryHelperTest.kt b/Drop-In/src/androidTest/java/com/braintreepayments/api/GraphQLQueryHelperTest.kt new file mode 100644 index 00000000..7cb40729 --- /dev/null +++ b/Drop-In/src/androidTest/java/com/braintreepayments/api/GraphQLQueryHelperTest.kt @@ -0,0 +1,17 @@ +package com.braintreepayments.api + +import android.content.res.Resources.NotFoundException +import androidx.test.core.app.ApplicationProvider +import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner +import com.braintreepayments.api.GraphQLQueryHelper.getQuery +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4ClassRunner::class) +class GraphQLQueryHelperTest { + + @Test(expected = NotFoundException::class) + fun query_throwsResourcesNotFoundExceptionForInvalidResources() { + getQuery(ApplicationProvider.getApplicationContext(), -1) + } +} diff --git a/Drop-In/src/main/java/com/braintreepayments/api/GraphQLQueryHelper.kt b/Drop-In/src/main/java/com/braintreepayments/api/GraphQLQueryHelper.kt new file mode 100644 index 00000000..ad38d010 --- /dev/null +++ b/Drop-In/src/main/java/com/braintreepayments/api/GraphQLQueryHelper.kt @@ -0,0 +1,20 @@ +package com.braintreepayments.api + +import android.content.Context +import android.content.res.Resources +import java.io.IOException +import java.io.InputStream + +internal object GraphQLQueryHelper { + @JvmStatic + @Throws(Resources.NotFoundException::class, IOException::class) + fun getQuery(context: Context, queryResource: Int): String { + var inputStream: InputStream? = null + return try { + inputStream = context.resources.openRawResource(queryResource) + StreamHelper.getString(inputStream) + } finally { + inputStream?.close() + } + } +} diff --git a/Drop-In/src/main/java/com/braintreepayments/api/StreamHelper.kt b/Drop-In/src/main/java/com/braintreepayments/api/StreamHelper.kt new file mode 100644 index 00000000..91911b77 --- /dev/null +++ b/Drop-In/src/main/java/com/braintreepayments/api/StreamHelper.kt @@ -0,0 +1,23 @@ +package com.braintreepayments.api + +import java.io.BufferedReader +import java.io.IOException +import java.io.InputStream +import java.io.InputStreamReader +import java.lang.StringBuilder +import java.nio.charset.Charset + +internal object StreamHelper { + @Throws(IOException::class) + fun getString(inputStream: InputStream?): String { + val buffReader = BufferedReader(InputStreamReader(inputStream, Charset.forName("UTF-8"))) + return buffReader.use { reader -> + val data = StringBuilder() + var line: String? + while (reader.readLine().also { line = it } != null) { + data.append(line) + } + data.toString() + } + } +} diff --git a/Drop-In/src/test/java/com/braintreepayments/api/StreamHelperUnitTest.kt b/Drop-In/src/test/java/com/braintreepayments/api/StreamHelperUnitTest.kt new file mode 100644 index 00000000..6b7b2401 --- /dev/null +++ b/Drop-In/src/test/java/com/braintreepayments/api/StreamHelperUnitTest.kt @@ -0,0 +1,18 @@ +package com.braintreepayments.api + +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import java.io.ByteArrayInputStream +import java.nio.charset.StandardCharsets + +@RunWith(RobolectricTestRunner::class) +class StreamHelperUnitTest { + + @Test + fun getString_readsAStringFromAStream() { + val inputStream = ByteArrayInputStream("Test string".toByteArray(StandardCharsets.UTF_8)); + assertEquals("Test string", StreamHelper.getString(inputStream)) + } +}