#1212 clean up KiwixMockServer

This commit is contained in:
Sean Mac Gillicuddy 2019-08-12 14:20:59 +01:00
parent 774d11377e
commit 0669f20666
5 changed files with 56 additions and 24 deletions

View File

@ -4,6 +4,7 @@ import okhttp3.mockwebserver.Dispatcher
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.RecordedRequest
import org.kiwix.kiwixmobile.di.modules.TestNetworkModule
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book
import org.kiwix.kiwixmobile.library.entity.MetaLinkNetworkEntity
@ -13,7 +14,7 @@ import org.kiwix.kiwixmobile.library.entity.MetaLinkNetworkEntity.Url
import org.simpleframework.xml.core.Persister
import java.io.StringWriter
import java.util.LinkedList
import java.util.concurrent.TimeUnit
import java.util.Stack
/*
* Kiwix Android
@ -33,28 +34,26 @@ import java.util.concurrent.TimeUnit
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class KiwixMockServer {
val queuedResponses: Stack<MockResponse> = Stack()
private val mockWebServer = MockWebServer().apply {
start(8080)
start(PORT)
}
fun stop() {
mockWebServer.shutdown()
}
fun enqueue(libraryNetworkEntity: LibraryNetworkEntity) {
mockWebServer.enqueue(MockResponse().apply {
setResponseCode(200)
setBody(libraryNetworkEntity.asXmlString())
})
}
fun enqueueForEvery(
pathsToResponses: Map<String, Any>
fun map(
vararg pathsToResponses: Pair<String, Any>
) {
val mapOfPathsToResponses = mapOf(*pathsToResponses)
mockWebServer.setDispatcher(object : Dispatcher() {
override fun dispatch(request: RecordedRequest) =
pathsToResponses[request.path]?.let(::successfulResponse)
?: MockResponse().throttleBody(1L, 1L, TimeUnit.SECONDS).setBody("0123456789")
mapOfPathsToResponses[request.path]?.let(::successfulResponse)
?: queuedResponses.pop()?.let { return@let it }
?: throw RuntimeException("No response mapped for ${request.path}")
})
}
@ -62,6 +61,13 @@ class KiwixMockServer {
setResponseCode(200)
setBody(bodyObject.asXmlString())
}
fun queueResponse(mockResponse: MockResponse) {
}
companion object {
const val PORT = 8080
}
}
fun Any.asXmlString() = StringWriter().let {
@ -96,7 +102,7 @@ fun pieces(
}
fun url(
value: String = "http://localhost:8080/relevantUrl.zim.meta4",
value: String = "${TestNetworkModule.MOCK_BASE_URL}/relevantUrl.zim.meta4",
location: String = "location"
) = Url().apply {
this.location = location
@ -111,7 +117,7 @@ fun book(
creator: String = "creator",
publisher: String = "publisher",
date: String = "date",
url: String = "http://localhost:8080/url",
url: String = "${TestNetworkModule.MOCK_BASE_URL}/url",
articleCount: String = "mediaCount",
mediaCount: String = "mediaCount",
size: String = "1024",

View File

@ -22,7 +22,7 @@ public class BookmarksActivityTest {
GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE);
@Test
public void testSimple() {
public void BookmarksActivitySimple() {
}
}

View File

@ -19,6 +19,7 @@ package org.kiwix.kiwixmobile.di.modules
import dagger.Module
import okhttp3.OkHttpClient
import org.kiwix.kiwixmobile.KiwixMockServer
import org.kiwix.kiwixmobile.data.remote.KiwixService
/**
@ -27,9 +28,14 @@ import org.kiwix.kiwixmobile.data.remote.KiwixService
@Module
class TestNetworkModule : NetworkModule() {
internal override fun provideKiwixService(okHttpClient: OkHttpClient): KiwixService =
KiwixService.ServiceCreator.newHacklistService(
okHttpClient,
"http://localhost:8080/"
MOCK_BASE_URL
)
companion object {
const val MOCK_BASE_URL = "http://localhost:${KiwixMockServer.PORT}/"
}
}

View File

@ -1,29 +1,31 @@
package org.kiwix.kiwixmobile.zim_manager
import okhttp3.mockwebserver.MockResponse
import org.junit.Rule
import org.junit.Test
import org.kiwix.kiwixmobile.BaseActivityTest
import org.kiwix.kiwixmobile.KiwixApplication
import org.kiwix.kiwixmobile.KiwixMockServer
import org.kiwix.kiwixmobile.book
import org.kiwix.kiwixmobile.data.remote.KiwixService.LIBRARY_NETWORK_PATH
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity
import org.kiwix.kiwixmobile.libraryNetworkEntity
import org.kiwix.kiwixmobile.metaLinkNetworkEntity
import org.kiwix.kiwixmobile.utils.SharedPreferenceUtil
import java.util.concurrent.TimeUnit.SECONDS
class ZimManageActivityTest : BaseActivityTest<ZimManageActivity>() {
@get:Rule
override var activityRule = activityTestRule<ZimManageActivity> {
KiwixApplication.setApplicationComponent(testComponent())
}
private val book = book()
private val mockServer = KiwixMockServer().apply {
enqueueForEvery(
mapOf(
"/library/library_zim.xml" to libraryNetworkEntity(listOf(book)),
"/${book.url.substringAfterLast("/")}" to metaLinkNetworkEntity()
)
map(
LIBRARY_NETWORK_PATH to libraryNetworkEntity(listOf(book)),
book.networkPath to metaLinkNetworkEntity()
)
}
@ -38,6 +40,7 @@ class ZimManageActivityTest : BaseActivityTest<ZimManageActivity>() {
searchFor(book)
pressBack()
pressBack()
queueMockResponseWith("0123456789")
clickOn(book)
}
clickOnDownloading {
@ -47,6 +50,7 @@ class ZimManageActivityTest : BaseActivityTest<ZimManageActivity>() {
clickPositiveDialogButton()
}
clickOnOnline {
queueMockResponseWith("01234")
clickOn(book)
}
clickOnDownloading {
@ -66,4 +70,17 @@ class ZimManageActivityTest : BaseActivityTest<ZimManageActivity>() {
clickOnOnline { }
} clickOnLanguageIcon { }
}
private fun queueMockResponseWith(body: String) {
mockServer.queueResponse(
MockResponse()
.setBody(body)
.throttleBody(
1L, 1L, SECONDS
)
)
}
private val LibraryNetworkEntity.Book.networkPath
get() = "/${url.substringAfterLast("/")}"
}

View File

@ -30,7 +30,10 @@ import retrofit2.http.GET;
import retrofit2.http.Url;
public interface KiwixService {
@GET("/library/library_zim.xml") Single<LibraryNetworkEntity> getLibrary();
String LIBRARY_NETWORK_PATH = "/library/library_zim.xml";
@GET(LIBRARY_NETWORK_PATH) Single<LibraryNetworkEntity> getLibrary();
@GET Observable<MetaLinkNetworkEntity> getMetaLinks(@Url String url);