|
| 1 | +/* |
| 2 | + * Copyright 2019 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.mediatype.alps; |
| 17 | + |
| 18 | +import static org.hamcrest.Matchers.*; |
| 19 | +import static org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType.*; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.context.ApplicationContext; |
| 25 | +import org.springframework.context.annotation.Bean; |
| 26 | +import org.springframework.context.annotation.Configuration; |
| 27 | +import org.springframework.hateoas.MediaTypes; |
| 28 | +import org.springframework.hateoas.config.EnableHypermediaSupport; |
| 29 | +import org.springframework.hateoas.config.WebClientConfigurer; |
| 30 | +import org.springframework.hateoas.support.WebFluxEmployeeController; |
| 31 | +import org.springframework.test.context.ContextConfiguration; |
| 32 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 33 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 34 | +import org.springframework.web.reactive.config.EnableWebFlux; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author Greg Turnquist |
| 38 | + */ |
| 39 | +@ExtendWith(SpringExtension.class) |
| 40 | +@ContextConfiguration |
| 41 | +public class AlpsWebFluxIntegrationTest { |
| 42 | + |
| 43 | + @Autowired WebTestClient testClient; |
| 44 | + |
| 45 | + @Test |
| 46 | + void profileEndpointReturnsAlps() { |
| 47 | + |
| 48 | + this.testClient.get().uri("/profile") // |
| 49 | + .accept(MediaTypes.ALPS_JSON) // |
| 50 | + .exchange() // |
| 51 | + .expectStatus().isOk() // |
| 52 | + .expectBody() // |
| 53 | + .jsonPath("$.version").isEqualTo("1.0") // |
| 54 | + .jsonPath("$.doc.format").isEqualTo("TEXT") // |
| 55 | + .jsonPath("$.doc.href").isEqualTo("https://example.org/samples/full/doc.html") // |
| 56 | + .jsonPath("$.doc.value").isEqualTo("value goes here") // |
| 57 | + .jsonPath("$.descriptor").value(hasSize(2)) // |
| 58 | + |
| 59 | + .jsonPath("$.descriptor[0].id").isEqualTo("class field [name]") // |
| 60 | + .jsonPath("$.descriptor[0].name").isEqualTo("name") // |
| 61 | + .jsonPath("$.descriptor[0].type").isEqualTo("SEMANTIC") // |
| 62 | + .jsonPath("$.descriptor[0].descriptor").value(hasSize(1)) // |
| 63 | + .jsonPath("$.descriptor[0].descriptor[0].id").isEqualTo("embedded") // |
| 64 | + .jsonPath("$.descriptor[0].ext.id").isEqualTo("ext [name]") // |
| 65 | + .jsonPath("$.descriptor[0].ext.href").isEqualTo("https://example.org/samples/ext/name") // |
| 66 | + .jsonPath("$.descriptor[0].ext.value").isEqualTo("value goes here") // |
| 67 | + .jsonPath("$.descriptor[0].rt").isEqualTo("rt for [name]") // |
| 68 | + |
| 69 | + .jsonPath("$.descriptor[1].id").isEqualTo("class field [role]") // |
| 70 | + .jsonPath("$.descriptor[1].name").isEqualTo("role") // |
| 71 | + .jsonPath("$.descriptor[1].type").isEqualTo("SEMANTIC") // |
| 72 | + .jsonPath("$.descriptor[1].descriptor").value(hasSize(1)) // |
| 73 | + .jsonPath("$.descriptor[1].descriptor[0].id").isEqualTo("embedded") // |
| 74 | + .jsonPath("$.descriptor[1].ext.id").isEqualTo("ext [role]") // |
| 75 | + .jsonPath("$.descriptor[1].ext.href").isEqualTo("https://example.org/samples/ext/role") // |
| 76 | + .jsonPath("$.descriptor[1].ext.value").isEqualTo("value goes here") // |
| 77 | + .jsonPath("$.descriptor[1].rt").isEqualTo("rt for [role]"); |
| 78 | + } |
| 79 | + |
| 80 | + @Configuration |
| 81 | + @EnableWebFlux |
| 82 | + @EnableHypermediaSupport(type = HAL) |
| 83 | + static class TestConfig { |
| 84 | + |
| 85 | + @Bean |
| 86 | + WebFluxEmployeeController employeeController() { |
| 87 | + return new WebFluxEmployeeController(); |
| 88 | + } |
| 89 | + |
| 90 | + @Bean |
| 91 | + WebTestClient webTestClient(WebClientConfigurer webClientConfigurer, ApplicationContext ctx) { |
| 92 | + |
| 93 | + return WebTestClient.bindToApplicationContext(ctx).build() // |
| 94 | + .mutate() // |
| 95 | + .exchangeStrategies(webClientConfigurer.hypermediaExchangeStrategies()) // |
| 96 | + .build(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments