From b7ecb185a47096d20bf052ad33b91593d4d4db90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernhard=20Str=C3=A4hle?= Date: Tue, 27 Aug 2024 14:44:19 +0200 Subject: [PATCH] refactor(styles): apply spotless on groovy files and java files in src/it/ (6301) Add groovy formatter settings to parent pom --- Increase scope for groovy formatter and format/fix existing groovy files --- Apply spotless after rebase to format added groovy file --- .../it/src/it/cert-manager/verify.groovy | 19 +- .../io/fabric8/test/ExistingJavaTypes.java | 4 +- .../DefaultMockServerAnyMethodTest.groovy | 104 +- .../DefaultMockServerCrudTest.groovy | 313 ++--- .../DefaultMockServerHttpsTest.groovy | 32 +- .../DefaultMockServerTest.groovy | 1244 ++++++++--------- .../DefaultMockServerWebSocketTest.groovy | 355 +++-- .../mockwebserver/JsonResponseComposer.groovy | 9 +- .../MockServerExceptionTest.groovy | 78 +- .../io/fabric8/mockwebserver/User.groovy | 123 +- .../UserAttributeExtractor.groovy | 26 +- .../crud/AttributeSetTest.groovy | 302 ++-- .../mockwebserver/crud/AttributeTest.groovy | 60 +- .../crud/CrudDispatcherTest.groovy | 264 ++-- .../it/kubernetes-model-core/verify.groovy | 28 +- pom.xml | 14 + 16 files changed, 1494 insertions(+), 1481 deletions(-) diff --git a/java-generator/it/src/it/cert-manager/verify.groovy b/java-generator/it/src/it/cert-manager/verify.groovy index 7687c937f0e..9833aa40d22 100644 --- a/java-generator/it/src/it/cert-manager/verify.groovy +++ b/java-generator/it/src/it/cert-manager/verify.groovy @@ -17,13 +17,12 @@ import static org.junit.jupiter.api.Assertions.assertEquals // TODO: This should be modified to test against the current extension sources assertEquals( - new File(basedir, "/expected/Auth.expected") - .getText("UTF-8") - .replace("\r\n", "\n") - .trim(), - new File(basedir, "/target/generated-sources/java/io/cert_manager/v1/issuerspec/vault/Auth.java") - .getText("UTF-8") - .replace("\r\n", "\n") - .trim() -) - + new File(basedir, "/expected/Auth.expected") + .getText("UTF-8") + .replace("\r\n", "\n") + .trim(), + new File(basedir, "/target/generated-sources/java/io/cert_manager/v1/issuerspec/vault/Auth.java") + .getText("UTF-8") + .replace("\r\n", "\n") + .trim() + ) diff --git a/java-generator/it/src/it/plugin/gradle/existing-java-types/src/main/java/io/fabric8/test/ExistingJavaTypes.java b/java-generator/it/src/it/plugin/gradle/existing-java-types/src/main/java/io/fabric8/test/ExistingJavaTypes.java index c750956e2e8..f0d40af33ae 100644 --- a/java-generator/it/src/it/plugin/gradle/existing-java-types/src/main/java/io/fabric8/test/ExistingJavaTypes.java +++ b/java-generator/it/src/it/plugin/gradle/existing-java-types/src/main/java/io/fabric8/test/ExistingJavaTypes.java @@ -15,8 +15,8 @@ */ package io.fabric8.test; -import com.example.v1.ExistingJavaTypeSpec; // Generated -import io.fabric8.kubernetes.api.model.Affinity; // Existing +import com.example.v1.ExistingJavaTypeSpec; +import io.fabric8.kubernetes.api.model.Affinity; public class ExistingJavaTypes { public void example() { diff --git a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerAnyMethodTest.groovy b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerAnyMethodTest.groovy index 8f001224dfb..fd392c8694d 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerAnyMethodTest.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerAnyMethodTest.groovy @@ -23,67 +23,67 @@ import spock.util.concurrent.AsyncConditions class DefaultMockServerAnyMethodTest extends Specification { - @Shared - static def vertx = Vertx.vertx() + @Shared + static def vertx = Vertx.vertx() - DefaultMockServer server + DefaultMockServer server - WebClient client + WebClient client - def setup() { - server = new DefaultMockServer() - server.start() - client = WebClient.create(vertx) - } + def setup() { + server = new DefaultMockServer() + server.start() + client = WebClient.create(vertx) + } - def cleanup() { - server.shutdown() - client.close() - } + def cleanup() { + server.shutdown() + client.close() + } - def cleanupSpec() { - vertx.close() - } + def cleanupSpec() { + vertx.close() + } - def "when setting an expectation for ANY should respond to POST"() { - given: "An expectation with any" - server.expect().any().withPath("/api/v1/resource").andReturn(200, "OK").always() - and: "A POST request" - def req1 = client.post(server.port, server.getHostName(), "/api/v1/resource") - .send() - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) + def "when setting an expectation for ANY should respond to POST"() { + given: "An expectation with any" + server.expect().any().withPath("/api/v1/resource").andReturn(200, "OK").always() + and: "A POST request" + def req1 = client.post(server.port, server.getHostName(), "/api/v1/resource") + .send() + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) - when: "The request is sent and completed" - req1.onComplete { isr -> - async.evaluate { - assert req1.result().statusCode() == 200 - assert req1.result().body().toString() == "OK" - } - } + when: "The request is sent and completed" + req1.onComplete { isr -> + async.evaluate { + assert req1.result().statusCode() == 200 + assert req1.result().body().toString() == "OK" + } + } - then: "Expect the result to be completed in the specified time" - async.await(10) - } + then: "Expect the result to be completed in the specified time" + async.await(10) + } - def "when setting an expectation for ANY should respond to PUT"() { - given: "An expectation with any" - server.expect().any().withPath("/api/v1/resource").andReturn(200, "OK").always() - and: "A PUT request" - def req1 = client.put(server.port, server.getHostName(), "/api/v1/resource") - .send() - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) + def "when setting an expectation for ANY should respond to PUT"() { + given: "An expectation with any" + server.expect().any().withPath("/api/v1/resource").andReturn(200, "OK").always() + and: "A PUT request" + def req1 = client.put(server.port, server.getHostName(), "/api/v1/resource") + .send() + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) - when: "The request is sent and completed" - req1.onComplete { isr -> - async.evaluate { - assert req1.result().statusCode() == 200 - assert req1.result().body().toString() == "OK" - } - } + when: "The request is sent and completed" + req1.onComplete { isr -> + async.evaluate { + assert req1.result().statusCode() == 200 + assert req1.result().body().toString() == "OK" + } + } - then: "Expect the result to be completed in the specified time" - async.await(10) - } + then: "Expect the result to be completed in the specified time" + async.await(10) + } } diff --git a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerCrudTest.groovy b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerCrudTest.groovy index 27f631eacc5..a43d476f40f 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerCrudTest.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerCrudTest.groovy @@ -26,160 +26,161 @@ import spock.util.concurrent.AsyncConditions class DefaultMockServerCrudTest extends Specification { - @Shared - static def vertx = Vertx.vertx() - @Shared - static def client = WebClient.create(vertx) - - DefaultMockServer server - - def setup() { - server = new DefaultMockServer(new Context(), new MockWebServer(), new HashMap<>(), - new CrudDispatcher(new Context(), new UserAttributeExtractor(), new JsonResponseComposer()), false) - server.start() - } - - def cleanup() { - server.shutdown() - } - - def cleanupSpec() { - client.close() - vertx.close() - } - - 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) - } - - 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: "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 = client.post(server.port, server.getHostName(), "/") - .sendJson(new User(1L, "user", true)) - .compose { _ -> - 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: "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"() { - 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: "Expect the result to be completed in the specified time" - async.await(10) - } - - 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) - } + @Shared + static def vertx = Vertx.vertx() + @Shared + static def client = WebClient.create(vertx) + + DefaultMockServer server + + def setup() { + server = new DefaultMockServer(new Context(), new MockWebServer(), new HashMap<>(), + new CrudDispatcher(new Context(), new UserAttributeExtractor(), new JsonResponseComposer()), false) + server.start() + } + + def cleanup() { + server.shutdown() + } + + def cleanupSpec() { + client.close() + vertx.close() + } + + 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) + } + + 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: "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 = client.post(server.port, server.getHostName(), "/") + .sendJson(new User(1L, "user", true)) + .compose { _ -> + 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: "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"() { + 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: "Expect the result to be completed in the specified time" + async.await(10) + } + + 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) + } } diff --git a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerHttpsTest.groovy b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerHttpsTest.groovy index 0bb0d97b65d..e441c8a4d52 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerHttpsTest.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerHttpsTest.groovy @@ -21,25 +21,25 @@ import spock.lang.Specification class DefaultMockServerHttpsTest extends Specification { - DefaultMockServer server + DefaultMockServer server - @Shared - OkHttpClient client = new OkHttpClient() + @Shared + OkHttpClient client = new OkHttpClient() - def setup() { - server = new DefaultMockServer(true) - server.start() - } + def setup() { + server = new DefaultMockServer(true) + server.start() + } - def cleanup() { - server.shutdown() - } + def cleanup() { + server.shutdown() + } - def "url, with path, returns URL with HTTPS protocol"() { - when: - def result = server.url("/") + def "url, with path, returns URL with HTTPS protocol"() { + when: + def result = server.url("/") - then: - assert result.startsWith("https://") - } + then: + assert result.startsWith("https://") + } } diff --git a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerTest.groovy b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerTest.groovy index b56da7b74d3..3b3ec14c372 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerTest.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerTest.groovy @@ -21,634 +21,632 @@ import io.vertx.core.Future import io.vertx.core.Vertx import io.vertx.core.http.WebSocketClient import io.vertx.ext.web.client.WebClient -import okhttp3.Headers -import okhttp3.mockwebserver.RecordedRequest -import spock.lang.Specification -import spock.util.concurrent.AsyncConditions - import java.util.concurrent.ArrayBlockingQueue import java.util.concurrent.CompletableFuture import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicInteger +import okhttp3.Headers +import okhttp3.mockwebserver.RecordedRequest +import spock.lang.Specification +import spock.util.concurrent.AsyncConditions class DefaultMockServerTest extends Specification { - Vertx vertx - - DefaultMockServer server - - WebClient client - - WebSocketClient wsClient - - def setup() { - vertx = Vertx.vertx() - server = new DefaultMockServer() - server.start() - client = WebClient.create(vertx) - wsClient = vertx.createWebSocketClient() - } - - def cleanup() { - server.shutdown() - client.close() - wsClient.close() - vertx.close() - } - - def "getPort, should return a valid port"() { - when: - def result = server.getPort() - - then: - assert result > 0 - assert result <= 65535 - } - - def "getHostName, should return a valid host name"() { - when: - def result = server.getHostName() - - then: - assert !result.isBlank() - } - - def "toProxy, should return Proxy with the current HostName and Port"() { - when: - def result = server.toProxyAddress() - - then: - assert result.address() instanceof InetSocketAddress - assert ((InetSocketAddress) result.address()).getPort() == server.getPort() - assert ((InetSocketAddress) result.address()).getHostName() == server.getHostName() - } - - def "getRequestCount, with no requests, should return 0"() { - when: - def result = server.getRequestCount() - - then: - assert result == 0 - } - - def "getRequestCount, with multiple, should return valid request count"() { - given: - def all = Future.all( - client.get(server.port, server.getHostName(), "/").send(), - client.get(server.port, server.getHostName(), "/one").send(), - client.get(server.port, server.getHostName(), "/two").send() - ) - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The request is sent and completed" - all.onComplete {isr -> - async.evaluate { assert server.getRequestCount() == 3 } - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "getLastRequest, with no requests, should return null"() { - when: - def result = server.getLastRequest() - - then: - assert result == null - } - - def "getLastRequest, with one request, should return the request"() { - given: - def request = client.get(server.port, server.getHostName(), "/").send() - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The request is sent and completed" - request.onComplete { isr -> - async.evaluate { assert server.getLastRequest().getPath() == "/" } - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "getLastRequest, with one request, can be invoked multiple times"() { - given: - def request = client.get(server.port, server.getHostName(), "/").send() - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The request is sent and completed" - request.onComplete { isr -> - server.getLastRequest() - async.evaluate { assert server.getLastRequest().getPath() == "/" } - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "getLastRequest, with multiple requests, should return the latest request"() { - given: - def all = client.get(server.port, server.getHostName(), "/").send() - .compose { _ -> client.get(server.port, server.getHostName(), "/one").send() } - .compose { _ -> client.get(server.port, server.getHostName(), "/two").send() } - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - - when: "The request is sent and completed" - all.onComplete {isr -> - async.evaluate { assert server.getLastRequest().getPath() == "/two" } - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "getLastRequest, with multiple requests, can be invoked multiple times"() { - given: - def all = client.get(server.port, server.getHostName(), "/").send() - .compose { _ -> client.get(server.port, server.getHostName(), "/one").send()} - .compose { _ -> client.get(server.port, server.getHostName(), "/two").send()} - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The request is sent and completed" - all.onComplete {isr -> - server.getLastRequest() - async.evaluate { assert server.getLastRequest().getPath() == "/two" } - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "takeRequest, with timeout and no requests, should return null and don't block (after timeout)"() { - when: - def result = server.takeRequest(1, TimeUnit.MICROSECONDS) - - then: - assert result == null - } - - def "when setting an expectation with once it should be met only the first time"() { - given: "An expectation with once" - server.expect().get().withPath("/api/v1/users").andReturn(200, "admin").once() - and: "A first request" - def req1 = client.get(server.port, server.getHostName(), "/api/v1/users").send() - and: "A second request" - def req2 = req1.compose { _ -> - client.get(server.port, server.getHostName(), "/api/v1/users").send() - } - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The requests are sent and completed" - Future.all(req1, req2).onComplete {isr -> - async.evaluate { - assert req1.result().statusCode() == 200 - assert req1.result().body().toString() == "admin" - assert req2.result().statusCode() == 404 - assert req2.result().body() == null - } - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "when setting an expectation with n-th times it should be met only the for the first n-th times"() { - given: "An expectation with times (3)" - server.expect().get().withPath("/api/v1/users").andReturn(200, "admin").times(3) - and: "A first request" - def req1 = client.get(server.port, server.getHostName(), "/api/v1/users").send() - and: "A second request" - def req2 = req1.compose { _ -> - client.get(server.port, server.getHostName(), "/api/v1/users").send() - } - and: "A third request" - def req3 = req2.compose { _ -> - client.get(server.port, server.getHostName(), "/api/v1/users").send() - } - and: "A fourth request" - def req4 = req3.compose { _ -> - client.get(server.port, server.getHostName(), "/api/v1/users").send() - } - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The requests are sent and completed" - Future.all(req1, req2, req3, req4).onComplete {isr -> - async.evaluate { - assert req1.result().statusCode() == 200 - assert req1.result().body().toString() == "admin" - assert req2.result().statusCode() == 200 - assert req2.result().body().toString() == "admin" - assert req3.result().statusCode() == 200 - assert req3.result().body().toString() == "admin" - assert req4.result().statusCode() == 404 - assert req4.result().body() == null - } - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "when setting an expectation with always it should always be met"() { - given: "An expectation with always" - server.expect().get().withPath("/api/v1/users").andReturn(200, "admin").always() - and: "A first request" - def req1 = client.get(server.port, server.getHostName(), "/api/v1/users").send() - and: "A second request" - def req2 = client.get(server.port, server.getHostName(), "/api/v1/users").send() - and: "A third request" - def req3 = client.get(server.port, server.getHostName(), "/api/v1/users").send() - and: "A fourth request" - def req4 = client.get(server.port, server.getHostName(), "/api/v1/users").send() - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The requests are sent and completed" - Future.all(req1, req2, req3, req4).onComplete {isr -> - async.evaluate { - assert req1.result().statusCode() == 200 - assert req1.result().body().toString() == "admin" - assert req2.result().statusCode() == 200 - assert req2.result().body().toString() == "admin" - assert req3.result().statusCode() == 200 - assert req3.result().body().toString() == "admin" - assert req4.result().statusCode() == 200 - assert req4.result().body().toString() == "admin" - } - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "when setting an expectation as an object it should be serialized to json"() { - given: "An expectation with always" - def root = new User(0, "root", true) - server.expect().get().withPath("/api/v1/users").andReturn(200, root).always() - and: "A request" - def req1 = client.get(server.port, server.getHostName(), "/api/v1/users").send() - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The request is sent and completed" - req1.onComplete {isr -> - async.evaluate { - assert req1.result().statusCode() == 200 - assert req1.result().body().toString() == "{\"id\":0,\"username\":\"root\",\"enabled\":true}" - } - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "when setting a timed websocket String message it should be fired at the specified time"() { - given: "A WebSocket expectation" - server.expect().get().withPath("/api/v1/users/watch") - .andUpgradeToWebSocket() - .open() - .waitFor(1000).andEmit("DELETED") - .done() - .once() - and: - Queue messages = new ArrayBlockingQueue<>(1) - and: "A WebSocket request" - def wsReq =wsClient.webSocket().connect(server.port, server.getHostName(), "/api/v1/users/watch") - and: "A WebSocket listener" - wsReq.onComplete { ws -> - ws.result().textMessageHandler { text -> - messages.add(text) - } - ws.result().closeHandler { _ -> - ws.result().close() - } - } - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The request is sent and completed" - async.evaluate { - assert messages.poll(10, TimeUnit.SECONDS) == "DELETED" - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "when setting a timed websocket binary message it should be fire at the specified time"() { - given: "A WebSocket expectation" - server.expect().get().withPath("/api/v1/users/watch") - .andUpgradeToWebSocket() - .open() - .waitFor(1000).andEmit(new WebSocketMessage(new byte[]{1, 2, 3})) - .done() - .once() - and: - Queue messages = new ArrayBlockingQueue<>(1) - and: "A WebSocket request" - def wsReq = wsClient.webSocket().connect(server.port, server.getHostName(), "/api/v1/users/watch") - and: "A WebSocket listener" - wsReq.onComplete { ws -> - ws.result().binaryMessageHandler { buffer -> - messages.add(buffer.getBytes(0, buffer.length())) - } - ws.result().closeHandler { _ -> - ws.result().close() - } - } - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The request is sent and completed" - async.evaluate { - assert messages.poll(10, TimeUnit.SECONDS) == new byte[]{1, 2, 3} - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "when setting a request/response websocket message it should be fired when the event is triggered"() { - given: "A WebSocket expectation" - server.expect().get().withPath("/api/v1/users/watch") - .andUpgradeToWebSocket() - .open() - .expect("create root").andEmit("CREATED").once() - .expect("delete root").andEmit("DELETED").once() - .done() - .once() - and: - Queue messages = new ArrayBlockingQueue<>(2) - and: "A WebSocket request" - def wsReq = wsClient.webSocket().connect(server.port, server.getHostName(), "/api/v1/users/watch") - and: "A WebSocket listener" - wsReq.onComplete { ws -> - ws.result().textMessageHandler { text -> - messages.add(text) - } - ws.result().writeTextMessage("create root") - ws.result().writeTextMessage("delete root") - } - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The request is sent and completed" - async.evaluate { - assert messages.poll(10, TimeUnit.SECONDS) == "CREATED" - assert messages.poll(10, TimeUnit.SECONDS) == "DELETED" - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "when receiving an unexpected websocket message it should close the connection with status code 1002"() { - given: "A WebSocket expectation" - server.expect().get().withPath("/api/v1/users/watch") - .andUpgradeToWebSocket() - .open() - .expect("expected message").andEmit("MESSAGE OK").once() - .done() - .once() - and: - def closeCode = new CompletableFuture() - def closeReason = new CompletableFuture() - and: "A WebSocket request" - def wsReq = wsClient.webSocket().connect(server.port, server.getHostName(), "/api/v1/users/watch") - and: "A WebSocket listener" - wsReq.onComplete { ws -> - ws.result().closeHandler { v -> - closeCode.complete(ws.result().closeStatusCode()) - closeReason.complete(ws.result().closeReason()) - } - ws.result().writeTextMessage("unexpected message") - } - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The request is sent and completed" - async.evaluate { - assert closeCode.get(10, TimeUnit.SECONDS) == 1002 - assert closeReason.get(10, TimeUnit.SECONDS) == "Unexpected message:unexpected message" - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "when setting a delayed response it should be delayed for the specified duration"() { - given: "An expectation with delay" - server.expect().get().withPath("/api/v1/users") - .delay(100, TimeUnit.MILLISECONDS) - .andReturn(200, "admin") - .once() - and: "A start time" - def startTime = System.currentTimeMillis() - and: "A request" - def req1 = client.get(server.port, server.getHostName(), "/api/v1/users").send() - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The request is sent and completed" - req1.onComplete {isr -> - async.evaluate { - assert req1.result().statusCode() == 200 - assert req1.result().body().toString() == "admin" - assert System.currentTimeMillis() - startTime >= 100 - } - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "when using a body provider it should work as for static responses"() { - given: "A counter" - def counter = new AtomicInteger(0); - and: "An expectation with body provider" - server.expect().get().withPath("/api/v1/users") - .andReply(200, {req -> "admin-" + counter.getAndIncrement()}) - .always() - and: "A request" - def req1 = client.get(server.port, server.getHostName(), "/api/v1/users").send() - and: "A second request" - def req2 = req1.compose { _ -> - client.get(server.port, server.getHostName(), "/api/v1/users").send() - } - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The request is sent and completed" - Future.all(req1, req2).onComplete {isr -> - async.evaluate { - assert req1.result().statusCode() == 200 - assert req1.result().body().toString() == "admin-0" - assert req2.result().statusCode() == 200 - assert req2.result().body().toString() == "admin-1" - } - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "when using a response provider it should work as for static responses"() { - given: "An expectation with response provider" - server.expect().get().withPath("/api/v1/users") - .andReply(new ResponseProvider() { - def counter = new AtomicInteger(0); - def headers = new Headers.Builder().build() - - int getStatusCode(RecordedRequest request) { - return 200 - } - - Object getBody(RecordedRequest request) { - return "admin-" + counter.get() - } - - @Override - Headers getHeaders() { - return headers.newBuilder().add("Count", "" + counter.incrementAndGet()).build() - } - - @Override - void setHeaders(Headers headers) { - this.headers = headers - } - }) - .always() - and: "A request" - def req1 = client.get(server.port, server.getHostName(), "/api/v1/users").send() - and: "A second request" - def req2 = req1.compose { _ -> - client.get(server.port, server.getHostName(), "/api/v1/users").send() - } - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The request is sent and completed" - Future.all(req1, req2).onComplete {isr -> - async.evaluate { - assert req1.result().statusCode() == 200 - assert req1.result().body().toString() == "admin-1" - assert req1.result().headers().get("Count") == "1" - assert req2.result().statusCode() == 200 - assert req2.result().body().toString() == "admin-2" - assert req2.result().headers().get("Count") == "2" - } - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "should be able to set headers on responses"() { - given: "An expectation with header" - server.expect().get().withPath("/api/v1/users") - .andReturn(200, "admin") - .withHeader("test: header") - .withHeader("test2", "header2") - .once() - and: "A request" - def req1 = client.get(server.port, server.getHostName(), "/api/v1/users").send() - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The request is sent and completed" - req1.onComplete {isr -> - async.evaluate { - assert req1.result().statusCode() == 200 - assert req1.result().body().toString() == "admin" - assert req1.result().headers().get("test") == "header" - assert req1.result().headers().get("test2") == "header2" - } - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "when setting an httprequest/response websocket message it should be fired when the event is triggered"() { - given: "A WebSocket + HTTP expectation" - server.expect().get().withPath("/api/v1/users/watch") - .andUpgradeToWebSocket() - .open() - .expectHttpRequest("/api/v1/create").andEmit("CREATED").once() - .expectHttpRequest("/api/v1/delete").andEmit("DELETED").once() - .done() - .once() - and: - Queue messages = new ArrayBlockingQueue<>(2) - and: "A WebSocket request" - def wsReq = wsClient.webSocket().connect(server.port, server.getHostName(), "/api/v1/users/watch") - and: "A WebSocket listener" - wsReq.andThen { ws -> - ws.result().textMessageHandler { text -> - messages.add(text) - } - } - and: "HTTP requests after WS connection initiated" - wsReq.onComplete { - client.get(server.port, server.getHostName(), "/api/v1/create").send() - .compose { _ -> client.get(server.port, server.getHostName(), "/api/v1/delete").send() } - - } - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The request is sent and completed" - async.evaluate { - assert messages.poll(10, TimeUnit.SECONDS) == "CREATED" - assert messages.poll(10, TimeUnit.SECONDS) == "DELETED" - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "when setting an sentWebSocketMessage it should be fired when the server sends the expected message"() { - given: "A WebSocket + HTTP expectation" - server.expect().get().withPath("/api/v1/users/watch") - .andUpgradeToWebSocket() - .open() - .waitFor(60L).andEmit("READY") - .expectHttpRequest("/api/v1/create").andEmit("CREATED").once() - .expectSentWebSocketMessage("CREATED").andEmit("WS-CREATED").once() - .done() - .once() - and: - Queue messages = new ArrayBlockingQueue<>(2) - and: "A WebSocket request" - def wsReq = wsClient.webSocket().connect(server.port, server.getHostName(), "/api/v1/users/watch") - and: "A WebSocket listener that sends an HTTP request after WS connection initiated" - wsReq.andThen { ws -> - ws.result().textMessageHandler { text -> - if (text == "READY") { - client.get(server.port, server.getHostName(), "/api/v1/create").send() - } else { - messages.add(text) - } - } - } - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The request is sent and completed" - async.evaluate { - assert messages.poll(10, TimeUnit.SECONDS) == "CREATED" - assert messages.poll(10, TimeUnit.SECONDS) == "WS-CREATED" - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } + Vertx vertx + + DefaultMockServer server + + WebClient client + + WebSocketClient wsClient + + def setup() { + vertx = Vertx.vertx() + server = new DefaultMockServer() + server.start() + client = WebClient.create(vertx) + wsClient = vertx.createWebSocketClient() + } + + def cleanup() { + server.shutdown() + client.close() + wsClient.close() + vertx.close() + } + + def "getPort, should return a valid port"() { + when: + def result = server.getPort() + + then: + assert result > 0 + assert result <= 65535 + } + + def "getHostName, should return a valid host name"() { + when: + def result = server.getHostName() + + then: + assert !result.isBlank() + } + + def "toProxy, should return Proxy with the current HostName and Port"() { + when: + def result = server.toProxyAddress() + + then: + assert result.address() instanceof InetSocketAddress + assert ((InetSocketAddress) result.address()).getPort() == server.getPort() + assert ((InetSocketAddress) result.address()).getHostName() == server.getHostName() + } + + def "getRequestCount, with no requests, should return 0"() { + when: + def result = server.getRequestCount() + + then: + assert result == 0 + } + + def "getRequestCount, with multiple, should return valid request count"() { + given: + def all = Future.all( + client.get(server.port, server.getHostName(), "/").send(), + client.get(server.port, server.getHostName(), "/one").send(), + client.get(server.port, server.getHostName(), "/two").send() + ) + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The request is sent and completed" + all.onComplete {isr -> + async.evaluate { assert server.getRequestCount() == 3 } + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "getLastRequest, with no requests, should return null"() { + when: + def result = server.getLastRequest() + + then: + assert result == null + } + + def "getLastRequest, with one request, should return the request"() { + given: + def request = client.get(server.port, server.getHostName(), "/").send() + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The request is sent and completed" + request.onComplete { isr -> + async.evaluate { assert server.getLastRequest().getPath() == "/" } + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "getLastRequest, with one request, can be invoked multiple times"() { + given: + def request = client.get(server.port, server.getHostName(), "/").send() + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The request is sent and completed" + request.onComplete { isr -> + server.getLastRequest() + async.evaluate { assert server.getLastRequest().getPath() == "/" } + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "getLastRequest, with multiple requests, should return the latest request"() { + given: + def all = client.get(server.port, server.getHostName(), "/").send() + .compose { _ -> client.get(server.port, server.getHostName(), "/one").send() } + .compose { _ -> client.get(server.port, server.getHostName(), "/two").send() } + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + + when: "The request is sent and completed" + all.onComplete {isr -> + async.evaluate { assert server.getLastRequest().getPath() == "/two" } + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "getLastRequest, with multiple requests, can be invoked multiple times"() { + given: + def all = client.get(server.port, server.getHostName(), "/").send() + .compose { _ -> client.get(server.port, server.getHostName(), "/one").send()} + .compose { _ -> client.get(server.port, server.getHostName(), "/two").send()} + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The request is sent and completed" + all.onComplete {isr -> + server.getLastRequest() + async.evaluate { assert server.getLastRequest().getPath() == "/two" } + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "takeRequest, with timeout and no requests, should return null and don't block (after timeout)"() { + when: + def result = server.takeRequest(1, TimeUnit.MICROSECONDS) + + then: + assert result == null + } + + def "when setting an expectation with once it should be met only the first time"() { + given: "An expectation with once" + server.expect().get().withPath("/api/v1/users").andReturn(200, "admin").once() + and: "A first request" + def req1 = client.get(server.port, server.getHostName(), "/api/v1/users").send() + and: "A second request" + def req2 = req1.compose { _ -> + client.get(server.port, server.getHostName(), "/api/v1/users").send() + } + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The requests are sent and completed" + Future.all(req1, req2).onComplete {isr -> + async.evaluate { + assert req1.result().statusCode() == 200 + assert req1.result().body().toString() == "admin" + assert req2.result().statusCode() == 404 + assert req2.result().body() == null + } + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "when setting an expectation with n-th times it should be met only the for the first n-th times"() { + given: "An expectation with times (3)" + server.expect().get().withPath("/api/v1/users").andReturn(200, "admin").times(3) + and: "A first request" + def req1 = client.get(server.port, server.getHostName(), "/api/v1/users").send() + and: "A second request" + def req2 = req1.compose { _ -> + client.get(server.port, server.getHostName(), "/api/v1/users").send() + } + and: "A third request" + def req3 = req2.compose { _ -> + client.get(server.port, server.getHostName(), "/api/v1/users").send() + } + and: "A fourth request" + def req4 = req3.compose { _ -> + client.get(server.port, server.getHostName(), "/api/v1/users").send() + } + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The requests are sent and completed" + Future.all(req1, req2, req3, req4).onComplete {isr -> + async.evaluate { + assert req1.result().statusCode() == 200 + assert req1.result().body().toString() == "admin" + assert req2.result().statusCode() == 200 + assert req2.result().body().toString() == "admin" + assert req3.result().statusCode() == 200 + assert req3.result().body().toString() == "admin" + assert req4.result().statusCode() == 404 + assert req4.result().body() == null + } + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "when setting an expectation with always it should always be met"() { + given: "An expectation with always" + server.expect().get().withPath("/api/v1/users").andReturn(200, "admin").always() + and: "A first request" + def req1 = client.get(server.port, server.getHostName(), "/api/v1/users").send() + and: "A second request" + def req2 = client.get(server.port, server.getHostName(), "/api/v1/users").send() + and: "A third request" + def req3 = client.get(server.port, server.getHostName(), "/api/v1/users").send() + and: "A fourth request" + def req4 = client.get(server.port, server.getHostName(), "/api/v1/users").send() + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The requests are sent and completed" + Future.all(req1, req2, req3, req4).onComplete {isr -> + async.evaluate { + assert req1.result().statusCode() == 200 + assert req1.result().body().toString() == "admin" + assert req2.result().statusCode() == 200 + assert req2.result().body().toString() == "admin" + assert req3.result().statusCode() == 200 + assert req3.result().body().toString() == "admin" + assert req4.result().statusCode() == 200 + assert req4.result().body().toString() == "admin" + } + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "when setting an expectation as an object it should be serialized to json"() { + given: "An expectation with always" + def root = new User(0, "root", true) + server.expect().get().withPath("/api/v1/users").andReturn(200, root).always() + and: "A request" + def req1 = client.get(server.port, server.getHostName(), "/api/v1/users").send() + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The request is sent and completed" + req1.onComplete {isr -> + async.evaluate { + assert req1.result().statusCode() == 200 + assert req1.result().body().toString() == "{\"id\":0,\"username\":\"root\",\"enabled\":true}" + } + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "when setting a timed websocket String message it should be fired at the specified time"() { + given: "A WebSocket expectation" + server.expect().get().withPath("/api/v1/users/watch") + .andUpgradeToWebSocket() + .open() + .waitFor(1000).andEmit("DELETED") + .done() + .once() + and: + Queue messages = new ArrayBlockingQueue<>(1) + and: "A WebSocket request" + def wsReq =wsClient.webSocket().connect(server.port, server.getHostName(), "/api/v1/users/watch") + and: "A WebSocket listener" + wsReq.onComplete { ws -> + ws.result().textMessageHandler { text -> + messages.add(text) + } + ws.result().closeHandler { _ -> + ws.result().close() + } + } + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The request is sent and completed" + async.evaluate { + assert messages.poll(10, TimeUnit.SECONDS) == "DELETED" + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "when setting a timed websocket binary message it should be fire at the specified time"() { + given: "A WebSocket expectation" + server.expect().get().withPath("/api/v1/users/watch") + .andUpgradeToWebSocket() + .open() + .waitFor(1000).andEmit(new WebSocketMessage([1, 2, 3] as byte[])) + .done() + .once() + and: + Queue messages = new ArrayBlockingQueue<>(1) + and: "A WebSocket request" + def wsReq = wsClient.webSocket().connect(server.port, server.getHostName(), "/api/v1/users/watch") + and: "A WebSocket listener" + wsReq.onComplete { ws -> + ws.result().binaryMessageHandler { buffer -> + messages.add(buffer.getBytes(0, buffer.length())) + } + ws.result().closeHandler { _ -> + ws.result().close() + } + } + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The request is sent and completed" + async.evaluate { + assert messages.poll(10, TimeUnit.SECONDS) == [1, 2, 3] as byte[] + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "when setting a request/response websocket message it should be fired when the event is triggered"() { + given: "A WebSocket expectation" + server.expect().get().withPath("/api/v1/users/watch") + .andUpgradeToWebSocket() + .open() + .expect("create root").andEmit("CREATED").once() + .expect("delete root").andEmit("DELETED").once() + .done() + .once() + and: + Queue messages = new ArrayBlockingQueue<>(2) + and: "A WebSocket request" + def wsReq = wsClient.webSocket().connect(server.port, server.getHostName(), "/api/v1/users/watch") + and: "A WebSocket listener" + wsReq.onComplete { ws -> + ws.result().textMessageHandler { text -> + messages.add(text) + } + ws.result().writeTextMessage("create root") + ws.result().writeTextMessage("delete root") + } + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The request is sent and completed" + async.evaluate { + assert messages.poll(10, TimeUnit.SECONDS) == "CREATED" + assert messages.poll(10, TimeUnit.SECONDS) == "DELETED" + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "when receiving an unexpected websocket message it should close the connection with status code 1002"() { + given: "A WebSocket expectation" + server.expect().get().withPath("/api/v1/users/watch") + .andUpgradeToWebSocket() + .open() + .expect("expected message").andEmit("MESSAGE OK").once() + .done() + .once() + and: + def closeCode = new CompletableFuture() + def closeReason = new CompletableFuture() + and: "A WebSocket request" + def wsReq = wsClient.webSocket().connect(server.port, server.getHostName(), "/api/v1/users/watch") + and: "A WebSocket listener" + wsReq.onComplete { ws -> + ws.result().closeHandler { v -> + closeCode.complete(ws.result().closeStatusCode()) + closeReason.complete(ws.result().closeReason()) + } + ws.result().writeTextMessage("unexpected message") + } + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The request is sent and completed" + async.evaluate { + assert closeCode.get(10, TimeUnit.SECONDS) == 1002 + assert closeReason.get(10, TimeUnit.SECONDS) == "Unexpected message:unexpected message" + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "when setting a delayed response it should be delayed for the specified duration"() { + given: "An expectation with delay" + server.expect().get().withPath("/api/v1/users") + .delay(100, TimeUnit.MILLISECONDS) + .andReturn(200, "admin") + .once() + and: "A start time" + def startTime = System.currentTimeMillis() + and: "A request" + def req1 = client.get(server.port, server.getHostName(), "/api/v1/users").send() + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The request is sent and completed" + req1.onComplete {isr -> + async.evaluate { + assert req1.result().statusCode() == 200 + assert req1.result().body().toString() == "admin" + assert System.currentTimeMillis() - startTime >= 100 + } + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "when using a body provider it should work as for static responses"() { + given: "A counter" + def counter = new AtomicInteger(0); + and: "An expectation with body provider" + server.expect().get().withPath("/api/v1/users") + .andReply(200, {req -> "admin-" + counter.getAndIncrement()}) + .always() + and: "A request" + def req1 = client.get(server.port, server.getHostName(), "/api/v1/users").send() + and: "A second request" + def req2 = req1.compose { _ -> + client.get(server.port, server.getHostName(), "/api/v1/users").send() + } + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The request is sent and completed" + Future.all(req1, req2).onComplete {isr -> + async.evaluate { + assert req1.result().statusCode() == 200 + assert req1.result().body().toString() == "admin-0" + assert req2.result().statusCode() == 200 + assert req2.result().body().toString() == "admin-1" + } + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "when using a response provider it should work as for static responses"() { + given: "An expectation with response provider" + server.expect().get().withPath("/api/v1/users") + .andReply(new ResponseProvider() { + def counter = new AtomicInteger(0); + def headers = new Headers.Builder().build() + + int getStatusCode(RecordedRequest request) { + return 200 + } + + Object getBody(RecordedRequest request) { + return "admin-" + counter.get() + } + + @Override + Headers getHeaders() { + return headers.newBuilder().add("Count", "" + counter.incrementAndGet()).build() + } + + @Override + void setHeaders(Headers headers) { + this.headers = headers + } + }) + .always() + and: "A request" + def req1 = client.get(server.port, server.getHostName(), "/api/v1/users").send() + and: "A second request" + def req2 = req1.compose { _ -> + client.get(server.port, server.getHostName(), "/api/v1/users").send() + } + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The request is sent and completed" + Future.all(req1, req2).onComplete {isr -> + async.evaluate { + assert req1.result().statusCode() == 200 + assert req1.result().body().toString() == "admin-1" + assert req1.result().headers().get("Count") == "1" + assert req2.result().statusCode() == 200 + assert req2.result().body().toString() == "admin-2" + assert req2.result().headers().get("Count") == "2" + } + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "should be able to set headers on responses"() { + given: "An expectation with header" + server.expect().get().withPath("/api/v1/users") + .andReturn(200, "admin") + .withHeader("test: header") + .withHeader("test2", "header2") + .once() + and: "A request" + def req1 = client.get(server.port, server.getHostName(), "/api/v1/users").send() + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The request is sent and completed" + req1.onComplete {isr -> + async.evaluate { + assert req1.result().statusCode() == 200 + assert req1.result().body().toString() == "admin" + assert req1.result().headers().get("test") == "header" + assert req1.result().headers().get("test2") == "header2" + } + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "when setting an httprequest/response websocket message it should be fired when the event is triggered"() { + given: "A WebSocket + HTTP expectation" + server.expect().get().withPath("/api/v1/users/watch") + .andUpgradeToWebSocket() + .open() + .expectHttpRequest("/api/v1/create").andEmit("CREATED").once() + .expectHttpRequest("/api/v1/delete").andEmit("DELETED").once() + .done() + .once() + and: + Queue messages = new ArrayBlockingQueue<>(2) + and: "A WebSocket request" + def wsReq = wsClient.webSocket().connect(server.port, server.getHostName(), "/api/v1/users/watch") + and: "A WebSocket listener" + wsReq.andThen { ws -> + ws.result().textMessageHandler { text -> + messages.add(text) + } + } + and: "HTTP requests after WS connection initiated" + wsReq.onComplete { + client.get(server.port, server.getHostName(), "/api/v1/create").send() + .compose { _ -> client.get(server.port, server.getHostName(), "/api/v1/delete").send() } + } + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The request is sent and completed" + async.evaluate { + assert messages.poll(10, TimeUnit.SECONDS) == "CREATED" + assert messages.poll(10, TimeUnit.SECONDS) == "DELETED" + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "when setting an sentWebSocketMessage it should be fired when the server sends the expected message"() { + given: "A WebSocket + HTTP expectation" + server.expect().get().withPath("/api/v1/users/watch") + .andUpgradeToWebSocket() + .open() + .waitFor(60L).andEmit("READY") + .expectHttpRequest("/api/v1/create").andEmit("CREATED").once() + .expectSentWebSocketMessage("CREATED").andEmit("WS-CREATED").once() + .done() + .once() + and: + Queue messages = new ArrayBlockingQueue<>(2) + and: "A WebSocket request" + def wsReq = wsClient.webSocket().connect(server.port, server.getHostName(), "/api/v1/users/watch") + and: "A WebSocket listener that sends an HTTP request after WS connection initiated" + wsReq.andThen { ws -> + ws.result().textMessageHandler { text -> + if (text == "READY") { + client.get(server.port, server.getHostName(), "/api/v1/create").send() + } else { + messages.add(text) + } + } + } + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The request is sent and completed" + async.evaluate { + assert messages.poll(10, TimeUnit.SECONDS) == "CREATED" + assert messages.poll(10, TimeUnit.SECONDS) == "WS-CREATED" + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } } diff --git a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerWebSocketTest.groovy b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerWebSocketTest.groovy index 8b7a7d5f17f..107eba47ab6 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerWebSocketTest.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerWebSocketTest.groovy @@ -21,190 +21,189 @@ import io.vertx.core.Vertx import io.vertx.core.http.WebSocket import io.vertx.core.http.WebSocketClient import io.vertx.core.http.WebSocketConnectOptions -import spock.lang.Shared -import spock.lang.Specification -import spock.util.concurrent.AsyncConditions - import java.util.concurrent.ArrayBlockingQueue import java.util.concurrent.CompletableFuture import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit import java.util.stream.IntStream +import spock.lang.Shared +import spock.lang.Specification +import spock.util.concurrent.AsyncConditions class DefaultMockServerWebSocketTest extends Specification { - @Shared - static def vertx = Vertx.vertx() - - DefaultMockServer server - - WebSocketClient wsClient - - def setup() { - server = new DefaultMockServer() - server.start() - wsClient = vertx.createWebSocketClient() - } - - def cleanup() { - server.shutdown() - wsClient.close() - } - - def "andUpgradeToWebSocket, with configured events, should emit events"() { - given: "A WebSocket expectation" - server.expect().withPath("/websocket") - .andUpgradeToWebSocket() - .open() - .waitFor(10L).andEmit("A text message") - .done() - .always() - and: - Queue messages = new ArrayBlockingQueue<>(1) - and: "A WebSocket request" - def wsReq = wsClient.webSocket().connect(server.port, server.getHostName(), "/websocket") - and: "A WebSocket listener" - wsReq.onComplete { ws -> - ws.result().textMessageHandler { text -> - messages.add(text) - } - ws.result().closeHandler { _ -> - ws.result().close() - } - } - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The request is sent and completed" - async.evaluate { - assert messages.poll(10, TimeUnit.SECONDS) == "A text message" - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "andUpgradeToWebSocket, with configured events, should emit onClose when done"() { - given: "A WebSocket expectation" - server.expect() - .withPath("/websocket") - .andUpgradeToWebSocket().open().immediately().andEmit("event").done().always() - and: - def future = new CompletableFuture() - and: "A WebSocket request" - def wsReq = wsClient.webSocket().connect(server.port, server.getHostName(), "/websocket") - and: "A WebSocket listener" - wsReq.onComplete { ws -> - ws.result().closeHandler { _ -> - ws.result().close() - future.complete(ws.result().closeReason()) - } - } - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The request is sent and completed" - async.evaluate { - assert future.get(10, TimeUnit.SECONDS) == "Closing..." - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "andUpgradeToWebSocket, with no events, should emit onClose"() { - given: "A WebSocket expectation" - server.expect() - .withPath("/websocket") - .andUpgradeToWebSocket().open().done().always() - and: - def future = new CompletableFuture() - and: "A WebSocket request" - def wsReq = wsClient.webSocket().connect(server.port, server.getHostName(), "/websocket") - and: "A WebSocket listener" - wsReq.onComplete { ws -> - if (ws.result().isClosed()) { - future.complete(ws.result().closeReason()) - } else { - ws.result().closeHandler { _ -> - ws.result().close() - future.complete(ws.result().closeReason()) - } - } - } - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The request is sent and completed" - async.evaluate { - assert future.get(10, TimeUnit.SECONDS) == "Closing..." - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - // https://github.com/fabric8io/mockwebserver/pull/66#issuecomment-944289335 - def "andUpgradeToWebSocket, with multiple upgrades, should emit events for all websocket listeners"() { - given: "A WebSocket expectation" - server.expect() - .withPath("/websocket") - .andUpgradeToWebSocket().open().waitFor(10L).andEmit("A text message").done().always() - and: "A CountDown latch to verify the event count" - def latch = new CountDownLatch(15) - and: "A Vert.x WebSocket completion handler" - Handler> completionHandler = { ws -> - ws.result().textMessageHandler { text -> - latch.countDown() - } - ws.result().closeHandler { _ -> - ws.result().close() - } - } - and: "WebSocket requests" - IntStream.range(0, 15).forEach {i -> - def wsReq = wsClient.webSocket().connect(server.port, server.getHostName(), "/websocket") - wsReq.onComplete(completionHandler) - } - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The requests are sent and completed" - async.evaluate { - assert latch.await(10, TimeUnit.SECONDS) - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - // https://github.com/fabric8io/mockwebserver/issues/77 - def "andUpgradeToWebSocket, with request header 'sec-websocket-protocol', should create response with matching header"() { - given: "A WebSocket expectation" - server.expect() - .withPath("/websocket") - .andUpgradeToWebSocket().open().done().always() - and: - def future = new CompletableFuture() - and: "A WebSocket request" - def wsReq = wsClient.webSocket().connect(new WebSocketConnectOptions() - .setPort(server.port) - .setHost(server.getHostName()) - .setURI("/websocket") - .addSubProtocol("v4.channel.k8s.io")) - and: "A WebSocket listener" - wsReq.onComplete { ws -> - future.complete(ws.result().headers().get("sec-websocket-protocol")) - } - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The request is sent and completed" - async.evaluate { - assert future.get(10, TimeUnit.SECONDS) == "v4.channel.k8s.io" - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } + @Shared + static def vertx = Vertx.vertx() + + DefaultMockServer server + + WebSocketClient wsClient + + def setup() { + server = new DefaultMockServer() + server.start() + wsClient = vertx.createWebSocketClient() + } + + def cleanup() { + server.shutdown() + wsClient.close() + } + + def "andUpgradeToWebSocket, with configured events, should emit events"() { + given: "A WebSocket expectation" + server.expect().withPath("/websocket") + .andUpgradeToWebSocket() + .open() + .waitFor(10L).andEmit("A text message") + .done() + .always() + and: + Queue messages = new ArrayBlockingQueue<>(1) + and: "A WebSocket request" + def wsReq = wsClient.webSocket().connect(server.port, server.getHostName(), "/websocket") + and: "A WebSocket listener" + wsReq.onComplete { ws -> + ws.result().textMessageHandler { text -> + messages.add(text) + } + ws.result().closeHandler { _ -> + ws.result().close() + } + } + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The request is sent and completed" + async.evaluate { + assert messages.poll(10, TimeUnit.SECONDS) == "A text message" + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "andUpgradeToWebSocket, with configured events, should emit onClose when done"() { + given: "A WebSocket expectation" + server.expect() + .withPath("/websocket") + .andUpgradeToWebSocket().open().immediately().andEmit("event").done().always() + and: + def future = new CompletableFuture() + and: "A WebSocket request" + def wsReq = wsClient.webSocket().connect(server.port, server.getHostName(), "/websocket") + and: "A WebSocket listener" + wsReq.onComplete { ws -> + ws.result().closeHandler { _ -> + ws.result().close() + future.complete(ws.result().closeReason()) + } + } + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The request is sent and completed" + async.evaluate { + assert future.get(10, TimeUnit.SECONDS) == "Closing..." + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "andUpgradeToWebSocket, with no events, should emit onClose"() { + given: "A WebSocket expectation" + server.expect() + .withPath("/websocket") + .andUpgradeToWebSocket().open().done().always() + and: + def future = new CompletableFuture() + and: "A WebSocket request" + def wsReq = wsClient.webSocket().connect(server.port, server.getHostName(), "/websocket") + and: "A WebSocket listener" + wsReq.onComplete { ws -> + if (ws.result().isClosed()) { + future.complete(ws.result().closeReason()) + } else { + ws.result().closeHandler { _ -> + ws.result().close() + future.complete(ws.result().closeReason()) + } + } + } + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The request is sent and completed" + async.evaluate { + assert future.get(10, TimeUnit.SECONDS) == "Closing..." + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + // https://github.com/fabric8io/mockwebserver/pull/66#issuecomment-944289335 + def "andUpgradeToWebSocket, with multiple upgrades, should emit events for all websocket listeners"() { + given: "A WebSocket expectation" + server.expect() + .withPath("/websocket") + .andUpgradeToWebSocket().open().waitFor(10L).andEmit("A text message").done().always() + and: "A CountDown latch to verify the event count" + def latch = new CountDownLatch(15) + and: "A Vert.x WebSocket completion handler" + Handler> completionHandler = { ws -> + ws.result().textMessageHandler { text -> + latch.countDown() + } + ws.result().closeHandler { _ -> + ws.result().close() + } + } + and: "WebSocket requests" + IntStream.range(0, 15).forEach {i -> + def wsReq = wsClient.webSocket().connect(server.port, server.getHostName(), "/websocket") + wsReq.onComplete(completionHandler) + } + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The requests are sent and completed" + async.evaluate { + assert latch.await(10, TimeUnit.SECONDS) + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + // https://github.com/fabric8io/mockwebserver/issues/77 + def "andUpgradeToWebSocket, with request header 'sec-websocket-protocol', should create response with matching header"() { + given: "A WebSocket expectation" + server.expect() + .withPath("/websocket") + .andUpgradeToWebSocket().open().done().always() + and: + def future = new CompletableFuture() + and: "A WebSocket request" + def wsReq = wsClient.webSocket().connect(new WebSocketConnectOptions() + .setPort(server.port) + .setHost(server.getHostName()) + .setURI("/websocket") + .addSubProtocol("v4.channel.k8s.io")) + and: "A WebSocket listener" + wsReq.onComplete { ws -> + future.complete(ws.result().headers().get("sec-websocket-protocol")) + } + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The request is sent and completed" + async.evaluate { + assert future.get(10, TimeUnit.SECONDS) == "v4.channel.k8s.io" + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } } diff --git a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/JsonResponseComposer.groovy b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/JsonResponseComposer.groovy index 6dbf197f649..1f74491a874 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/JsonResponseComposer.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/JsonResponseComposer.groovy @@ -16,12 +16,11 @@ package io.fabric8.mockwebserver import io.fabric8.mockwebserver.crud.ResponseComposer - import java.util.stream.Collectors class JsonResponseComposer implements ResponseComposer { - @Override - String compose(Collection items) { - return "[" + items.stream().collect(Collectors.joining(",")) + "]" - } + @Override + String compose(Collection items) { + return "[" + items.stream().collect(Collectors.joining(",")) + "]" + } } diff --git a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/MockServerExceptionTest.groovy b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/MockServerExceptionTest.groovy index d25a3c75c76..4f553a46501 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/MockServerExceptionTest.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/MockServerExceptionTest.groovy @@ -19,47 +19,47 @@ import spock.lang.Specification class MockServerExceptionTest extends Specification { - def "launderThrowable, with null, should throw MockServerException"() { - when: - MockServerException.launderThrowable(null) - then: - def result = thrown(MockServerException) - assert result.getMessage() == "An error has occurred." - assert result.getCause() == null - } + def "launderThrowable, with null, should throw MockServerException"() { + when: + MockServerException.launderThrowable(null) + then: + def result = thrown(MockServerException) + assert result.getMessage() == "An error has occurred." + assert result.getCause() == null + } - def "launderThrowable, with checked exception, should throw MockServerException"() { - when: - MockServerException.launderThrowable(new Exception("I'm checked")) - then: - def result = thrown(MockServerException) - assert result.getMessage() == "An error has occurred." - assert result.getCause().getClass() == Exception - assert result.getCause().getMessage() == "I'm checked" - } + def "launderThrowable, with checked exception, should throw MockServerException"() { + when: + MockServerException.launderThrowable(new Exception("I'm checked")) + then: + def result = thrown(MockServerException) + assert result.getMessage() == "An error has occurred." + assert result.getCause().getClass() == Exception + assert result.getCause().getMessage() == "I'm checked" + } - def "launderThrowable, with unchecked exception, should throw MockServerException"() { - when: - def result = MockServerException.launderThrowable(new RuntimeException("I'm unchecked")) - then: - assert result.getMessage() == "I'm unchecked" - assert result.getClass() != MockServerException - } + def "launderThrowable, with unchecked exception, should throw MockServerException"() { + when: + def result = MockServerException.launderThrowable(new RuntimeException("I'm unchecked")) + then: + assert result.getMessage() == "I'm unchecked" + assert result.getClass() != MockServerException + } - def "launderThrowable, with Error, should not be handled"() { - when: - MockServerException.launderThrowable(new Error("I'm an Error")) - then: - def result = thrown(Error) - assert result.getMessage() == "I'm an Error" - } + def "launderThrowable, with Error, should not be handled"() { + when: + MockServerException.launderThrowable(new Error("I'm an Error")) + then: + def result = thrown(Error) + assert result.getMessage() == "I'm an Error" + } - def "launderThrowable, with Interrupted Exception, should re-interrupt"() { - when: - MockServerException.launderThrowable(new InterruptedException("I'm interrupted")) - then: - assert Thread.currentThread().isInterrupted() - def result = thrown(MockServerException) - assert result.getMessage() == "An error has occurred." - } + def "launderThrowable, with Interrupted Exception, should re-interrupt"() { + when: + MockServerException.launderThrowable(new InterruptedException("I'm interrupted")) + then: + assert Thread.currentThread().isInterrupted() + def result = thrown(MockServerException) + assert result.getMessage() == "An error has occurred." + } } diff --git a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/User.groovy b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/User.groovy index 8bc50954869..c360cecd387 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/User.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/User.groovy @@ -21,66 +21,65 @@ import com.fasterxml.jackson.annotation.JsonProperty @JsonInclude(JsonInclude.Include.NON_NULL) class User { - @JsonProperty("id") - private Long id - - @JsonProperty("username") - private String username - - @JsonProperty("enabled") - private Boolean enabled - - private User() { - } - - public User(Long id, String username, Boolean enabled) { - this.id = id - this.username = username - this.enabled = enabled - } - - public Long getId() { - return id - } - - public void setId(Long id) { - this.id = id - } - - public String getUsername() { - return username - } - - public void setUsername(String username) { - this.username = username - } - - public Boolean getEnabled() { - return enabled - } - - public void setEnabled(Boolean enabled) { - this.enabled = enabled - } - - @Override - public boolean equals(Object o) { - if (this == o) return true - if (o == null || getClass() != o.getClass()) return false - - User user = (User) o - - if (id != null ? !id.equals(user.id) : user.id != null) return false - if (username != null ? !username.equals(user.username) : user.username != null) return false - return enabled != null ? enabled.equals(user.enabled) : user.enabled == null - - } - - @Override - public int hashCode() { - int result = id != null ? id.hashCode() : 0 - result = 31 * result + (username != null ? username.hashCode() : 0) - result = 31 * result + (enabled != null ? enabled.hashCode() : 0) - return result - } + @JsonProperty("id") + private Long id + + @JsonProperty("username") + private String username + + @JsonProperty("enabled") + private Boolean enabled + + private User() { + } + + public User(Long id, String username, Boolean enabled) { + this.id = id + this.username = username + this.enabled = enabled + } + + public Long getId() { + return id + } + + public void setId(Long id) { + this.id = id + } + + public String getUsername() { + return username + } + + public void setUsername(String username) { + this.username = username + } + + public Boolean getEnabled() { + return enabled + } + + public void setEnabled(Boolean enabled) { + this.enabled = enabled + } + + @Override + public boolean equals(Object o) { + if (this == o) return true + if (o == null || getClass() != o.getClass()) return false + + User user = (User) o + + if (id != null ? !id.equals(user.id) : user.id != null) return false + if (username != null ? !username.equals(user.username) : user.username != null) return false + return enabled != null ? enabled.equals(user.enabled) : user.enabled == null + } + + @Override + public int hashCode() { + int result = id != null ? id.hashCode() : 0 + result = 31 * result + (username != null ? username.hashCode() : 0) + result = 31 * result + (enabled != null ? enabled.hashCode() : 0) + return result + } } diff --git a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/UserAttributeExtractor.groovy b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/UserAttributeExtractor.groovy index 2529daafabc..8765cde4480 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/UserAttributeExtractor.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/UserAttributeExtractor.groovy @@ -22,19 +22,19 @@ import io.fabric8.mockwebserver.crud.AttributeSet class UserAttributeExtractor implements AttributeExtractor { - static def mapper = new ObjectMapper() + static def mapper = new ObjectMapper() - @Override - AttributeSet fromPath(String path) { - if (path.trim().isBlank() || path.trim() == "/") { - return new AttributeSet(); - } - return new AttributeSet(new Attribute("id", path.substring(1))) - } + @Override + AttributeSet fromPath(String path) { + if (path.trim().isBlank() || path.trim() == "/") { + return new AttributeSet(); + } + return new AttributeSet(new Attribute("id", path.substring(1))) + } - @Override - AttributeSet fromResource(String resource) { - def user = mapper.readValue(resource, User) - return new AttributeSet(new Attribute("id", user.getId().toString())) - } + @Override + AttributeSet fromResource(String resource) { + def user = mapper.readValue(resource, User) + return new AttributeSet(new Attribute("id", user.getId().toString())) + } } diff --git a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/AttributeSetTest.groovy b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/AttributeSetTest.groovy index b63f6c3ad5b..b2a7cd39416 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/AttributeSetTest.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/AttributeSetTest.groovy @@ -15,160 +15,158 @@ */ package io.fabric8.mockwebserver.crud -import spock.lang.Specification - import java.util.stream.Collectors +import spock.lang.Specification class AttributeSetTest extends Specification { - def "when two feature set are empty the should be equals"() { - given: - when: - AttributeSet f1 = new AttributeSet() - AttributeSet f2 = new AttributeSet() - then: - assert f1.equals(f2) - } - - def "when two feature sets contain the same feature"() { - given: - Attribute f = new Attribute("key1", "value1") - when: - AttributeSet f1 = new AttributeSet(f) - AttributeSet f2 = new AttributeSet(f) - then: - assert f1.equals(f2) - } - - def "when two feature sets contain the same features order should not matter"() { - given: - Attribute f1 = new Attribute("key1", "value1") - Attribute f2 = new Attribute("key2", "value2") - Attribute f3 = new Attribute("key3", "value3") - when: - AttributeSet fs12 = new AttributeSet(f1, f2) - AttributeSet fs21 = new AttributeSet(f2, f1) - AttributeSet fs23 = new AttributeSet(f2, f3) - - then: - assert fs12.equals(fs21) - assert !fs12.equals(fs23) - assert !fs21.equals(fs23) - } - - def "when an EXISTS attribute exists in both sets they should match"() { - given: - Attribute a1 = new Attribute("key1", "", AttributeType.EXISTS) - Attribute a2 = new Attribute("key2", "value2") - when: - AttributeSet selector = new AttributeSet(a1, a2) - AttributeSet attributeSet = new AttributeSet(a1, a2) - then: - assert attributeSet.matches(selector) - } - - def "when an EXISTS attribute exists in one set but not the other sets they should not match"() { - given: - Attribute a1 = new Attribute("key1", "", AttributeType.EXISTS) - Attribute a2 = new Attribute("key2", "value2") - when: - AttributeSet selector = new AttributeSet(a1, a2) - AttributeSet attributeSet = new AttributeSet(a2) - then: - assert !attributeSet.matches(selector) - } - - def "when a NOT_EXISTS attribute exists in both sets they should not match"() { - given: - Attribute a1 = new Attribute("key1", "", AttributeType.NOT_EXISTS) - Attribute a2 = new Attribute("key2", "value2") - when: - AttributeSet selector = new AttributeSet(a1, a2) - AttributeSet attributeSet = new AttributeSet(a1, a2) - then: - assert !attributeSet.matches(selector) - } - - def "when a NOT_EXISTS attribute exists in one set but not the other sets they should match"() { - given: - Attribute a1 = new Attribute("key1", "", AttributeType.NOT_EXISTS) - Attribute a2 = new Attribute("key2", "value2") - when: - AttributeSet selector = new AttributeSet(a1, a2) - AttributeSet attributeSet = new AttributeSet(a2) - then: - assert attributeSet.matches(selector) - } - - def "when multiple attributes are specified it should not match an attribute set with a single attribute"() { - given: "multiple attributes" - def a2 = new Attribute("key2", "value2") - def a3 = new Attribute("key3", "", AttributeType.EXISTS) - and: "an AttributeSet with only one attribute" - def attributeSet = new AttributeSet(a2) - and: "an AttributeSet with two attributes as selector" - def selectorWithTwo = new AttributeSet(a2, a3) - when: "matching" - def matches = attributeSet.matches(selectorWithTwo) - then: "it should not match" - assert !matches - } - - def "when multiple attributes are specified it should examine all"() { - given: "multiple attributes" - def a2 = new Attribute("key2", "value2") - def a3 = new Attribute("key3", "", AttributeType.EXISTS) - and: "an AttributeSet with all attributes" - def attributeSet = new AttributeSet(a2, a3) - when: "listing its values" - def attributes = attributeSet.attributes.values().stream().sorted().collect(Collectors.toList()) - then: "it should contain all attributes" - assert attributes.indexOf(a3) == 1 - assert attributes.size() == 2 - } - - def "when IN attribute in selector"() { - given: - Attribute a1 = new Attribute("key1", "value") - Attribute a2 = new Attribute("key2", "value1") - Attribute a3 = new Attribute("key2", "value2") - Attribute a4 = new Attribute("key2", "value3") - Attribute a5 = new Attribute("key2", Arrays.asList("value1", "value2"), AttributeType.IN) - - when: - AttributeSet attributeSetWithoutAttr = new AttributeSet(a1) - AttributeSet attributeSetWithVal1 = new AttributeSet(a2) - AttributeSet attributeSetWithVal2 = new AttributeSet(a3) - AttributeSet attributeSetWithWrongVal = new AttributeSet(a4) - AttributeSet selector = new AttributeSet(a5); - - then: - assert !attributeSetWithoutAttr.matches(selector) - assert attributeSetWithVal1.matches(selector) - assert attributeSetWithVal2.matches(selector) - assert !attributeSetWithWrongVal.matches(selector) - } - - def "when NOT_IN attribute in selector"() { - given: - Attribute a1 = new Attribute("key1", "value") - Attribute a2 = new Attribute("key2", "value1") - Attribute a3 = new Attribute("key2", "value2") - Attribute a4 = new Attribute("key2", "value3") - Attribute a5 = new Attribute("key2", Arrays.asList("value1", "value2"), AttributeType.NOT_IN) - - when: - AttributeSet attributeSetWithoutAttr = new AttributeSet(a1) - AttributeSet attributeSetWithVal1 = new AttributeSet(a2) - AttributeSet attributeSetWithVal2 = new AttributeSet(a3) - AttributeSet attributeSetWithWrongVal = new AttributeSet(a4) - AttributeSet selector = new AttributeSet(a5); - - then: - assert attributeSetWithoutAttr.matches(selector) - assert !attributeSetWithVal1.matches(selector) - assert !attributeSetWithVal2.matches(selector) - assert attributeSetWithWrongVal.matches(selector) - } - + def "when two feature set are empty the should be equals"() { + given: + when: + AttributeSet f1 = new AttributeSet() + AttributeSet f2 = new AttributeSet() + then: + assert f1.equals(f2) + } + + def "when two feature sets contain the same feature"() { + given: + Attribute f = new Attribute("key1", "value1") + when: + AttributeSet f1 = new AttributeSet(f) + AttributeSet f2 = new AttributeSet(f) + then: + assert f1.equals(f2) + } + + def "when two feature sets contain the same features order should not matter"() { + given: + Attribute f1 = new Attribute("key1", "value1") + Attribute f2 = new Attribute("key2", "value2") + Attribute f3 = new Attribute("key3", "value3") + when: + AttributeSet fs12 = new AttributeSet(f1, f2) + AttributeSet fs21 = new AttributeSet(f2, f1) + AttributeSet fs23 = new AttributeSet(f2, f3) + + then: + assert fs12.equals(fs21) + assert !fs12.equals(fs23) + assert !fs21.equals(fs23) + } + + def "when an EXISTS attribute exists in both sets they should match"() { + given: + Attribute a1 = new Attribute("key1", "", AttributeType.EXISTS) + Attribute a2 = new Attribute("key2", "value2") + when: + AttributeSet selector = new AttributeSet(a1, a2) + AttributeSet attributeSet = new AttributeSet(a1, a2) + then: + assert attributeSet.matches(selector) + } + + def "when an EXISTS attribute exists in one set but not the other sets they should not match"() { + given: + Attribute a1 = new Attribute("key1", "", AttributeType.EXISTS) + Attribute a2 = new Attribute("key2", "value2") + when: + AttributeSet selector = new AttributeSet(a1, a2) + AttributeSet attributeSet = new AttributeSet(a2) + then: + assert !attributeSet.matches(selector) + } + + def "when a NOT_EXISTS attribute exists in both sets they should not match"() { + given: + Attribute a1 = new Attribute("key1", "", AttributeType.NOT_EXISTS) + Attribute a2 = new Attribute("key2", "value2") + when: + AttributeSet selector = new AttributeSet(a1, a2) + AttributeSet attributeSet = new AttributeSet(a1, a2) + then: + assert !attributeSet.matches(selector) + } + + def "when a NOT_EXISTS attribute exists in one set but not the other sets they should match"() { + given: + Attribute a1 = new Attribute("key1", "", AttributeType.NOT_EXISTS) + Attribute a2 = new Attribute("key2", "value2") + when: + AttributeSet selector = new AttributeSet(a1, a2) + AttributeSet attributeSet = new AttributeSet(a2) + then: + assert attributeSet.matches(selector) + } + + def "when multiple attributes are specified it should not match an attribute set with a single attribute"() { + given: "multiple attributes" + def a2 = new Attribute("key2", "value2") + def a3 = new Attribute("key3", "", AttributeType.EXISTS) + and: "an AttributeSet with only one attribute" + def attributeSet = new AttributeSet(a2) + and: "an AttributeSet with two attributes as selector" + def selectorWithTwo = new AttributeSet(a2, a3) + when: "matching" + def matches = attributeSet.matches(selectorWithTwo) + then: "it should not match" + assert !matches + } + + def "when multiple attributes are specified it should examine all"() { + given: "multiple attributes" + def a2 = new Attribute("key2", "value2") + def a3 = new Attribute("key3", "", AttributeType.EXISTS) + and: "an AttributeSet with all attributes" + def attributeSet = new AttributeSet(a2, a3) + when: "listing its values" + def attributes = attributeSet.attributes.values().stream().sorted().collect(Collectors.toList()) + then: "it should contain all attributes" + assert attributes.indexOf(a3) == 1 + assert attributes.size() == 2 + } + + def "when IN attribute in selector"() { + given: + Attribute a1 = new Attribute("key1", "value") + Attribute a2 = new Attribute("key2", "value1") + Attribute a3 = new Attribute("key2", "value2") + Attribute a4 = new Attribute("key2", "value3") + Attribute a5 = new Attribute("key2", Arrays.asList("value1", "value2"), AttributeType.IN) + + when: + AttributeSet attributeSetWithoutAttr = new AttributeSet(a1) + AttributeSet attributeSetWithVal1 = new AttributeSet(a2) + AttributeSet attributeSetWithVal2 = new AttributeSet(a3) + AttributeSet attributeSetWithWrongVal = new AttributeSet(a4) + AttributeSet selector = new AttributeSet(a5); + + then: + assert !attributeSetWithoutAttr.matches(selector) + assert attributeSetWithVal1.matches(selector) + assert attributeSetWithVal2.matches(selector) + assert !attributeSetWithWrongVal.matches(selector) + } + + def "when NOT_IN attribute in selector"() { + given: + Attribute a1 = new Attribute("key1", "value") + Attribute a2 = new Attribute("key2", "value1") + Attribute a3 = new Attribute("key2", "value2") + Attribute a4 = new Attribute("key2", "value3") + Attribute a5 = new Attribute("key2", Arrays.asList("value1", "value2"), AttributeType.NOT_IN) + + when: + AttributeSet attributeSetWithoutAttr = new AttributeSet(a1) + AttributeSet attributeSetWithVal1 = new AttributeSet(a2) + AttributeSet attributeSetWithVal2 = new AttributeSet(a3) + AttributeSet attributeSetWithWrongVal = new AttributeSet(a4) + AttributeSet selector = new AttributeSet(a5); + + then: + assert attributeSetWithoutAttr.matches(selector) + assert !attributeSetWithVal1.matches(selector) + assert !attributeSetWithVal2.matches(selector) + assert attributeSetWithWrongVal.matches(selector) + } } diff --git a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/AttributeTest.groovy b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/AttributeTest.groovy index 8dc28673c24..867980acb36 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/AttributeTest.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/AttributeTest.groovy @@ -19,37 +19,37 @@ import spock.lang.Specification class AttributeTest extends Specification { - def "when key and value equals features should equal"() { - given: - when: - Attribute f11 = new Attribute("key1", "value1") - Attribute f11a = new Attribute("key1", "value1") - Attribute f12 = new Attribute("key1", "value2") - Attribute f22 = new Attribute("key2", "value2") + def "when key and value equals features should equal"() { + given: + when: + Attribute f11 = new Attribute("key1", "value1") + Attribute f11a = new Attribute("key1", "value1") + Attribute f12 = new Attribute("key1", "value2") + Attribute f22 = new Attribute("key2", "value2") - then: - assert !f11.equals(f22) - assert !f11.equals(f12) - assert f11.equals(f11a) - } + then: + assert !f11.equals(f22) + assert !f11.equals(f12) + assert f11.equals(f11a) + } - def "equals, with same attributes but different type, should be true"() { - given: - Attribute a1 = new Attribute("key1", "value1", AttributeType.EXISTS) - Attribute a2 = new Attribute("key1", "value1", AttributeType.NOT_EXISTS) - when: - var result = a1 == a2 - then: - assert result - } + def "equals, with same attributes but different type, should be true"() { + given: + Attribute a1 = new Attribute("key1", "value1", AttributeType.EXISTS) + Attribute a2 = new Attribute("key1", "value1", AttributeType.NOT_EXISTS) + when: + var result = a1 == a2 + then: + assert result + } - def "hashCode, with same attributes but different type, should be equal"() { - given: - Attribute a1 = new Attribute("key1", "value1", AttributeType.EXISTS) - Attribute a2 = new Attribute("key1", "value1", AttributeType.NOT_EXISTS) - when: - var result = a1.hashCode() == a2.hashCode() - then: - assert result - } + def "hashCode, with same attributes but different type, should be equal"() { + given: + Attribute a1 = new Attribute("key1", "value1", AttributeType.EXISTS) + Attribute a2 = new Attribute("key1", "value1", AttributeType.NOT_EXISTS) + when: + var result = a1.hashCode() == a2.hashCode() + then: + assert result + } } diff --git a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/CrudDispatcherTest.groovy b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/CrudDispatcherTest.groovy index 83bcd259ea5..4e15b93042c 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/CrudDispatcherTest.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/CrudDispatcherTest.groovy @@ -29,136 +29,136 @@ import spock.util.concurrent.AsyncConditions class CrudDispatcherTest extends Specification { - @Shared - static def vertx = Vertx.vertx() - - DefaultMockServer server - - WebClient client - - def setup() { - final var context = new Context(); - server = new DefaultMockServer(context, new MockWebServer(), new HashMap>(), - new CrudDispatcher(context, new CrudDispatcherTestAttributeExtractor(), new CrudDispatcherTestComposer()), false) - server.start() - client = WebClient.create(vertx) - } - - def cleanup() { - server.shutdown() - client.close() - } - - def "should be able to get after a patch"() { - given: "A POST request with a starting object" - final var postNew = client.post(server.port, server.hostName, "/namespace/test/name/one") - .putHeader("Content-Type", "application/json") - .sendBuffer(Buffer.buffer("""{"foo":{"bar":"startingValue","baz":"keepThis"} }""")) - and: "A PATCH request to update the object" - final var patch = postNew.compose { _ -> - client - .patch(server.port, server.hostName, "/namespace/test/name/one") - .putHeader("Content-Type", "application/strategic-merge-patch+json") - .sendBuffer(Buffer.buffer("""[{"op":"replace","path":"/foo/bar","value":"canary"}]""")) - } - and: "A GET request to retrieve and verify the object" - final var get = patch.compose { _ -> - client.get(server.port, server.hostName, "/namespace/test/name/one").send() - } - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The requests are sent and completed" - get.onComplete { res -> - async.evaluate { - assert res.result().statusCode() == 200 - assert res.result().body().toString() == """{"foo":{"bar":"canary","baz":"keepThis"}}""" - } - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "should be able to get after a post"() { - given: "A POST request with a starting object" - final var postNew = client.post(server.port, server.hostName, "/namespace/test/name/one") - .putHeader("Content-Type", "text/html") - .sendBuffer(Buffer.buffer("one")) - and: "A GET request to retrieve and verify the object" - final var get = postNew.compose { _ -> - client.get(server.port, server.hostName, "/namespace/test/name/one").send() - } - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The requests are sent and completed" - get.onComplete { res -> - async.evaluate { - assert res.result().statusCode() == 200 - assert res.result().body().toString() == "one" - } - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - def "should be able to delete after a post"() { - given: "A POST request with a starting object" - final var postNew = client.post(server.port, server.hostName, "/namespace/test/name/one") - .putHeader("Content-Type", "text/html") - .sendBuffer(Buffer.buffer("one")) - and: "A DELETE request to delete the object" - final var delete = postNew.compose { _ -> - client.delete(server.port, server.hostName, "/namespace/test/name/one").send() - } - and: "A GET request to retrieve and verify the object" - final var get = delete.compose { _ -> - client.get(server.port, server.hostName, "/namespace/test/name/one").send() - } - and: "An instance of AsyncConditions" - def async = new AsyncConditions(1) - - when: "The requests are sent and completed" - get.onComplete { res -> - async.evaluate { - assert res.result().statusCode() == 404 - } - } - - then: "Expect the result to be completed in the specified time" - async.await(10) - } - - private static final class CrudDispatcherTestAttributeExtractor implements AttributeExtractor { - - @Override - AttributeSet fromPath(String path) { - final var set = new AttributeSet() - final var parts = path.split("/") - if (parts.length > 2) { - set.add(new Attribute("namespace", parts[2])) - } else if (parts.length > 4) { - set.add(new Attribute("name", parts[4])) - } - return set - } - - @Override - AttributeSet fromResource(String resource) { - return null - } - } - - private static final class CrudDispatcherTestComposer implements ResponseComposer { - @Override - String compose(Collection items) { - StringBuilder sb = new StringBuilder(); - for (String item : items) { - sb.append(item).append(" ") - } - return sb.toString().trim() - } - } + @Shared + static def vertx = Vertx.vertx() + + DefaultMockServer server + + WebClient client + + def setup() { + var context = new Context(); + server = new DefaultMockServer(context, new MockWebServer(), new HashMap>(), + new CrudDispatcher(context, new CrudDispatcherTestAttributeExtractor(), new CrudDispatcherTestComposer()), false) + server.start() + client = WebClient.create(vertx) + } + + def cleanup() { + server.shutdown() + client.close() + } + + def "should be able to get after a patch"() { + given: "A POST request with a starting object" + var postNew = client.post(server.port, server.hostName, "/namespace/test/name/one") + .putHeader("Content-Type", "application/json") + .sendBuffer(Buffer.buffer("""{"foo":{"bar":"startingValue","baz":"keepThis"} }""")) + and: "A PATCH request to update the object" + var patch = postNew.compose { _ -> + client + .patch(server.port, server.hostName, "/namespace/test/name/one") + .putHeader("Content-Type", "application/strategic-merge-patch+json") + .sendBuffer(Buffer.buffer("""[{"op":"replace","path":"/foo/bar","value":"canary"}]""")) + } + and: "A GET request to retrieve and verify the object" + var get = patch.compose { _ -> + client.get(server.port, server.hostName, "/namespace/test/name/one").send() + } + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The requests are sent and completed" + get.onComplete { res -> + async.evaluate { + assert res.result().statusCode() == 200 + assert res.result().body().toString() == """{"foo":{"bar":"canary","baz":"keepThis"}}""" + } + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "should be able to get after a post"() { + given: "A POST request with a starting object" + var postNew = client.post(server.port, server.hostName, "/namespace/test/name/one") + .putHeader("Content-Type", "text/html") + .sendBuffer(Buffer.buffer("one")) + and: "A GET request to retrieve and verify the object" + var get = postNew.compose { _ -> + client.get(server.port, server.hostName, "/namespace/test/name/one").send() + } + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The requests are sent and completed" + get.onComplete { res -> + async.evaluate { + assert res.result().statusCode() == 200 + assert res.result().body().toString() == "one" + } + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + def "should be able to delete after a post"() { + given: "A POST request with a starting object" + var postNew = client.post(server.port, server.hostName, "/namespace/test/name/one") + .putHeader("Content-Type", "text/html") + .sendBuffer(Buffer.buffer("one")) + and: "A DELETE request to delete the object" + var delete = postNew.compose { _ -> + client.delete(server.port, server.hostName, "/namespace/test/name/one").send() + } + and: "A GET request to retrieve and verify the object" + var get = delete.compose { _ -> + client.get(server.port, server.hostName, "/namespace/test/name/one").send() + } + and: "An instance of AsyncConditions" + def async = new AsyncConditions(1) + + when: "The requests are sent and completed" + get.onComplete { res -> + async.evaluate { + assert res.result().statusCode() == 404 + } + } + + then: "Expect the result to be completed in the specified time" + async.await(10) + } + + private static final class CrudDispatcherTestAttributeExtractor implements AttributeExtractor { + + @Override + AttributeSet fromPath(String path) { + var set = new AttributeSet() + var parts = path.split("/") + if (parts.length > 2) { + set.add(new Attribute("namespace", parts[2])) + } else if (parts.length > 4) { + set.add(new Attribute("name", parts[4])) + } + return set + } + + @Override + AttributeSet fromResource(String resource) { + return null + } + } + + private static final class CrudDispatcherTestComposer implements ResponseComposer { + @Override + String compose(Collection items) { + StringBuilder sb = new StringBuilder(); + for (String item : items) { + sb.append(item).append(" ") + } + return sb.toString().trim() + } + } } diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/it/kubernetes-model-core/verify.groovy b/kubernetes-model-generator/openapi/maven-plugin/src/it/kubernetes-model-core/verify.groovy index a0098940cfa..ce1f8283971 100644 --- a/kubernetes-model-generator/openapi/maven-plugin/src/it/kubernetes-model-core/verify.groovy +++ b/kubernetes-model-generator/openapi/maven-plugin/src/it/kubernetes-model-core/verify.groovy @@ -16,17 +16,23 @@ import static org.junit.jupiter.api.Assertions.assertEquals import static org.junit.jupiter.api.Assertions.assertTrue -["PodList", "Pod", "PodSpec", "PodStatus", "PodTemplate"].each { - var file = new File(basedir, sprintf("/src/generated/java/io/fabric8/kubernetes/api/model/%s.java", it)) - assertTrue(file.exists()) - assertEquals( - new File(basedir, sprintf("/expected/%s.expected", it)) - .getText("UTF-8") - .replace("\r\n", "\n") - .replaceAll(" +\n", "\n") - .trim(), - file.getText("UTF-8").replace("\r\n", "\n").replaceAll(" +\n", "\n").trim() - ) +[ + "PodList", + "Pod", + "PodSpec", + "PodStatus", + "PodTemplate" +].each { + var file = new File(basedir, sprintf("/src/generated/java/io/fabric8/kubernetes/api/model/%s.java", it)) + assertTrue(file.exists()) + assertEquals( + new File(basedir, sprintf("/expected/%s.expected", it)) + .getText("UTF-8") + .replace("\r\n", "\n") + .replaceAll(" +\n", "\n") + .trim(), + file.getText("UTF-8").replace("\r\n", "\n").replaceAll(" +\n", "\n").trim() + ) } true diff --git a/pom.xml b/pom.xml index c726f7aaf46..3148748ec53 100644 --- a/pom.xml +++ b/pom.xml @@ -1200,6 +1200,11 @@ ${spotless-maven-plugin.version} + + src/main/java/**/*.java + src/test/java/**/*.java + src/it/**/src/main/java/**/*.java + ide-config/eclipse-format.xml @@ -1208,6 +1213,15 @@ + + + src/main/groovy/**/*.groovy + src/test/groovy/**/*.groovy + src/it/**/*.groovy + + + +