forked from mozilla-mobile/android-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For mozilla-mobile#11483 - Part 3: Implement a ContileTopSitesProvider
- Loading branch information
1 parent
06a6073
commit 05686c5
Showing
15 changed files
with
317 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
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 @@ | ||
# [Android Components](../../../README.md) > Service > Contile | ||
|
||
A library for communicating with the Contile services API. | ||
|
||
## Usage | ||
|
||
### Setting up the dependency | ||
|
||
Use Gradle to download the library from [maven.mozilla.org](https://maven.mozilla.org/) | ||
([Setup repository](../../../README.md#maven-repository)): | ||
|
||
```Groovy | ||
implementation "org.mozilla.components:service-contile:{latest-version}" | ||
``` | ||
|
||
## License | ||
|
||
This Source Code Form is subject to the terms of the Mozilla Public | ||
License, v. 2.0. If a copy of the MPL was not distributed with this | ||
file, You can obtain one at http://mozilla.org/MPL/2.0/ |
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,42 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
apply plugin: 'com.android.library' | ||
apply plugin: 'kotlin-android' | ||
|
||
android { | ||
compileSdkVersion config.compileSdkVersion | ||
|
||
defaultConfig { | ||
minSdkVersion config.minSdkVersion | ||
targetSdkVersion config.targetSdkVersion | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation Dependencies.kotlin_stdlib | ||
implementation Dependencies.kotlin_coroutines | ||
|
||
implementation project(':concept-fetch') | ||
implementation project(':support-ktx') | ||
implementation project(':support-base') | ||
implementation project(':feature-top-sites') | ||
|
||
testImplementation Dependencies.androidx_test_core | ||
testImplementation Dependencies.androidx_test_junit | ||
testImplementation Dependencies.testing_robolectric | ||
testImplementation Dependencies.testing_mockito | ||
|
||
testImplementation project(':support-test') | ||
} | ||
|
||
apply from: '../../../publish.gradle' | ||
ext.configurePublish(config.componentsGroupId, archivesBaseName, project.ext.description) |
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,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
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,6 @@ | ||
<!-- This Source Code Form is subject to the terms of the Mozilla Public | ||
- License, v. 2.0. If a copy of the MPL was not distributed with this | ||
- file, You can obtain one at http://mozilla.org/MPL/2.0/. --> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="mozilla.components.service.contile" > | ||
</manifest> |
84 changes: 84 additions & 0 deletions
84
...rvice/contile/src/main/java/mozilla/components/service/contile/ContileTopSitesProvider.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,84 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.service.contile | ||
|
||
import mozilla.components.concept.fetch.Client | ||
import mozilla.components.concept.fetch.Request | ||
import mozilla.components.concept.fetch.isSuccess | ||
import mozilla.components.feature.top.sites.TopSite | ||
import mozilla.components.feature.top.sites.TopSitesProvider | ||
import mozilla.components.support.base.log.logger.Logger | ||
import mozilla.components.support.ktx.android.org.json.asSequence | ||
import org.json.JSONException | ||
import org.json.JSONObject | ||
import java.io.IOException | ||
|
||
internal const val CONTILE_ENDPOINT_URL = "https://contile.services.mozilla.com/v1/tiles" | ||
|
||
/** | ||
* Provide access to the Contile services API. | ||
* | ||
* @property client [Client] used for interacting with the Contile HTTP API. | ||
*/ | ||
class ContileTopSitesProvider( | ||
private val client: Client | ||
) : TopSitesProvider { | ||
|
||
private val logger = Logger("ContileTopSitesProvider") | ||
|
||
@Throws(IOException::class) | ||
override suspend fun getTopSites(): List<TopSite.Contile> { | ||
return try { | ||
fetchTopSites() | ||
} catch (e: IOException) { | ||
logger.error("Failed to fetch contile top sites", e) | ||
throw e | ||
} | ||
} | ||
|
||
private fun fetchTopSites(): List<TopSite.Contile> { | ||
client.fetch( | ||
Request(url = CONTILE_ENDPOINT_URL) | ||
).use { response -> | ||
if (response.isSuccess) { | ||
val responseBody = response.body.string(Charsets.UTF_8) | ||
|
||
return try { | ||
JSONObject(responseBody).getTopSites() | ||
} catch (e: JSONException) { | ||
throw IOException(e) | ||
} | ||
} else { | ||
val errorMessage = | ||
"Failed to fetch contile top sites. Status code: ${response.status}" | ||
logger.error(errorMessage) | ||
throw IOException(errorMessage) | ||
} | ||
} | ||
} | ||
} | ||
|
||
internal fun JSONObject.getTopSites(): List<TopSite.Contile> = | ||
getJSONArray("tiles") | ||
.asSequence { i -> getJSONObject(i) } | ||
.mapNotNull { it.toTopSite() } | ||
.toList() | ||
|
||
private fun JSONObject.toTopSite(): TopSite.Contile? { | ||
return try { | ||
TopSite.Contile( | ||
id = getLong("id"), | ||
title = getString("name"), | ||
url = getString("url"), | ||
clickUrl = getString("click_url"), | ||
imageUrl = getString("image_url"), | ||
impressionUrl = getString("impression_url"), | ||
position = getInt("position"), | ||
createdAt = null | ||
) | ||
} catch (e: JSONException) { | ||
null | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...e/contile/src/test/java/mozilla/components/service/contile/ContileTopSitesProviderTest.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,74 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.service.contile | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import kotlinx.coroutines.runBlocking | ||
import mozilla.components.concept.fetch.Client | ||
import mozilla.components.concept.fetch.Response | ||
import mozilla.components.support.test.any | ||
import mozilla.components.support.test.file.loadResourceAsString | ||
import mozilla.components.support.test.mock | ||
import mozilla.components.support.test.whenever | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import java.io.IOException | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class ContileTopSitesProviderTest { | ||
|
||
@Test | ||
fun `GIVEN a successful status response WHEN getTopSites is called THEN response should contain top sites`() = runBlocking { | ||
val client = prepareClient() | ||
val provider = ContileTopSitesProvider(client) | ||
val topSites = provider.getTopSites() | ||
var topSite = topSites.first() | ||
|
||
assertEquals(2, topSites.size) | ||
|
||
assertEquals(1L, topSite.id) | ||
assertEquals("Firefox", topSite.title) | ||
assertEquals("https://firefox.com", topSite.url) | ||
assertEquals("https://firefox.com/click", topSite.clickUrl) | ||
assertEquals("https://test.com/image1.jpg", topSite.imageUrl) | ||
assertEquals("https://test.com", topSite.impressionUrl) | ||
assertEquals(1, topSite.position) | ||
|
||
topSite = topSites.last() | ||
|
||
assertEquals(2L, topSite.id) | ||
assertEquals("Mozilla", topSite.title) | ||
assertEquals("https://mozilla.com", topSite.url) | ||
assertEquals("https://mozilla.com/click", topSite.clickUrl) | ||
assertEquals("https://test.com/image2.jpg", topSite.imageUrl) | ||
assertEquals("https://example.com", topSite.impressionUrl) | ||
assertEquals(2, topSite.position) | ||
} | ||
|
||
@Test(expected = IOException::class) | ||
fun `GIVEN a 500 status response WHEN getTopSites is called THEN throw an exception`() = runBlocking { | ||
val client = prepareClient(status = 500) | ||
val provider = ContileTopSitesProvider(client) | ||
provider.getTopSites() | ||
Unit | ||
} | ||
|
||
private fun prepareClient( | ||
jsonResponse: String = loadResourceAsString("/contile/contile.json"), | ||
status: Int = 200 | ||
): Client { | ||
val mockedClient = mock<Client>() | ||
val mockedResponse = mock<Response>() | ||
val mockedBody = mock<Response.Body>() | ||
|
||
whenever(mockedBody.string(any())).thenReturn(jsonResponse) | ||
whenever(mockedResponse.body).thenReturn(mockedBody) | ||
whenever(mockedResponse.status).thenReturn(status) | ||
whenever(mockedClient.fetch(any())).thenReturn(mockedResponse) | ||
|
||
return mockedClient | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
components/service/contile/src/test/resources/contile/contile.json
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,24 @@ | ||
{ | ||
"tiles": [ | ||
{ | ||
"id": 1, | ||
"name": "Firefox", | ||
"url": "https://firefox.com", | ||
"click_url": "https://firefox.com/click", | ||
"image_url": "https://test.com/image1.jpg", | ||
"image_size": 200, | ||
"impression_url": "https://test.com", | ||
"position": 1 | ||
}, | ||
{ | ||
"id": 2, | ||
"name": "Mozilla", | ||
"url": "https://mozilla.com", | ||
"click_url": "https://mozilla.com/click", | ||
"image_url": "https://test.com/image2.jpg", | ||
"image_size": 200, | ||
"impression_url": "https://example.com", | ||
"position": 2 | ||
} | ||
] | ||
} |
2 changes: 2 additions & 0 deletions
2
...nents/service/contile/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker
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,2 @@ | ||
mock-maker-inline | ||
// This allows mocking final classes (classes are final by default in Kotlin) |
1 change: 1 addition & 0 deletions
1
components/service/contile/src/test/resources/robolectric.properties
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 @@ | ||
sdk=28 |
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
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
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