@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); PushCommand command = new PushCommand(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) -> { HttpPost post = i.getArgument(0); assertThat(post.getURI().toString(), is("https: assertTrue(post.getEntity().getContentType().getValue().startsWith("multipart/form-data; boundary=")); assertTrue(IOUtils.toString(post.getEntity().getContent(), ContentType.MULTIPART_FORM_DATA.getCharset()).matches("^.*\r\nContent-Disposition: form-data; name=\"jar\"; filename=\"push-command-test.jar\"[\\s\\S]*?")); 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(getClass().getResource("/push-command-test.jar").getPath()); command.run(new Line("push", options, args)); line = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_INTERNAL_SERVER_ERROR, ""); doReturn(line).when(response).getStatusLine(); command.run(new Line("push", options, args)); args = new ArrayList<>(); args.add("/path/to/not-found.jar"); command.run(new Line("push", options, args)); command = new PushCommand(new Shell(), configuration); args = new ArrayList<>(); command.run(new Line("push", options, args)); command = new PushCommand(new Shell(), configuration); args = new ArrayList<>(); args.add("/push-command-test-1.jar"); args.add("/push-command-test-2.jar"); command.run(new Line("push", options, args)); }
@Override @SuppressWarnings("unchecked") public void run(Line line) throws Exception { if (line.args().size() == 1) { Path path = Paths.get(line.args().get(0)); if (Files.notExists(path)) { log.warn("Fault jar [" + path + "] does not exist"); return; } log.info("Pushing jar [" + path + "]"); String profile = (String) configuration.get("default"); HttpPost post = new HttpPost(((Map<String, Object>) configuration.get(profile)).get("endpoint") + "/push"); post.setEntity(MultipartEntityBuilder.create().addBinaryBody("jar", path.toFile()).build()); 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) { shell.console().println("Fault jar has been successfully pushed"); } else { log.warn("Saving fault jar failed in [" + status + "] for a reason of [" + content + "]"); } } return; } shell.console().println("Unsupported usage"); shell.console().println(usage().toString()); }
PushCommand implements Command { @Override @SuppressWarnings("unchecked") public void run(Line line) throws Exception { if (line.args().size() == 1) { Path path = Paths.get(line.args().get(0)); if (Files.notExists(path)) { log.warn("Fault jar [" + path + "] does not exist"); return; } log.info("Pushing jar [" + path + "]"); String profile = (String) configuration.get("default"); HttpPost post = new HttpPost(((Map<String, Object>) configuration.get(profile)).get("endpoint") + "/push"); post.setEntity(MultipartEntityBuilder.create().addBinaryBody("jar", path.toFile()).build()); 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) { shell.console().println("Fault jar has been successfully pushed"); } else { log.warn("Saving fault jar failed in [" + status + "] for a reason of [" + content + "]"); } } return; } shell.console().println("Unsupported usage"); shell.console().println(usage().toString()); } }
PushCommand implements Command { @Override @SuppressWarnings("unchecked") public void run(Line line) throws Exception { if (line.args().size() == 1) { Path path = Paths.get(line.args().get(0)); if (Files.notExists(path)) { log.warn("Fault jar [" + path + "] does not exist"); return; } log.info("Pushing jar [" + path + "]"); String profile = (String) configuration.get("default"); HttpPost post = new HttpPost(((Map<String, Object>) configuration.get(profile)).get("endpoint") + "/push"); post.setEntity(MultipartEntityBuilder.create().addBinaryBody("jar", path.toFile()).build()); 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) { shell.console().println("Fault jar has been successfully pushed"); } else { log.warn("Saving fault jar failed in [" + status + "] for a reason of [" + content + "]"); } } return; } shell.console().println("Unsupported usage"); shell.console().println(usage().toString()); } PushCommand(Shell shell, Map<String, Object> configuration); }
PushCommand implements Command { @Override @SuppressWarnings("unchecked") public void run(Line line) throws Exception { if (line.args().size() == 1) { Path path = Paths.get(line.args().get(0)); if (Files.notExists(path)) { log.warn("Fault jar [" + path + "] does not exist"); return; } log.info("Pushing jar [" + path + "]"); String profile = (String) configuration.get("default"); HttpPost post = new HttpPost(((Map<String, Object>) configuration.get(profile)).get("endpoint") + "/push"); post.setEntity(MultipartEntityBuilder.create().addBinaryBody("jar", path.toFile()).build()); 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) { shell.console().println("Fault jar has been successfully pushed"); } else { log.warn("Saving fault jar failed in [" + status + "] for a reason of [" + content + "]"); } } return; } shell.console().println("Unsupported usage"); shell.console().println(usage().toString()); } PushCommand(Shell shell, Map<String, Object> configuration); @Override String name(); @Override Usage usage(); @Override @SuppressWarnings("unchecked") void run(Line line); void httpClient(CloseableHttpClient httpClient); }
PushCommand implements Command { @Override @SuppressWarnings("unchecked") public void run(Line line) throws Exception { if (line.args().size() == 1) { Path path = Paths.get(line.args().get(0)); if (Files.notExists(path)) { log.warn("Fault jar [" + path + "] does not exist"); return; } log.info("Pushing jar [" + path + "]"); String profile = (String) configuration.get("default"); HttpPost post = new HttpPost(((Map<String, Object>) configuration.get(profile)).get("endpoint") + "/push"); post.setEntity(MultipartEntityBuilder.create().addBinaryBody("jar", path.toFile()).build()); 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) { shell.console().println("Fault jar has been successfully pushed"); } else { log.warn("Saving fault jar failed in [" + status + "] for a reason of [" + content + "]"); } } return; } shell.console().println("Unsupported usage"); shell.console().println(usage().toString()); } PushCommand(Shell shell, Map<String, Object> configuration); @Override String name(); @Override Usage usage(); @Override @SuppressWarnings("unchecked") void run(Line line); void httpClient(CloseableHttpClient httpClient);  }