Skip to content

Commit

Permalink
Move GraphQLQueryHelper and StreamHelper from the Core module (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdchow committed Aug 8, 2024
1 parent 8753b90 commit d8fbb38
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
23 changes: 23 additions & 0 deletions Drop-In/src/main/java/com/braintreepayments/api/StreamHelper.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit d8fbb38

Please sign in to comment.