/**
 * The MIT License
 * Copyright (c) 2014 Benoit Lacelle
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package blasd.apex.core.metrics;

import org.assertj.core.api.Assertions;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

import com.google.common.collect.ImmutableMap;
import com.google.common.eventbus.EventBus;

import blasd.apex.core.metrics.EndMetricEvent;
import blasd.apex.core.metrics.StartMetricEvent;

public class TestStartMetricEvent {

	@After
	public void resetDoRememberStack() {
		StartMetricEvent.doRememberStack = false;
	}

	@Test
	public void testStackGeneration() {
		StartMetricEvent.doRememberStack = true;

		StartMetricEvent startEvent = new StartMetricEvent("detailName", "source", "names");

		Assert.assertTrue(startEvent.toString().contains("StartMetricEvent.<init>("));
		Assert.assertFalse(startEvent.toStringNoStack().contains("StartMetricEvent.<init>("));
	}

	@Test
	public void testStackNoGeneration() {
		StartMetricEvent.doRememberStack = false;

		StartMetricEvent startEvent = new StartMetricEvent("detailName", "source", "names");

		Assert.assertFalse(startEvent.toString().contains("StartMetricEvent.<init>("));
	}

	@Test
	public void testToStringWithUser() {
		StartMetricEvent.doRememberStack = false;

		StartMetricEvent startEvent = new StartMetricEvent("sourceObject",
				ImmutableMap.of(StartMetricEvent.KEY_USERNAME, "Benoit"),
				() -> -1L,
				"Test");

		Assertions.assertThat(startEvent.toString()).contains("Benoit");

		Assert.assertEquals("StartMetricEvent{names=[Test], source=sourceObject} startDetails={UserName=Benoit}",
				startEvent.toString());
	}

	@Test
	public void testToStringWithEndDetails() {
		StartMetricEvent.doRememberStack = false;

		StartMetricEvent startEvent = new StartMetricEvent("sourceObject", "Test");

		startEvent.setEndDetails(ImmutableMap.of("endKey", "endValue"));

		Assert.assertEquals("StartMetricEvent{names=[Test], source=sourceObject} endDetails={endKey=endValue}",
				startEvent.toString());
	}

	@Test
	public void testCloseSeveralTIme() {
		StartMetricEvent startEvent = new StartMetricEvent("detailName", "source", "names");

		EventBus eventBus = Mockito.spy(new EventBus());

		// Check we submit an endEvent
		EndMetricEvent end1 = EndMetricEvent.postEndEvent(eventBus::post, startEvent);
		Mockito.verify(eventBus).post(end1);

		// Check we did not submitted a second endEvent
		EndMetricEvent end2 = EndMetricEvent.postEndEvent(eventBus::post, startEvent);
		Mockito.verify(eventBus).post(end1);

		// Ensure closing several time the same event leads to a single end event
		Assert.assertSame(end1, end2);
	}
}

@@ -1590,33 +1590,36 @@
 tartMetricEvent.
-d
+setD
 oRememberStack =
@@ -1616,24 +1616,23 @@
 berStack
- = 
+(
 false
+)
 ;%0A%09%7D%0A%0A%09@
@@ -1684,33 +1684,36 @@
 tartMetricEvent.
-d
+setD
 oRememberStack =
@@ -1714,15 +1714,14 @@
 tack
- = 
+(
 true
+)
 ;%0A%0A%09
@@ -2038,33 +2038,36 @@
 tartMetricEvent.
-d
+setD
 oRememberStack =
@@ -2060,32 +2060,31 @@
 ememberStack
- = 
+(
 false
+)
 ;%0A%0A%09%09StartMe
@@ -2304,33 +2304,36 @@
 tartMetricEvent.
-d
+setD
 oRememberStack =
@@ -2326,32 +2326,31 @@
 ememberStack
- = 
+(
 false
+)
 ;%0A%0A%09%09StartMe
@@ -2778,17 +2778,20 @@
 icEvent.
-d
+setD
 oRemembe
@@ -2800,16 +2800,15 @@
 tack
- = 
+(
 false
+)
 ;%0A%0A%09

Fix compil