Search in sources :

Example 1 with ElasticsearchCommand

use of com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand in project elasticsearch-maven-plugin by alexcojocaru.

the class BootstrapClusterStep method parseStringCommand.

protected ElasticsearchCommand parseStringCommand(String command) {
    ElasticsearchCommand esCommand = new ElasticsearchCommand();
    String formattedCommand = command.trim();
    // skip empty lines or lines starting with '#'
    if (formattedCommand.isEmpty() || formattedCommand.charAt(0) == '#') {
        esCommand.setSkip(true);
    } else {
        int firstSeparatorIndex = formattedCommand.indexOf(':');
        int secondSeparatorIndex = formattedCommand.indexOf(':', firstSeparatorIndex + 1);
        if (firstSeparatorIndex == -1 || secondSeparatorIndex == -1) {
            throw new ElasticsearchSetupException("Command '" + command + "' in the script file is not properly formatted." + " The format is: REQUEST_METHOD:path:json_script." + " Ex: PUT:indexName/typeName/id:{\"shoe_size\":39, \"shoe_color\":\"orange\"}");
        }
        String methodName = formattedCommand.substring(0, firstSeparatorIndex).trim();
        ElasticsearchCommand.RequestMethod method = ElasticsearchCommand.RequestMethod.fromName(methodName);
        esCommand.setRequestMethod(method);
        String relativeUrl = formattedCommand.substring(firstSeparatorIndex + 1, secondSeparatorIndex).trim();
        esCommand.setRelativeUrl(relativeUrl);
        String json = formattedCommand.substring(secondSeparatorIndex + 1).trim();
        esCommand.setJson(json);
    }
    return esCommand;
}
Also used : ElasticsearchCommand(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand) ElasticsearchSetupException(com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException)

Example 2 with ElasticsearchCommand

use of com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand in project elasticsearch-maven-plugin by alexcojocaru.

the class BootstrapClusterStep method executeInitCommand.

protected void executeInitCommand(ElasticsearchClient client, Log log, ElasticsearchCommand command) {
    String url = "/" + command.getRelativeUrl();
    String content = command.getJson();
    try {
        switch(command.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", command.getRequestMethod()));
        }
    } catch (ElasticsearchClientException e) {
        throw new ElasticsearchSetupException(String.format("Cannot execute command %s", command), e);
    }
}
Also used : ElasticsearchClientException(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchClientException) ElasticsearchSetupException(com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException)

Example 3 with ElasticsearchCommand

use of com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand in project elasticsearch-maven-plugin by alexcojocaru.

the class BootstrapClusterStep method parseMapCommand.

protected ElasticsearchCommand parseMapCommand(Map<String, Object> command) {
    ElasticsearchCommand esCommand = new ElasticsearchCommand();
    String methodName = (String) command.get("method");
    esCommand.setRequestMethod(ElasticsearchCommand.RequestMethod.fromName(methodName));
    String path = (String) command.get("path");
    esCommand.setRelativeUrl(path);
    Object payload = command.get("payload");
    if (ElasticsearchCommand.RequestMethod.DELETE == esCommand.getRequestMethod()) {
        Validate.isTrue(payload == null, "For DELETE commands the payload should be undefined");
    } else {
        try {
            esCommand.setJson(new ObjectMapper().writeValueAsString(payload));
        } catch (JsonProcessingException e) {
            throw new ElasticsearchSetupException("Cannot serialize the JSON payload for command '" + command + "'", e);
        }
    }
    return esCommand;
}
Also used : ElasticsearchCommand(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand) ElasticsearchSetupException(com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with ElasticsearchCommand

use of com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand in project elasticsearch-maven-plugin by alexcojocaru.

the class BootstrapClusterStepTest method testExecuteInitCommandPost.

@Test
public void testExecuteInitCommandPost() throws ElasticsearchClientException {
    ElasticsearchCommand command = mock(ElasticsearchCommand.class);
    when(command.getRequestMethod()).thenReturn(RequestMethod.POST);
    when(command.getJson()).thenReturn("json");
    when(command.getRelativeUrl()).thenReturn("index/type/id");
    step.executeInitCommand(client, log, command);
    verify(client).post("/index/type/id", "json", String.class);
}
Also used : ElasticsearchCommand(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand) Test(org.junit.Test)

Example 5 with ElasticsearchCommand

use of com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand in project elasticsearch-maven-plugin by alexcojocaru.

the class BootstrapClusterStepTest method parseStringCommand.

@Test
public void parseStringCommand() {
    String command = "PUT:index/type/id:{}";
    ElasticsearchCommand esCommand = step.parseStringCommand(command);
    assertFalse(esCommand.isSkip());
    assertEquals(RequestMethod.PUT, esCommand.getRequestMethod());
    assertEquals("index/type/id", esCommand.getRelativeUrl());
    assertEquals("{}", esCommand.getJson());
}
Also used : ElasticsearchCommand(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

ElasticsearchCommand (com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand)17 Test (org.junit.Test)12 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)8 ElasticsearchSetupException (com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException)7 HashMap (java.util.HashMap)4 ElasticsearchClientException (com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchClientException)3 Map (java.util.Map)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 IOException (java.io.IOException)2 List (java.util.List)2 ArgumentMatchers.anyMap (org.mockito.ArgumentMatchers.anyMap)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ClusterConfiguration (com.github.alexcojocaru.mojo.elasticsearch.v2.ClusterConfiguration)1 InstanceConfiguration (com.github.alexcojocaru.mojo.elasticsearch.v2.InstanceConfiguration)1 ElasticsearchClient (com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchClient)1 RequestMethod (com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand.RequestMethod)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1