Search in sources :

Example 16 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class ModelAttributeMethodArgumentResolverTests method bindExistingMonoToMono.

@Test
public void bindExistingMonoToMono() throws Exception {
    Foo foo = new Foo();
    foo.setName("Jim");
    this.bindContext.getModel().addAttribute("foo", Mono.just(foo));
    MethodParameter parameter = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Mono.class, Foo.class);
    testBindFoo(parameter, mono -> {
        assertTrue(mono.getClass().getName(), mono instanceof Mono);
        Object value = ((Mono<?>) mono).block(Duration.ofSeconds(5));
        assertEquals(Foo.class, value.getClass());
        return (Foo) value;
    });
}
Also used : Mono(reactor.core.publisher.Mono) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 17 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class ModelInitializerTests method basic.

@SuppressWarnings("unchecked")
@Test
public void basic() throws Exception {
    TestController controller = new TestController();
    Validator validator = mock(Validator.class);
    controller.setValidator(validator);
    List<SyncInvocableHandlerMethod> binderMethods = getBinderMethods(controller);
    List<InvocableHandlerMethod> attributeMethods = getAttributeMethods(controller);
    WebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
    BindingContext bindingContext = new InitBinderBindingContext(bindingInitializer, binderMethods);
    this.modelInitializer.initModel(bindingContext, attributeMethods, this.exchange).block(Duration.ofMillis(5000));
    WebExchangeDataBinder binder = bindingContext.createDataBinder(this.exchange, "name");
    assertEquals(Collections.singletonList(validator), binder.getValidators());
    Map<String, Object> model = bindingContext.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());
}
Also used : InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) Mono(reactor.core.publisher.Mono) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) BindingContext(org.springframework.web.reactive.BindingContext) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) Validator(org.springframework.validation.Validator) WebBindingInitializer(org.springframework.web.bind.support.WebBindingInitializer) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) Test(org.junit.Test)

Example 18 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class RequestBodyArgumentResolverTests method emptyBodyWithMono.

@Test
@SuppressWarnings("unchecked")
public void emptyBodyWithMono() throws Exception {
    MethodParameter param = this.testMethod.annot(requestBody()).arg(Mono.class, String.class);
    StepVerifier.create((Mono<Void>) resolveValueWithEmptyBody(param)).expectNextCount(0).expectError(ServerWebInputException.class).verify();
    param = this.testMethod.annot(requestBody().notRequired()).arg(Mono.class, String.class);
    StepVerifier.create((Mono<Void>) resolveValueWithEmptyBody(param)).expectNextCount(0).expectComplete().verify();
}
Also used : ServerWebInputException(org.springframework.web.server.ServerWebInputException) Mono(reactor.core.publisher.Mono) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 19 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class InvocableHandlerMethodTests method invokeMethodWithValue.

@Test
public void invokeMethodWithValue() throws Exception {
    Mono<Object> resolvedValue = Mono.just("value1");
    Method method = on(TestController.class).mockCall(o -> o.singleArg(null)).method();
    Mono<HandlerResult> mono = invoke(new TestController(), method, resolverFor(resolvedValue));
    assertHandlerResultValue(mono, "success:value1");
}
Also used : Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) ResolvableMethod.on(org.springframework.web.method.ResolvableMethod.on) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) BindingContext(org.springframework.web.reactive.BindingContext) Mockito.when(org.mockito.Mockito.when) HandlerResult(org.springframework.web.reactive.HandlerResult) Assert.assertThat(org.junit.Assert.assertThat) HttpStatus(org.springframework.http.HttpStatus) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) UnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) Assert.fail(org.junit.Assert.fail) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) Mockito.any(org.mockito.Mockito.any) Method(java.lang.reflect.Method) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) HandlerResult(org.springframework.web.reactive.HandlerResult) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 20 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class HttpEntityArgumentResolverTests method httpEntityWithMonoBody.

@Test
public void httpEntityWithMonoBody() throws Exception {
    ServerWebExchange exchange = postExchange("line1");
    ResolvableType type = httpEntityType(Mono.class, String.class);
    HttpEntity<Mono<String>> httpEntity = resolveValue(exchange, type);
    assertEquals(exchange.getRequest().getHeaders(), httpEntity.getHeaders());
    assertEquals("line1", httpEntity.getBody().block());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) Mono(reactor.core.publisher.Mono) ResolvableType(org.springframework.core.ResolvableType) Test(org.junit.Test)

Aggregations

Mono (reactor.core.publisher.Mono)44 Test (org.junit.Test)28 ServerWebExchange (org.springframework.web.server.ServerWebExchange)21 MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)18 List (java.util.List)17 HttpStatus (org.springframework.http.HttpStatus)17 StepVerifier (reactor.test.StepVerifier)17 Assert.assertEquals (org.junit.Assert.assertEquals)14 Collections (java.util.Collections)12 Publisher (org.reactivestreams.Publisher)12 Assert (org.springframework.util.Assert)12 BindingContext (org.springframework.web.reactive.BindingContext)12 Matchers.is (org.hamcrest.Matchers.is)11 Assert.assertThat (org.junit.Assert.assertThat)11 Map (java.util.Map)10 ServerWebInputException (org.springframework.web.server.ServerWebInputException)10 ResponseStatusException (org.springframework.web.server.ResponseStatusException)9 MethodParameter (org.springframework.core.MethodParameter)8 MediaType (org.springframework.http.MediaType)8 HandlerResult (org.springframework.web.reactive.HandlerResult)8