Skip to content

Instantly share code, notes, and snippets.

@jbrisbin
Last active December 21, 2015 11:29
Show Gist options
  • Save jbrisbin/6299435 to your computer and use it in GitHub Desktop.
Save jbrisbin/6299435 to your computer and use it in GitHub Desktop.
PromiseReturnValueHandlerTests
package reactor.spring.webmvc;
import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.context.request.async.WebAsyncUtils;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;
import reactor.core.composable.Promise;
import reactor.function.Consumer;
/**
* @author Jon Brisbin
*/
public class PromiseHandlerMethodReturnValueHandler implements HandlerMethodReturnValueHandler {
@Override
public boolean supportsReturnType(MethodParameter returnType) {
return Promise.class.isAssignableFrom(returnType.getParameterType());
}
@SuppressWarnings("unchecked")
@Override
public void handleReturnValue(Object returnValue,
final MethodParameter returnType,
final ModelAndViewContainer mavContainer,
final NativeWebRequest webRequest) throws Exception {
final DeferredResult<Object> deferredResult = new DeferredResult<Object>();
((Promise) returnValue)
.onSuccess(new Consumer() {
@Override
public void accept(Object o) {
deferredResult.setResult(o);
}
})
.onError(new Consumer<Throwable>() {
@Override
public void accept(Throwable t) {
deferredResult.setErrorResult(t);
}
});
WebAsyncUtils.getAsyncManager(webRequest)
.startDeferredResultProcessing(deferredResult, mavContainer);
}
}
package reactor.spring.webmvc;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import reactor.core.Environment;
import reactor.core.Reactor;
import reactor.core.spec.Reactors;
import reactor.spring.context.config.EnableReactor;
import java.util.List;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* @author Jon Brisbin
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration
public class PromiseReturnValueHandlerTests {
@Autowired
private WebApplicationContext wac;
private MockMvc mvc;
@Before
public void setup() {
mvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void promiseReturnValueHandlerAwaitsValues() throws Exception {
mvc.perform(get("/promise"))
.andExpect(status().isOk())
.andExpect(content().string("Hello World!"));
}
@Configuration
@EnableReactor
@ComponentScan
static class ContextConfig extends WebMvcConfigurationSupport {
@Bean
public Reactor reactor(Environment env) {
return Reactors.reactor().env(env).get();
}
@Override
protected void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
returnValueHandlers.add(new PromiseHandlerMethodReturnValueHandler());
}
}
}
15:00:31.706 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Initializing new StandardServletEnvironment
15:00:31.707 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [servletConfigInitParams] PropertySource with lowest search precedence
15:00:31.707 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [servletContextInitParams] PropertySource with lowest search precedence
15:00:31.712 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [jndiProperties] PropertySource with lowest search precedence
15:00:31.712 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
15:00:31.714 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
15:00:31.714 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
15:00:31.748 [main] INFO o.s.w.c.s.GenericWebApplicationContext - Refreshing org.springframework.web.context.support.GenericWebApplicationContext@709438dd: startup date [Wed Aug 21 15:00:31 CDT 2013]; root of context hierarchy
15:00:31.751 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Replacing [servletContextInitParams] PropertySource with [servletContextInitParams]
15:00:31.752 [main] DEBUG o.s.w.c.s.GenericWebApplicationContext - Bean factory for org.springframework.web.context.support.GenericWebApplicationContext@709438dd: org.springframework.beans.factory.support.DefaultListableBeanFactory@25ff3700: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,promiseReturnValueHandlerTests.ContextConfig]; root of factory hierarchy
15:00:32.173 [main] INFO o.s.w.c.s.GenericWebApplicationContext - Bean 'promiseReturnValueHandlerTests.ContextConfig' of type [class reactor.spring.webmvc.PromiseReturnValueHandlerTests$ContextConfig$$EnhancerByCGLIB$$f6b19206] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
15:00:32.228 [main] INFO o.s.w.c.s.GenericWebApplicationContext - Bean 'mvcConversionService' of type [class org.springframework.format.support.DefaultFormattingConversionService] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
15:00:32.238 [main] DEBUG o.s.w.c.s.GenericWebApplicationContext - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@75e04ee8]
15:00:32.241 [main] DEBUG o.s.w.c.s.GenericWebApplicationContext - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@7abbee99]
15:00:32.444 [main] DEBUG r.s.b.f.c.ConsumerBeanPostProcessor - Attaching Consumer to Reactor[reactor.core.Reactor@33ff6f40] using Selector[Selector{object=test, uuid=55f348b0-0a9c-11e3-b137-109addba76a9, tags=null}]
15:00:32.498 [main] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking for request mappings in application context: org.springframework.web.context.support.GenericWebApplicationContext@709438dd: startup date [Wed Aug 21 15:00:31 CDT 2013]; root of context hierarchy
15:00:32.511 [main] INFO o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/promise],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public reactor.core.composable.Promise<org.springframework.http.ResponseEntity<java.lang.String>> reactor.spring.webmvc.PromiseController.get()
15:00:32.548 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Looking for URL mappings in application context: org.springframework.web.context.support.GenericWebApplicationContext@709438dd: startup date [Wed Aug 21 15:00:31 CDT 2013]; root of context hierarchy
15:00:32.549 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor': no URL paths identified
15:00:32.550 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor': no URL paths identified
15:00:32.550 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'org.springframework.context.annotation.internalRequiredAnnotationProcessor': no URL paths identified
15:00:32.550 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'org.springframework.context.annotation.internalCommonAnnotationProcessor': no URL paths identified
15:00:32.551 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'promiseReturnValueHandlerTests.ContextConfig': no URL paths identified
15:00:32.551 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor': no URL paths identified
15:00:32.552 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor': no URL paths identified
15:00:32.552 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'deferredHandler': no URL paths identified
15:00:32.552 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'promiseController': no URL paths identified
15:00:32.553 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'reactor': no URL paths identified
15:00:32.553 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'requestMappingHandlerMapping': no URL paths identified
15:00:32.553 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'mvcContentNegotiationManager': no URL paths identified
15:00:32.554 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'viewControllerHandlerMapping': no URL paths identified
15:00:32.554 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'beanNameHandlerMapping': no URL paths identified
15:00:32.554 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'resourceHandlerMapping': no URL paths identified
15:00:32.555 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'defaultServletHandlerMapping': no URL paths identified
15:00:32.555 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'requestMappingHandlerAdapter': no URL paths identified
15:00:32.555 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'mvcConversionService': no URL paths identified
15:00:32.556 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'mvcValidator': no URL paths identified
15:00:32.556 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'httpRequestHandlerAdapter': no URL paths identified
15:00:32.556 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'simpleControllerHandlerAdapter': no URL paths identified
15:00:32.557 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'handlerExceptionResolver': no URL paths identified
15:00:32.557 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'reactor.core.Environment': no URL paths identified
15:00:32.557 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'reactor.spring.beans.factory.config.ConsumerBeanPostProcessor': no URL paths identified
15:00:32.558 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'environment': no URL paths identified
15:00:32.558 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'systemProperties': no URL paths identified
15:00:32.558 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'systemEnvironment': no URL paths identified
15:00:32.559 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'servletContext': no URL paths identified
15:00:32.559 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'contextParameters': no URL paths identified
15:00:32.559 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'contextAttributes': no URL paths identified
15:00:32.559 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry': no URL paths identified
15:00:32.560 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'messageSource': no URL paths identified
15:00:32.560 [main] DEBUG o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'applicationEventMulticaster': no URL paths identified
15:00:32.800 [main] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for controller advice: org.springframework.web.context.support.GenericWebApplicationContext@709438dd: startup date [Wed Aug 21 15:00:31 CDT 2013]; root of context hierarchy
15:00:32.860 [main] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Looking for exception mappings: org.springframework.web.context.support.GenericWebApplicationContext@709438dd: startup date [Wed Aug 21 15:00:31 CDT 2013]; root of context hierarchy
15:00:32.870 [main] DEBUG o.s.w.c.s.GenericWebApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@79fd87c8]
15:00:32.922 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Initializing new StandardServletEnvironment
15:00:32.923 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [servletConfigInitParams] PropertySource with lowest search precedence
15:00:32.923 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [servletContextInitParams] PropertySource with lowest search precedence
15:00:32.923 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [jndiProperties] PropertySource with lowest search precedence
15:00:32.923 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
15:00:32.924 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
15:00:32.924 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
15:00:32.925 [main] INFO o.s.mock.web.MockServletContext - Initializing Spring FrameworkServlet ''
15:00:32.925 [main] INFO o.s.t.w.s.TestDispatcherServlet - FrameworkServlet '': initialization started
15:00:32.974 [main] INFO o.s.t.w.s.TestDispatcherServlet - FrameworkServlet '': initialization completed in 49 ms
15:00:33.004 [main] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /promise
15:00:33.007 [main] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Returning handler method [public reactor.core.composable.Promise<org.springframework.http.ResponseEntity<java.lang.String>> reactor.spring.webmvc.PromiseController.get()]
15:00:33.019 [ringBuffer-ringbuffer-1] DEBUG r.s.b.f.c.ConsumerBeanPostProcessor - Invoking method[public void reactor.spring.webmvc.DeferredHandler.test(reactor.core.composable.Deferred)] on reactor.spring.webmvc.DeferredHandler@4876e144 using reactor.core.composable.Deferred@3f539d4b
15:00:33.025 [main] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [public reactor.core.composable.Promise<org.springframework.http.ResponseEntity<java.lang.String>> reactor.spring.webmvc.PromiseController.get()]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
15:00:33.028 [main] DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [public reactor.core.composable.Promise<org.springframework.http.ResponseEntity<java.lang.String>> reactor.spring.webmvc.PromiseController.get()]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
15:00:33.028 [main] DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [public reactor.core.composable.Promise<org.springframework.http.ResponseEntity<java.lang.String>> reactor.spring.webmvc.PromiseController.get()]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
java.lang.AssertionError: Status expected:<200> but was:<406>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:60)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:89)
at org.springframework.test.web.servlet.result.StatusResultMatchers$5.match(StatusResultMatchers.java:554)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:142)
at reactor.spring.webmvc.PromiseReturnValueHandlerTests.promiseReturnValueHandlerAwaitsValues(PromiseReturnValueHandlerTests.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:229)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:86)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:172)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
15:00:33.047 [Thread-3] INFO o.s.w.c.s.GenericWebApplicationContext - Closing org.springframework.web.context.support.GenericWebApplicationContext@709438dd: startup date [Wed Aug 21 15:00:31 CDT 2013]; root of context hierarchy
15:00:33.048 [Thread-3] INFO o.s.b.f.s.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@25ff3700: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,promiseReturnValueHandlerTests.ContextConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor,deferredHandler,promiseController,reactor,requestMappingHandlerMapping,mvcContentNegotiationManager,viewControllerHandlerMapping,beanNameHandlerMapping,resourceHandlerMapping,defaultServletHandlerMapping,requestMappingHandlerAdapter,mvcConversionService,mvcValidator,httpRequestHandlerAdapter,simpleControllerHandlerAdapter,handlerExceptionResolver,reactor.core.Environment,reactor.spring.beans.factory.config.ConsumerBeanPostProcessor]; root of factory hierarchy
Process finished with exit code 255
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment