Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(mockwebserver): DefaultMockServerCrudTest uses Vert.x to perform verifications #5659

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions junit/mockwebserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@
<groupId>io.fabric8</groupId>
<artifactId>zjsonpatch</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-uri-template</artifactId>
<version>${vertx.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,23 @@
*/
package io.fabric8.mockwebserver

import com.fasterxml.jackson.databind.ObjectMapper
import io.fabric8.mockwebserver.crud.CrudDispatcher
import okhttp3.MediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import io.vertx.core.Future
import io.vertx.core.Vertx
import io.vertx.ext.web.client.WebClient
import okhttp3.mockwebserver.MockWebServer
import spock.lang.Shared
import spock.lang.Specification
import spock.util.concurrent.AsyncConditions

class DefaultMockServerCrudTest extends Specification {

DefaultMockServer server

@Shared
def client = new OkHttpClient()

static def vertx = Vertx.vertx()
@Shared
def mapper = new ObjectMapper()
static def client = WebClient.create(vertx)

DefaultMockServer server

def setup() {
server = new DefaultMockServer(new Context(), new MockWebServer(), new HashMap<>(),
Expand All @@ -45,98 +43,143 @@ class DefaultMockServerCrudTest extends Specification {
server.shutdown()
}

def "get /, with empty store, should return 404"() {
when:
def result = client.newCall(new Request.Builder().url(server.url("/")).build()).execute()

then:
assert result.code() == 404
assert result.body().string() == ""
def cleanupSpec() {
client.close()
vertx.close()
}

def "get /, with one item, should return item"() {
given:
client.newCall(new Request.Builder().url(server.url("/")).post(
RequestBody.create(MediaType.parse("application/json"),
mapper.writeValueAsString(new User(1L, "user", true)))).build()).
execute()

when:
def result = client.newCall(new Request.Builder().url(server.url("/")).build()).execute()
def "GET /, with empty store, should return 404"() {
given: "An HTTP request to /"
def request = client.get(server.port, server.getHostName(), "/")
and: "An instance of AsyncConditions"
def async = new AsyncConditions(1)

when: "The request is sent and completed"
request.send().onComplete { res ->
async.evaluate {
assert res.result().statusCode() == 404
assert res.result().body() == null
}
}

then: "Expect the result to be completed in the specified time"
async.await(10)
}

then:
assert result.code() == 200
assert result.body().string() == "{\"id\":1,\"username\":\"user\",\"enabled\":true}"
def "POST /, with one item, should return item"() {
given: "An HTTP request to /"
def request = client.post(server.port, server.getHostName(), "/")
and: "An instance of AsyncConditions"
def async = new AsyncConditions(1)

when: "The request is sent with one JSON item and completed"
request.sendJson(new User(1L, "user", true)).onComplete { res ->
async.evaluate {
assert res.result().statusCode() == 202
assert res.result().body().toString() == "{\"id\":1,\"username\":\"user\",\"enabled\":true}"
}
}

then: "Expect the result to be completed in the specified time"
async.await(10)
}

def "get /, with multiple items, should return array"() {
given:
client.newCall(new Request.Builder().url(server.url("/")).post(
RequestBody.create(MediaType.parse("application/json"),
mapper.writeValueAsString(new User(1L, "user", true)))).build()).
execute()
client.newCall(new Request.Builder().url(server.url("/")).post(
RequestBody.create(MediaType.parse("application/json"),
mapper.writeValueAsString(new User(2L, "user-2", true)))).build()).
execute()

when:
def result = client.newCall(new Request.Builder().url(server.url("/")).build()).execute()

then:
assert result.code() == 200
assert result.body().string() ==
"[{\"id\":1,\"username\":\"user\",\"enabled\":true},{\"id\":2,\"username\":\"user-2\",\"enabled\":true}]"
def "GET /, with multiple items, should return array"() {
given: "An HTTP request to /"
def request = client.get(server.port, server.getHostName(), "/")
and: "An instance of AsyncConditions"
def async = new AsyncConditions(1)
and: "Items in the server"
def itemsInServer = Future.all(
client.post(server.port, server.getHostName(), "/")
.sendJson(new User(1L, "user", true)),
client.post(server.port, server.getHostName(), "/")
.sendJson(new User(2L, "user-2", true))
)

when: "The request is sent and completed"
itemsInServer.onComplete {isr ->
request.send().onComplete { res ->
async.evaluate { assert res.result().statusCode() == 200
assert res.result().body().toString() == "[{\"id\":1,\"username\":\"user\",\"enabled\":true},{\"id\":2,\"username\":\"user-2\",\"enabled\":true}]"
}
}
}

then: "Expect the result to be completed in the specified time"
async.await(10)
}

def "get /1, with existent item, should return item"() {
given:
client.newCall(new Request.Builder().url(server.url("/")).post(
RequestBody.create(MediaType.parse("application/json"),
mapper.writeValueAsString(new User(1L, "user", true)))).build()).
execute()
client.newCall(new Request.Builder().url(server.url("/")).post(
RequestBody.create(MediaType.parse("application/json"),
mapper.writeValueAsString(new User(2L, "user-2", true)))).build()).
execute()

when:
def result = client.newCall(new Request.Builder().url(server.url("/1")).build()).execute()

then:
assert result.code() == 200
assert result.body().string() == "{\"id\":1,\"username\":\"user\",\"enabled\":true}"
def "GET /1, with existent item, should return item"() {
given: "An HTTP request to /1"
def request = client.get(server.port, server.getHostName(), "/1")
and: "An instance of AsyncConditions"
def async = new AsyncConditions(1)
and: "Items in the server"
def itemsInServer = Future.all(
client.post(server.port, server.getHostName(), "/")
.sendJson(new User(1L, "user", true)),
client.post(server.port, server.getHostName(), "/")
.sendJson(new User(2L, "user-2", true))
)

when: "The request is sent and completed"
itemsInServer.onComplete {isr ->
request.send().onComplete { res ->
async.evaluate {
assert res.result().statusCode() == 200
assert res.result().body().toString() == "{\"id\":1,\"username\":\"user\",\"enabled\":true}"
}
}
}

then: "Expect the result to be completed in the specified time"
async.await(10)
}

def "put /1, with missing item, should create item"() {
when:
def result = client.newCall(new Request.Builder().url(server.url("/1")).put(
RequestBody.create(MediaType.parse("application/json"),
mapper.writeValueAsString(new User(1L, "user-replaced", true)))).build()).
execute()
def "PUT /1, with missing item, should create item"() {
given: "An HTTP request to /1"
def request = client.put(server.port, server.getHostName(), "/1")
and: "An instance of AsyncConditions"
def async = new AsyncConditions(1)


when: "The request is sent with one JSON item and completed"
request.sendJson(new User(1L, "user-replaced", true)).onComplete { res ->
async.evaluate {
assert res.result().statusCode() == 201
assert res.result().body().toString() == "{\"id\":1,\"username\":\"user-replaced\",\"enabled\":true}"
}
}

then:
assert result.code() == 201
assert result.body().string() == "{\"id\":1,\"username\":\"user-replaced\",\"enabled\":true}"
then: "Expect the result to be completed in the specified time"
async.await(10)
}

def "put /1, with existent item, should replace item"() {
given:
client.newCall(new Request.Builder().url(server.url("/")).post(
RequestBody.create(MediaType.parse("application/json"),
mapper.writeValueAsString(new User(1L, "user", true)))).build()).
execute()

when:
def result = client.newCall(new Request.Builder().url(server.url("/1")).put(
RequestBody.create(MediaType.parse("application/json"),
mapper.writeValueAsString(new User(1L, "user-replaced", true)))).build()).
execute()

then:
assert result.code() == 202
assert result.body().string() == "{\"id\":1,\"username\":\"user-replaced\",\"enabled\":true}"
def item = client.newCall(new Request.Builder().url(server.url("/1")).build()).execute()
assert item.body().string() == "{\"id\":1,\"username\":\"user-replaced\",\"enabled\":true}"
def "PUT /1, with existent item, should replace item"() {
given: "An HTTP request to /1"
def request = client.put(server.port, server.getHostName(), "/1")
and: "An instance of AsyncConditions"
def async = new AsyncConditions(1)
and: "Items in the server"
def itemsInServer = Future.all(
client.post(server.port, server.getHostName(), "/")
.sendJson(new User(1L, "user", true)),
client.post(server.port, server.getHostName(), "/")
.sendJson(new User(2L, "user-2", true))
)

when: "The request is sent with one JSON item and completed"
itemsInServer.onComplete { isr ->
request.sendJson(new User(1L, "user-replaced", true)).onComplete { res ->
async.evaluate {
assert res.result().statusCode() == 202
assert res.result().body().toString() == "{\"id\":1,\"username\":\"user-replaced\",\"enabled\":true}"
}
}
}

then: "Expect the result to be completed in the specified time"
async.await(10)
}
}
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
Expand Down
Loading