-
Notifications
You must be signed in to change notification settings - Fork 664
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from novoda/add_repository_for_test
Add repository for test
- Loading branch information
Showing
7 changed files
with
175 additions
and
0 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
27 changes: 27 additions & 0 deletions
27
...onStarter/app/src/androidTest/java/com/novoda/androidstoreexample/data/dto/CategoryDTO.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,27 @@ | ||
package com.novoda.androidstoreexample.data.dto | ||
|
||
import com.novoda.androidstoreexample.models.Category | ||
import com.squareup.moshi.Moshi | ||
import khttp.get | ||
import org.json.JSONArray | ||
|
||
class CategoryDTO { | ||
private val moshi = Moshi.Builder().build() | ||
private val jsonAdapter = moshi.adapter(Category::class.java) | ||
|
||
fun getAllCategories(): ArrayList<Category> { | ||
val categoryResponse = requestCategories() | ||
return mapJsonOnModel(categoryResponse) | ||
} | ||
|
||
private fun mapJsonOnModel(categoryResponse: JSONArray): ArrayList<Category> { | ||
val categories = arrayListOf<Category>() | ||
for (i in 0 until categoryResponse.length()) { | ||
categories.add(jsonAdapter.fromJson(categoryResponse.getString(i))) | ||
} | ||
return categories | ||
} | ||
|
||
private fun requestCategories() = | ||
get(Constants.Urls.category).jsonObject.getJSONArray(Constants.Identifier.category) | ||
} |
16 changes: 16 additions & 0 deletions
16
...tionStarter/app/src/androidTest/java/com/novoda/androidstoreexample/data/dto/Constants.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,16 @@ | ||
package com.novoda.androidstoreexample.data.dto | ||
|
||
import com.novoda.androidstoreexample.BuildConfig | ||
|
||
object Constants { | ||
object Urls { | ||
const val category = "${BuildConfig.API_URL}/categories" | ||
const val itemsPrefix = "${BuildConfig.API_URL}/category/" | ||
const val itemsSuffix = "items" | ||
} | ||
|
||
object Identifier { | ||
const val category = "categories" | ||
const val product = "products" | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...ionStarter/app/src/androidTest/java/com/novoda/androidstoreexample/data/dto/ProductDTO.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,30 @@ | ||
package com.novoda.androidstoreexample.data.dto | ||
|
||
import com.novoda.androidstoreexample.models.Product | ||
import com.squareup.moshi.Moshi | ||
import khttp.get | ||
import org.json.JSONArray | ||
|
||
class ProductDTO { | ||
private val moshi = Moshi.Builder().build() | ||
private val jsonAdapter = moshi.adapter(Product::class.java) | ||
|
||
fun getArticlesForCategory(id: Int): ArrayList<Product> { | ||
val articleJson = requestProductJson(id) | ||
return mapJsonOnModel(articleJson) | ||
} | ||
|
||
private fun requestProductJson(id: Int): JSONArray { | ||
val articleUrl = "${Constants.Urls.itemsPrefix}$id/${Constants.Urls.itemsSuffix}" | ||
return get(articleUrl) | ||
.jsonObject.getJSONArray(Constants.Identifier.product) | ||
} | ||
|
||
private fun mapJsonOnModel(articleResponse: JSONArray): ArrayList<Product> { | ||
val articles = arrayListOf<Product>() | ||
for (i in 0 until articleResponse.length()) { | ||
articles.add(jsonAdapter.fromJson(articleResponse.getString(i))) | ||
} | ||
return articles | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...ionStarter/app/src/androidTest/java/com/novoda/androidstoreexample/data/models/Article.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,3 @@ | ||
package com.novoda.androidstoreexample.data.models | ||
|
||
data class Article(val title: String, val category: String, val description: String = "", val price: String = "") |
35 changes: 35 additions & 0 deletions
35
...rc/androidTest/java/com/novoda/androidstoreexample/data/repositories/ArticleRepository.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,35 @@ | ||
package com.novoda.androidstoreexample.data.repositories | ||
|
||
import com.novoda.androidstoreexample.data.dto.CategoryDTO | ||
import com.novoda.androidstoreexample.data.dto.ProductDTO | ||
import com.novoda.androidstoreexample.data.models.Article | ||
import com.novoda.androidstoreexample.models.Product | ||
|
||
class ArticleRepository { | ||
private val categories = CategoryDTO().getAllCategories() | ||
private val articleDto = ProductDTO() | ||
private val hatsCategoryName = "HATS" | ||
|
||
val standardArticle = Article(title = "Hat white", category = "HATS") | ||
|
||
fun getHat(id: Int = 0): Article { | ||
val categoryId = getCategoryIdForCategory(hatsCategoryName) | ||
val product = articleDto.getArticlesForCategory(categoryId)[id] | ||
return buildItem(product, hatsCategoryName) | ||
} | ||
|
||
fun getRandomHat(): Article { | ||
val categoryId = getCategoryIdForCategory(hatsCategoryName) | ||
val articles = articleDto.getArticlesForCategory(categoryId) | ||
val product = articles.shuffled().take(articles.size)[0] | ||
return buildItem(product, hatsCategoryName) | ||
} | ||
|
||
private fun buildItem(article: Product, categoryName: String): Article { | ||
return Article(title = article.title, price = article.price, category = categoryName, description = article.productDescription) | ||
} | ||
|
||
private fun getCategoryIdForCategory(categoryName: String): Int { | ||
return categories.filter { it.title == categoryName }[0].id | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...roidTest/java/com/novoda/androidstoreexample/tests/EspressoTestExampleWithRepositories.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,62 @@ | ||
package com.novoda.androidstoreexample.tests | ||
|
||
import android.support.test.rule.ActivityTestRule | ||
import com.novoda.androidstoreexample.activities.MainActivity | ||
import com.novoda.androidstoreexample.data.repositories.ArticleRepository | ||
import com.novoda.androidstoreexample.userflows.productUserFlow | ||
import org.junit.BeforeClass | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class EspressoTestExampleWithRepositories { | ||
|
||
private val activityTestRule = ActivityTestRule<MainActivity>(MainActivity::class.java) | ||
|
||
|
||
@get:Rule | ||
var activityRule: ActivityTestRule<MainActivity> = activityTestRule | ||
|
||
companion object { | ||
lateinit var articleRepository: ArticleRepository | ||
|
||
@JvmStatic | ||
@BeforeClass | ||
fun setUp() { | ||
articleRepository = ArticleRepository() | ||
} | ||
} | ||
|
||
|
||
@Test | ||
fun testNavigationWithFirstItemFromRepository() { | ||
val hat = articleRepository.getHat() | ||
|
||
productUserFlow { | ||
navigateToCategory(hat.category) | ||
openItemFromProductlist(hat.title) | ||
checkThatCorrectProductIsDisplayed() | ||
} | ||
} | ||
|
||
@Test | ||
fun testNavigationWithRandomItemFromRepository() { | ||
val hat = articleRepository.getRandomHat() | ||
|
||
productUserFlow { | ||
navigateToCategory(hat.category) | ||
openItemFromProductlist(hat.title) | ||
checkThatCorrectProductIsDisplayed() | ||
} | ||
} | ||
|
||
@Test | ||
fun testNavigationWithItemFromFixture() { | ||
val hat = articleRepository.standardArticle | ||
|
||
productUserFlow { | ||
navigateToCategory(hat.category) | ||
openItemFromProductlist(hat.title) | ||
checkThatCorrectProductIsDisplayed() | ||
} | ||
} | ||
} |