|
| 1 | +/* |
| 2 | + * Copyright 2012-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 | + |
| 17 | +package org.springframework.boot.test.autoconfigure.hateoas; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 21 | +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; |
| 22 | +import org.springframework.boot.test.autoconfigure.web.reactive.WebTestClientAutoConfiguration; |
| 23 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 24 | +import org.springframework.context.annotation.Bean; |
| 25 | +import org.springframework.context.annotation.Configuration; |
| 26 | +import org.springframework.hateoas.MediaTypes; |
| 27 | +import org.springframework.hateoas.config.EnableHypermediaSupport; |
| 28 | +import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType; |
| 29 | +import org.springframework.http.MediaType; |
| 30 | +import org.springframework.http.codec.HttpMessageReader; |
| 31 | +import org.springframework.http.codec.HttpMessageWriter; |
| 32 | +import org.springframework.test.util.ReflectionTestUtils; |
| 33 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 34 | +import org.springframework.web.reactive.function.client.ExchangeStrategies; |
| 35 | +import org.springframework.web.reactive.function.client.WebClient; |
| 36 | +import org.springframework.web.server.WebHandler; |
| 37 | + |
| 38 | +import static org.assertj.core.api.Assertions.assertThat; |
| 39 | +import static org.mockito.Mockito.mock; |
| 40 | + |
| 41 | +/** |
| 42 | + * Tests for hypermedia-based {@link WebTestClient}. |
| 43 | + * |
| 44 | + * @author Brian Clozel |
| 45 | + * @author Stephane Nicoll |
| 46 | + * @author Greg Turnquist |
| 47 | + */ |
| 48 | +class HypermediaTestAutoConfigurationTests { |
| 49 | + |
| 50 | + private ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 51 | + .withConfiguration(AutoConfigurations.of(WebTestClientAutoConfiguration.class)); |
| 52 | + |
| 53 | + @Test |
| 54 | + void codecsCustomizerShouldRegisterHypermediaTypesWithWebTestClient() { |
| 55 | + this.contextRunner.withUserConfiguration(BaseConfiguration.class).run((context) -> { |
| 56 | + WebTestClient webTestClient = context.getBean(WebTestClient.class); |
| 57 | + assertWebTestClientHasHypermedia(webTestClient, MediaTypes.HAL_JSON); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void codecsCustomizerShouldRegisterAlternativeHypermediaTypesWithWebTestClient() { |
| 63 | + this.contextRunner.withUserConfiguration(HalFormsConfiguration.class).run((context) -> { |
| 64 | + WebTestClient webTestClient = context.getBean(WebTestClient.class); |
| 65 | + assertWebTestClientHasHypermedia(webTestClient, MediaTypes.HAL_FORMS_JSON); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void codecsCustomizerShouldRegisterAllHypermediaTypesWithWebTestClient() { |
| 71 | + this.contextRunner.withUserConfiguration(AllHypermediaTypesConfiguration.class).run((context) -> { |
| 72 | + WebTestClient webTestClient = context.getBean(WebTestClient.class); |
| 73 | + assertWebTestClientHasHypermedia(webTestClient, MediaTypes.HAL_JSON, MediaTypes.HAL_FORMS_JSON, |
| 74 | + MediaTypes.COLLECTION_JSON, MediaTypes.UBER_JSON); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + private static void assertWebTestClientHasHypermedia(WebTestClient webTestClient, MediaType... mediaTypes) { |
| 79 | + |
| 80 | + WebClient webClient = (WebClient) ReflectionTestUtils.getField(webTestClient, "webClient"); |
| 81 | + ExchangeStrategies strategies = (ExchangeStrategies) ReflectionTestUtils |
| 82 | + .getField(ReflectionTestUtils.getField(webClient, "exchangeFunction"), "strategies"); |
| 83 | + |
| 84 | + assertThat(strategies.messageReaders()).flatExtracting(HttpMessageReader::getReadableMediaTypes) |
| 85 | + .contains(mediaTypes); |
| 86 | + assertThat(strategies.messageWriters()).flatExtracting(HttpMessageWriter::getWritableMediaTypes) |
| 87 | + .contains(mediaTypes); |
| 88 | + } |
| 89 | + |
| 90 | + @ImportAutoConfiguration(HypermediaTestAutoConfiguration.class) |
| 91 | + @Configuration(proxyBeanMethods = false) |
| 92 | + @EnableHypermediaSupport(type = HypermediaType.HAL) |
| 93 | + static class BaseConfiguration { |
| 94 | + |
| 95 | + @Bean |
| 96 | + WebHandler webHandler() { |
| 97 | + return mock(WebHandler.class); |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + @ImportAutoConfiguration(HypermediaTestAutoConfiguration.class) |
| 103 | + @Configuration(proxyBeanMethods = false) |
| 104 | + @EnableHypermediaSupport(type = HypermediaType.HAL_FORMS) |
| 105 | + static class HalFormsConfiguration { |
| 106 | + |
| 107 | + @Bean |
| 108 | + WebHandler webHandler() { |
| 109 | + return mock(WebHandler.class); |
| 110 | + } |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + @ImportAutoConfiguration(HypermediaTestAutoConfiguration.class) |
| 115 | + @Configuration(proxyBeanMethods = false) |
| 116 | + @EnableHypermediaSupport(type = { HypermediaType.HAL, HypermediaType.HAL_FORMS, HypermediaType.COLLECTION_JSON, |
| 117 | + HypermediaType.UBER }) |
| 118 | + static class AllHypermediaTypesConfiguration { |
| 119 | + |
| 120 | + @Bean |
| 121 | + WebHandler webHandler() { |
| 122 | + return mock(WebHandler.class); |
| 123 | + } |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments