@Test public void testRun() throws Exception { Map<String, Object> configuration = new HashMap<>(); configuration.put("default", "profile-1"); Map<String, String> profile1 = new HashMap<>(); profile1.put("endpoint", "https: configuration.put("profile-1", profile1); Map<String, String> profile2 = new HashMap<>(); profile2.put("endpoint", "https: configuration.put("profile-2", profile2); InjectCommand command = new InjectCommand(new Shell(), configuration); CloseableHttpResponse response = mock(CloseableHttpResponse.class); StatusLine line = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, ""); doReturn(line).when(response).getStatusLine(); HttpEntity entity = new StringEntity("{\"id\" : \"fault-id-1\"}", ContentType.APPLICATION_JSON); doReturn(entity).when(response).getEntity(); CloseableHttpClient httpClient = mock(CloseableHttpClient.class); doAnswer(i -> { HttpPost post = i.getArgument(0); assertThat(post.getURI().toString(), is("https: assertThat(post.getEntity().getContentType().getValue(), is(ContentType.APPLICATION_JSON.toString())); assertThat(IOUtils.toString(post.getEntity().getContent(), ContentType.APPLICATION_JSON.getCharset()), is("{\"name\":\"fault-1\"}")); return response; }).when(httpClient).execute(ArgumentMatchers.any(HttpUriRequest.class)); command.httpClient(httpClient); Map<String, String> options = new HashMap<>(); List<String> args = new ArrayList<>(); args.add("fault-1"); command.run(new Line("inject", options, args)); doAnswer(i -> { HttpPost post = i.getArgument(0); assertThat(post.getURI().toString(), is("https: assertThat(post.getEntity().getContentType().getValue(), is(ContentType.APPLICATION_JSON.toString())); assertThat(IOUtils.toString(post.getEntity().getContent(), ContentType.APPLICATION_JSON.getCharset()), is( "{\"name\":\"fault-1\",\"params\":\"{\\\"key-1\\\" : \\\"value-1\\\", \\\"key-2\\\" : \\\"val ue-2\\\"\"}")); return response; }).when(httpClient).execute(ArgumentMatchers.any(HttpUriRequest.class)); options = new HashMap<>(); args = new ArrayList<>(); args.add("fault-1"); args.add("{\"key-1\" :"); args.add(" \"value-1\", "); args.add("\"key-2\" : \"val "); args.add(" ue-2\""); command.run(new Line("inject", options, args)); line = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_INTERNAL_SERVER_ERROR, ""); doReturn(line).when(response).getStatusLine(); command.run(new Line("inject", options, args)); command = new InjectCommand(new Shell(), configuration); args = new ArrayList<>(); command.run(new Line("inject", options, args)); }
@Override @SuppressWarnings("unchecked") public void run(Line line) throws Exception { if (line.args().size() > 0) { Map<String, String> input = new LinkedHashMap<>(); String name = line.args().get(0); input.put("name", name); if (line.args().size() > 1) { input.put("params", String.join("", line.args().subList(1, line.args().size()))); } ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(input); log.info("Injecting fault [" + name + "]"); log.debug(json); String profile = (String) configuration.get("default"); HttpPost post = new HttpPost( ((Map<String, Object>) configuration.get(profile)).get("endpoint") + "/inject"); post.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON)); try (CloseableHttpResponse response = httpClient.execute(post)) { StatusLine status = response.getStatusLine(); String content = EntityUtils.toString(response.getEntity(), ContentType.APPLICATION_JSON.getCharset()); log.debug(content); if (status.getStatusCode() == HttpStatus.SC_OK) { Map<String, Object> output = mapper.readValue(content, new TypeReference<Map<String, Object>>() { }); shell.console().println("Fault has been successfully injected with id [" + output.get("id") + "]"); } else { log.warn("Injecting fault failed in [" + status + "] for a reason of [" + content + "]"); } } return; } shell.console().println("Unsupported usage"); shell.console().println(usage().toString()); }
InjectCommand implements Command { @Override @SuppressWarnings("unchecked") public void run(Line line) throws Exception { if (line.args().size() > 0) { Map<String, String> input = new LinkedHashMap<>(); String name = line.args().get(0); input.put("name", name); if (line.args().size() > 1) { input.put("params", String.join("", line.args().subList(1, line.args().size()))); } ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(input); log.info("Injecting fault [" + name + "]"); log.debug(json); String profile = (String) configuration.get("default"); HttpPost post = new HttpPost( ((Map<String, Object>) configuration.get(profile)).get("endpoint") + "/inject"); post.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON)); try (CloseableHttpResponse response = httpClient.execute(post)) { StatusLine status = response.getStatusLine(); String content = EntityUtils.toString(response.getEntity(), ContentType.APPLICATION_JSON.getCharset()); log.debug(content); if (status.getStatusCode() == HttpStatus.SC_OK) { Map<String, Object> output = mapper.readValue(content, new TypeReference<Map<String, Object>>() { }); shell.console().println("Fault has been successfully injected with id [" + output.get("id") + "]"); } else { log.warn("Injecting fault failed in [" + status + "] for a reason of [" + content + "]"); } } return; } shell.console().println("Unsupported usage"); shell.console().println(usage().toString()); } }
InjectCommand implements Command { @Override @SuppressWarnings("unchecked") public void run(Line line) throws Exception { if (line.args().size() > 0) { Map<String, String> input = new LinkedHashMap<>(); String name = line.args().get(0); input.put("name", name); if (line.args().size() > 1) { input.put("params", String.join("", line.args().subList(1, line.args().size()))); } ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(input); log.info("Injecting fault [" + name + "]"); log.debug(json); String profile = (String) configuration.get("default"); HttpPost post = new HttpPost( ((Map<String, Object>) configuration.get(profile)).get("endpoint") + "/inject"); post.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON)); try (CloseableHttpResponse response = httpClient.execute(post)) { StatusLine status = response.getStatusLine(); String content = EntityUtils.toString(response.getEntity(), ContentType.APPLICATION_JSON.getCharset()); log.debug(content); if (status.getStatusCode() == HttpStatus.SC_OK) { Map<String, Object> output = mapper.readValue(content, new TypeReference<Map<String, Object>>() { }); shell.console().println("Fault has been successfully injected with id [" + output.get("id") + "]"); } else { log.warn("Injecting fault failed in [" + status + "] for a reason of [" + content + "]"); } } return; } shell.console().println("Unsupported usage"); shell.console().println(usage().toString()); } InjectCommand(Shell shell, Map<String, Object> configuration); }
InjectCommand implements Command { @Override @SuppressWarnings("unchecked") public void run(Line line) throws Exception { if (line.args().size() > 0) { Map<String, String> input = new LinkedHashMap<>(); String name = line.args().get(0); input.put("name", name); if (line.args().size() > 1) { input.put("params", String.join("", line.args().subList(1, line.args().size()))); } ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(input); log.info("Injecting fault [" + name + "]"); log.debug(json); String profile = (String) configuration.get("default"); HttpPost post = new HttpPost( ((Map<String, Object>) configuration.get(profile)).get("endpoint") + "/inject"); post.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON)); try (CloseableHttpResponse response = httpClient.execute(post)) { StatusLine status = response.getStatusLine(); String content = EntityUtils.toString(response.getEntity(), ContentType.APPLICATION_JSON.getCharset()); log.debug(content); if (status.getStatusCode() == HttpStatus.SC_OK) { Map<String, Object> output = mapper.readValue(content, new TypeReference<Map<String, Object>>() { }); shell.console().println("Fault has been successfully injected with id [" + output.get("id") + "]"); } else { log.warn("Injecting fault failed in [" + status + "] for a reason of [" + content + "]"); } } return; } shell.console().println("Unsupported usage"); shell.console().println(usage().toString()); } InjectCommand(Shell shell, Map<String, Object> configuration); @Override String name(); @Override Usage usage(); @Override @SuppressWarnings("unchecked") void run(Line line); void httpClient(CloseableHttpClient httpClient); }
InjectCommand implements Command { @Override @SuppressWarnings("unchecked") public void run(Line line) throws Exception { if (line.args().size() > 0) { Map<String, String> input = new LinkedHashMap<>(); String name = line.args().get(0); input.put("name", name); if (line.args().size() > 1) { input.put("params", String.join("", line.args().subList(1, line.args().size()))); } ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(input); log.info("Injecting fault [" + name + "]"); log.debug(json); String profile = (String) configuration.get("default"); HttpPost post = new HttpPost( ((Map<String, Object>) configuration.get(profile)).get("endpoint") + "/inject"); post.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON)); try (CloseableHttpResponse response = httpClient.execute(post)) { StatusLine status = response.getStatusLine(); String content = EntityUtils.toString(response.getEntity(), ContentType.APPLICATION_JSON.getCharset()); log.debug(content); if (status.getStatusCode() == HttpStatus.SC_OK) { Map<String, Object> output = mapper.readValue(content, new TypeReference<Map<String, Object>>() { }); shell.console().println("Fault has been successfully injected with id [" + output.get("id") + "]"); } else { log.warn("Injecting fault failed in [" + status + "] for a reason of [" + content + "]"); } } return; } shell.console().println("Unsupported usage"); shell.console().println(usage().toString()); } InjectCommand(Shell shell, Map<String, Object> configuration); @Override String name(); @Override Usage usage(); @Override @SuppressWarnings("unchecked") void run(Line line); void httpClient(CloseableHttpClient httpClient);  }