Leaving out hamcrest for now, to address more broadly.

Issue: SPR-16741
This commit is contained in:
Rossen Stoyanchev
2018-07-20 10:36:57 -04:00
parent 2734f01067
commit 0c62d6b5da
5 changed files with 117 additions and 191 deletions

View File

@@ -15,18 +15,30 @@
*/
package org.springframework.test.web.reactive.server.samples;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
@XmlRootElement
class Person {
private final String name;
private String name;
// No-arg constructor for XML
public Person() {
}
@JsonCreator
public Person(@JsonProperty("name") String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}

View File

@@ -20,24 +20,17 @@ import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import org.hamcrest.Matchers;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
@@ -46,30 +39,32 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Samples of tests using {@link WebTestClient} with serialized XML content.
* Samples of tests using {@link WebTestClient} with XML content.
*
* @author Eric Deandrea
* @since 5.1
*/
public class XmlContentTests {
private static final String PEOPLE_XML =
private static final String persons_XML =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
+ "<people><people>"
+ "<persons>"
+ "<person><name>Jane</name></person>"
+ "<person><name>Jason</name></person>"
+ "<person><name>John</name></person>"
+ "</people></people>";
+ "</persons>";
private final WebTestClient client = WebTestClient.bindToController(new PersonController()).build();
@Test
public void xmlContent() {
this.client.get().uri("/persons")
.accept(MediaType.APPLICATION_XML)
.exchange()
.expectStatus().isOk()
.expectBody().xml(PEOPLE_XML);
.expectBody().xml(persons_XML);
}
@Test
@@ -80,28 +75,12 @@ public class XmlContentTests {
.expectStatus().isOk()
.expectBody()
.xpath("/").exists()
.xpath("/people").exists()
.xpath("/people/people").exists()
.xpath("/people/people/person").exists()
.xpath("/people/people/person").nodeCount(3)
.xpath("/people/people/person[1]/name").isEqualTo("Jane")
.xpath("/people/people/person[2]/name").isEqualTo("Jason")
.xpath("/people/people/person[3]/name").isEqualTo("John");
}
@Test
public void xpathMatches() {
this.client.get().uri("/persons")
.accept(MediaType.APPLICATION_XML)
.exchange()
.expectStatus().isOk()
.expectBody()
.xpath("/").exists()
.xpath("/people").exists()
.xpath("/people/people").exists()
.xpath("/people/people/person").exists()
.xpath("/people/people/person").nodeCount(3)
.xpath("//person/name").matchesString(Matchers.startsWith("J"));
.xpath("/persons").exists()
.xpath("/persons/person").exists()
.xpath("/persons/person").nodeCount(3)
.xpath("/persons/person[1]/name").isEqualTo("Jane")
.xpath("/persons/person[2]/name").isEqualTo("Jason")
.xpath("/persons/person[3]/name").isEqualTo("John");
}
@Test
@@ -116,86 +95,42 @@ public class XmlContentTests {
@Test
public void postXmlContent() {
String content =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"<person><name>John</name></person>";
this.client.post().uri("/persons")
.contentType(MediaType.APPLICATION_XML)
.syncBody("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><person><name>John</name></person>")
.syncBody(content)
.exchange()
.expectStatus().isCreated()
.expectHeader().valueEquals(HttpHeaders.LOCATION, "/persons/John")
.expectBody().isEmpty();
}
@XmlRootElement
private static class Person {
@NotNull
private String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Person name(String name) {
setName(name);
return this;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof Person)) {
return false;
}
Person otherPerson = (Person) other;
return ObjectUtils.nullSafeEquals(this.name, otherPerson.name);
}
@Override
public int hashCode() {
return Person.class.hashCode();
}
@Override
public String toString() {
return "Person [name=" + this.name + "]";
}
}
@SuppressWarnings("unused")
@XmlRootElement(name="people")
@XmlRootElement(name="persons")
@XmlAccessorType(XmlAccessType.FIELD)
private static class PeopleWrapper {
private static class PersonsWrapper {
@XmlElementWrapper(name="people")
@XmlElement(name="person")
private final List<Person> people = new ArrayList<>();
private final List<Person> persons = new ArrayList<>();
public PeopleWrapper() {
public PersonsWrapper() {
}
public PeopleWrapper(List<Person> people) {
this.people.addAll(people);
public PersonsWrapper(List<Person> persons) {
this.persons.addAll(persons);
}
public PeopleWrapper(Person... people) {
this.people.addAll(Arrays.asList(people));
public PersonsWrapper(Person... persons) {
this.persons.addAll(Arrays.asList(persons));
}
public List<Person> getPeople() {
return this.people;
public List<Person> getpersons() {
return this.persons;
}
}
@@ -204,24 +139,19 @@ public class XmlContentTests {
static class PersonController {
@GetMapping(produces = MediaType.APPLICATION_XML_VALUE)
Mono<PeopleWrapper> getPersons() {
return Mono.just(new PeopleWrapper(new Person("Jane"), new Person("Jason"), new Person("John")));
PersonsWrapper getPersons() {
return new PersonsWrapper(new Person("Jane"), new Person("Jason"), new Person("John"));
}
@GetMapping(path = "/{name}", produces = MediaType.APPLICATION_XML_VALUE)
Mono<Person> getPerson(@PathVariable String name) {
return Mono.just(new Person(name));
Person getPerson(@PathVariable String name) {
return new Person(name);
}
@PostMapping(consumes = MediaType.APPLICATION_XML_VALUE)
Mono<ResponseEntity<Object>> savePeople(@RequestBody Flux<Person> person) {
return person
.map(Person::getName)
.map(name -> String.format("/persons/%s", name))
.map(URI::create)
.map(ResponseEntity::created)
.map(ResponseEntity.BodyBuilder::build)
.next();
ResponseEntity<Object> savepersons(@RequestBody Person person) {
URI location = URI.create(String.format("/persons/%s", person.getName()));
return ResponseEntity.created(location).build();
}
}