Skip to content

Commit

Permalink
Use the repository in the use case
Browse files Browse the repository at this point in the history
  • Loading branch information
TWiStErRob committed Jan 13, 2024
1 parent fab3da5 commit 70f9238
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ class DomainHistoryRepository(
return result.map { it.parse() }
}

override fun getCurrent(feed: Feed): StatusItem =
statusDataSource.getCurrent(feed)

override fun getLatest(feed: Feed): StatusItem? =
statusHistoryDataSource.getAll(feed, 1).singleOrNull()

override fun save(item: StatusItem) {
statusHistoryDataSource.add(item)
}

private fun StatusItem.parse(): ParsedStatusItem =
when (this) {
is StatusItem.SuccessfulStatusItem -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package net.twisterrob.travel.domain.london.status

import net.twisterrob.travel.domain.london.status.api.HistoryRepository
import net.twisterrob.travel.domain.london.status.api.RefreshResult
import net.twisterrob.travel.domain.london.status.api.RefreshUseCase
import net.twisterrob.travel.domain.london.status.api.StatusHistoryDataSource
import net.twisterrob.travel.domain.london.status.api.StatusDataSource

class DomainRefreshUseCase(
private val statusHistoryDataSource: StatusHistoryDataSource,
private val statusDataSource: StatusDataSource,
private val historyRepository: HistoryRepository,
) : RefreshUseCase {

override fun refreshLatest(feed: Feed): RefreshResult {
val current = statusDataSource.getCurrent(feed)
val latest = statusHistoryDataSource.getAll(feed, 1).singleOrNull()
val current = historyRepository.getCurrent(feed)
val latest = historyRepository.getLatest(feed)

return when {
latest == null -> {
statusHistoryDataSource.add(current)
historyRepository.save(current)
RefreshResult.Created(current)
}

Expand All @@ -29,7 +27,7 @@ class DomainRefreshUseCase(
}

else -> {
statusHistoryDataSource.add(current)
historyRepository.save(current)
RefreshResult.Refreshed(current, latest)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
package net.twisterrob.travel.domain.london.status.api

import net.twisterrob.travel.domain.london.status.Feed
import net.twisterrob.travel.domain.london.status.StatusItem

interface HistoryRepository {

/**
* Returns the top [max] items from the history of [feed] ordered by [StatusItem.retrievedDate],
* including the current status if [includeCurrent] is true.
*
* @param max maximum number of items to return, current is not included in the count.
* @param includeCurrent whether to include the current status in the result.
*/
fun history(feed: Feed, max: Int, includeCurrent: Boolean): List<ParsedStatusItem>

/**
* @return the current live status.
*/
fun getCurrent(feed: Feed): StatusItem

/**
* @return the latest item from history, or null if there is no history.
*/
fun getLatest(feed: Feed): StatusItem?

/**
* Saves the item to history.
* If [StatusItem.retrievedDate] is later than the latest item in history, this item will become the latest.
*
* @param item the item to save.
*/
fun save(item: StatusItem)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package net.twisterrob.travel.domain.london.status

import io.mockative.any
import io.mockative.every
import io.mockative.mock
import io.mockative.verify
import io.mockative.verifyNoUnmetExpectations
import io.mockative.verifyNoUnverifiedExpectations
import net.twisterrob.travel.domain.london.status.api.FeedParser
import net.twisterrob.travel.domain.london.status.api.HistoryRepository
import net.twisterrob.travel.domain.london.status.api.StatusDataSource
import net.twisterrob.travel.domain.london.status.api.StatusHistoryDataSource
import kotlin.test.AfterTest
import kotlin.test.Test
import kotlin.test.assertSame

class DomainHistoryRepositoryUnitTest_getCurrent {

private val mockHistory: StatusHistoryDataSource = mock()
private val mockStatus: StatusDataSource = mock()
private val mockParser: FeedParser = mock()
private val subject: HistoryRepository = DomainHistoryRepository(mockHistory, mockStatus, mockParser)
private val feed = Feed.TubeDepartureBoardsLineStatus

@AfterTest
fun verify() {
listOf(mockHistory, mockStatus, mockParser).forEach {
verifyNoUnverifiedExpectations(it)
verifyNoUnmetExpectations(it)
}
}

@Test fun `returns successful item`() {
val current = SuccessfulStatusItem()
every { mockStatus.getCurrent(any()) }.returns(current)

val result = subject.getCurrent(feed)
assertSame(current, result)

verify { mockStatus.getCurrent(feed) }.wasInvoked()
}

@Test fun `returns failed item`() {
val current = FailedStatusItem()
every { mockStatus.getCurrent(any()) }.returns(current)

val result = subject.getCurrent(feed)
assertSame(current, result)

verify { mockStatus.getCurrent(feed) }.wasInvoked()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package net.twisterrob.travel.domain.london.status

import io.mockative.any
import io.mockative.every
import io.mockative.mock
import io.mockative.verify
import io.mockative.verifyNoUnmetExpectations
import io.mockative.verifyNoUnverifiedExpectations
import net.twisterrob.travel.domain.london.status.api.FeedParser
import net.twisterrob.travel.domain.london.status.api.HistoryRepository
import net.twisterrob.travel.domain.london.status.api.StatusDataSource
import net.twisterrob.travel.domain.london.status.api.StatusHistoryDataSource
import kotlin.test.AfterTest
import kotlin.test.Test
import kotlin.test.assertNull
import kotlin.test.assertSame

class DomainHistoryRepositoryUnitTest_getLatest {

private val mockHistory: StatusHistoryDataSource = mock()
private val mockStatus: StatusDataSource = mock()
private val mockParser: FeedParser = mock()
private val subject: HistoryRepository = DomainHistoryRepository(mockHistory, mockStatus, mockParser)
private val feed = Feed.TubeDepartureBoardsLineStatus

@AfterTest
fun verify() {
listOf(mockHistory, mockStatus, mockParser).forEach {
verifyNoUnverifiedExpectations(it)
verifyNoUnmetExpectations(it)
}
}

@Test fun `returns successful item`() {
val latest = SuccessfulStatusItem()
every { mockHistory.getAll(any(), any()) }.returns(listOf(latest))

val result = subject.getLatest(feed)
assertSame(latest, result)

verify { mockHistory.getAll(feed, 1) }.wasInvoked()
}

@Test fun `returns failed item`() {
val latest = FailedStatusItem()
every { mockHistory.getAll(any(), any()) }.returns(listOf(latest))

val result = subject.getLatest(feed)
assertSame(latest, result)

verify { mockHistory.getAll(feed, 1) }.wasInvoked()
}

@Test fun `returns no item`() {
every { mockHistory.getAll(any(), any()) }.returns(emptyList())

val result = subject.getLatest(feed)
assertNull(result)

verify { mockHistory.getAll(feed, 1) }.wasInvoked()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import kotlin.test.AfterTest
import kotlin.test.Test
import kotlin.test.assertEquals

class DomainHistoryRepositoryUnitTest {
class DomainHistoryRepositoryUnitTest_history {

private val mockHistory: StatusHistoryDataSource = mock()
private val mockStatus: StatusDataSource = mock()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package net.twisterrob.travel.domain.london.status

import io.mockative.mock
import io.mockative.verify
import io.mockative.verifyNoUnmetExpectations
import io.mockative.verifyNoUnverifiedExpectations
import net.twisterrob.travel.domain.london.status.api.FeedParser
import net.twisterrob.travel.domain.london.status.api.HistoryRepository
import net.twisterrob.travel.domain.london.status.api.StatusDataSource
import net.twisterrob.travel.domain.london.status.api.StatusHistoryDataSource
import kotlin.test.AfterTest
import kotlin.test.Test

class DomainHistoryRepositoryUnitTest_save {

private val mockHistory: StatusHistoryDataSource = mock()
private val mockStatus: StatusDataSource = mock()
private val mockParser: FeedParser = mock()
private val subject: HistoryRepository = DomainHistoryRepository(mockHistory, mockStatus, mockParser)

@AfterTest
fun verify() {
listOf(mockHistory, mockStatus, mockParser).forEach {
verifyNoUnverifiedExpectations(it)
verifyNoUnmetExpectations(it)
}
}

@Test fun `returns successful item`() {
val current = SuccessfulStatusItem()

subject.save(current)

verify { mockHistory.add(current) }.wasInvoked()
}

@Test fun `returns failed item`() {
val current = FailedStatusItem()

subject.save(current)

verify { mockHistory.add(current) }.wasInvoked()
}
}
Loading

0 comments on commit 70f9238

Please sign in to comment.