|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 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.web.reactive.result.method.annotation |
| 18 | + |
| 19 | +import kotlinx.coroutines.delay |
| 20 | +import org.assertj.core.api.Assertions |
| 21 | +import org.junit.jupiter.api.BeforeEach |
| 22 | +import org.junit.jupiter.api.Test |
| 23 | +import org.springframework.context.support.StaticApplicationContext |
| 24 | +import org.springframework.core.ReactiveAdapterRegistry |
| 25 | +import org.springframework.ui.Model |
| 26 | +import org.springframework.web.bind.annotation.GetMapping |
| 27 | +import org.springframework.web.bind.annotation.ModelAttribute |
| 28 | +import org.springframework.web.bind.support.ConfigurableWebBindingInitializer |
| 29 | +import org.springframework.web.method.HandlerMethod |
| 30 | +import org.springframework.web.server.ServerWebExchange |
| 31 | +import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest |
| 32 | +import org.springframework.web.testfixture.method.ResolvableMethod |
| 33 | +import org.springframework.web.testfixture.server.MockServerWebExchange |
| 34 | +import reactor.core.publisher.Mono |
| 35 | +import java.time.Duration |
| 36 | + |
| 37 | +/** |
| 38 | + * Kotlin test fixture for [ModelInitializer]. |
| 39 | + * |
| 40 | + * @author Sebastien Deleuze |
| 41 | + */ |
| 42 | +class ModelInitializerKotlinTests { |
| 43 | + |
| 44 | + private val timeout = Duration.ofMillis(5000) |
| 45 | + |
| 46 | + private lateinit var modelInitializer: ModelInitializer |
| 47 | + |
| 48 | + private val exchange: ServerWebExchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path")) |
| 49 | + |
| 50 | + @BeforeEach |
| 51 | + fun setup() { |
| 52 | + val adapterRegistry = ReactiveAdapterRegistry.getSharedInstance() |
| 53 | + val resolverConfigurer = ArgumentResolverConfigurer() |
| 54 | + resolverConfigurer.addCustomResolver(ModelMethodArgumentResolver(adapterRegistry)) |
| 55 | + val methodResolver = ControllerMethodResolver(resolverConfigurer, adapterRegistry, StaticApplicationContext(), |
| 56 | + emptyList()) |
| 57 | + modelInitializer = ModelInitializer(methodResolver, adapterRegistry) |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + @Suppress("UNCHECKED_CAST") |
| 62 | + fun modelAttributeMethods() { |
| 63 | + val controller = TestController() |
| 64 | + val method = ResolvableMethod.on(TestController::class.java).annotPresent(GetMapping::class.java) |
| 65 | + .resolveMethod() |
| 66 | + val handlerMethod = HandlerMethod(controller, method) |
| 67 | + val context = InitBinderBindingContext(ConfigurableWebBindingInitializer(), emptyList()) |
| 68 | + this.modelInitializer.initModel(handlerMethod, context, this.exchange).block(timeout) |
| 69 | + val model = context.model.asMap() |
| 70 | + Assertions.assertThat(model).hasSize(2) |
| 71 | + val monoValue = model["suspendingReturnValue"] as Mono<TestBean> |
| 72 | + Assertions.assertThat(monoValue.block(timeout)!!.name).isEqualTo("Suspending return value") |
| 73 | + val value = model["suspendingModelParameter"] as TestBean |
| 74 | + Assertions.assertThat(value.name).isEqualTo("Suspending model parameter") |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + private data class TestBean(val name: String) |
| 79 | + |
| 80 | + private class TestController { |
| 81 | + |
| 82 | + @ModelAttribute("suspendingReturnValue") |
| 83 | + suspend fun suspendingReturnValue(): TestBean { |
| 84 | + delay(1) |
| 85 | + return TestBean("Suspending return value") |
| 86 | + } |
| 87 | + |
| 88 | + @ModelAttribute |
| 89 | + suspend fun suspendingModelParameter(model: Model) { |
| 90 | + delay(1) |
| 91 | + model.addAttribute("suspendingModelParameter", TestBean("Suspending model parameter")) |
| 92 | + } |
| 93 | + |
| 94 | + @GetMapping |
| 95 | + fun handleGet() { |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments