Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move GraphQLQueryHelper and StreamHelper from the Core module #484

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
}
Loading