@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); FaultsCommand command = new FaultsCommand(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("{}", ContentType.APPLICATION_JSON); doReturn(entity).when(response).getEntity(); CloseableHttpClient httpClient = mock(CloseableHttpClient.class); doAnswer((i) -> { HttpGet get = i.getArgument(0); assertThat(get.getURI().toString(), is("https: return response; }).when(httpClient).execute(ArgumentMatchers.any(HttpUriRequest.class)); command.httpClient(httpClient); Map<String, String> options = new HashMap<>(); List<String> args = new ArrayList<>(); command.run(new Line("faults", options, args)); entity = new StringEntity("{\"fault-2\" : \"io.eiichiro.prodigy.Fault2\", \"fault-1\" : \"io.eiichiro.prodigy.Fault1\"}", ContentType.APPLICATION_JSON); doReturn(entity).when(response).getEntity(); command.run(new Line("faults", options, args)); line = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_INTERNAL_SERVER_ERROR, ""); doReturn(line).when(response).getStatusLine(); command.run(new Line("faults", options, args)); }
@Override @SuppressWarnings("unchecked") public void run(Line line) throws Exception { String profile = (String) configuration.get("default"); URIBuilder builder = new URIBuilder( ((Map<String, Object>) configuration.get(profile)).get("endpoint") + "/faults"); HttpGet get = new HttpGet(builder.build()); try (CloseableHttpResponse response = httpClient.execute(get)) { StatusLine status = response.getStatusLine(); String content = EntityUtils.toString(response.getEntity(), ContentType.APPLICATION_JSON.getCharset()); log.debug(content); if (status.getStatusCode() == HttpStatus.SC_OK) { ObjectMapper mapper = new ObjectMapper(); Map<String, String> output = mapper.readValue(content, new TypeReference<Map<String, String>>() { }); if (output.isEmpty()) { shell.console().println("No fault classes listed in repository"); } else { MutableInt name = new MutableInt(); MutableInt clazz = new MutableInt(); Map<String, String> o = new TreeMap<>((e1, e2) -> { name.setValue(Math.max(e1.length(), e2.length())); clazz.setValue(Math.max(output.get(e1).length(), output.get(e2).length())); return e1.compareTo(e2); }); o.putAll(output); shell.console().println(StringUtils.rightPad("name", name.intValue() + 1) + "class"); shell.console().println(StringUtils.repeat("-", name.intValue()) + " " + StringUtils.repeat("-", clazz.intValue())); o.forEach((k, v) -> shell.console().println(StringUtils.rightPad(k, name.intValue() + 1) + v)); } } else { log.warn("Listing fault classes failed in [" + status + "] for a reason of [" + content + "]"); } } }
FaultsCommand implements Command { @Override @SuppressWarnings("unchecked") public void run(Line line) throws Exception { String profile = (String) configuration.get("default"); URIBuilder builder = new URIBuilder( ((Map<String, Object>) configuration.get(profile)).get("endpoint") + "/faults"); HttpGet get = new HttpGet(builder.build()); try (CloseableHttpResponse response = httpClient.execute(get)) { StatusLine status = response.getStatusLine(); String content = EntityUtils.toString(response.getEntity(), ContentType.APPLICATION_JSON.getCharset()); log.debug(content); if (status.getStatusCode() == HttpStatus.SC_OK) { ObjectMapper mapper = new ObjectMapper(); Map<String, String> output = mapper.readValue(content, new TypeReference<Map<String, String>>() { }); if (output.isEmpty()) { shell.console().println("No fault classes listed in repository"); } else { MutableInt name = new MutableInt(); MutableInt clazz = new MutableInt(); Map<String, String> o = new TreeMap<>((e1, e2) -> { name.setValue(Math.max(e1.length(), e2.length())); clazz.setValue(Math.max(output.get(e1).length(), output.get(e2).length())); return e1.compareTo(e2); }); o.putAll(output); shell.console().println(StringUtils.rightPad("name", name.intValue() + 1) + "class"); shell.console().println(StringUtils.repeat("-", name.intValue()) + " " + StringUtils.repeat("-", clazz.intValue())); o.forEach((k, v) -> shell.console().println(StringUtils.rightPad(k, name.intValue() + 1) + v)); } } else { log.warn("Listing fault classes failed in [" + status + "] for a reason of [" + content + "]"); } } } }
FaultsCommand implements Command { @Override @SuppressWarnings("unchecked") public void run(Line line) throws Exception { String profile = (String) configuration.get("default"); URIBuilder builder = new URIBuilder( ((Map<String, Object>) configuration.get(profile)).get("endpoint") + "/faults"); HttpGet get = new HttpGet(builder.build()); try (CloseableHttpResponse response = httpClient.execute(get)) { StatusLine status = response.getStatusLine(); String content = EntityUtils.toString(response.getEntity(), ContentType.APPLICATION_JSON.getCharset()); log.debug(content); if (status.getStatusCode() == HttpStatus.SC_OK) { ObjectMapper mapper = new ObjectMapper(); Map<String, String> output = mapper.readValue(content, new TypeReference<Map<String, String>>() { }); if (output.isEmpty()) { shell.console().println("No fault classes listed in repository"); } else { MutableInt name = new MutableInt(); MutableInt clazz = new MutableInt(); Map<String, String> o = new TreeMap<>((e1, e2) -> { name.setValue(Math.max(e1.length(), e2.length())); clazz.setValue(Math.max(output.get(e1).length(), output.get(e2).length())); return e1.compareTo(e2); }); o.putAll(output); shell.console().println(StringUtils.rightPad("name", name.intValue() + 1) + "class"); shell.console().println(StringUtils.repeat("-", name.intValue()) + " " + StringUtils.repeat("-", clazz.intValue())); o.forEach((k, v) -> shell.console().println(StringUtils.rightPad(k, name.intValue() + 1) + v)); } } else { log.warn("Listing fault classes failed in [" + status + "] for a reason of [" + content + "]"); } } } FaultsCommand(Shell shell, Map<String, Object> configuration); }
FaultsCommand implements Command { @Override @SuppressWarnings("unchecked") public void run(Line line) throws Exception { String profile = (String) configuration.get("default"); URIBuilder builder = new URIBuilder( ((Map<String, Object>) configuration.get(profile)).get("endpoint") + "/faults"); HttpGet get = new HttpGet(builder.build()); try (CloseableHttpResponse response = httpClient.execute(get)) { StatusLine status = response.getStatusLine(); String content = EntityUtils.toString(response.getEntity(), ContentType.APPLICATION_JSON.getCharset()); log.debug(content); if (status.getStatusCode() == HttpStatus.SC_OK) { ObjectMapper mapper = new ObjectMapper(); Map<String, String> output = mapper.readValue(content, new TypeReference<Map<String, String>>() { }); if (output.isEmpty()) { shell.console().println("No fault classes listed in repository"); } else { MutableInt name = new MutableInt(); MutableInt clazz = new MutableInt(); Map<String, String> o = new TreeMap<>((e1, e2) -> { name.setValue(Math.max(e1.length(), e2.length())); clazz.setValue(Math.max(output.get(e1).length(), output.get(e2).length())); return e1.compareTo(e2); }); o.putAll(output); shell.console().println(StringUtils.rightPad("name", name.intValue() + 1) + "class"); shell.console().println(StringUtils.repeat("-", name.intValue()) + " " + StringUtils.repeat("-", clazz.intValue())); o.forEach((k, v) -> shell.console().println(StringUtils.rightPad(k, name.intValue() + 1) + v)); } } else { log.warn("Listing fault classes failed in [" + status + "] for a reason of [" + content + "]"); } } } FaultsCommand(Shell shell, Map<String, Object> configuration); @Override String name(); @Override Usage usage(); @Override @SuppressWarnings("unchecked") void run(Line line); void httpClient(CloseableHttpClient httpClient); }
FaultsCommand implements Command { @Override @SuppressWarnings("unchecked") public void run(Line line) throws Exception { String profile = (String) configuration.get("default"); URIBuilder builder = new URIBuilder( ((Map<String, Object>) configuration.get(profile)).get("endpoint") + "/faults"); HttpGet get = new HttpGet(builder.build()); try (CloseableHttpResponse response = httpClient.execute(get)) { StatusLine status = response.getStatusLine(); String content = EntityUtils.toString(response.getEntity(), ContentType.APPLICATION_JSON.getCharset()); log.debug(content); if (status.getStatusCode() == HttpStatus.SC_OK) { ObjectMapper mapper = new ObjectMapper(); Map<String, String> output = mapper.readValue(content, new TypeReference<Map<String, String>>() { }); if (output.isEmpty()) { shell.console().println("No fault classes listed in repository"); } else { MutableInt name = new MutableInt(); MutableInt clazz = new MutableInt(); Map<String, String> o = new TreeMap<>((e1, e2) -> { name.setValue(Math.max(e1.length(), e2.length())); clazz.setValue(Math.max(output.get(e1).length(), output.get(e2).length())); return e1.compareTo(e2); }); o.putAll(output); shell.console().println(StringUtils.rightPad("name", name.intValue() + 1) + "class"); shell.console().println(StringUtils.repeat("-", name.intValue()) + " " + StringUtils.repeat("-", clazz.intValue())); o.forEach((k, v) -> shell.console().println(StringUtils.rightPad(k, name.intValue() + 1) + v)); } } else { log.warn("Listing fault classes failed in [" + status + "] for a reason of [" + content + "]"); } } } FaultsCommand(Shell shell, Map<String, Object> configuration); @Override String name(); @Override Usage usage(); @Override @SuppressWarnings("unchecked") void run(Line line); void httpClient(CloseableHttpClient httpClient);  }