package org.nudge.elasticstack;

import org.joda.time.LocalDate;
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * Test class for {@link Utils}.
 */
public class UtilsTest {

	// inspired from
	// https://stackoverflow.com/questions/4021151/java-dateformat-is-not-threadsafe-what-does-this-leads-to
	private List<Future<?>> computeTask(Callable<?> task) throws Exception {
		//pool with 5 threads
		ExecutorService exec = Executors.newFixedThreadPool(5);
		List<Future<?>> results = new ArrayList<>();

		//perform 10 date conversions
		for(int i = 0 ; i < 10 ; i++){
			results.add(exec.submit(task));
		}
		exec.shutdown();

		return results;
	}

	/**
	 * Multi-threading test, except no exception and always the same result
	 */
	@Test
	public void formatTimeToString() throws Exception {
		final long ts = 1498147939000L;
		Callable<String> task = new Callable<String>(){
			@Override
			public String call() throws Exception {
				return Utils.formatTimeToString(ts);
			}
		};

		for (Future<?> future : computeTask(task)) {
			Assert.assertEquals("2017-06-22T18:12:19.000+0200", future.get());
		}
	}

	@Test
	public void getIndexSuffix() throws Exception {
		LocalDate localDate = new LocalDate();
		Assert.assertEquals(localDate.toString(), Utils.getIndexSuffix());
	}

}
@@ -123,45 +123,128 @@
 ava.
-util.ArrayList;%0Aimport java.util.List
+text.SimpleDateFormat;%0Aimport java.util.ArrayList;%0Aimport java.util.Date;%0Aimport java.util.List;%0Aimport java.util.Locale
 ;%0Aim
@@ -1084,16 +1084,27 @@
 ption %7B%0A
+%09%09// given%0A
 %09%09final 
@@ -1129,16 +1129,144 @@
 39000L;%0A
+%09%09String pattern = %22yyyy-MM-dd'T'HH:mm:ss.SSSZ%22;%0A%09%09Locale currentLocale = Locale.getDefault();%0A%09%09final Date dt = new Date(ts);%0A%0A
 %09%09Callab
@@ -1363,16 +1363,28 @@
 ption %7B%0A
+%09%09%09%09// when%0A
 %09%09%09%09retu
@@ -1481,58 +1481,125 @@
 %0A%09%09%09
-Assert.assertEquals(%222017-06-22T18:12:19.000+0200%22
+SimpleDateFormat sdf = new SimpleDateFormat(pattern, currentLocale);%0A%09%09%09// then%0A%09%09%09Assert.assertEquals(sdf.format(dt)
 , fu

fix test