Skip to content

Commit

Permalink
MOBC-471 || use values-in folder for Indonesian locale (#3)
Browse files Browse the repository at this point in the history
* Make API url injectable to run GradleRunner-based tests

* Use values-in folder for "id" locale

* Ensure line breaks at the end of files

* Use better name

* Make overriding default OneSky API url more explicit
  • Loading branch information
scana authored Jun 8, 2021
1 parent 486760a commit cd1a16b
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 21 deletions.
9 changes: 8 additions & 1 deletion plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ dependencies {
because("0.5.0 crashes on Table.renderText() call")
}

testImplementation("junit:junit:4.12")
testImplementation(gradleTestKit())
testImplementation("org.junit.jupiter:junit-jupiter:5.5.2")
testImplementation("org.assertj:assertj-core:3.11.1")
testImplementation("com.squareup.okhttp3:mockwebserver:$okHttp")
}


tasks.withType<Test>().configureEach {
useJUnitPlatform()
}
12 changes: 10 additions & 2 deletions plugin/src/main/java/co/brainly/onesky/OneSkyPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package co.brainly.onesky

import co.brainly.onesky.client.ONESKY_API_URL
import co.brainly.onesky.task.DownloadTranslationsTask
import co.brainly.onesky.task.TranslationsProgressTask
import co.brainly.onesky.task.UploadTranslationTask
import co.brainly.onesky.util.Constants
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.jetbrains.annotations.TestOnly

