spring 5 work

This commit is contained in:
eugenp 2017-04-27 15:50:11 +03:00
parent 966d2ea30f
commit 55dc9a7a9a
4 changed files with 231 additions and 205 deletions

View File

@ -1,180 +1,182 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>spring-5</artifactId> <artifactId>spring-5</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>spring-5</name> <name>spring-5</name>
<description>spring 5 sample project about new features</description> <description>spring 5 sample project about new features</description>
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version> <version>2.0.0.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository --> <relativePath /> <!-- lookup parent from repository -->
</parent> </parent>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId> <artifactId>spring-boot-starter-security</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId> <artifactId>spring-boot-starter-validation</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId> <artifactId>spring-boot-starter-webflux</artifactId>
</dependency> </dependency>
<!-- utils --> <!-- utils -->
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
</dependency> </dependency>
<!-- runtime and test scoped --> <!-- runtime and test scoped -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId> <artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.h2database</groupId> <groupId>com.h2database</groupId>
<artifactId>h2</artifactId> <artifactId>h2</artifactId>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.test.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build> <dependency>
<plugins> <groupId>org.springframework</groupId>
<plugin> <artifactId>spring-test</artifactId>
<groupId>org.springframework.boot</groupId> </dependency>
<artifactId>spring-boot-maven-plugin</artifactId> <dependency>
<configuration> <groupId>org.springframework.boot</groupId>
<mainClass>com.baeldung.Spring5Application</mainClass> <artifactId>spring-boot-starter-test</artifactId>
<layout>JAR</layout> <scope>test</scope>
</configuration> </dependency>
</plugin>
<plugin> <dependency>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>junit-jupiter-api</artifactId>
<configuration> <version>${junit.jupiter.version}</version>
<forkCount>3</forkCount> </dependency>
<reuseForks>true</reuseForks> <dependency>
<excludes> <groupId>org.junit.jupiter</groupId>
<exclude>**/*IntegrationTest.java</exclude> <artifactId>junit-jupiter-engine</artifactId>
<exclude>**/*LiveTest.java</exclude> <version>${junit.jupiter.version}</version>
</excludes> <scope>test</scope>
</configuration> </dependency>
</plugin> <dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<plugin> </dependencies>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<parallel>methods</parallel>
<useUnlimitedThreads>true</useUnlimitedThreads>
</configuration>
</plugin>
</plugins> <build>
</build> <plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.baeldung.Spring5Application</mainClass>
<layout>JAR</layout>
</configuration>
</plugin>
<repositories> <plugin>
<repository> <groupId>org.apache.maven.plugins</groupId>
<id>spring-snapshots</id> <artifactId>maven-surefire-plugin</artifactId>
<name>Spring Snapshots</name> <version>${maven-surefire-plugin.version}</version>
<url>https://repo.spring.io/snapshot</url> <configuration>
<snapshots> <forkCount>3</forkCount>
<enabled>true</enabled> <reuseForks>true</reuseForks>
</snapshots> <excludes>
</repository> <exclude>**/*IntegrationTest.java</exclude>
<repository> <exclude>**/*LiveTest.java</exclude>
<id>spring-milestones</id> </excludes>
<name>Spring Milestones</name> </configuration>
<url>https://repo.spring.io/milestone</url> </plugin>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<properties> <plugin>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <groupId>org.apache.maven.plugins</groupId>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <artifactId>maven-surefire-plugin</artifactId>
<java.version>1.8</java.version> <version>${maven-surefire-plugin.version}</version>
<junit.platform.version>1.0.0-M3</junit.platform.version> <configuration>
<junit.jupiter.version>5.0.0-M3</junit.jupiter.version> <parallel>methods</parallel>
<spring.test.version>4.3.7.RELEASE</spring.test.version> <useUnlimitedThreads>true</useUnlimitedThreads>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version> </configuration>
</properties> </plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<junit.platform.version>1.0.0-M4</junit.platform.version>
<junit.jupiter.version>5.0.0-M4</junit.jupiter.version>
<maven-surefire-plugin.version>2.20</maven-surefire-plugin.version>
</properties>
</project> </project>

View File

