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");
}
Aggregations