package io.vertx.example.unit.test;

import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpServer;
import io.vertx.example.unit.SomeVerticle;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/*
 * Example of an asynchronous unit test written in JUnit style using vertx-unit
 *
 * @author <a href="http://tfox.org">Tim Fox</a>
 */
@RunWith(VertxUnitRunner.class)
public class MyJUnitTest {

  Vertx vertx;
  HttpServer server;

  @Before
  public void before(TestContext context) {
    vertx = Vertx.vertx();
    Async async = context.async();
    server =
      vertx.createHttpServer().requestHandler(req -> req.response().end("foo")).listen(8080, res -> {
        if (res.succeeded()) {
          async.complete();
        } else {
          context.fail();
        }
      });
  }

  @After
  public void after(TestContext context) {
    Async async = context.async();
    vertx.close(ar -> {
      if (ar.succeeded()) {
        async.complete();
      } else if (ar.failed()) {
        context.fail();
      }
    });
  }

  @Test
  public void test1(TestContext test) {
    // Send a request and get a response
    HttpClient client = vertx.createHttpClient();
    Async async = test.async();
    client.getNow(8080, "localhost", "/", resp -> {
      resp.bodyHandler(body -> test.assertEquals("foo", body.toString()));
      client.close();
      async.complete();
    });
  }

  @Test
  public void test2(TestContext test) {
    // Deploy and undeploy a verticle
    Async async = test.async();
    vertx.deployVerticle(SomeVerticle.class.getName(), res -> {
      if (res.succeeded()) {
        String deploymentID = res.result();
        vertx.undeploy(deploymentID, res2 -> {
          if (res2.succeeded()) {
            async.complete();
          } else {
            test.fail();
          }
        });
      } else {
        test.fail();
      }
    });
  }
}

@@ -722,43 +722,8 @@
 ();%0A
-    Async async = context.async();%0A
     
@@ -815,391 +815,164 @@
 %22)).
-listen(8080, res -%3E %7B%0A        if (res.succeeded()) %7B%0A          async.complete();%0A        %7D else %7B%0A          context.fail();%0A        %7D%0A      %7D);%0A  %7D%0A%0A  @After%0A  public void after(TestContext context) %7B%0A    Async async = context.async();%0A    vertx.close(ar -%3E %7B%0A      if (ar.succeeded()) %7B%0A        async.complete();%0A      %7D else if (ar.failed()) %7B%0A        context.fail();%0A      %7D%0A    %7D
+%0A          listen(8080, context.asyncAssertSuccess());%0A  %7D%0A%0A  @After%0A  public void after(TestContext context) %7B%0A    vertx.close(context.asyncAssertSuccess()
 );%0A 
@@ -1007,35 +1007,38 @@
 st1(TestContext 
+con
 te
-s
+x
 t) %7B%0A    // Send
@@ -1127,35 +1127,38 @@
   Async async = 
+con
 te
-s
+x
 t.async();%0A    c
@@ -1235,19 +1235,22 @@
 body -%3E 
+con
 te
-s
+x
 t.assert
@@ -1381,19 +1381,22 @@
 Context 
+con
 te
-s
+x
 t) %7B%0A   
@@ -1434,40 +1434,8 @@
 cle%0A
-    Async async = test.async();%0A
     
@@ -1489,318 +1489,125 @@
 (), 
-res -%3E %7B%0A      if (res.succeeded()) %7B%0A        String deploymentID = res.result();%0A        vertx.undeploy(deploymentID, res2 -%3E %7B%0A          if (res2.succeeded()) %7B%0A            async.complete();%0A          %7D else %7B%0A            test.fail();%0A          %7D%0A        %7D
+context.asyncAssertSuccess(deploymentID -%3E %7B%0A      vertx.undeploy(deploymentID, context.asyncAssertSuccess()
 );%0A    
- 
- %7D else %7B%0A        test.fail();%0A      %7D%0A    
 %7D
+)
 );%0A 

Use new async handler assertion to simplify junit test