|
| 1 | +package fr.free.nrw.commons.explore.depictions |
| 2 | + |
| 3 | +import com.nhaarman.mockitokotlin2.mock |
| 4 | +import com.nhaarman.mockitokotlin2.whenever |
| 5 | +import depictSearchItem |
| 6 | +import fr.free.nrw.commons.mwapi.Binding |
| 7 | +import fr.free.nrw.commons.mwapi.Result |
| 8 | +import fr.free.nrw.commons.mwapi.SparqlResponse |
| 9 | +import fr.free.nrw.commons.upload.depicts.DepictsInterface |
| 10 | +import fr.free.nrw.commons.wikidata.model.DepictSearchResponse |
| 11 | +import io.reactivex.Single |
| 12 | +import org.junit.Before |
| 13 | +import org.junit.Test |
| 14 | +import org.mockito.Mock |
| 15 | +import org.mockito.MockitoAnnotations |
| 16 | +import org.wikipedia.wikidata.Entities |
| 17 | + |
| 18 | +class DepictsClientTest { |
| 19 | + |
| 20 | + @Mock |
| 21 | + private lateinit var depictsInterface: DepictsInterface |
| 22 | + private lateinit var depictsClient: DepictsClient |
| 23 | + |
| 24 | + @Before |
| 25 | + fun setUp() { |
| 26 | + MockitoAnnotations.initMocks(this) |
| 27 | + depictsClient = DepictsClient(depictsInterface) |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + fun searchForDepictions() { |
| 32 | + val depictSearchResponse = mock<DepictSearchResponse>() |
| 33 | + whenever(depictsInterface.searchForDepicts("query", "1", "en", "en", "0")) |
| 34 | + .thenReturn(Single.just(depictSearchResponse)) |
| 35 | + whenever(depictSearchResponse.search).thenReturn(listOf(depictSearchItem("1"),depictSearchItem("2"))) |
| 36 | + val entities = mock<Entities>() |
| 37 | + whenever(depictsInterface.getEntities("1|2")).thenReturn(Single.just(entities)) |
| 38 | + whenever(entities.entities()).thenReturn(emptyMap()) |
| 39 | + depictsClient.searchForDepictions("query", 1, 0) |
| 40 | + .test() |
| 41 | + .assertValue(emptyList()) |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + @Test |
| 46 | + fun getEntities() { |
| 47 | + val entities = mock<Entities>() |
| 48 | + whenever(depictsInterface.getEntities("ids")).thenReturn(Single.just(entities)) |
| 49 | + depictsClient.getEntities("ids").test().assertValue(entities) |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + fun toDepictions() { |
| 54 | + val sparqlResponse = mock<SparqlResponse>() |
| 55 | + val result = mock<Result>() |
| 56 | + whenever(sparqlResponse.results).thenReturn(result) |
| 57 | + val binding1 = mock<Binding>() |
| 58 | + val binding2 = mock<Binding>() |
| 59 | + whenever(result.bindings).thenReturn(listOf(binding1, binding2)) |
| 60 | + whenever(binding1.id).thenReturn("1") |
| 61 | + whenever(binding2.id).thenReturn("2") |
| 62 | + val entities = mock<Entities>() |
| 63 | + whenever(depictsInterface.getEntities("1|2")).thenReturn(Single.just(entities)) |
| 64 | + whenever(entities.entities()).thenReturn(emptyMap()) |
| 65 | + depictsClient.toDepictions(Single.just(sparqlResponse)) |
| 66 | + .test() |
| 67 | + .assertValue(emptyList()) |
| 68 | + } |
| 69 | +} |
0 commit comments