Search in sources :

Example 6 with ElasticsearchCommand

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());
}
Also used : ElasticsearchCommand(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 7 with ElasticsearchCommand

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());
}
Also used : ElasticsearchCommand(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand) HashMap(java.util.HashMap) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 8 with ElasticsearchCommand

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);
    });
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RunWith(org.junit.runner.RunWith) RequestMethod(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand.RequestMethod) HashMap(java.util.HashMap) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) ArrayList(java.util.ArrayList) ElasticsearchClientException(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchClientException) Mockito.doThrow(org.mockito.Mockito.doThrow) ArgumentCaptor(org.mockito.ArgumentCaptor) Spy(org.mockito.Spy) Map(java.util.Map) Path(java.nio.file.Path) Mockito.doReturn(org.mockito.Mockito.doReturn) ClusterConfiguration(com.github.alexcojocaru.mojo.elasticsearch.v2.ClusterConfiguration) ElasticsearchSetupException(com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException) Before(org.junit.Before) Assert.assertNotNull(org.junit.Assert.assertNotNull) ElasticsearchClient(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchClient) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.doNothing(org.mockito.Mockito.doNothing) Mockito.when(org.mockito.Mockito.when) InstanceConfiguration(com.github.alexcojocaru.mojo.elasticsearch.v2.InstanceConfiguration) Log(org.apache.maven.plugin.logging.Log) Mockito.verify(org.mockito.Mockito.verify) List(java.util.List) Mockito.never(org.mockito.Mockito.never) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) Assert.assertNull(org.junit.Assert.assertNull) Paths(java.nio.file.Paths) Assert.assertFalse(org.junit.Assert.assertFalse) ElasticsearchCommand(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) ElasticsearchCommand(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 9 with ElasticsearchCommand

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());
}
Also used : ElasticsearchCommand(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand) HashMap(java.util.HashMap) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 10 with ElasticsearchCommand

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);
    }
}
Also used : ElasticsearchCommand(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand) ElasticsearchClientException(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchClientException) ElasticsearchSetupException(com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException)

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