* BAEL-2294

* BAEL-2294

* live tests...

* formatting

* Live Tests! But not parent pom. Yet.
This commit is contained in:
Andrea Ligios
2019-01-23 18:12:06 +01:00
committed by maibin
parent effde80333
commit 4c7ef02bf3
35 changed files with 1004 additions and 0 deletions
@@ -0,0 +1,56 @@
package com.baeldung.blade.sample;
import static org.assertj.core.api.Assertions.assertThat;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
public class AppLiveTest {
@Test
public void givenBasicRoute_whenGet_thenCorrectOutput() throws Exception {
final HttpUriRequest request = new HttpGet("http://localhost:9000/basic-route-example");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("GET called");
}
}
@Test
public void givenBasicRoute_whenPost_thenCorrectOutput() throws Exception {
final HttpUriRequest request = new HttpPost("http://localhost:9000/basic-route-example");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("POST called");
}
}
@Test
public void givenBasicRoute_whenPut_thenCorrectOutput() throws Exception {
final HttpUriRequest request = new HttpPut("http://localhost:9000/basic-route-example");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("PUT called");
}
}
@Test
public void givenBasicRoute_whenDelete_thenCorrectOutput() throws Exception {
final HttpUriRequest request = new HttpDelete("http://localhost:9000/basic-route-example");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("DELETE called");
}
}
}
@@ -0,0 +1,55 @@
package com.baeldung.blade.sample;
import static org.assertj.core.api.Assertions.assertThat;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
public class AttributesExampleControllerLiveTest {
@Test
public void givenRequestAttribute_whenSet_thenRetrieveWithGet() throws Exception {
final HttpUriRequest request = new HttpGet("http://localhost:9000/request-attribute-example");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo(AttributesExampleController.REQUEST_VALUE);
}
}
@Test
public void givenSessionAttribute_whenSet_thenRetrieveWithGet() throws Exception {
final HttpUriRequest request = new HttpGet("http://localhost:9000/session-attribute-example");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo(AttributesExampleController.SESSION_VALUE);
}
}
@Test
public void givenHeader_whenSet_thenRetrieveWithGet() throws Exception {
final HttpUriRequest request = new HttpGet("http://localhost:9000/header-example");
request.addHeader("a-header","foobar");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(httpResponse.getHeaders("a-header")[0].getValue()).isEqualTo("foobar");
}
}
@Test
public void givenNoHeader_whenSet_thenRetrieveDefaultValueWithGet() throws Exception {
final HttpUriRequest request = new HttpGet("http://localhost:9000/header-example");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(httpResponse.getHeaders("a-header")[0].getValue()).isEqualTo(AttributesExampleController.HEADER);
}
}
}
@@ -0,0 +1,82 @@
package com.baeldung.blade.sample;
import static org.assertj.core.api.Assertions.assertThat;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
public class ParameterInjectionExampleControllerLiveTest {
@Test
public void givenFormParam_whenSet_thenRetrieveWithGet() throws Exception {
URIBuilder builder = new URIBuilder("http://localhost:9000/params/form");
builder.setParameter("name", "Andrea Ligios");
final HttpUriRequest request = new HttpGet(builder.build());
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("Andrea Ligios");
}
}
@Test
public void givenPathParam_whenSet_thenRetrieveWithGet() throws Exception {
final HttpUriRequest request = new HttpGet("http://localhost:9000/params/path/1337");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("1337");
}
}
// @Test
// public void givenFileParam_whenSet_thenRetrieveWithGet() throws Exception {
//
// byte[] data = "this is some temp file content".getBytes("UTF-8");
// java.nio.file.Path tempFile = Files.createTempFile("baeldung_test_tempfiles", ".tmp");
// Files.write(tempFile, data, StandardOpenOption.WRITE);
//
// //HttpEntity entity = MultipartEntityBuilder.create().addPart("file", new FileBody(tempFile.toFile())).build();
// HttpEntity entity = MultipartEntityBuilder.create().addTextBody("field1", "value1")
// .addBinaryBody("fileItem", tempFile.toFile(), ContentType.create("application/octet-stream"), "file1.txt").build();
//
// final HttpPost post = new HttpPost("http://localhost:9000/params-file");
// post.setEntity(entity);
//
// try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create().build().execute(post);) {
// assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("file1.txt");
// }
// }
@Test
public void givenHeader_whenSet_thenRetrieveWithGet() throws Exception {
final HttpUriRequest request = new HttpGet("http://localhost:9000/params/header");
request.addHeader("customheader", "foobar");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("foobar");
}
}
@Test
public void givenNoCookie_whenCalled_thenReadDefaultValue() throws Exception {
final HttpUriRequest request = new HttpGet("http://localhost:9000/params/cookie");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("default value");
}
}
}
@@ -0,0 +1,117 @@
package com.baeldung.blade.sample;
import static org.assertj.core.api.Assertions.assertThat;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
public class RouteExampleControllerLiveTest {
@Test
public void givenRoute_whenGet_thenCorrectOutput() throws Exception {
final HttpUriRequest request = new HttpGet("http://localhost:9000/route-example");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("GET called");
}
}
@Test
public void givenRoute_whenPost_thenCorrectOutput() throws Exception {
final HttpUriRequest request = new HttpPost("http://localhost:9000/route-example");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("POST called");
}
}
@Test
public void givenRoute_whenPut_thenCorrectOutput() throws Exception {
final HttpUriRequest request = new HttpPut("http://localhost:9000/route-example");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("PUT called");
}
}
@Test
public void givenRoute_whenDelete_thenCorrectOutput() throws Exception {
final HttpUriRequest request = new HttpDelete("http://localhost:9000/route-example");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("DELETE called");
}
}
@Test
public void givenAnotherRoute_whenGet_thenCorrectOutput() throws Exception {
final HttpUriRequest request = new HttpGet("http://localhost:9000/another-route-example");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("GET called");
}
}
@Test
public void givenAllMatchRoute_whenGet_thenCorrectOutput() throws Exception {
final HttpUriRequest request = new HttpGet("http://localhost:9000/allmatch-route-example");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("ALLMATCH called");
}
}
@Test
public void givenAllMatchRoute_whenPost_thenCorrectOutput() throws Exception {
final HttpUriRequest request = new HttpPost("http://localhost:9000/allmatch-route-example");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("ALLMATCH called");
}
}
@Test
public void givenAllMatchRoute_whenPut_thenCorrectOutput() throws Exception {
final HttpUriRequest request = new HttpPut("http://localhost:9000/allmatch-route-example");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("ALLMATCH called");
}
}
@Test
public void givenAllMatchRoute_whenDelete_thenCorrectOutput() throws Exception {
final HttpUriRequest request = new HttpDelete("http://localhost:9000/allmatch-route-example");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("ALLMATCH called");
}
}
@Test
public void givenRequestAttribute_whenRenderedWithTemplate_thenCorrectlyEvaluateIt() throws Exception {
final HttpUriRequest request = new HttpGet("http://localhost:9000/template-output-test");
try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create()
.build()
.execute(request);) {
assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("<h1>Hello, Blade!</h1>");
}
}
}