Last active
July 4, 2019 01:23
-
-
Save juliuscanute/25d6e4cf5a04d1a8246b84c866cdc36e to your computer and use it in GitHub Desktop.
Dictionary Handler Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DictionaryHandlerTest { | |
@Test | |
fun testGetTotalPagesInDictionary() { | |
val handler = DictionaryTotalPageHandler() | |
val gson = Gson() | |
loadKoinModules(module(override = true) { | |
single { MockRepository() as RepositoryInterface } | |
}) | |
val req = mock(HttpRequestMessage::class.java) | |
val context = mock(ExecutionContext::class.java) | |
doReturn(Logger.getGlobal()).`when`(context).logger | |
doAnswer { invocation -> | |
val status = invocation.arguments[0] as HttpStatus | |
HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status) | |
}.`when`(req).createResponseBuilder(any(HttpStatus::class.java)) | |
val ret = handler.run(req as HttpRequestMessage<Optional<String>>, context) | |
val body = ret.body.toString() | |
val result = gson.fromJson(body, Page::class.java) | |
assertEquals(1, result.start) | |
assertEquals(3, result.end) | |
assertEquals(10, result.recordsPerPage) | |
assertEquals(ret.status, HttpStatus.OK) | |
} | |
@Test | |
fun testGetTotalPagesInDictionary_withError() { | |
val handler = DictionaryTotalPageHandler() | |
val repository = mock(RepositoryInterface::class.java) | |
val gson = Gson() | |
// Setup | |
loadKoinModules(module(override = true) { | |
single { repository } | |
}) | |
val req = mock(HttpRequestMessage::class.java) | |
doReturn(URI("v1/dictionary/pages")).`when`(req).uri | |
val context = mock(ExecutionContext::class.java) | |
doReturn(Logger.getGlobal()).`when`(context).logger | |
doAnswer { invocation -> | |
val status = invocation.arguments[0] as HttpStatus | |
HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status) | |
}.`when`(req).createResponseBuilder(any(HttpStatus::class.java)) | |
doAnswer { | |
throw PageRetrievalException("error") | |
}.`when`(repository).getNumberOfPagesInDictionary() | |
val ret = handler.run(req as HttpRequestMessage<Optional<String>>, context) | |
val body = ret.body.toString() | |
val result = gson.fromJson(body, ErrorMesssage::class.java) | |
assertEquals("error", result.message) | |
assertEquals("v1/dictionary/pages", result.path) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment