/*
 * #%L
 * vertx-util
 * %%
 * Copyright (C) 2015 Braintags GmbH
 * %%
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * #L%
 */
package de.braintags.io.vertx.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import de.braintags.io.vertx.BtVertxTestBase;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;

/**
 * Testing for vertx-async from https://github.com/gchauvet/vertx-async
 * 
 * @author Michael Remme
 * 
 */
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class VertxAsyncTest extends BtVertxTestBase {
  private static final io.vertx.core.logging.Logger LOGGER = io.vertx.core.logging.LoggerFactory
      .getLogger(VertxAsyncTest.class);

  private int COUNT = 50000;
  private static final List<String> toRemove = Arrays.asList("1", "2", "3");

  /**
   * Test method for {@link de.braintags.io.vertx.util.CounterObject#CounterObject(int, io.vertx.core.Handler)}.
   */
  @Test
  public void testAsyncCounter() {
    final List<String> sourceList = generateList();
    List<String> workList = new ArrayList<>(sourceList);

    LOGGER.info("list length: " + workList.size());
    Handler<AsyncResult<String>> handler = new Handler<AsyncResult<String>>() {
      @Override
      public void handle(AsyncResult<String> result) {
        int lastElementPosition = sourceList.indexOf(result.result());
        LOGGER.info("last position of element " + result.result() + " is " + lastElementPosition);
      }
    };

    CounterObject<String> co = new CounterObject<>(workList.size(), handler);
    workList.forEach(entry -> {
      entry.toUpperCase().chars().filter(s -> toRemove.contains(s)).findAny();
      if (co.reduce()) {
        co.setResult(entry);
      }
    });

  }

  private List<String> generateList() {
    List<String> sourceList = Arrays.asList("eins", "zwei", "drei", "vier", "fünf", "sechs", "sieben", "acht", "neun",
        "zehn", "elf", "zweiundzwanzig", "vierzehg", "achtzig");
    List<String> returnList = new ArrayList<>(sourceList);
    for (int i = 0; i < COUNT; i++) {
      for (String entry : sourceList) {
        returnList.add(entry + String.valueOf(i));
      }
    }
    return returnList;
  }

}

@@ -635,23 +635,160 @@
 tx.core.
-Handler
+CompositeFuture;%0Aimport io.vertx.core.Future;%0Aimport io.vertx.core.Handler;%0Aimport io.vertx.ext.unit.Async;%0Aimport io.vertx.ext.unit.TestContext
 ;%0A%0A/**%0A 
@@ -1162,16 +1162,16 @@
  50000;%0A
-
   privat
@@ -1240,16 +1240,989 @@
  %223%22);%0A%0A
+  @Test%0A  public void testComposite_WithError1(TestContext context) %7B%0A    Async async = context.async();%0A    ErrorObject err = new ErrorObject%3C%3E(null);%0A    CompositeFuture cf = CompositeFuture.all(createFutureListWithError());%0A    cf.setHandler(res -%3E %7B%0A      if (res.failed()) %7B%0A        // expected%0A        async.complete();%0A      %7D else %7B%0A        err.setThrowable(new IllegalArgumentException(%22expected eception%22));%0A        async.complete();%0A      %7D%0A    %7D);%0A%0A    async.await();%0A    if (err.isError()) %7B%0A      context.fail(err.getThrowable());%0A    %7D%0A  %7D%0A%0A  /**%0A   * Create a list of duture, where one is a failedFuture%0A   * %0A   * @return%0A   */%0A  List%3CFuture%3E createFutureListWithError() %7B%0A    List%3CFuture%3E fl = new ArrayList%3C%3E();%0A    fl.add(Future.succeededFuture());%0A    fl.add(Future.succeededFuture());%0A    fl.add(Future.succeededFuture());%0A    fl.add(Future.failedFuture(new UnsupportedOperationException()));%0A    fl.add(Future.succeededFuture());%0A    return fl;%0A  %7D%0A%0A
   /**%0A  

add composite test