BAEL-2452: Using curl from Java (#6043)

* BAEL-2452: Using curl from Java

* A re-do of the examples using tests, instead of main class

* Fix un-used variable
This commit is contained in:
Olaniyi Anjola 2019-01-04 11:44:24 -05:00 committed by maibin
parent cd3f0fcb1f
commit d5ab82475d
2 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,29 @@
package com.baeldung.curltojava;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
public class JavaCurlExamples {
public static String inputStreamToString(InputStream inputStream) {
final int bufferSize = 8 * 1024;
byte[] buffer = new byte[bufferSize];
final StringBuilder builder = new StringBuilder();
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream, bufferSize)) {
while (bufferedInputStream.read(buffer) != -1) {
builder.append(new String(buffer));
}
} catch (IOException ex) {
Logger.getLogger(JavaCurlExamples.class.getName()).log(Level.SEVERE, null, ex);
}
return builder.toString();
}
public static void consumeInputStream(InputStream inputStream) {
inputStreamToString(inputStream);
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.curltojava;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.junit.Assert;
import org.junit.Test;
public class JavaCurlExamplesUnitTest {
@Test
public void givenCommand_whenCalled_thenProduceZeroExitCode() throws IOException {
String command = "curl --location --request GET \"https://postman-echo.com/get?foo1=bar1&foo2=bar2\"";
ProcessBuilder processBuilder = new ProcessBuilder(command.replaceAll("\"", "").split(" "));
processBuilder.directory(new File("/home/"));
Process process = processBuilder.start();
InputStream inputStream = process.getInputStream();
// Consume the inputStream so the process can exit
JavaCurlExamples.consumeInputStream(inputStream);
int exitCode = process.exitValue();
Assert.assertEquals(0, exitCode);
}
@Test
public void givenNewCommands_whenCalled_thenCheckIfIsAlive() throws IOException {
String command = "curl --location --request GET \"https://postman-echo.com/get?foo1=bar1&foo2=bar2\"";
ProcessBuilder processBuilder = new ProcessBuilder(command.replaceAll("\"", "").split(" "));
processBuilder.directory(new File("/home/"));
Process process = processBuilder.start();
// Re-use processBuilder
processBuilder.command(new String[]{"newCommand", "arguments"});
Assert.assertEquals(true, process.isAlive());
}
@Test
public void whenRequestGet_thenReturnSuccessResponseCode() throws IOException {
String url = "https://postman-echo.com/get?foo1=bar1&foo2=bar2";
URL urlObj = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("GET");
connection.connect();
Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
}
}