Skip to content

Commit

Permalink
Merge pull request #50 from novoda/add_repository_for_test
Browse files Browse the repository at this point in the history
Add repository for test
  • Loading branch information
dkaravias authored Oct 23, 2018
2 parents 50daf34 + 597b0e9 commit ec6d69e
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 0 deletions.
2 changes: 2 additions & 0 deletions AndroidTestAutomationStarter/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ dependencies {
testImplementation 'io.rest-assured:json-schema-validator:3.0.6'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
androidTestImplementation 'com.squareup.moshi:moshi:1.4.0'
androidTestImplementation 'com.squareup.okhttp:mockwebserver:1.3.0'
androidTestImplementation 'khttp:khttp:0.1.0'
androidTestImplementation 'org.mockito:mockito-core:2.8.9'
androidTestImplementation('com.android.support.test.espresso:espresso-contrib:3.0.1') {
exclude module: 'support-annotations'
Expand Down
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)
}
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"
}
}
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
}
}
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 = "")
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
}
}
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()
}
}
}

0 comments on commit ec6d69e

Please sign in to comment.