Skip to content
Permalink
Browse files

Make experiments variable private and fetch experiments from local st…

…orage when updating
  • Loading branch information...
fercarcedo committed May 30, 2018
1 parent 7996c0c commit 63c9792d9c98b7b5cb12aa62fec792710fa116d0
@@ -14,21 +14,24 @@ class Fretboard(
private val source: ExperimentSource,
private val storage: ExperimentStorage
) {
internal var experiments: List<Experiment> = listOf()
private set
private var experiments: List<Experiment> = listOf()
private var experimentsLoaded: Boolean = false

/**
* Loads experiments from local storage
*/
fun loadExperiments() {
experiments = storage.retrieve()
experimentsLoaded = true
}

/**
* Requests new experiments from the server and
* saves them to local storage
*/
fun updateExperiments() {
if (!experimentsLoaded)
loadExperiments()
try {
val serverExperiments = source.getExperiments(experiments)
experiments = serverExperiments
@@ -4,13 +4,12 @@

package mozilla.components.service.fretboard

import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
import org.mockito.Mockito.`when`
import org.mockito.Mockito.anyList
import org.mockito.Mockito.mock
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.robolectric.RobolectricTestRunner

@@ -20,26 +19,42 @@ class FretboardTest {
fun testLoadExperiments() {
val experimentSource = mock(ExperimentSource::class.java)
val experimentStorage = mock(ExperimentStorage::class.java)
`when`(experimentStorage.retrieve()).thenReturn(listOf(Experiment("id")))
val fretboard = Fretboard(experimentSource, experimentStorage)
assertEquals(0, fretboard.experiments.size)
fretboard.loadExperiments()
verify(experimentStorage).retrieve()
assertEquals(1, fretboard.experiments.size)
assertEquals("id", fretboard.experiments[0].id)
}

@Test
fun testUpdateExperiments() {
fun testUpdateExperimentsStorageNotLoaded() {
val experimentSource = mock(ExperimentSource::class.java)
val experimentStorage = mock(ExperimentStorage::class.java)
val fretboard = Fretboard(experimentSource, experimentStorage)
fretboard.updateExperiments()
verify(experimentStorage, times(1)).retrieve()
fretboard.updateExperiments()
verify(experimentStorage, times(1)).retrieve()
}

@Test
fun testUpdateExperimentsEmptyStorage() {
val experimentSource = mock(ExperimentSource::class.java)
`when`(experimentSource.getExperiments(ArgumentMatchers.anyList())).thenReturn(listOf(Experiment("id")))
val experimentStorage = mock(ExperimentStorage::class.java)
val fretboard = Fretboard(experimentSource, experimentStorage)
assertEquals(0, fretboard.experiments.size)
fretboard.updateExperiments()
verify(experimentSource).getExperiments(listOf())
verify(experimentStorage).save(listOf(Experiment("id")))
assertEquals(1, fretboard.experiments.size)
assertEquals("id", fretboard.experiments[0].id)
}

@Test
fun testUpdateExperimentsFromStorage() {
val experimentSource = mock(ExperimentSource::class.java)
`when`(experimentSource.getExperiments(ArgumentMatchers.anyList())).thenReturn(listOf(Experiment("id")))
val experimentStorage = mock(ExperimentStorage::class.java)
`when`(experimentStorage.retrieve()).thenReturn(listOf(Experiment("id0")))
val fretboard = Fretboard(experimentSource, experimentStorage)
fretboard.updateExperiments()
verify(experimentSource).getExperiments(listOf(Experiment("id0")))
verify(experimentStorage).save(listOf(Experiment("id")))
}
}

0 comments on commit 63c9792

Please sign in to comment.
You can’t perform that action at this time.