@SuppressWarnings("unchecked") @Test public void initBinderMethod() throws Exception { Validator validator = mock(Validator.class); TestController controller = new TestController(); controller.setValidator(validator); InitBinderBindingContext context = getBindingContext(controller); Method method = ResolvableMethod.on(TestController.class).annotPresent(GetMapping.class).resolveMethod(); HandlerMethod handlerMethod = new HandlerMethod(controller, method); this.modelInitializer.initModel(handlerMethod, context, this.exchange).block(Duration.ofMillis(5000)); WebExchangeDataBinder binder = context.createDataBinder(this.exchange, "name"); assertEquals(Collections.singletonList(validator), binder.getValidators()); }
@SuppressWarnings("unchecked") @Test public void modelAttributeMethods() throws Exception { TestController controller = new TestController(); InitBinderBindingContext context = getBindingContext(controller); Method method = ResolvableMethod.on(TestController.class).annotPresent(GetMapping.class).resolveMethod(); HandlerMethod handlerMethod = new HandlerMethod(controller, method); this.modelInitializer.initModel(handlerMethod, context, this.exchange).block(Duration.ofMillis(5000)); Map<String, Object> model = context.getModel().asMap(); assertEquals(5, model.size()); Object value = model.get("bean"); assertEquals("Bean", ((TestBean) value).getName()); value = model.get("monoBean"); assertEquals("Mono Bean", ((Mono<TestBean>) value).block(Duration.ofMillis(5000)).getName()); value = model.get("singleBean"); assertEquals("Single Bean", ((Single<TestBean>) value).toBlocking().value().getName()); value = model.get("voidMethodBean"); assertEquals("Void Method Bean", ((TestBean) value).getName()); value = model.get("voidMonoMethodBean"); assertEquals("Void Mono Method Bean", ((TestBean) value).getName()); }
@Test public void requiredSessionAttributeMissing() throws Exception { TestController controller = new TestController(); InitBinderBindingContext context = getBindingContext(controller); Method method = ResolvableMethod.on(TestController.class).annotPresent(PostMapping.class).resolveMethod(); HandlerMethod handlerMethod = new HandlerMethod(controller, method); try { this.modelInitializer.initModel(handlerMethod, context, this.exchange).block(Duration.ofMillis(5000)); fail(); } catch (IllegalArgumentException ex) { assertEquals("Required attribute 'missing-bean' is missing.", ex.getMessage()); } }
@Test public void clearModelAttributeFromSession() throws Exception { WebSession session = this.exchange.getSession().block(Duration.ZERO); assertNotNull(session); TestBean testBean = new TestBean("Session Bean"); session.getAttributes().put("bean", testBean); TestController controller = new TestController(); InitBinderBindingContext context = getBindingContext(controller); Method method = ResolvableMethod.on(TestController.class).annotPresent(GetMapping.class).resolveMethod(); HandlerMethod handlerMethod = new HandlerMethod(controller, method); this.modelInitializer.initModel(handlerMethod, context, this.exchange).block(Duration.ofMillis(5000)); context.getSessionStatus().setComplete(); context.saveModel(); assertEquals(0, session.getAttributes().size()); }
@Test public void retrieveModelAttributeFromSession() throws Exception { WebSession session = this.exchange.getSession().block(Duration.ZERO); assertNotNull(session); TestBean testBean = new TestBean("Session Bean"); session.getAttributes().put("bean", testBean); TestController controller = new TestController(); InitBinderBindingContext context = getBindingContext(controller); Method method = ResolvableMethod.on(TestController.class).annotPresent(GetMapping.class).resolveMethod(); HandlerMethod handlerMethod = new HandlerMethod(controller, method); this.modelInitializer.initModel(handlerMethod, context, this.exchange).block(Duration.ofMillis(5000)); context.saveModel(); assertEquals(1, session.getAttributes().size()); assertEquals("Session Bean", ((TestBean) session.getRequiredAttribute("bean")).getName()); }
@Test public void saveModelAttributeToSession() throws Exception { TestController controller = new TestController(); InitBinderBindingContext context = getBindingContext(controller); Method method = ResolvableMethod.on(TestController.class).annotPresent(GetMapping.class).resolveMethod(); HandlerMethod handlerMethod = new HandlerMethod(controller, method); this.modelInitializer.initModel(handlerMethod, context, this.exchange).block(Duration.ofMillis(5000)); WebSession session = this.exchange.getSession().block(Duration.ZERO); assertNotNull(session); assertEquals(0, session.getAttributes().size()); context.saveModel(); assertEquals(1, session.getAttributes().size()); assertEquals("Bean", ((TestBean) session.getRequiredAttribute("bean")).getName()); }
@Test public void supportsRestController() throws NoSuchMethodException { Object controller = new TestRestController(); Method method; method = on(TestRestController.class).returning(String.class).resolveMethod(); testSupports(controller, method); method = on(TestRestController.class).returning(Mono.class, String.class).resolveMethod(); testSupports(controller, method); method = on(TestRestController.class).returning(Single.class, String.class).resolveMethod(); testSupports(controller, method); method = on(TestRestController.class).returning(Completable.class).resolveMethod(); testSupports(controller, method); }
@Test public void getHandlerBestMatch() { Method expected = on(TestController.class).annot(getMapping("/foo").params("p")).resolveMethod(); ServerWebExchange exchange = MockServerWebExchange.from(get("/foo?p=anything")); HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); assertEquals(expected, hm.getMethod()); }
@Test public void getHandlerGlobMatch() { Method expected = on(TestController.class).annot(requestMapping("/ba*").method(GET, HEAD)).resolveMethod(); ServerWebExchange exchange = MockServerWebExchange.from(get("/bar")); HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); assertEquals(expected, hm.getMethod()); }
@Test public void supports() throws NoSuchMethodException { Object controller = new TestController(); Method method; method = on(TestController.class).annotPresent(ResponseBody.class).resolveMethod(); testSupports(controller, method); method = on(TestController.class).annotNotPresent(ResponseBody.class).resolveMethod("doWork"); HandlerResult handlerResult = getHandlerResult(controller, method); assertFalse(this.resultHandler.supports(handlerResult)); }
@Test public void getHandlerEmptyPathMatch() { Method expected = on(TestController.class).annot(requestMapping("")).resolveMethod(); ServerWebExchange exchange = MockServerWebExchange.from(get("")); HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); assertEquals(expected, hm.getMethod()); exchange = MockServerWebExchange.from(get("/")); hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); assertEquals(expected, hm.getMethod()); }
@Test public void getHandlerDirectMatch() { Method expected = on(TestController.class).annot(getMapping("/foo").params()).resolveMethod(); ServerWebExchange exchange = MockServerWebExchange.from(get("/foo")); HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); assertEquals(expected, hm.getMethod()); }
private InvocableHandlerMethod getInvocable(Class<?>... argTypes) { Method method = ResolvableMethod.on(Handler.class).argTypes(argTypes).resolveMethod(); InvocableHandlerMethod handlerMethod = new InvocableHandlerMethod(new Handler(), method); handlerMethod.setHandlerMethodArgumentResolvers(this.composite); return handlerMethod; }