Search in sources :

Example 1 with HttpStatusReturnValueHandler

use of cn.taketoday.web.handler.result.HttpStatusReturnValueHandler in project today-infrastructure by TAKETODAY.

the class ReturnValueHandlerManagerTests method addHandler.

@Test
void addHandler() {
    ReturnValueHandlerManager manager = new ReturnValueHandlerManager();
    HttpStatusReturnValueHandler returnValueHandler = new HttpStatusReturnValueHandler();
    manager.addHandlers(returnValueHandler);
    assertThat(manager.getHandlers()).hasSize(1);
    HttpStatusReturnValueHandler highestValueHandler = new HttpStatusReturnValueHandler();
    manager.addHandlers(List.of(highestValueHandler));
    assertThat(manager.getHandlers()).hasSize(2);
    assertThat(manager.getByReturnValue(HttpStatus.OK)).isNotNull();
    assertThat(manager.getByReturnValue("")).isNull();
    assertThat(manager.getByReturnValue(null)).isNull();
    assertThat(manager.getHandler("")).isNull();
    // getHandler(handler)
    HandlerMethod handler = Mockito.mock(HandlerMethod.class);
    Mockito.when(handler.isReturn(HttpStatus.class)).thenReturn(true);
    ActionMappingAnnotationHandler annotationHandler = new ActionMappingAnnotationHandler(handler, null, Object.class) {

        @Override
        public Object getHandlerObject() {
            return null;
        }
    };
    assertThat(manager.getHandler(annotationHandler)).isNotNull();
    // obtainHandler
    assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(returnValueHandler).isNotNull();
    // sort
    // returnValueHandler.setOrder(2);
    // highestValueHandler.setOrder(1);
    assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(highestValueHandler).isNotNull();
    // returnValueHandler.setOrder(1);
    // highestValueHandler.setOrder(2);
    assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(returnValueHandler).isNotNull();
    assertThatThrownBy(() -> manager.obtainHandler("")).isInstanceOf(ReturnValueHandlerNotFoundException.class).hasMessageStartingWith("No ReturnValueHandler for handler");
}
Also used : ActionMappingAnnotationHandler(cn.taketoday.web.handler.method.ActionMappingAnnotationHandler) HttpStatusReturnValueHandler(cn.taketoday.web.handler.result.HttpStatusReturnValueHandler) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 2 with HttpStatusReturnValueHandler

use of cn.taketoday.web.handler.result.HttpStatusReturnValueHandler in project today-framework by TAKETODAY.

the class ReturnValueHandlerManagerTests method contains.

@Test
void contains() {
    ReturnValueHandlerManager manager = new ReturnValueHandlerManager();
    HttpStatusReturnValueHandler returnValueHandler = new HttpStatusReturnValueHandler();
    manager.addHandlers(returnValueHandler);
    assertThat(manager.contains(null)).isFalse();
    assertThat(manager.contains(String.class)).isFalse();
    assertThat(manager.contains(HttpStatusReturnValueHandler.class)).isTrue();
    assertThat(manager.get(HttpStatusReturnValueHandler.class)).isNotNull();
    assertThat(manager.get((Class<?>) null)).isNull();
}
Also used : HttpStatusReturnValueHandler(cn.taketoday.web.handler.result.HttpStatusReturnValueHandler) Test(org.junit.jupiter.api.Test)

Example 3 with HttpStatusReturnValueHandler

use of cn.taketoday.web.handler.result.HttpStatusReturnValueHandler in project today-framework by TAKETODAY.

the class ReturnValueHandlerManagerTests method addHandler.

@Test
void addHandler() {
    ReturnValueHandlerManager manager = new ReturnValueHandlerManager();
    HttpStatusReturnValueHandler returnValueHandler = new HttpStatusReturnValueHandler();
    manager.addHandlers(returnValueHandler);
    assertThat(manager.getHandlers()).hasSize(1);
    HttpStatusReturnValueHandler highestValueHandler = new HttpStatusReturnValueHandler();
    manager.addHandlers(List.of(highestValueHandler));
    assertThat(manager.getHandlers()).hasSize(2);
    assertThat(manager.getByReturnValue(HttpStatus.OK)).isNotNull();
    assertThat(manager.getByReturnValue("")).isNull();
    assertThat(manager.getByReturnValue(null)).isNull();
    assertThat(manager.getHandler("")).isNull();
    // getHandler(handler)
    HandlerMethod handler = Mockito.mock(HandlerMethod.class);
    Mockito.when(handler.isReturn(HttpStatus.class)).thenReturn(true);
    ActionMappingAnnotationHandler annotationHandler = new ActionMappingAnnotationHandler(handler, null, Object.class) {

        @Override
        public Object getHandlerObject() {
            return null;
        }
    };
    assertThat(manager.getHandler(annotationHandler)).isNotNull();
    // obtainHandler
    assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(returnValueHandler).isNotNull();
    // sort
    // returnValueHandler.setOrder(2);
    // highestValueHandler.setOrder(1);
    assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(highestValueHandler).isNotNull();
    // returnValueHandler.setOrder(1);
    // highestValueHandler.setOrder(2);
    assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(returnValueHandler).isNotNull();
    assertThatThrownBy(() -> manager.obtainHandler("")).isInstanceOf(ReturnValueHandlerNotFoundException.class).hasMessageStartingWith("No ReturnValueHandler for handler");
}
Also used : ActionMappingAnnotationHandler(cn.taketoday.web.handler.method.ActionMappingAnnotationHandler) HttpStatusReturnValueHandler(cn.taketoday.web.handler.result.HttpStatusReturnValueHandler) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 4 with HttpStatusReturnValueHandler

use of cn.taketoday.web.handler.result.HttpStatusReturnValueHandler in project today-infrastructure by TAKETODAY.

the class ReturnValueHandlerManagerTests method contains.

@Test
void contains() {
    ReturnValueHandlerManager manager = new ReturnValueHandlerManager();
    HttpStatusReturnValueHandler returnValueHandler = new HttpStatusReturnValueHandler();
    manager.addHandlers(returnValueHandler);
    assertThat(manager.contains(null)).isFalse();
    assertThat(manager.contains(String.class)).isFalse();
    assertThat(manager.contains(HttpStatusReturnValueHandler.class)).isTrue();
    assertThat(manager.get(HttpStatusReturnValueHandler.class)).isNotNull();
    assertThat(manager.get((Class<?>) null)).isNull();
}
Also used : HttpStatusReturnValueHandler(cn.taketoday.web.handler.result.HttpStatusReturnValueHandler) Test(org.junit.jupiter.api.Test)

Aggregations

HttpStatusReturnValueHandler (cn.taketoday.web.handler.result.HttpStatusReturnValueHandler)4 Test (org.junit.jupiter.api.Test)4 ActionMappingAnnotationHandler (cn.taketoday.web.handler.method.ActionMappingAnnotationHandler)2 HandlerMethod (cn.taketoday.web.handler.method.HandlerMethod)2