/*
 * Copyright 2014 Red Hat, Inc.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution.
 *
 * The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * The Apache License v2.0 is available at
 * http://www.opensource.org/licenses/apache2.0.php
 *
 * You may elect to redistribute this code under either of these licenses.
 */

package io.vertx.test.ext.eventbusbridge;

import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.http.WebSocketConnectOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.sockjs.BridgeOptions;
import io.vertx.ext.sockjs.SockJSServer;
import io.vertx.ext.sockjs.SockJSServerOptions;
import io.vertx.test.core.HttpTestBase;
import io.vertx.test.core.VertxTestBase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;

/**
 * @author <a href="http://tfox.org">Tim Fox</a>
 */
public class EventBusBridgeTest extends VertxTestBase {

  private HttpServer server;

  @Before
  public void before() {
    server = vertx.createHttpServer(HttpServerOptions.options().setPort(HttpTestBase.DEFAULT_HTTP_PORT));
    SockJSServer sockJSServer = SockJSServer.sockJSServer(vertx, server);
    sockJSServer.bridge(SockJSServerOptions.options().setPrefix("/eventbus"), BridgeOptions.options().addInboundPermitted(new JsonObject()).addOutboundPermitted(new JsonObject()));
  }

  @After
  public void after() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    server.close(ar -> {
      assertTrue(ar.succeeded());
      latch.countDown();
    });
    awaitLatch(latch);
  }

  @Test
  public void testSimple() {
    HttpClient client = vertx.createHttpClient(HttpClientOptions.options());

    server.listen(ar -> {
      assertTrue(ar.succeeded());
      // We use raw websocket transport
      WebSocketConnectOptions options = WebSocketConnectOptions.options().setPort(HttpTestBase.DEFAULT_HTTP_PORT).setRequestURI("/eventbus/websocket");
      client.connectWebsocket(options, ws -> {

        // Register
        JsonObject msg = new JsonObject().putString("type", "register").putString("address", "someaddress");
        ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(msg.encode(), true));

        // Send
        msg = new JsonObject().putString("type", "send").putString("address", "someaddress").putString("body", "hello world");
        ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(msg.encode(), true));

        ws.dataHandler(buff -> {
          String str = buff.toString();
          JsonObject received = new JsonObject(str);
          assertEquals("hello world", received.getString("body"));
          testComplete();
        });
      });
    });

    await();
  }
}

@@ -1025,57 +1025,8 @@
 se;%0A
-import org.junit.After;%0Aimport org.junit.Before;%0A
 impo
@@ -1044,16 +1044,16 @@
 t.Test;%0A
+
 %0Aimport 
@@ -1241,13 +1241,15 @@
 %0A  @
-Befor
+Overrid
 e%0A  
@@ -1264,18 +1264,53 @@
 oid 
-before() %7B
+setUp() throws Exception %7B%0A    super.setUp();
 %0A   
@@ -1679,33 +1679,42 @@
 %0A  @
-After%0A  public void after
+Override%0A  protected void tearDown
 () t
@@ -1869,24 +1869,24 @@
 ();%0A    %7D);%0A
-
     awaitLat
@@ -1896,16 +1896,38 @@
 latch);%0A
+    super.tearDown();%0A
   %7D%0A%0A  @

Fix tests life cycle order