Spring 5 reff (#1786)

* Fix Spring5 snippets

* Refactor Spring 5 samples

* Refactor Spring 5 samples

* Enable Spring5
This commit is contained in:
Grzegorz Piwowarek 2017-05-05 16:26:25 +02:00 committed by GitHub
parent 67968089a0
commit 30df1541a1
10 changed files with 161 additions and 196 deletions

View File

@ -111,7 +111,7 @@
<module>selenium-junit-testng</module>
<module>solr</module>
<module>spark-java</module>
<!-- <module>spring-5</module> TODO: CI problems-->
<module>spring-5</module>
<module>spring-akka</module>
<module>spring-amqp</module>
<module>spring-all</module>

3
spring-5/.gitignore vendored
View File

@ -1,6 +1,5 @@
*.class
#folders#
.idea
/target
/neoDb*
/data

View File

@ -18,33 +18,24 @@ public class FormHandler {
return request
.body(toFormData())
.map(MultiValueMap::toSingleValueMap)
.map(formData -> {
System.out.println("form data: " + formData.toString());
if ("baeldung".equals(formData.get("user")) && "you_know_what_to_do".equals(formData.get("token"))) {
return ok()
.body(Mono.just("welcome back!"), String.class)
.block();
}
return ServerResponse
.badRequest()
.build()
.block();
});
.filter(formData -> "baeldung".equals(formData.get("user")))
.filter(formData -> "you_know_what_to_do".equals(formData.get("token")))
.flatMap(formData -> ok().body(Mono.just("welcome back!"), String.class))
.or(ServerResponse.badRequest().build());
}
Mono<ServerResponse> handleUpload(ServerRequest request) {
return request
.body(toDataBuffers())
.collectList()
.map(dataBuffers -> {
.flatMap(dataBuffers -> {
AtomicLong atomicLong = new AtomicLong(0);
dataBuffers.forEach(d -> atomicLong.addAndGet(d
.asByteBuffer()
.array().length));
System.out.println("data length:" + atomicLong.get());
return ok()
.body(fromObject(atomicLong.toString()))
.block();
.body(fromObject(atomicLong.toString()));
});
}
}

View File

@ -1,26 +1,15 @@
package com.baeldung.web;
import java.util.List;
import java.util.Optional;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import com.baeldung.persistence.FooRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.baeldung.persistence.FooRepository;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;
@RestController("/foos")
public class FooController {
@ -30,30 +19,29 @@ public class FooController {
// API - read
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}")
@GetMapping("/foos/{id}")
@ResponseBody
@Validated
public Foo findById(@PathVariable @Min(0) final long id) {
return repo.findOne(id).orElse(null);
return repo.findById(id).orElse(null);
}
@RequestMapping(method = RequestMethod.GET)
@GetMapping
@ResponseBody
public List<Foo> findAll() {
return repo.findAll();
}
@RequestMapping(params = { "page", "size" }, method = RequestMethod.GET)
@GetMapping(params = { "page", "size" })
@ResponseBody
@Validated
public List<Foo> findPaginated(@RequestParam("page") @Min(0) final int page, @Max(100) @RequestParam("size") final int size) {
final Page<Foo> resultPage = repo.findAll(new PageRequest(page, size));
return resultPage.getContent();
return repo.findAll(PageRequest.of(page, size)).getContent();
}
// API - write
@RequestMapping(method = RequestMethod.PUT, value = "/foos/{id}")
@PutMapping("/foos/{id}")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Foo update(@PathVariable("id") final String id, @RequestBody final Foo foo) {

View File

@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class IntegrationTestExample1 {
public class Example1IntegrationTest {
@Test
public void test1a() {

View File

@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class IntegrationTestExample2 {
public class Example2IntegrationTest {
@Test
public void test1a() {

View File

@ -5,18 +5,18 @@ import org.junit.experimental.ParallelComputer;
import org.junit.runner.Computer;
import org.junit.runner.JUnitCore;
public class ParallelTestExample {
public class ParallelIntegrationTest {
@Test
public void runTests() {
final Class<?>[] classes = { IntegrationTestExample1.class, IntegrationTestExample2.class };
final Class<?>[] classes = { Example1IntegrationTest.class, Example2IntegrationTest.class };
JUnitCore.runClasses(new Computer(), classes);
}
@Test
public void runTestsInParallel() {
final Class<?>[] classes = { IntegrationTestExample1.class, IntegrationTestExample2.class };
final Class<?>[] classes = { Example1IntegrationTest.class, Example2IntegrationTest.class };
JUnitCore.runClasses(new ParallelComputer(true, true), classes);
}

View File

@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Spring5ApplicationTests {
public class Spring5ApplicationIntegrationTest {
@Test
public void contextLoads() {

View File

@ -4,152 +4,139 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.web.server.WebServer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
//import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.test.web.reactive.server.WebTestClient;
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
import static org.springframework.web.reactive.function.BodyInserters.fromResource;
// TODO The class does not compile, WebTestClient cannot be resolved. Missing dependency?
public class FunctionalWebApplicationIntegrationTest {
// private static WebTestClient client;
// private static WebServer server;
//
// @BeforeClass
// public static void setup() throws Exception {
// server = new FunctionalWebApplication().start();
// client = WebTestClient
// .bindToServer()
// .baseUrl("http://localhost:" + server.getPort())
// .build();
// }
//
// @AfterClass
// public static void destroy() {
// server.stop();
// }
//
// @Test
// public void givenRouter_whenGetTest_thenGotHelloWorld() throws Exception {
// client
// .get()
// .uri("/test")
// .exchange()
// .expectStatus()
// .isOk()
// .expectBody(String.class)
// .value()
// .isEqualTo("helloworld");
// }
//
// @Test
// public void givenIndexFilter_whenRequestRoot_thenRewrittenToTest() throws Exception {
// client
// .get()
// .uri("/")
// .exchange()
// .expectStatus()
// .isOk()
// .expectBody(String.class)
// .value()
// .isEqualTo("helloworld");
// }
//
// @Test
// public void givenLoginForm_whenPostValidToken_thenSuccess() throws Exception {
// MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(1);
// formData.add("user", "baeldung");
// formData.add("token", "you_know_what_to_do");
//
// client
// .post()
// .uri("/login")
// .contentType(MediaType.APPLICATION_FORM_URLENCODED)
// .exchange(BodyInserters.fromFormData(formData))
// .expectStatus()
// .isOk()
// .expectBody(String.class)
// .value()
// .isEqualTo("welcome back!");
// }
//
// @Test
// public void givenLoginForm_whenRequestWithInvalidToken_thenFail() throws Exception {
// MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(2);
// formData.add("user", "baeldung");
// formData.add("token", "try_again");
//
// client
// .post()
// .uri("/login")
// .contentType(MediaType.APPLICATION_FORM_URLENCODED)
// .exchange(BodyInserters.fromFormData(formData))
// .expectStatus()
// .isBadRequest();
// }
//
// @Test
// public void givenUploadForm_whenRequestWithMultipartData_thenSuccess() throws Exception {
// Resource resource = new ClassPathResource("/baeldung-weekly.png");
// client
// .post()
// .uri("/upload")
// .contentType(MediaType.MULTIPART_FORM_DATA)
// .exchange(fromResource(resource))
// .expectStatus()
// .isOk()
// .expectBody(String.class)
// .value()
// .isEqualTo(String.valueOf(resource.contentLength()));
// }
//
// @Test
// public void givenActors_whenAddActor_thenAdded() throws Exception {
// client
// .get()
// .uri("/actor")
// .exchange()
// .expectStatus()
// .isOk()
// .expectBody(Actor.class)
// .list()
// .hasSize(2);
//
// client
// .post()
// .uri("/actor")
// .exchange(fromObject(new Actor("Clint", "Eastwood")))
// .expectStatus()
// .isOk();
//
// client
// .get()
// .uri("/actor")
// .exchange()
// .expectStatus()
// .isOk()
// .expectBody(Actor.class)
// .list()
// .hasSize(3);
// }
//
// @Test
// public void givenResources_whenAccess_thenGot() throws Exception {
// client
// .get()
// .uri("/files/hello.txt")
// .exchange()
// .expectStatus()
// .isOk()
// .expectBody(String.class)
// .value()
// .isEqualTo("hello");
// }
private static WebTestClient client;
private static WebServer server;
@BeforeClass
public static void setup() throws Exception {
server = new FunctionalWebApplication().start();
client = WebTestClient
.bindToServer()
.baseUrl("http://localhost:" + server.getPort())
.build();
}
@AfterClass
public static void destroy() {
server.stop();
}
@Test
public void givenRouter_whenGetTest_thenGotHelloWorld() throws Exception {
client
.get()
.uri("/test")
.exchange()
.expectStatus()
.isOk()
.expectBody(String.class)
.isEqualTo("helloworld");
}
@Test
public void givenIndexFilter_whenRequestRoot_thenRewrittenToTest() throws Exception {
client
.get()
.uri("/")
.exchange()
.expectStatus()
.isOk()
.expectBody(String.class)
.isEqualTo("helloworld");
}
/* @Test
public void givenLoginForm_whenPostValidToken_thenSuccess() throws Exception {
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(1);
formData.add("user", "baeldung");
formData.add("token", "you_know_what_to_do");
client
.post()
.uri("/login")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.exchange(BodyInserters.fromFormData(formData))
.expectStatus()
.isOk()
.expectBody(String.class)
.value()
.isEqualTo("welcome back!");
}*/
/* @Test
public void givenLoginForm_whenRequestWithInvalidToken_thenFail() throws Exception {
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(2);
formData.add("user", "baeldung");
formData.add("token", "try_again");
client
.post()
.uri("/login")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.exchange(BodyInserters.fromFormData(formData))
.expectStatus()
.isBadRequest();
}
*/
/* @Test
public void givenUploadForm_whenRequestWithMultipartData_thenSuccess() throws Exception {
Resource resource = new ClassPathResource("/baeldung-weekly.png");
client
.post()
.uri("/upload")
.contentType(MediaType.MULTIPART_FORM_DATA)
.exchange(fromResource(resource))
.expectStatus()
.isOk()
.expectBody(String.class)
.value()
.isEqualTo(String.valueOf(resource.contentLength()));
}
*/
/* @Test
public void givenActors_whenAddActor_thenAdded() throws Exception {
client
.get()
.uri("/actor")
.exchange()
.expectStatus()
.isOk()
.expectBody(Actor.class)
.list()
.hasSize(2);
client
.post()
.uri("/actor")
.exchange(fromObject(new Actor("Clint", "Eastwood")))
.expectStatus()
.isOk();
client
.get()
.uri("/actor")
.exchange()
.expectStatus()
.isOk()
.expectBody(Actor.class)
.list()
.hasSize(3);
}*/
@Test
public void givenResources_whenAccess_thenGot() throws Exception {
client
.get()
.uri("/files/hello.txt")
.exchange()
.expectStatus()
.isOk()
.expectBody(String.class)
.isEqualTo("hello");
}
}

View File

@ -1,7 +1,7 @@
package com.baeldung.jupiter;
import com.baeldung.IntegrationTestExample1;
import com.baeldung.IntegrationTestExample2;
import com.baeldung.Example1IntegrationTest;
import com.baeldung.Example2IntegrationTest;
import org.junit.experimental.ParallelComputer;
import org.junit.jupiter.api.Test;
import org.junit.runner.Computer;
@ -12,7 +12,7 @@ public class Spring5JUnit5ParallelTest {
@Test
public void givenTwoTestClasses_whenJUnitRunParallel_thenTheTestsExecutingParallel() {
final Class<?>[] classes = {
IntegrationTestExample1.class, IntegrationTestExample2.class
Example1IntegrationTest.class, Example2IntegrationTest.class
};
JUnitCore.runClasses(new ParallelComputer(true, true), classes);
@ -21,7 +21,7 @@ public class Spring5JUnit5ParallelTest {
@Test
public void givenTwoTestClasses_whenJUnitRunParallel_thenTheTestsExecutingLinear() {
final Class<?>[] classes = {
IntegrationTestExample1.class, IntegrationTestExample2.class
Example1IntegrationTest.class, Example2IntegrationTest.class
};
JUnitCore.runClasses(new Computer(), classes);