Closed alexluix closed 6 years ago
Can you paste the spec you're running?
`package io.hapicloud.testk
import com.hapi.kafka.client.KafkaPublisherImpl
import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import io.hapicloud.test.ConfigurationContext
import io.hapicloud.testk.ows.KafkaListener
import io.hapicloud.testk.ows.OxiHubClient
import io.hapicloud.testk.ows.OxiRequest
import io.hapicloud.testk.ows.TestContext
import io.hapicloud.testk.oxi.model.Header
import io.hapicloud.testk.oxi.model.Message
import org.hamcrest.CoreMatchers.is
import org.hamcrest.CoreMatchers.containsString
import org.hamcrest.CoreMatchers.notNullValue
import org.hamcrest.MatcherAssert.assertThat
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.given
import org.jetbrains.spek.api.dsl.it
import org.jetbrains.spek.api.dsl.on
import org.mockserver.client.server.MockServerClient
import org.mockserver.model.HttpRequest.request
import org.mockserver.model.HttpResponse.response
import org.springframework.http.HttpStatus
import java.util.concurrent.TimeUnit
object OxiHubSpec : Spek({
describe("OXI Hub API") {
val oxiHubUrl = ConfigurationContext.getOxiHubUrl()
val client = OxiHubClient(oxiHubUrl)
afterGroup {
client.close()
}
given("OXI Receiver API is available") {
client.checkUpAndRunning()
val channelId = getAnyAvailableChannelId(client)
on("channel $channelId is configured") {
client.isChannelConfigured(channelId)
val channel = client.getChannel(channelId)
val channelKey = channel.apiKey
it("should accept an OXI request to that channel") {
val oxiRequest = createOxiRequest(channelId, channelKey)
val response = client.sendOxiRequest(oxiRequest)
assertThat("Response body: ${response.responseBody}",
response.statusCode, is(HttpStatus.OK.value()))
}
val topicName = "oxi-inbound"
val groupId = "oxi-inbound-test"
it("should publish OXI request to topic $topicName") {
val deserializer = { it: String -> TestContext.objectMapper.readValue<Message>(it, Message::class.java) }
val filter = { it: Message -> it.header.channelId == channelId }
val kafkaListener = KafkaListener(topicName, groupId, deserializer, filter)
kafkaListener.use {
val oxiRequest = createOxiRequest(channelId, channelKey)
val response = client.sendOxiRequest(oxiRequest)
assertThat("Response body: ${response.responseBody}",
response.statusCode, is(HttpStatus.OK.value()))
val message = kafkaListener.getReceivedMessagesQueue().poll(1, TimeUnit.MINUTES)
assertThat("Message not received", message, notNullValue())
assertThat(message.header.channelId, equalTo(channelId))
}
}
it("should not accept an OXI request with wrong channelKey") {
val oxiRequest = createOxiRequest(channelId, "wrong-key")
val response = client.sendOxiRequest(oxiRequest)
assertThat("Response body: ${response.responseBody}",
response.statusCode, is(HttpStatus.UNAUTHORIZED.value()))
}
}
}
given("OXI Mock Endpoint is available") {
val mockServerHost = "localhost"
val mockServerPort = 1080
val mockBaseUrl = "http://$mockServerHost:$mockServerPort"
val mockServerClient = MockServerClient(mockServerHost, mockServerPort)
assertThat(mockServerClient.isRunning(5, 1, TimeUnit.SECONDS), `is`(true))
val channelId = getAnyAvailableChannelId(client)
on("channel $channelId is configured to use OXI mock endpoint") {
client.isChannelConfigured(channelId)
val channel = client.getChannel(channelId)
assertThat(channel.baseUrl, containsString(mockBaseUrl))
it("should post outbound OXI message to OXI mock endpoint") {
mockServerClient
.`when`(
request()
.withMethod("POST")
.withPath("/oxi-channel-1")
)
.respond(
response()
.withStatusCode(200)
)
val outboundTopicName = "oxi-outbound"
val serializer = { message: Message -> TestContext.objectMapper.writeValueAsString(message) }
val kafkaPublisher = KafkaPublisherImpl(outboundTopicName, serializer)
val messageBody = "body"
val message = Message(Header(channelId), messageBody)
kafkaPublisher.publish(message)
TimeUnit.SECONDS.sleep(1)
mockServerClient
.verify(
request()
.withMethod("POST")
.withPath("/oxi-channel-1")
.withBody(messageBody)
)
}
}
}
}
})
private fun getAnyAvailableChannelId(client: OxiHubClient) = getAvailableChannelIds(client)[0]
private fun getAvailableChannelIds(client: OxiHubClient): List
private fun createOxiRequest(channelId: String, channelKey: String): OxiRequest { return OxiRequest( messageType = "MSGType", propertyName = "Hilton", transactionId = "transaction-id", status = "Done", channelId = channelId, channelKey = channelKey, body = "{}" ) } `
@landpro sorry for the delay, so busy with moving countries. I see you're using string interpolation in the description, which is not supported by the plugin as for the moment.
Thank you for your response @raniejade – we eventually switched to JUnit5 + Kotlin
Looking forward to seeing Spek IDEA plugin to develop
IntelliJ IDEA 2017.3.1 (Ultimate Edition) Build #IU-173.3942.27, built on December 11, 2017 JRE: 1.8.0_152-release-1024-b8 x86_64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Mac OS X 10.13.2