use of com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand in project elasticsearch-maven-plugin by alexcojocaru.
the class BootstrapClusterStepTest method parseStringCommandWithSpaces.
@Test
public void parseStringCommandWithSpaces() {
String command = " PUT : index/type/id : { \"name\" : \"value\" } ";
ElasticsearchCommand esCommand = step.parseStringCommand(command);
assertFalse(esCommand.isSkip());
assertEquals(RequestMethod.PUT, esCommand.getRequestMethod());
assertEquals("index/type/id", esCommand.getRelativeUrl());
assertEquals("{ \"name\" : \"value\" }", esCommand.getJson());
}
use of com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand in project elasticsearch-maven-plugin by alexcojocaru.
the class BootstrapClusterStepTest method testParseMapCommand.
@Test
public void testParseMapCommand() {
Map<String, Object> command = new HashMap<>();
command.put("method", "PUT");
command.put("path", "index_name");
Map<String, String> payload = new HashMap<>();
payload.put("attribute1", "value1");
payload.put("attribute2", "value2");
command.put("payload", payload);
ElasticsearchCommand esCommand = step.parseMapCommand(command);
assertEquals(RequestMethod.PUT, esCommand.getRequestMethod());
assertEquals("index_name", esCommand.getRelativeUrl());
assertEquals("{\"attribute1\":\"value1\",\"attribute2\":\"value2\"}", esCommand.getJson());
}
use of com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand in project elasticsearch-maven-plugin by alexcojocaru.
the class BootstrapClusterStepTest method testParseScript.
@Test
public void testParseScript() {
String scriptFile = "src/test/resources/init.script";
// there are 4 requests in the json file;
// mock four ElasticsearchCommand objects to be returned for each
ElasticsearchCommand[] esCommands = IntStream.rangeClosed(1, 4).mapToObj(i -> {
ElasticsearchCommand esCommand = mock(ElasticsearchCommand.class);
// tell the step to do nothing when it's told to execute this
doNothing().when(step).executeInitCommand(eq(client), eq(log), eq(esCommand));
return esCommand;
}).toArray(ElasticsearchCommand[]::new);
// return the corresponding mock command for the given string command
doReturn(esCommands[0]).when(step).parseStringCommand("PUT:load_test_index:{ \"settings\" : { \"number_of_shards\" : 1, \"number_of_replicas\" : 0 } }");
doReturn(esCommands[1]).when(step).parseStringCommand("PUT:load_test_index/test_type/1:{ \"name\" : \"alex\" }");
doReturn(esCommands[2]).when(step).parseStringCommand("DELETE:load_test_index/test_type/2:");
doReturn(esCommands[3]).when(step).parseStringCommand("POST:load_test_index/_refresh:");
// there are another 8 comments and empty lines, mock all with the same mock skip command
ElasticsearchCommand emptyCommand = mock(ElasticsearchCommand.class);
when(emptyCommand.isSkip()).thenReturn(true);
doReturn(emptyCommand).when(step).parseStringCommand("# create the index");
doReturn(emptyCommand).when(step).parseStringCommand("# the index name is hardcoded");
doReturn(emptyCommand).when(step).parseStringCommand("# index a document");
doReturn(emptyCommand).when(step).parseStringCommand("# delete the 2nd document");
doReturn(emptyCommand).when(step).parseStringCommand("# refresh the index");
step.parseScript(client, log, Paths.get(scriptFile));
// the parse method should have been called for all string commands, in order
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(step, times(12)).parseStringCommand(captor.capture());
// the exec method should have been called with all commands, in order
Arrays.stream(esCommands).forEach(esCommand -> {
verify(step).executeInitCommand(client, log, esCommand);
});
}
use of com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand in project elasticsearch-maven-plugin by alexcojocaru.
the class BootstrapClusterStepTest method testParseMapCommandDelete.
@Test
public void testParseMapCommandDelete() {
Map<String, Object> command = new HashMap<>();
command.put("method", "DELETE");
command.put("path", "index_name");
ElasticsearchCommand esCommand = step.parseMapCommand(command);
assertEquals(RequestMethod.DELETE, esCommand.getRequestMethod());
assertEquals("index_name", esCommand.getRelativeUrl());
assertNull(esCommand.getJson());
}
use of com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand in project elasticsearch-maven-plugin by alexcojocaru.
the class BootstrapClusterStep method executeInitCommand.
private void executeInitCommand(ElasticsearchClient client, Log log, String command) {
log.debug(String.format("Parsing command: %s", command));
ElasticsearchCommand esCommand = parseStringCommand(command);
if (esCommand.isSkip()) {
return;
}
String url = "/" + esCommand.getRelativeUrl();
String content = esCommand.getJson();
try {
switch(esCommand.getRequestMethod()) {
case PUT:
client.put(url, content);
break;
case POST:
client.post(url, content, String.class);
break;
case DELETE:
client.delete(url);
break;
default:
throw new IllegalStateException(String.format("Unsupported request method: %s", esCommand.getRequestMethod()));
}
} catch (ElasticsearchClientException e) {
throw new ElasticsearchSetupException(String.format("Cannot execute command %s", command), e);
}
}
Aggregations