@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); StatusCommand command = new StatusCommand(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("[{\"id\" : \"fault-id-1\", \"name\" : \"fault-2\", \"status\" : \"ACTIVE\", \"params\" : \"{\\\"hello\\\" : \\\"goodbye\\\"}\"}, " + "{\"id\" : \"fault-id-2\", \"name\" : \"fault-1\", \"status\" : \"INACTIVE\", \"params\" : \"{\\\"hello\\\" : \\\"goodbye\\\"}\"}, " + "{\"id\" : \"fault-id-3\", \"name\" : \"fault-1\", \"status\" : \"ACTIVE\", \"params\" : \"{\\\"hello\\\" : \\\"goodbye\\\"}\"}]", ContentType.APPLICATION_JSON); doReturn(entity).when(response).getEntity(); command.run(new Line("faults", options, args)); entity = new StringEntity("[{\"id\" : \"fault-id-1\", \"name\" : \"fault-0123456789\", \"status\" : \"ACTIVE\", \"params\" : \"{\\\"hello\\\" : \\\"goodbye\\\"}\"}]", ContentType.APPLICATION_JSON); doReturn(entity).when(response).getEntity(); command.run(new Line("faults", options, args)); entity = new StringEntity("{\"id\" : \"flt-id-1\", \"name\" : \"fault-1\", \"status\" : \"ACTIVE\", \"params\" : \"{\\\"hello\\\" : \\\"goodbye\\\"}\"}", ContentType.APPLICATION_JSON); doReturn(entity).when(response).getEntity(); 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); options = new HashMap<>(); args = new ArrayList<>(); args.add("fault-id-1"); 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") + "/status"); if (line.args().size() == 1) { builder.addParameter("id", line.args().get(0)); } 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(); if (line.args().size() == 1) { Map<String, Object> output = mapper.readValue(content, new TypeReference<Map<String, Object>>() { }); String name = (String) output.get("name"); String params = (String) output.get("params"); shell.console().println(StringUtils.rightPad("id", 8 + 1) + StringUtils.rightPad("name", name.length() + 1) + StringUtils.rightPad("status", 7 + 1) + "params"); shell.console().println(StringUtils.repeat("-", 8) + " " + StringUtils.repeat("-", name.length()) + " " + StringUtils.repeat("-", 7) + " " + StringUtils.repeat("-", params.length())); shell.console().println(output.get("id") + " " + name + " " + StringUtils.rightPad((String) output.get("status"), 7) + " " + params); } else { List<Map<String, Object>> output = mapper.readValue(content, new TypeReference<List<Map<String, Object>>>() { }); if (output.isEmpty()) { shell.console().println("No fault status listed in scheduler"); return; } MutableInt length = new MutableInt(); if (output.size() == 1) { length.setValue(((String) output.get(0).get("name")).length()); } else { output.sort((e1, e2) -> { String n1 = (String) e1.get("name"); String n2 = (String) e2.get("name"); length.setValue(Math.max(n1.length(), n2.length())); int i = n1.compareTo(n2); if (i == 0) { return ((String) e1.get("status")).compareTo((String) e2.get("status")); } return i; }); } shell.console().println(StringUtils.rightPad("name", length.intValue() + 1) + StringUtils.rightPad("id", 8 + 1) + "status"); shell.console().println(StringUtils.repeat("-", length.intValue()) + " " + StringUtils.repeat("-", 8) + " " + StringUtils.repeat("-", 8)); output.forEach(e -> shell.console().println(StringUtils.rightPad((String) e.get("name"), length.intValue()) + " " + e.get("id") + " " + e.get("status"))); } } else { log.warn("Listing fault status failed in [" + status + "] for a reason of [" + content + "]"); } } }
StatusCommand 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") + "/status"); if (line.args().size() == 1) { builder.addParameter("id", line.args().get(0)); } 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(); if (line.args().size() == 1) { Map<String, Object> output = mapper.readValue(content, new TypeReference<Map<String, Object>>() { }); String name = (String) output.get("name"); String params = (String) output.get("params"); shell.console().println(StringUtils.rightPad("id", 8 + 1) + StringUtils.rightPad("name", name.length() + 1) + StringUtils.rightPad("status", 7 + 1) + "params"); shell.console().println(StringUtils.repeat("-", 8) + " " + StringUtils.repeat("-", name.length()) + " " + StringUtils.repeat("-", 7) + " " + StringUtils.repeat("-", params.length())); shell.console().println(output.get("id") + " " + name + " " + StringUtils.rightPad((String) output.get("status"), 7) + " " + params); } else { List<Map<String, Object>> output = mapper.readValue(content, new TypeReference<List<Map<String, Object>>>() { }); if (output.isEmpty()) { shell.console().println("No fault status listed in scheduler"); return; } MutableInt length = new MutableInt(); if (output.size() == 1) { length.setValue(((String) output.get(0).get("name")).length()); } else { output.sort((e1, e2) -> { String n1 = (String) e1.get("name"); String n2 = (String) e2.get("name"); length.setValue(Math.max(n1.length(), n2.length())); int i = n1.compareTo(n2); if (i == 0) { return ((String) e1.get("status")).compareTo((String) e2.get("status")); } return i; }); } shell.console().println(StringUtils.rightPad("name", length.intValue() + 1) + StringUtils.rightPad("id", 8 + 1) + "status"); shell.console().println(StringUtils.repeat("-", length.intValue()) + " " + StringUtils.repeat("-", 8) + " " + StringUtils.repeat("-", 8)); output.forEach(e -> shell.console().println(StringUtils.rightPad((String) e.get("name"), length.intValue()) + " " + e.get("id") + " " + e.get("status"))); } } else { log.warn("Listing fault status failed in [" + status + "] for a reason of [" + content + "]"); } } } }
StatusCommand 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") + "/status"); if (line.args().size() == 1) { builder.addParameter("id", line.args().get(0)); } 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(); if (line.args().size() == 1) { Map<String, Object> output = mapper.readValue(content, new TypeReference<Map<String, Object>>() { }); String name = (String) output.get("name"); String params = (String) output.get("params"); shell.console().println(StringUtils.rightPad("id", 8 + 1) + StringUtils.rightPad("name", name.length() + 1) + StringUtils.rightPad("status", 7 + 1) + "params"); shell.console().println(StringUtils.repeat("-", 8) + " " + StringUtils.repeat("-", name.length()) + " " + StringUtils.repeat("-", 7) + " " + StringUtils.repeat("-", params.length())); shell.console().println(output.get("id") + " " + name + " " + StringUtils.rightPad((String) output.get("status"), 7) + " " + params); } else { List<Map<String, Object>> output = mapper.readValue(content, new TypeReference<List<Map<String, Object>>>() { }); if (output.isEmpty()) { shell.console().println("No fault status listed in scheduler"); return; } MutableInt length = new MutableInt(); if (output.size() == 1) { length.setValue(((String) output.get(0).get("name")).length()); } else { output.sort((e1, e2) -> { String n1 = (String) e1.get("name"); String n2 = (String) e2.get("name"); length.setValue(Math.max(n1.length(), n2.length())); int i = n1.compareTo(n2); if (i == 0) { return ((String) e1.get("status")).compareTo((String) e2.get("status")); } return i; }); } shell.console().println(StringUtils.rightPad("name", length.intValue() + 1) + StringUtils.rightPad("id", 8 + 1) + "status"); shell.console().println(StringUtils.repeat("-", length.intValue()) + " " + StringUtils.repeat("-", 8) + " " + StringUtils.repeat("-", 8)); output.forEach(e -> shell.console().println(StringUtils.rightPad((String) e.get("name"), length.intValue()) + " " + e.get("id") + " " + e.get("status"))); } } else { log.warn("Listing fault status failed in [" + status + "] for a reason of [" + content + "]"); } } } StatusCommand(Shell shell, Map<String, Object> configuration); }
StatusCommand 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") + "/status"); if (line.args().size() == 1) { builder.addParameter("id", line.args().get(0)); } 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(); if (line.args().size() == 1) { Map<String, Object> output = mapper.readValue(content, new TypeReference<Map<String, Object>>() { }); String name = (String) output.get("name"); String params = (String) output.get("params"); shell.console().println(StringUtils.rightPad("id", 8 + 1) + StringUtils.rightPad("name", name.length() + 1) + StringUtils.rightPad("status", 7 + 1) + "params"); shell.console().println(StringUtils.repeat("-", 8) + " " + StringUtils.repeat("-", name.length()) + " " + StringUtils.repeat("-", 7) + " " + StringUtils.repeat("-", params.length())); shell.console().println(output.get("id") + " " + name + " " + StringUtils.rightPad((String) output.get("status"), 7) + " " + params); } else { List<Map<String, Object>> output = mapper.readValue(content, new TypeReference<List<Map<String, Object>>>() { }); if (output.isEmpty()) { shell.console().println("No fault status listed in scheduler"); return; } MutableInt length = new MutableInt(); if (output.size() == 1) { length.setValue(((String) output.get(0).get("name")).length()); } else { output.sort((e1, e2) -> { String n1 = (String) e1.get("name"); String n2 = (String) e2.get("name"); length.setValue(Math.max(n1.length(), n2.length())); int i = n1.compareTo(n2); if (i == 0) { return ((String) e1.get("status")).compareTo((String) e2.get("status")); } return i; }); } shell.console().println(StringUtils.rightPad("name", length.intValue() + 1) + StringUtils.rightPad("id", 8 + 1) + "status"); shell.console().println(StringUtils.repeat("-", length.intValue()) + " " + StringUtils.repeat("-", 8) + " " + StringUtils.repeat("-", 8)); output.forEach(e -> shell.console().println(StringUtils.rightPad((String) e.get("name"), length.intValue()) + " " + e.get("id") + " " + e.get("status"))); } } else { log.warn("Listing fault status failed in [" + status + "] for a reason of [" + content + "]"); } } } StatusCommand(Shell shell, Map<String, Object> configuration); @Override String name(); @Override Usage usage(); @Override @SuppressWarnings("unchecked") void run(Line line); void httpClient(CloseableHttpClient httpClient); }
StatusCommand 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") + "/status"); if (line.args().size() == 1) { builder.addParameter("id", line.args().get(0)); } 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(); if (line.args().size() == 1) { Map<String, Object> output = mapper.readValue(content, new TypeReference<Map<String, Object>>() { }); String name = (String) output.get("name"); String params = (String) output.get("params"); shell.console().println(StringUtils.rightPad("id", 8 + 1) + StringUtils.rightPad("name", name.length() + 1) + StringUtils.rightPad("status", 7 + 1) + "params"); shell.console().println(StringUtils.repeat("-", 8) + " " + StringUtils.repeat("-", name.length()) + " " + StringUtils.repeat("-", 7) + " " + StringUtils.repeat("-", params.length())); shell.console().println(output.get("id") + " " + name + " " + StringUtils.rightPad((String) output.get("status"), 7) + " " + params); } else { List<Map<String, Object>> output = mapper.readValue(content, new TypeReference<List<Map<String, Object>>>() { }); if (output.isEmpty()) { shell.console().println("No fault status listed in scheduler"); return; } MutableInt length = new MutableInt(); if (output.size() == 1) { length.setValue(((String) output.get(0).get("name")).length()); } else { output.sort((e1, e2) -> { String n1 = (String) e1.get("name"); String n2 = (String) e2.get("name"); length.setValue(Math.max(n1.length(), n2.length())); int i = n1.compareTo(n2); if (i == 0) { return ((String) e1.get("status")).compareTo((String) e2.get("status")); } return i; }); } shell.console().println(StringUtils.rightPad("name", length.intValue() + 1) + StringUtils.rightPad("id", 8 + 1) + "status"); shell.console().println(StringUtils.repeat("-", length.intValue()) + " " + StringUtils.repeat("-", 8) + " " + StringUtils.repeat("-", 8)); output.forEach(e -> shell.console().println(StringUtils.rightPad((String) e.get("name"), length.intValue()) + " " + e.get("id") + " " + e.get("status"))); } } else { log.warn("Listing fault status failed in [" + status + "] for a reason of [" + content + "]"); } } } StatusCommand(Shell shell, Map<String, Object> configuration); @Override String name(); @Override Usage usage(); @Override @SuppressWarnings("unchecked") void run(Line line); void httpClient(CloseableHttpClient httpClient);  }