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); } }
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(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 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 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 testExecuteInitCommandPut() throws ElasticsearchClientException { ElasticsearchCommand command = mock(ElasticsearchCommand.class); when(command.getRequestMethod()).thenReturn(RequestMethod.PUT); when(command.getJson()).thenReturn("json"); when(command.getRelativeUrl()).thenReturn("index/type/id"); step.executeInitCommand(client, log, command); verify(client).put("/index/type/id", "json"); }
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)); assertEquals(0, ((Map<String, String>)command2.get("payload")).size()); verify(step).executeInitCommand(client, log, esCommand1); verify(step).executeInitCommand(client, log, esCommand2);
doNothing().when(step).executeInitCommand(eq(client), eq(log), eq(esCommand)); verify(step).executeInitCommand(client, log, esCommand); });