Skip to content

Instantly share code, notes, and snippets.

@juliuscanute
Last active July 4, 2019 01:23
Show Gist options
  • Save juliuscanute/25d6e4cf5a04d1a8246b84c866cdc36e to your computer and use it in GitHub Desktop.
Save juliuscanute/25d6e4cf5a04d1a8246b84c866cdc36e to your computer and use it in GitHub Desktop.
Dictionary Handler Test
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