Search in sources :

Example 1 with ConcurrentModel

use of org.springframework.ui.ConcurrentModel in project spring-framework by spring-projects.

the class ViewResolutionResultHandlerTests method handleReturnValueTypes.

@Test
public void handleReturnValueTypes() {
    Object returnValue;
    MethodParameter returnType;
    ViewResolver resolver = new TestViewResolver("account");
    returnType = on(Handler.class).resolveReturnType(View.class);
    returnValue = new TestView("account");
    testHandle("/path", returnType, returnValue, "account: {id=123}");
    returnType = on(Handler.class).resolveReturnType(Mono.class, View.class);
    returnValue = Mono.just(new TestView("account"));
    testHandle("/path", returnType, returnValue, "account: {id=123}");
    returnType = on(Handler.class).annotNotPresent(ModelAttribute.class).resolveReturnType(String.class);
    returnValue = "account";
    testHandle("/path", returnType, returnValue, "account: {id=123}", resolver);
    returnType = on(Handler.class).annotPresent(ModelAttribute.class).resolveReturnType(String.class);
    returnValue = "123";
    testHandle("/account", returnType, returnValue, "account: {id=123, myString=123}", resolver);
    returnType = on(Handler.class).resolveReturnType(Mono.class, String.class);
    returnValue = Mono.just("account");
    testHandle("/path", returnType, returnValue, "account: {id=123}", resolver);
    returnType = on(Handler.class).resolveReturnType(Model.class);
    returnValue = new ConcurrentModel().addAttribute("name", "Joe").addAttribute("ignore", null);
    testHandle("/account", returnType, returnValue, "account: {id=123, name=Joe}", resolver);
    // Work around  caching issue...
    ResolvableType.clearCache();
    returnType = on(Handler.class).annotNotPresent(ModelAttribute.class).resolveReturnType(Map.class);
    returnValue = Collections.singletonMap("name", "Joe");
    testHandle("/account", returnType, returnValue, "account: {id=123, name=Joe}", resolver);
    // Work around  caching issue...
    ResolvableType.clearCache();
    returnType = on(Handler.class).annotPresent(ModelAttribute.class).resolveReturnType(Map.class);
    returnValue = Collections.singletonMap("name", "Joe");
    testHandle("/account", returnType, returnValue, "account: {id=123, myMap={name=Joe}}", resolver);
    returnType = on(Handler.class).resolveReturnType(TestBean.class);
    returnValue = new TestBean("Joe");
    String responseBody = "account: {id=123, " + "org.springframework.validation.BindingResult.testBean=" + "org.springframework.validation.BeanPropertyBindingResult: 0 errors, " + "testBean=TestBean[name=Joe]}";
    testHandle("/account", returnType, returnValue, responseBody, resolver);
    returnType = on(Handler.class).annotPresent(ModelAttribute.class).resolveReturnType(Long.class);
    testHandle("/account", returnType, 99L, "account: {id=123, myLong=99}", resolver);
    returnType = on(Handler.class).resolveReturnType(Rendering.class);
    HttpStatus status = HttpStatus.UNPROCESSABLE_ENTITY;
    returnValue = Rendering.view("account").modelAttribute("a", "a1").status(status).header("h", "h1").build();
    String expected = "account: {a=a1, id=123}";
    ServerWebExchange exchange = testHandle("/path", returnType, returnValue, expected, resolver);
    assertThat(exchange.getResponse().getStatusCode()).isEqualTo(status);
    assertThat(exchange.getResponse().getHeaders().getFirst("h")).isEqualTo("h1");
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) ConcurrentModel(org.springframework.ui.ConcurrentModel) HttpStatus(org.springframework.http.HttpStatus) Mono(reactor.core.publisher.Mono) ConcurrentModel(org.springframework.ui.ConcurrentModel) Model(org.springframework.ui.Model) MethodParameter(org.springframework.core.MethodParameter) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) Test(org.junit.jupiter.api.Test)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Test (org.junit.jupiter.api.Test)1 MethodParameter (org.springframework.core.MethodParameter)1 HttpStatus (org.springframework.http.HttpStatus)1 ConcurrentModel (org.springframework.ui.ConcurrentModel)1 Model (org.springframework.ui.Model)1 ServerWebExchange (org.springframework.web.server.ServerWebExchange)1 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)1 Mono (reactor.core.publisher.Mono)1