Skip to content

Instantly share code, notes, and snippets.

@Kalimaha
Last active October 26, 2019 07:40
Show Gist options
  • Save Kalimaha/6180db0e21aab71b7138131e771f69f4 to your computer and use it in GitHub Desktop.
Save Kalimaha/6180db0e21aab71b7138131e771f69f4 to your computer and use it in GitHub Desktop.
Test ServerResponse!
package au.com.mebank.integration.dwbfaprovisioning.operation.pushAccount
import au.com.mebank.integration.dwbfaprovisioning.errors.BFAProvisioningErrorSpecification
import au.com.mebank.integration.dwbfaprovisioning.models.MDESPushAccountResponse
import au.com.mebank.integration.reactivesoap.error.ServiceException
import org.springframework.http.MediaType
import org.springframework.web.reactive.function.BodyInserters
import org.springframework.web.reactive.function.server.ServerResponse
import reactor.core.publisher.Mono
fun handleError(e: Throwable): Mono<ServerResponse> {
return if (e is ServiceException) {
handleServiceException(e)
} else {
ServerResponse
.unprocessableEntity()
.contentType(MediaType.APPLICATION_JSON_UTF8)
.syncBody(e.localizedMessage)
}
}
private fun handleServiceException(e: ServiceException): Mono<ServerResponse> =
ServerResponse
.unprocessableEntity()
.contentType(MediaType.APPLICATION_JSON_UTF8)
.body(BodyInserters.fromObject(failure(e)))
private fun failure(e: ServiceException) =
MDESPushAccountResponse.Failure(
requestId = (e.specification as BFAProvisioningErrorSpecification).requestId,
source = e.specification.errorType.toString(),
reasonCode = e.specification.errorCode,
reason = e.specification.errorMessage
)
package blog.guidobarbaglia.stocks.mebank
import org.junit.Test
import org.springframework.http.HttpMethod
import org.springframework.http.codec.HttpMessageWriter
import org.springframework.mock.http.server.reactive.MockServerHttpRequest
import org.springframework.mock.http.server.reactive.MockServerHttpResponse
import org.springframework.mock.web.server.MockServerWebExchange
import org.springframework.web.reactive.function.server.ServerResponse
import org.springframework.web.reactive.result.view.ViewResolver
import reactor.core.publisher.Mono
import reactor.core.publisher.toMono
import reactor.test.StepVerifier
import java.util.*
val EMPTY_CONTEXT: ServerResponse.Context = object : ServerResponse.Context {
override fun messageWriters(): List<HttpMessageWriter<*>> {
return Collections.emptyList()
}
override fun viewResolvers(): List<ViewResolver> {
return Collections.emptyList()
}
}
class HomeworkTest {
@Test
fun `something`() {
val out: Mono<ServerResponse> = cinnamon()
val request = MockServerHttpRequest.method(HttpMethod.GET, "/")
val server = MockServerWebExchange.from(request)
StepVerifier.create(out)
.consumeNextWith { pippo -> pippo.writeTo(server, EMPTY_CONTEXT) }
.verifyComplete()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment