package com.flozano.metrics.client.log;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyVararg;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import java.time.Duration;
import java.time.Instant;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;

import com.flozano.metrics.Tags;
import com.flozano.metrics.client.CountValue;
import com.flozano.metrics.client.MetricsClient;

public class LogMetricsClientTest {

	@Mock
	Logger logger;

	LogMetricsClient client;

	@Before
	public void setUp() {
		MockitoAnnotations.initMocks(this);
		client = new LogMetricsClient(logger);
	}

	@Test
	public void testRightParameters() {
		client.send(new CountValue("x", 1234, Tags.empty().with("mytag", "myvalue")));
		ArgumentCaptor<Object> varargsCaptor = ArgumentCaptor.forClass(Object.class);

		verify(logger, times(1)).info(eq(LogMetricsClient.FORMAT), (Object[]) varargsCaptor.capture());
		List<Object> values = varargsCaptor.getAllValues();
		Instant now = Instant.now();
		Instant then = Instant.parse((CharSequence) values.get(0));
		assertTrue(Duration.between(then, now).toMillis() < 500);
		assertEquals("x", values.get(1));
		assertEquals("c", values.get(2));
		assertEquals(1234l, values.get(3));
		assertEquals("\tmytag:myvalue", values.get(4));
	}

	@Test
	public void testSimple() {

		client.send(new CountValue("y", 1234), new CountValue("z", 1234));

		verify(logger, times(3)).info(eq(LogMetricsClient.FORMAT), (Object[]) anyVararg());
		verifyNoMoreInteractions(logger);
	}

	@Test
	public void testBatch() {
		try (MetricsClient c = client.batch()) {
			client.send(new CountValue("x", 1234));
			client.send(new CountValue("y", 1234), new CountValue("z", 1234));
		}

		verify(logger, times(3)).info(eq(LogMetricsClient.FORMAT), (Object[]) anyVararg());
		verifyNoMoreInteractions(logger);
	}
}

@@ -1054,16 +1054,37 @@
 yvalue%22)
+.with(%22other%22, %22abc%22)
 ));%0A%09%09Ar
@@ -1318,54 +1318,36 @@
 ;%0A%09%09
-Instant now = Instant.now();%0A%09%09Instant then = 
+assertTrue(Duration.between(
 Inst
@@ -1389,49 +1389,23 @@
 (0))
-;%0A%09%09assertTrue(Duration.between(then, 
+, Instant.
 now
+()
 ).to
@@ -1419,9 +1419,9 @@
 ) %3C 
-5
+1
 00);
@@ -1562,16 +1562,27 @@
 :myvalue
+%5Ctother:abc
 %22, value
@@ -1630,16 +1630,58 @@
 mple() %7B
+%0A%09%09client.send(new CountValue(%22x%22, 1234));
 %0A%0A%09%09clie
@@ -1743,17 +1743,16 @@
 1234));%0A
-%0A
 %09%09verify

Fix log client tests