-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move GraphQLQueryHelper and StreamHelper from the Core module (#484)
- Loading branch information
Showing
4 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
Drop-In/src/androidTest/java/com/braintreepayments/api/GraphQLQueryHelperTest.kt
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,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) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
Drop-In/src/main/java/com/braintreepayments/api/GraphQLQueryHelper.kt
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,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
23
Drop-In/src/main/java/com/braintreepayments/api/StreamHelper.kt
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,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() | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Drop-In/src/test/java/com/braintreepayments/api/StreamHelperUnitTest.kt
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,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)) | ||
} | ||
} |