mirror of
https://github.com/MinimalBible/MinimalBible
synced 2024-11-04 23:28:19 -05:00
Test the MBIndexManager
This commit is contained in:
parent
a77076c96c
commit
7a35ad69ea
@ -0,0 +1,78 @@
|
||||
package org.bspeice.minimalbible.activity.search
|
||||
|
||||
import org.jetbrains.spek.api.Spek
|
||||
import org.mockito.Mockito
|
||||
import org.crosswire.jsword.index.IndexManager
|
||||
import org.crosswire.jsword.book.Book
|
||||
import org.crosswire.jsword.index.IndexStatus
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
import java.util.concurrent.atomic.AtomicReference
|
||||
import kotlin.test.assertEquals
|
||||
import com.jayway.awaitility.Awaitility
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
/**
|
||||
* Created by bspeice on 2/16/15.
|
||||
*/
|
||||
class MBIndexManagerSpek() : Spek() {{
|
||||
|
||||
given("a mock IndexManager, Book, and real MBIndexManager") {
|
||||
val returnDelay: Long = 1000
|
||||
val mockIndex = Mockito.mock(javaClass<IndexManager>())
|
||||
val mockBook = Mockito.mock(javaClass<Book>())
|
||||
val indexManager = MBIndexManager(mockIndex)
|
||||
|
||||
val firstStatus = IndexStatus.UNDONE
|
||||
val secondStatus = IndexStatus.DONE
|
||||
|
||||
// We sleep the first response to give us time to actually subscribe
|
||||
Mockito.`when`(mockBook.getIndexStatus())
|
||||
.thenAnswer { Thread.sleep(returnDelay); firstStatus }
|
||||
.thenReturn(secondStatus)
|
||||
|
||||
on("attempting to create the index") {
|
||||
val firstNext = AtomicReference<IndexStatus>()
|
||||
val secondNext = AtomicReference<IndexStatus>()
|
||||
val completedReference = AtomicBoolean(false)
|
||||
|
||||
val subject = indexManager.buildIndex(mockBook)
|
||||
|
||||
subject.subscribe({
|
||||
if (firstNext.get() == null)
|
||||
firstNext.set(it)
|
||||
else
|
||||
secondNext.set(it)
|
||||
},
|
||||
{},
|
||||
{ completedReference.set(true) })
|
||||
|
||||
it("should fire an onComplete so we can continue further validation") {
|
||||
Awaitility.waitAtMost(returnDelay * 2, TimeUnit.MILLISECONDS)
|
||||
.untilTrue(completedReference)
|
||||
}
|
||||
|
||||
it("should fire the correct first status") {
|
||||
assertEquals(firstStatus, firstNext.get())
|
||||
}
|
||||
|
||||
it("should fire the correct second status") {
|
||||
assertEquals(secondStatus, secondNext.get())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
given("a mock IndexManager, Book, and real MBIndexManager") {
|
||||
val indexManager = Mockito.mock(javaClass<IndexManager>())
|
||||
val book = Mockito.mock(javaClass<Book>())
|
||||
val mbIndex = MBIndexManager(indexManager)
|
||||
|
||||
on("trying to remove a book's index") {
|
||||
mbIndex.removeIndex(book)
|
||||
|
||||
it("should call the IndexManager.deleteIndex() function") {
|
||||
Mockito.verify(indexManager, Mockito.times(1)) deleteIndex book
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -5,27 +5,34 @@ import org.crosswire.jsword.book.Book
|
||||
import android.util.Log
|
||||
import rx.schedulers.Schedulers
|
||||
import rx.Observable
|
||||
import rx.subjects.PublishSubject
|
||||
import org.crosswire.jsword.index.IndexStatus
|
||||
|
||||
/**
|
||||
* There's already an IndexManager, that's why the funky name
|
||||
*/
|
||||
class MBIndexManager(val indexManager: IndexManager) {
|
||||
|
||||
fun buildIndex(b: Book) {
|
||||
/**
|
||||
* Do the hard work of actually building the book index.
|
||||
* Returns a PublishSubject<> that completes when the
|
||||
* index is complete. Also is nice enough to broadcast
|
||||
* what work is being done when.
|
||||
*/
|
||||
fun buildIndex(b: Book): PublishSubject<IndexStatus> {
|
||||
val indexStatus: PublishSubject<IndexStatus> = PublishSubject.create();
|
||||
Observable.just(b)
|
||||
.observeOn(Schedulers.computation())
|
||||
.subscribe({
|
||||
try {
|
||||
Log.e("MBIndexManager", "Beginning index status: ${b.getIndexStatus()}")
|
||||
indexManager scheduleIndexCreation b
|
||||
Log.e("MBIndexManager", "Ending index status: ${b.getIndexStatus()}")
|
||||
} catch (e: Exception) {
|
||||
Log.e("MBIndexManager", "Exception building index: ${e}", e)
|
||||
}
|
||||
}, {
|
||||
Log.e("MBIndexManager", "Exception building index: $it", it)
|
||||
})
|
||||
Log.d("MBIndexManager", "Building index for ${b.getInitials()}")
|
||||
indexStatus.onNext(b.getIndexStatus())
|
||||
|
||||
indexManager scheduleIndexCreation b
|
||||
|
||||
indexStatus.onNext(b.getIndexStatus())
|
||||
indexStatus.onCompleted()
|
||||
}, { Log.e("MBIndexManager", "Exception building index: $it", it) })
|
||||
|
||||
return indexStatus
|
||||
}
|
||||
|
||||
fun removeIndex(b: Book) = indexManager.deleteIndex(b)
|
||||
|
Loading…
Reference in New Issue
Block a user