|
| 1 | +/* |
| 2 | + * Copyright 2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.hateoas.server.reactive; |
| 17 | + |
| 18 | +import static org.assertj.core.api.AssertionsForInterfaceTypes.*; |
| 19 | +import static org.springframework.hateoas.server.reactive.WebFluxLinkBuilder.*; |
| 20 | + |
| 21 | +import reactor.core.publisher.Mono; |
| 22 | +import reactor.test.StepVerifier; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | +import org.springframework.context.ApplicationContext; |
| 28 | +import org.springframework.context.annotation.Bean; |
| 29 | +import org.springframework.context.annotation.Configuration; |
| 30 | +import org.springframework.hateoas.IanaLinkRelations; |
| 31 | +import org.springframework.hateoas.Link; |
| 32 | +import org.springframework.hateoas.MediaTypes; |
| 33 | +import org.springframework.hateoas.RepresentationModel; |
| 34 | +import org.springframework.hateoas.config.EnableHypermediaSupport; |
| 35 | +import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType; |
| 36 | +import org.springframework.hateoas.config.WebClientConfigurer; |
| 37 | +import org.springframework.test.context.ContextConfiguration; |
| 38 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 39 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 40 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 41 | +import org.springframework.web.bind.annotation.GetMapping; |
| 42 | +import org.springframework.web.bind.annotation.RequestParam; |
| 43 | +import org.springframework.web.bind.annotation.RestController; |
| 44 | +import org.springframework.web.reactive.config.EnableWebFlux; |
| 45 | + |
| 46 | +/** |
| 47 | + * @author Greg Turnquist |
| 48 | + */ |
| 49 | +@ExtendWith(SpringExtension.class) |
| 50 | +@WebAppConfiguration |
| 51 | +@ContextConfiguration |
| 52 | +public class WebFluxLinkBuilderInterfaceClassTest { |
| 53 | + |
| 54 | + @Autowired WebTestClient testClient; |
| 55 | + |
| 56 | + @Test |
| 57 | + void parentInterfaceCanHoldSpringWebAnnotations() throws Exception { |
| 58 | + |
| 59 | + this.testClient.get().uri("http://example.com/api?view=short") // |
| 60 | + .accept(MediaTypes.HAL_JSON) // |
| 61 | + .exchange() // |
| 62 | + .expectStatus().isOk() // |
| 63 | + .expectHeader().contentType(MediaTypes.HAL_JSON) // |
| 64 | + .returnResult(RepresentationModel.class) // |
| 65 | + .getResponseBody() // |
| 66 | + .as(StepVerifier::create) // |
| 67 | + .expectNextMatches(resourceSupport -> { |
| 68 | + |
| 69 | + assertThat(resourceSupport.getLinks())// |
| 70 | + .containsExactly(Link.of("http://example.com/api?view=short", IanaLinkRelations.SELF)); |
| 71 | + |
| 72 | + return true; |
| 73 | + }) // |
| 74 | + .verifyComplete(); |
| 75 | + } |
| 76 | + |
| 77 | + interface WebFluxInterface { |
| 78 | + |
| 79 | + @GetMapping("/api") |
| 80 | + Mono<RepresentationModel<?>> root(@RequestParam String view); |
| 81 | + } |
| 82 | + |
| 83 | + @RestController |
| 84 | + static class WebFluxClass implements WebFluxInterface { |
| 85 | + |
| 86 | + @Override |
| 87 | + public Mono<RepresentationModel<?>> root(String view) { |
| 88 | + |
| 89 | + Mono<Link> selfLink = linkTo(methodOn(WebFluxClass.class).root(view)).withSelfRel().toMono(); |
| 90 | + |
| 91 | + return selfLink.map(RepresentationModel::new); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Configuration |
| 96 | + @EnableWebFlux |
| 97 | + @EnableHypermediaSupport(type = { HypermediaType.HAL }) |
| 98 | + static class TestConfig { |
| 99 | + |
| 100 | + @Bean |
| 101 | + WebFluxClass concreteController() { |
| 102 | + return new WebFluxClass(); |
| 103 | + } |
| 104 | + |
| 105 | + @Bean |
| 106 | + WebTestClient webTestClient(WebClientConfigurer webClientConfigurer, ApplicationContext ctx) { |
| 107 | + |
| 108 | + return WebTestClient.bindToApplicationContext(ctx).build().mutate() |
| 109 | + .exchangeStrategies(webClientConfigurer.hypermediaExchangeStrategies()).build(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments