package com.googlecode.jmxtrans.model.output.elastic;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.googlecode.jmxtrans.model.Query;
import com.googlecode.jmxtrans.model.Result;
import com.googlecode.jmxtrans.model.Server;
import io.searchbox.action.Action;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.indices.IndicesExists;
import io.searchbox.indices.mapping.PutMapping;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.HashMap;
import java.util.Map;

import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ElasticWriterTests {

	@Mock
	private JestClient mockClient;
	@Mock
	private JestResult jestResultTrue;
	@Mock
	private JestResult jestResultFalse;

	@Test
	public void sendMessageToElastic() throws Exception {
		Server server = Server.builder().setHost("host").setPort("123").build();
		Query query = Query.builder().build();
		Result result = new Result(1, "attributeName", "className", "objDomain", "classNameAlias", "typeName", ImmutableMap.of("key", (Object)1));

		ImmutableList<String> typenames = ImmutableList.of();
		Map<String,Object> settings = new HashMap<String,Object>();

		String connectionUrl = "http://localhost";

		// return for call, does index exist
		when(jestResultFalse.isSucceeded()).thenReturn(Boolean.FALSE);
		when(mockClient.execute(Matchers.<IndicesExists>isA(IndicesExists.class))).thenReturn(jestResultFalse);

		// return for call, is index created
		when(jestResultTrue.isSucceeded()).thenReturn(Boolean.TRUE);
		when(mockClient.execute(Matchers.<PutMapping>isA(PutMapping.class))).thenReturn(jestResultTrue);

		ElasticWriter writer = new ElasticWriter(typenames, true, "rootPrefix", true, connectionUrl, settings);
		// client for testing
		writer.setJestClient(mockClient);

		// creates the index if needed
		writer.start();

		writer.doWrite(server, query, ImmutableList.of(result));

		Mockito.verify(mockClient, atLeast(2)).execute(Matchers.<Action<JestResult>>any());

	}

}

@@ -481,16 +481,71 @@
 apping;%0A
+import org.junit.Before;%0Aimport org.junit.BeforeClass;%0A
 import o
@@ -722,16 +722,44 @@
 unner;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -1080,16 +1080,190 @@
 False;%0A%0A
+%09@Before%0A%09public void initializeMocks() %7B%0A%09%09when(jestResultFalse.isSucceeded()).thenReturn(Boolean.FALSE);%0A%09%09when(jestResultTrue.isSucceeded()).thenReturn(Boolean.TRUE);%0A%09%7D%0A%0A
 %09@Test%0A%09
@@ -1580,274 +1580,94 @@
 %0A%0A%09%09
-ImmutableList%3CString%3E typenames = ImmutableList.of();%0A%09%09Map%3CString,Object%3E settings = new HashMap%3CString,Object%3E();%0A%0A%09%09String connectionUrl = %22http://localhost%22;%0A%0A%09%09// return for call, does index exist%0A%09%09when(jestResultFalse.isSucceeded()).thenReturn(Boolean.FALSE);
+ElasticWriter writer =  createElasticWriter();%0A%0A%09%09// return for call, does index exist
 %0A%09%09w
@@ -1702,23 +1702,8 @@
 ers.
-%3CIndicesExists%3E
 isA(
@@ -1798,71 +1798,8 @@
 ted%0A
-%09%09when(jestResultTrue.isSucceeded()).thenReturn(Boolean.TRUE);%0A
 %09%09wh
@@ -1833,20 +1833,8 @@
 ers.
-%3CPutMapping%3E
 isA(
@@ -1886,114 +1886,8 @@
 );%0A%0A
-%09%09ElasticWriter writer = new ElasticWriter(typenames, true, %22rootPrefix%22, true, connectionUrl, settings);%0A
 %09%09//
@@ -2055,16 +2055,34 @@
 ult));%0A%0A
+%09%09writer.stop();%0A%0A
 %09%09Mockit
@@ -2164,10 +2164,949 @@
 );%0A%0A%09%7D%0A%0A
+%09@Test%0A%09public void indexCreateFailure() throws Exception %7B%0A%0A%09%09ElasticWriter writer = createElasticWriter();%0A%0A%09%09// return for call, does index exist%0A%09%09when(mockClient.execute(Matchers.isA(IndicesExists.class))).thenReturn(jestResultFalse);%0A%0A%09%09// return for call, is index created: return false%0A%09%09when(jestResultFalse.getErrorMessage()).thenReturn(%22Unknown error creating index in elastic%22);%0A%0A%09%09// client for testing%0A%09%09writer.setJestClient(mockClient);%0A%0A%09%09// creates the index if needed%0A%09%09writer.start();%0A%0A%09%09writer.stop();%0A%0A%09%09Mockito.verify(mockClient, atLeast(2)).execute(Matchers.%3CAction%3CJestResult%3E%3Eany());%0A%0A%09%7D%0A%0A%09private ElasticWriter createElasticWriter() throws IOException %7B%0A%09%09ImmutableList%3CString%3E typenames = ImmutableList.of();%0A%09%09Map%3CString,Object%3E settings = new HashMap%3CString,Object%3E();%0A%0A%09%09String connectionUrl = %22http://localhost%22;%0A%0A%09%09return new ElasticWriter(typenames, true, %22rootPrefix%22, true, connectionUrl, settings);%0A%09%7D%0A%0A
 %7D%0A

increase branch code coverage