Last active
September 22, 2018 07:45
-
-
Save alfianyusufabdullah/50e57c61871365e89acf43f91992a899 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.alfianyusufabdullah.footballmatchschedule.ui.matchdetail | |
import android.util.Log | |
import com.alfianyusufabdullah.footballmatchschedule.BuildConfig | |
import com.alfianyusufabdullah.footballmatchschedule.database.DatabaseHelper | |
import com.alfianyusufabdullah.footballmatchschedule.database.database | |
import com.alfianyusufabdullah.footballmatchschedule.entity.MatchDetail | |
import com.alfianyusufabdullah.footballmatchschedule.entity.MatchDetailResponse | |
import com.alfianyusufabdullah.footballmatchschedule.entity.MatchFavorite | |
import com.alfianyusufabdullah.footballmatchschedule.entity.TeamInfoResponse | |
import com.alfianyusufabdullah.footballmatchschedule.repository.DatabaseRepository | |
import com.alfianyusufabdullah.footballmatchschedule.repository.FootballRepository | |
import com.alfianyusufabdullah.footballmatchschedule.util.CoroutineContextProvider | |
import com.google.gson.Gson | |
import com.squareup.okhttp.OkHttpClient | |
import com.squareup.okhttp.Request | |
import kotlinx.coroutines.experimental.Deferred | |
import kotlinx.coroutines.experimental.android.UI | |
import kotlinx.coroutines.experimental.async | |
import org.jetbrains.anko.coroutines.experimental.bg | |
import org.jetbrains.anko.db.* | |
/** | |
* Created by jonesrandom on 4/18/18. | |
* | |
* @site www.androidexample.web.id | |
* @github @alfianyusufabdullah | |
* | |
*/ | |
class MatchDetailPresenter(private val view: MatchDetailView?, private val footballRepository: FootballRepository, private val database: DatabaseHelper, private val coroutineContext: CoroutineContextProvider = CoroutineContextProvider()) { | |
private var job: Deferred<Unit?>? = null | |
fun loadDetailMatch(homeTeamID: String, awayTeamID: String, matchFileName: String) { | |
view?.onProgress() | |
job = async(coroutineContext.main) { | |
val requestHomeTeam = bg { | |
footballRepository.loadTeamInfo(homeTeamID) | |
} | |
val requestAwayTeam = bg { | |
footballRepository.loadTeamInfo(awayTeamID) | |
} | |
val requestDetailMatch = bg { | |
footballRepository.loadMatchDetail(matchFileName) | |
} | |
try { | |
val home = Gson().fromJson(requestHomeTeam.await(), TeamInfoResponse::class.java) | |
val away = Gson().fromJson(requestAwayTeam.await(), TeamInfoResponse::class.java) | |
val detail = Gson().fromJson(requestDetailMatch.await(), MatchDetailResponse::class.java) | |
view?.onLoad(home.teams, away.teams, detail.event) | |
} catch (e: Exception) { | |
view?.onLoad(mutableListOf(), mutableListOf(), mutableListOf()) | |
} | |
} | |
} | |
fun cancelJob() { | |
job?.let { | |
if (it.isActive) { | |
it.cancel() | |
} | |
} | |
} | |
} |
This file contains hidden or 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
package com.alfianyusufabdullah.footballmatchschedule.ui.matchdetail | |
import com.alfianyusufabdullah.footballmatchschedule.TestContextProvider | |
import com.alfianyusufabdullah.footballmatchschedule.database.DatabaseHelper | |
import com.alfianyusufabdullah.footballmatchschedule.entity.MatchDetailResponse | |
import com.alfianyusufabdullah.footballmatchschedule.entity.TeamInfoResponse | |
import com.alfianyusufabdullah.footballmatchschedule.repository.FootballRepository | |
import com.google.gson.Gson | |
import org.junit.Before | |
import org.junit.Test | |
import org.mockito.Mock | |
import org.mockito.Mockito.* | |
import org.mockito.MockitoAnnotations | |
/** | |
* Created by jonesrandom on 4/27/18. | |
* | |
* @site www.androidexample.web.id | |
* @github @alfianyusufabdullah | |
*/ | |
class MatchDetailPresenterTest { | |
@Mock | |
private lateinit var databaseHelper: DatabaseHelper | |
@Mock | |
private lateinit var footballRepository: FootballRepository | |
@Mock | |
private lateinit var gson: Gson | |
@Mock | |
private lateinit var matchDetailView: MatchDetailView | |
@Mock | |
private lateinit var homeTeamInfoResponse: TeamInfoResponse | |
@Mock | |
private lateinit var awayTeamInfoResponse: TeamInfoResponse | |
@Mock | |
private lateinit var matchDetailResponse: MatchDetailResponse | |
private lateinit var matchDetailPresenter: MatchDetailPresenter | |
@Before | |
fun setUp() { | |
MockitoAnnotations.initMocks(this) | |
matchDetailPresenter = MatchDetailPresenter(matchDetailView, footballRepository, databaseHelper, TestContextProvider()) | |
} | |
@Test | |
fun loadMatchDetailTest() { | |
val homeID = "133675" | |
val awayID = "134784" | |
val filenameMatch = "Serie_A_2018-04-22_Genoa_vs_Verona" | |
`when`(gson.fromJson(footballRepository.loadTeamInfo(homeID), TeamInfoResponse::class.java)).thenReturn(homeTeamInfoResponse) | |
`when`(gson.fromJson(footballRepository.loadTeamInfo(awayID), TeamInfoResponse::class.java)).thenReturn(awayTeamInfoResponse) | |
`when`(gson.fromJson(footballRepository.loadMatchDetail(filenameMatch), MatchDetailResponse::class.java)).thenReturn(matchDetailResponse) | |
matchDetailPresenter.loadDetailMatch(homeID, awayID, filenameMatch) | |
verify(matchDetailView).onProgress() | |
verify(matchDetailView).onLoad(homeTeamInfoResponse.teams , awayTeamInfoResponse.teams , matchDetailResponse.event) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment