|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package hello; |
| 18 | + |
| 19 | +import io.vertx.core.Vertx; |
| 20 | +import io.vertx.core.VertxOptions; |
| 21 | +import io.vertx.ext.web.client.WebClient; |
| 22 | +import io.vertx.ext.web.codec.BodyCodec; |
| 23 | +import io.vertx.junit5.Checkpoint; |
| 24 | +import io.vertx.junit5.VertxExtension; |
| 25 | +import io.vertx.junit5.VertxTestContext; |
| 26 | +import org.assertj.core.api.Assertions; |
| 27 | +import org.junit.jupiter.api.*; |
| 28 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 29 | + |
| 30 | +import java.util.concurrent.atomic.AtomicInteger; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author <a href="https://julien.ponge.org/">Julien Ponge</a> |
| 36 | + */ |
| 37 | +@DisplayName("👋 A fairly basic test example") |
| 38 | +@ExtendWith(VertxExtension.class) |
| 39 | +class SampleVerticleTest { |
| 40 | + |
| 41 | + @Test |
| 42 | + @DisplayName("⏱ Count 3 timer ticks") |
| 43 | + void countThreeTicks(Vertx vertx, VertxTestContext testContext) { |
| 44 | + AtomicInteger counter = new AtomicInteger(); |
| 45 | + vertx.setPeriodic(100, id -> { |
| 46 | + if (counter.incrementAndGet() == 3) { |
| 47 | + testContext.completeNow(); |
| 48 | + } |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + @DisplayName("⏱ Count 3 timer ticks, with a checkpoint") |
| 54 | + void countThreeTicksWithCheckpoints(Vertx vertx, VertxTestContext testContext) { |
| 55 | + Checkpoint checkpoint = testContext.checkpoint(3); |
| 56 | + vertx.setPeriodic(100, id -> checkpoint.flag()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + @DisplayName("🚀 Deploy a HTTP service verticle and make 10 requests") |
| 61 | + void useSampleVerticle(Vertx vertx, VertxTestContext testContext) { |
| 62 | + WebClient webClient = WebClient.create(vertx); |
| 63 | + Checkpoint deploymentCheckpoint = testContext.checkpoint(); |
| 64 | + Checkpoint requestCheckpoint = testContext.checkpoint(10); |
| 65 | + |
| 66 | + vertx.deployVerticle(new SampleVerticle(), testContext.succeeding(id -> { |
| 67 | + deploymentCheckpoint.flag(); |
| 68 | + |
| 69 | + for (int i = 0; i < 10; i++) { |
| 70 | + webClient.get(11981, "localhost", "/") |
| 71 | + .as(BodyCodec.string()) |
| 72 | + .send(testContext.succeeding(resp -> { |
| 73 | + testContext.verify(() -> { |
| 74 | + assertThat(resp.statusCode()).isEqualTo(200); |
| 75 | + assertThat(resp.body()).contains("Yo!"); |
| 76 | + requestCheckpoint.flag(); |
| 77 | + }); |
| 78 | + })); |
| 79 | + } |
| 80 | + })); |
| 81 | + } |
| 82 | + |
| 83 | + @DisplayName("➡️ A nested test with customized lifecycle") |
| 84 | + @Nested |
| 85 | + class CustomLifecycleTest { |
| 86 | + |
| 87 | + Vertx vertx; |
| 88 | + |
| 89 | + @BeforeEach |
| 90 | + void prepare() { |
| 91 | + vertx = Vertx.vertx(new VertxOptions() |
| 92 | + .setMaxEventLoopExecuteTime(1000) |
| 93 | + .setPreferNativeTransport(true) |
| 94 | + .setFileResolverCachingEnabled(true)); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + @DisplayName("⬆️ Deploy SampleVerticle") |
| 99 | + void deploySampleVerticle(VertxTestContext testContext) { |
| 100 | + vertx.deployVerticle(new SampleVerticle(), testContext.succeeding(id -> testContext.completeNow())); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + @DisplayName("🛂 Make a HTTP client request to SampleVerticle") |
| 105 | + void httpRequest(VertxTestContext testContext) { |
| 106 | + WebClient webClient = WebClient.create(vertx); |
| 107 | + vertx.deployVerticle(new SampleVerticle(), testContext.succeeding(id -> { |
| 108 | + webClient.get(11981, "localhost", "/yo") |
| 109 | + .as(BodyCodec.string()) |
| 110 | + .send(testContext.succeeding(resp -> { |
| 111 | + testContext.verify(() -> { |
| 112 | + assertThat(resp.statusCode()).isEqualTo(200); |
| 113 | + assertThat(resp.body()).contains("Yo!"); |
| 114 | + testContext.completeNow(); |
| 115 | + }); |
| 116 | + })); |
| 117 | + })); |
| 118 | + } |
| 119 | + |
| 120 | + @AfterEach |
| 121 | + void cleanup() { |
| 122 | + vertx.close(); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments