protected void parseJson(ElasticsearchClient client, Log log, Path path) { try { String json = new String(Files.readAllBytes(path)); List<Map<String, Object>> commands = new ObjectMapper().readValue( json, new TypeReference<List<Map<String, Object>>>(){}); commands.forEach(command -> { log.debug(String.format("Parsing command: %s", command)); ElasticsearchCommand esCommand = parseMapCommand(command); executeInitCommand(client, log, esCommand); }); } catch (IOException e) { throw new ElasticsearchSetupException("Cannot read the init json file", e); } }
@Override public void execute(ClusterConfiguration config) { if (StringUtils.isBlank(config.getPathInitScript())) { // nothing to do; return return; } String filePath = config.getPathInitScript(); validateFile(filePath); // we'll run all commands against the first node in the cluster ElasticsearchClient client = new ElasticsearchClient.Builder() .withInstanceConfiguration(config.getInstanceConfigurationList().get(0)) .withHostname("localhost") .build(); Path path = Paths.get(filePath); if ("json".equalsIgnoreCase(FilenameUtils.getExtension(filePath))) { parseJson(client, config.getLog(), path); } else { parseScript(client, config.getLog(), path); } }
protected void parseScript(ElasticsearchClient client, Log log, Path path) { try (Stream<String> stream = Files.lines(path)) { stream.forEach(command -> { log.debug(String.format("Parsing command: %s", command)); ElasticsearchCommand esCommand = parseStringCommand(command); if (esCommand.isSkip() == false) { executeInitCommand(client, log, esCommand); } }); } catch (IOException e) { throw new ElasticsearchSetupException("Cannot read the init script file", e); } }
@Test public void testExecuteWithoutFile() { when(config.getPathInitScript()).thenReturn(""); step.execute(config); verify(step, never()).parseJson(any(ElasticsearchClient.class), eq(log), any(Path.class)); verify(step, never()).parseScript(any(ElasticsearchClient.class), eq(log), any(Path.class)); }
doNothing().when(step).executeInitCommand(eq(client), eq(log), eq(esCommand)); .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:{}"); 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)); verify(step, times(12)).parseStringCommand(captor.capture()); verify(step).executeInitCommand(client, log, esCommand); });
doReturn(esCommand1, esCommand2).when(step).parseMapCommand(anyMap()); doNothing().when(step).executeInitCommand(eq(client), eq(log), eq(esCommand1)); doNothing().when(step).executeInitCommand(eq(client), eq(log), eq(esCommand2)); step.parseJson(client, log, Paths.get(jsonFile)); verify(step, times(2)).parseMapCommand(captor.capture()); assertEquals(0, ((Map<String, String>)command2.get("payload")).size()); verify(step).executeInitCommand(client, log, esCommand1); verify(step).executeInitCommand(client, log, esCommand2);
@Test public void testExecuteScriptFile() { String filePath = "folder/init.script"; when(config.getPathInitScript()).thenReturn(filePath); doNothing().when(step).validateFile(filePath); doNothing() .when(step) .parseScript(any(ElasticsearchClient.class), eq(log), any(Path.class)); step.execute(config); verify(step).validateFile(filePath); ArgumentCaptor<Path> pathCaptor = ArgumentCaptor.forClass(Path.class); verify(step).parseScript(any(ElasticsearchClient.class), eq(log), pathCaptor.capture()); assertEquals(filePath, pathCaptor.getValue().toString().replace('\\', '/')); }
@Test public void testExecuteJsonFile() { String filePath = "folder/init.json"; when(config.getPathInitScript()).thenReturn(filePath); doNothing().when(step).validateFile(filePath); doNothing() .when(step) .parseJson(any(ElasticsearchClient.class), eq(log), any(Path.class)); step.execute(config); verify(step).validateFile(filePath); ArgumentCaptor<Path> pathCaptor = ArgumentCaptor.forClass(Path.class); verify(step).parseJson(any(ElasticsearchClient.class), eq(log), pathCaptor.capture()); assertEquals(filePath, pathCaptor.getValue().toString().replace('\\', '/')); }
@Test(expected = ElasticsearchSetupException.class) public void parseStringCommandWith2Segments() { step.parseStringCommand("PUT:index"); }
@Test(expected = ElasticsearchSetupException.class) public void testExecuteInitCommandWithException() throws ElasticsearchClientException { ElasticsearchCommand command = mock(ElasticsearchCommand.class); when(command.getRequestMethod()).thenReturn(RequestMethod.DELETE); when(command.getRelativeUrl()).thenReturn("index/type/id"); doThrow(ElasticsearchClientException.class).when(client).delete("/index/type/id"); step.executeInitCommand(client, log, command); }
@Test(expected = IllegalArgumentException.class) public void testParseMapCommandUnknownMethodName() { Map<String, Object> command = new HashMap<>(); command.put("method", "PATCH"); command.put("path", "index_name"); command.put("payload", new HashMap<>()); step.parseMapCommand(command); }
@Test(expected = ElasticsearchSetupException.class) public void testValidateFile() { step.validateFile("not_a_file"); }
@Test(expected = ElasticsearchSetupException.class) public void testParseJsonInvalidFile() { step.parseJson(client, log, Paths.get("not_a_file")); }
public PostStartClusterSequence() { add(new WaitToStartClusterStep()); add(new BootstrapClusterStep()); add(new BlockProcessExecutionStep()); } }
@Test(expected = IllegalArgumentException.class) public void parseStringCommandInvalidRequestMethod() { step.parseStringCommand("PAT:index:{}"); }
@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); }
@Test(expected = IllegalArgumentException.class) public void testParseMapCommandDeleteWithPayload() { Map<String, Object> command = new HashMap<>(); command.put("method", "DELETE"); command.put("path", "index_name"); command.put("payload", new HashMap<>()); step.parseMapCommand(command); }
@Test(expected = ElasticsearchSetupException.class) public void parseStringCommandWith1Segment() { step.parseStringCommand("PUT"); }
@Test public void testExecuteInitCommandDelete() throws ElasticsearchClientException { ElasticsearchCommand command = mock(ElasticsearchCommand.class); when(command.getRequestMethod()).thenReturn(RequestMethod.DELETE); when(command.getRelativeUrl()).thenReturn("index/type/id"); step.executeInitCommand(client, log, command); verify(client).delete("/index/type/id"); }
@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()); }