I have the following setup:
The idea is to use Spring to test the behavior of the external service. Usually, we make a request over HTTP and get a response from the service. In this case we have to call the service over a JMS Queue, and we get the response over HTTP.
(In production, a publisher will send the message to the external service, which will route to other subscribers, and that's what I'm trying to mock)
So far I have been able to call put the message on the JMS Queue, and have it received by the Spring Boot rest server. I can tell this because of some console output.
Test I wrote so far
@ContextConfiguration(classes = [(Bw6TestsApplication::class)])
class LocusHttpInterfaceTests : BehaviorSpec() {
override fun listeners() = listOf(SpringListener)
@Autowired
lateinit var jmsService: MyJmsService
init {
given("a Locus envelope with an article payload") {
val objectFactory = ObjectFactory()
val sdf = SimpleDateFormat("dd-MM-yyyy/HH:mm:ss")
val envelope = objectFactory.createEnvelope().apply {
fromsys = "TEST_FROM"
tosys = "TEST_TO"
msgID = "AR"
timestamp = sdf.format(Date(System.currentTimeMillis()))
}
`when`("the message is sent to test.queue") {
jmsService.sendEnvelope(envelope)
then("the API server should receive a POST request with the message") {
eventually(5.seconds) { // Meaning we'll wait for a maximum of 5 seconds
// Verify somehow the external service called the rest api before the timeout
}
}
}
}
}
}
So far I figured the best thing would be to setup an event
class ReceivedEnvelopeEvent(source: Any, val envelope: Envelope) : ApplicationEvent(source)
@Component
class ReceivedEnvelopeEventPublisher(private val applicationEventPublisher: ApplicationEventPublisher) {
fun publish(envelope: Envelope) {
println("Publishing received envelope: $envelope")
val receivedEnvelopeEvent = ReceivedEnvelopeEvent(this, envelope)
applicationEventPublisher.publishEvent(receivedEnvelopeEvent)
}
}
@Component
class ReceivedEnvelopeEventListener : ApplicationListener<ReceivedEnvelopeEvent> {
override fun onApplicationEvent(event: ReceivedEnvelopeEvent) {
println("Received envelope: ${event.envelope}")
}
}
In the controller I'll call ReceivedEnvelopeEventPublisher
@Controller class MockController(private val receivedEnvelopeEventPublisher: ReceivedEnvelopeEventPublisher) {
@PostMapping("/tests")
fun getEnvelope(@RequestBody message: String): ResponseEntity<String> {
val envelope = unmarshalEnvelope(envelopeXML = message)
receivedEnvelopeEventPublisher.publish(envelope = envelope)
return ResponseEntity("OK", HttpStatus.OK)
}
}
And I can see in the console the ReceivedEnvelopeEventListener is called.
Now the question is: how can I use this to verify the external service called the rest endpoint within five seconds?

Aucun commentaire:
Enregistrer un commentaire