Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reactive and coroutine support for Kotlin #2228

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.springframework.hateoas.reactive;

import org.springframework.hateoas.EntityModel;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import static org.springframework.hateoas.server.reactive.WebFluxLinkBuilder.linkTo;
import static org.springframework.hateoas.server.reactive.WebFluxLinkBuilder.methodOn;

@Controller
@RequestMapping("/people")
class PersonController {

@GetMapping
Flux<PersonModel> showAll() {
return Flux.just(new PersonModel(new Person(1L)));
}

@GetMapping("/{person}")
Mono<PersonModel> show(@PathVariable Long person) {
return Mono.just(new PersonModel(new Person(person)));
}

private Mono<ResponseEntity<PersonModel>> getHeader(Long person) {
var baseLink = linkTo(methodOn(PersonController.class).showAll());
return baseLink.slash(person.toString()).withSelfRel().toMono()
.map(l -> {
HttpHeaders headers = new HttpHeaders();
headers.setLocation(l.toUri());
return new ResponseEntity<>(headers, HttpStatus.CREATED);
});
}

record Person(Long id) {
}

static class PersonModel extends EntityModel<Person> {
public PersonModel(Person person) {
super(person);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.springframework.hateoas.reactive;

import org.junit.jupiter.api.Test;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkRelation;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.springframework.hateoas.server.reactive.WebFluxLinkBuilder.linkTo;
import static org.springframework.hateoas.server.reactive.WebFluxLinkBuilder.methodOn;

class PersonControllerTest {
@Test
public void testLink() {
Mono<Link> link = linkTo(methodOn(PersonController.class).showAll()).withRel("people").toMono();

StepVerifier.create(link)
.expectNextMatches(l -> {
assertThat(l.getRel()).isEqualTo(LinkRelation.of("people"));
assertThat(l.getHref()).endsWith("/people");
return true;
})
.verifyComplete();
}

@Test
public void testLink2() {
var person = new PersonController.Person(1L);
// /people
var baseLink = linkTo(methodOn(PersonController.class).showAll());
// / 1
var link = baseLink.slash(person.id().toString()).withSelfRel().toMono();

StepVerifier.create(link)
.expectNextMatches(l -> {
assertThat(l.getRel()).isEqualTo(IanaLinkRelations.SELF);
assertThat(l.getHref()).endsWith("/people/1");
return true;
})
.verifyComplete();
}

@Test
public void testLink3() {
var link = linkTo(methodOn(PersonController.class).show(1L))
.withSelfRel().toMono();

StepVerifier.create(link)
.expectNextMatches(l -> {
assertThat(l.getRel()).isEqualTo(IanaLinkRelations.SELF);
assertThat(l.getHref()).endsWith("/people/1");
return true;
})
.verifyComplete();
}
}
Loading