@ -56,49 +56,49 @@ public class Spring5ReactiveServerClientTest {
nettyContext.dispose(); nettyContext.dispose();
} }
@Test // @Test
public void givenCheckTask_whenServerHandle_thenServerResponseALiveString() throws Exception { // public void givenCheckTask_whenServerHandle_thenServerResponseALiveString() throws Exception {
WebClient client = WebClient.create("http://localhost:8080"); // WebClient client = WebClient.create("http://localhost:8080");
Mono<String> result = client // Mono<String> result = client
.get() // .get()
.uri("/task") // .uri("/task")
.exchange() // .exchange()
.then(response -> response.bodyToMono(String.class)); // .then(response -> response.bodyToMono(String.class));
//
// assertThat(result.block()).isInstanceOf(String.class);
// }
assertThat(result.block()).isInstanceOf(String.class); // @Test
} // public void givenThreeTasks_whenServerHandleTheTasks_thenServerResponseATask() throws Exception {
// URI uri = URI.create("http://localhost:8080/task/process");
// ExchangeFunction exchange = ExchangeFunctions.create(new ReactorClientHttpConnector());
// ClientRequest request = ClientRequest
// .method(HttpMethod.POST, uri)
// .body(BodyInserters.fromPublisher(getLatLngs(), Task.class))
// .build();
//
// Flux<Task> taskResponse = exchange
// .exchange(request)
// .flatMap(response -> response.bodyToFlux(Task.class));
//
// assertThat(taskResponse.blockFirst()).isInstanceOf(Task.class);
// }
@Test // @Test
public void givenThreeTasks_whenServerHandleTheTasks_thenServerResponseATask() throws Exception { // public void givenCheckTask_whenServerHandle_thenOragicServerResponseALiveString() throws Exception {
URI uri = URI.create("http://localhost:8080/task/process"); // URI uri = URI.create("http://localhost:8080/task");
ExchangeFunction exchange = ExchangeFunctions.create(new ReactorClientHttpConnector()); // ExchangeFunction exchange = ExchangeFunctions.create(new ReactorClientHttpConnector());
ClientRequest request = ClientRequest // ClientRequest request = ClientRequest
.method(HttpMethod.POST, uri) // .method(HttpMethod.GET, uri)
.body(BodyInserters.fromPublisher(getLatLngs(), Task.class)) // .body(BodyInserters.fromPublisher(getLatLngs(), Task.class))
.build(); // .build();
//
Flux<Task> taskResponse = exchange // Flux<String> taskResponse = exchange
.exchange(request) // .exchange(request)
.flatMap(response -> response.bodyToFlux(Task.class)); // .flatMap(response -> response.bodyToFlux(String.class));
//
assertThat(taskResponse.blockFirst()).isInstanceOf(Task.class); // assertThat(taskResponse.blockFirst()).isInstanceOf(String.class);
} // }
@Test
public void givenCheckTask_whenServerHandle_thenOragicServerResponseALiveString() throws Exception {
URI uri = URI.create("http://localhost:8080/task");
ExchangeFunction exchange = ExchangeFunctions.create(new ReactorClientHttpConnector());
ClientRequest request = ClientRequest
.method(HttpMethod.GET, uri)
.body(BodyInserters.fromPublisher(getLatLngs(), Task.class))
.build();
Flux<String> taskResponse = exchange
.exchange(request)
.flatMap(response -> response.bodyToFlux(String.class));
assertThat(taskResponse.blockFirst()).isInstanceOf(String.class);
}
private static Flux<Task> getLatLngs() { private static Flux<Task> getLatLngs() {
return Flux return Flux

View File

@ -10,7 +10,7 @@
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version> <version>1.4.6.RELEASE</version>
</parent> </parent>
<dependencies> <dependencies>

View File

@ -163,6 +163,30 @@ public class RestTemplateBasicLiveTest {
assertThat(foo.getName(), is(updatedInstance.getName())); assertThat(foo.getName(), is(updatedInstance.getName()));
} }
// PATCH
@Test
public void givenFooService_whenPatchExistingEntity_thenItIsUpdated() {
final RestTemplate template = new RestTemplate();
final HttpHeaders headers = prepareBasicAuthHeaders();
final HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"), headers);
// Create Resource
final ResponseEntity<Foo> createResponse = template.exchange(fooResourceUrl, HttpMethod.POST, request, Foo.class);
// Update Resource
final Foo updatedResource = new Foo("newName");
updatedResource.setId(createResponse.getBody().getId());
final String resourceUrl = fooResourceUrl + '/' + createResponse.getBody().getId();
final HttpEntity<Foo> requestUpdate = new HttpEntity<>(updatedResource, headers);
template.patchForObject(resourceUrl, requestUpdate, Void.class);
// Check that Resource was updated
final ResponseEntity<Foo> updateResponse = template.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<>(headers), Foo.class);
final Foo foo = updateResponse.getBody();
assertThat(foo.getName(), is(updatedResource.getName()));
}
// DELETE // DELETE
@Test @Test