/**
* @property apiKey API key found on OneSky account's settings page
Expand All @@ -19,12 +21,18 @@ open class OneSkyPluginExtension(
var apiSecret: String = "",
var projectId: Int = -1,
var sourceStringFiles: List<String> = emptyList()
)
) {
internal var oneSkyApiUrl: String = ONESKY_API_URL

@TestOnly
fun overrideOneSkyApiUrl(oneSkyApiUrl: String) {
this.oneSkyApiUrl = oneSkyApiUrl
}
}

class OneSkyPlugin : Plugin<Project> {
override fun apply(project: Project): Unit = with(project) {
val extension = extensions.create("OneSkyPluginExtension", OneSkyPluginExtension::class.java)

tasks.register("translationsProgress", TranslationsProgressTask::class.java, extension)
.configure {
group = Constants.TASK_GROUP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ import okio.ByteString.Companion.encodeUtf8
import java.io.File
import java.util.concurrent.TimeUnit

private val ONESKY_API_URL = "https://platform.api.onesky.io/1/".toHttpUrl()
internal const val ONESKY_API_URL = "https://platform.api.onesky.io/1/"

class OneSkyApiClient(
private val apiKey: String,
private val apiSecret: String,
private val timeProvider: TimeProvider = SystemTimeProvider,
private val baseUrl: HttpUrl = ONESKY_API_URL
apiUrl: String
) {

private val baseUrl = apiUrl.toHttpUrl()

private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ open class DownloadTranslationsTask @Inject constructor(
private val projectId = extension.projectId
private val files = extension.sourceStringFiles

private val client = OneSkyApiClient(
extension.apiKey,
extension.apiSecret
)

private val logger = LoggerFactory.getLogger("downloadTranslations")
private val progressLogger by lazy {
services.get(ProgressLoggerFactory::class.java)
.newOperation("Downloading translations")
}

private val client = OneSkyApiClient(
apiKey = extension.apiKey,
apiSecret = extension.apiSecret,
apiUrl = extension.oneSkyApiUrl
)

@TaskAction
fun run() {
progressLogger.start("Downloading translations", "")
Expand Down Expand Up @@ -101,6 +102,12 @@ open class DownloadTranslationsTask @Inject constructor(
val (locale, region) = code.split("-")
return "$locale-r$region"
}

// https://developer.android.com/reference/java/util/Locale.html#toLanguageTag()
if (code.contains("id")) {
return "in"
}

return code
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ open class TranslationsProgressTask @Inject constructor(
private val projectId = extension.projectId

private val client = OneSkyApiClient(
extension.apiKey,
extension.apiSecret
apiKey = extension.apiKey,
apiSecret = extension.apiSecret,
apiUrl = extension.oneSkyApiUrl
)

@TaskAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ open class UploadTranslationTask @Inject constructor(
private val files = extension.sourceStringFiles

private val client = OneSkyApiClient(
extension.apiKey,
extension.apiSecret
apiKey = extension.apiKey,
apiSecret = extension.apiSecret,
apiUrl = extension.oneSkyApiUrl
)

private val logger = LoggerFactory.getLogger("uploadTranslations")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package co.brainly.onesky.client

import co.brainly.onesky.client.util.enqueueResponseWithJsonFile
import co.brainly.onesky.client.util.enqueueResponseWithFilesContent
import co.brainly.onesky.util.TimeProvider
import okhttp3.mockwebserver.MockWebServer
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.jupiter.api.Test
import java.io.File

class OneSkyApiClientTest {
Expand All @@ -15,7 +15,7 @@ class OneSkyApiClientTest {
private val client = OneSkyApiClient(
"my-api-key",
"my-api-secret",
baseUrl = server.url("/"),
apiUrl = server.url("/").toString(),
timeProvider = object : TimeProvider {
override fun currentTimeMillis(): Long {
return 12345L
Expand All @@ -25,7 +25,7 @@ class OneSkyApiClientTest {

@Test
fun `downloads list of project's languages`() {
server.enqueueResponseWithJsonFile("project_languages_response.json")
server.enqueueResponseWithFilesContent("project_languages_response.json")

val response = client.fetchProjectLanguages(projectId)

Expand Down Expand Up @@ -76,7 +76,7 @@ class OneSkyApiClientTest {

@Test
fun `downloads a translation file`() {
server.enqueueResponseWithJsonFile("example_translation_file.xml")
server.enqueueResponseWithFilesContent("example_translation_file.xml")

val language = Language("fr", "French", false, "100.0%")
val translationResult = client.fetchTranslation(projectId, "strings.xml", language)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package co.brainly.onesky.client.util
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer

fun MockWebServer.enqueueResponseWithJsonFile(resourceFilename: String) {
fun MockWebServer.enqueueResponseWithFilesContent(resourceFilename: String) {
val content = javaClass.getResource("/$resourceFilename")
.readText()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package co.brainly.onesky.task

import co.brainly.onesky.client.util.enqueueResponseWithFilesContent
import co.brainly.onesky.util.buildFile
import co.brainly.onesky.util.settingsFile
import okhttp3.mockwebserver.MockWebServer
import org.gradle.testkit.runner.GradleRunner
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import java.io.File
import org.junit.jupiter.api.Assertions.assertEquals as assertEquals

class DownloadTranslationsTaskTest {

private val server = MockWebServer()

@Test
fun `uses values-in directory for Indonesian locale (id) `(@TempDir tempDir: File) {
val projectDir = tempDir.resolve("onesky-project").apply { mkdirs() }
val moduleDir = projectDir.resolve("my-module").apply { mkdirs() }

projectDir.settingsFile().writeText(
"""
include ":my-module"
""".trimIndent()
)

moduleDir.buildFile().writeText(
"""
import co.brainly.onesky.OneSkyPluginExtension
buildscript {
repositories {
mavenLocal()
mavenCentral()
gradlePluginPortal()
}
}
plugins {
id("co.brainly.onesky")
}
configure<OneSkyPluginExtension> {
apiKey = "???"
apiSecret = "???"
projectId = 378760
sourceStringFiles = listOf("strings.xml")
overrideOneSkyApiUrl("${server.url("/")}")
}
""".trimIndent()
)

server.enqueueResponseWithFilesContent("project_languages_response_just_id.json")
server.enqueueResponseWithFilesContent("example_translation_file.xml")

GradleRunner.create()
.withProjectDir(projectDir)
.withPluginClasspath()
.withArguments(":my-module:downloadTranslations")
.build()

assertEquals(
false,
moduleDir.resolve("src/main/res/values-id/strings.xml").exists(),
"files not created in values-id directory"
)

assertEquals(
true,
moduleDir.resolve("src/main/res/values-in/strings.xml").exists(),
"files created in values-in directory"
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package co.brainly.onesky.util

import java.io.File

fun File.buildFile() = resolve("build.gradle.kts")
fun File.settingsFile() = resolve("settings.gradle")
2 changes: 1 addition & 1 deletion plugin/src/test/resources/project_languages_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,4 @@
"last_updated_at": "2020-10-28T04:19:09+0000",
"last_updated_at_timestamp": 1603858749
}]
}
}
19 changes: 19 additions & 0 deletions plugin/src/test/resources/project_languages_response_just_id.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"meta": {
"status": 200,
"record_count": 33
},
"data": [{
"code": "id",
"english_name": "Indonesian",
"local_name": "Bahasa Indonesia",
"custom_locale": null,
"locale": "id",
"region": "",
"is_base_language": false,
"is_ready_to_publish": false,
"translation_progress": "99.8%",
"last_updated_at": "2020-11-09T03:34:42+0000",
"last_updated_at_timestamp": 1604892882
}]
}

0 comments on commit cd1a16b

Please sign in to comment.