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); } }
@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 = 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 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()); }
doReturn(esCommand1, esCommand2).when(step).parseMapCommand(anyMap()); verify(step, times(2)).parseMapCommand(captor.capture());
@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()); }