ControllerMethodInvoker { public Object invoke(Object controller, Context context) { Object[] arguments = new Object[argumentExtractors.length]; for (int i = 0; i < argumentExtractors.length; i++) { arguments[i] = argumentExtractors[i].extract(context); } checkNullArgumentsAndThrowBadRequestExceptionIfConfigured(arguments); try { return method.invoke(controller, arguments); } catch (IllegalAccessException | IllegalArgumentException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { if (e.getCause() instanceof RuntimeException) { throw (RuntimeException) e.getCause(); } else { throw new RuntimeException(e.getCause()); } } } ControllerMethodInvoker(
            Method method, 
            ArgumentExtractor<?>[] argumentExtractors,
            boolean useStrictArgumentExtractors); Object invoke(Object controller, Context context); static ControllerMethodInvoker build(
            Method functionalMethod, 
            Method implementationMethod, 
            Injector injector,
            NinjaProperties ninjaProperties);  }
@Test public void noParameterMethodShouldBeInvoked() throws Exception { create("noParameter").invoke(mockController, context); verify(mockController).noParameter(); }
@Test public void contextShouldBePassed() throws Exception { create("context").invoke(mockController, context); verify(mockController).context(context); }
@Test public void sessionShouldBePassed() throws Exception { create("session").invoke(mockController, context); verify(mockController).session(session); }
@Test public void flashArgumentShouldBePassed() throws Exception { create("flash").invoke(mockController, context); verify(mockController).flash(flash); }
@Test public void paramAnnotatedArgumentShouldBePassed() throws Exception { when(context.getParameter("param1")).thenReturn("value"); create("param").invoke(mockController, context); verify(mockController).param("value"); }
@Test public void pathParamAnnotatedArgumentShouldBePassed() throws Exception { when(context.getPathParameter("param1")).thenReturn("value"); create("pathParam").invoke(mockController, context); verify(mockController).pathParam("value"); }
@Test public void sessionParamAnnotatedArgumentShouldBePassed() throws Exception { when(session.get("param1")).thenReturn("value"); create("sessionParam").invoke(mockController, context); verify(mockController).sessionParam("value"); }
@Test public void attributeAnnotatedArgumentShouldBePassed() throws Exception { Dep dep = new Dep("dep"); when(context.getAttribute("param1", Dep.class)).thenReturn(dep); create("attribute").invoke(mockController, context); verify(mockController).attribute(dep); }
@Test public void headerAnnotatedArgumentShouldBePassed() throws Exception { when(context.getHeader("param1")).thenReturn("value"); create("header").invoke(mockController, context); verify(mockController).header("value"); }
@Test public void headerAnnotatedArgumentShouldHandleNull() throws Exception { when(context.getHeader("param1")).thenReturn(null); create("header").invoke(mockController, context); verify(mockController).header(null); }
@Test public void headersAnnotatedArgumentShouldReturnNull() throws Exception { when(context.getHeaders("param1")).thenReturn(new ArrayList<String>()); create("headers").invoke(mockController, context); verify(mockController).headers(null); }
@Test public void headersAnnotatedArgumentShouldBePassed() throws Exception { when(context.getHeaders("param1")).thenReturn(Arrays.asList("a", "b", "c")); create("headers").invoke(mockController, context); verify(mockController).headers(new String[]{"a", "b", "c"}); }
@Test public void headersAnnotatedArgumentShouldHandleNull() throws Exception { create("headers").invoke(mockController, context); verify(mockController).headers(null); }
@Test public void integerParamShouldBeParsedToInteger() throws Exception { when(context.getParameter("param1")).thenReturn("20"); create("integerParam").invoke(mockController, context); verify(mockController).integerParam(20); }
@Test public void integerParamShouldHandleNull() throws Exception { create("integerParam").invoke(mockController, context); verify(mockController).integerParam(null); assertFalse(validation.hasViolations()); }
@Test public void integerValidationShouldWork() throws Exception { when(context.getParameter("param1")).thenReturn("blah"); create("integerParam").invoke(mockController, context); verify(mockController).integerParam(null); assertTrue(validation.hasViolation("param1")); }
@Test public void intParamShouldBeParsedToInteger() throws Exception { when(context.getParameter("param1")).thenReturn("20"); create("intParam").invoke(mockController, context); verify(mockController).intParam(20); }
@Test public void intParamShouldHandleNull() throws Exception { create("intParam").invoke(mockController, context); verify(mockController).intParam(0); assertFalse(validation.hasViolations()); }
@Test public void intValidationShouldWork() throws Exception { when(context.getParameter("param1")).thenReturn("blah"); create("intParam").invoke(mockController, context); verify(mockController).intParam(0); assertTrue(validation.hasViolation("param1")); }
@Test public void shortParamShouldBeParsedToShort() throws Exception { when(context.getParameter("param1")).thenReturn("20"); create("shortParam").invoke(mockController, context); verify(mockController).shortParam((short) 20); }
@Test public void shortParamShouldHandleNull() throws Exception { create("shortParam").invoke(mockController, context); verify(mockController).shortParam(null); assertFalse(validation.hasViolations()); }
@Test public void shortValidationShouldWork() throws Exception { when(context.getParameter("param1")).thenReturn("blah"); create("shortParam").invoke(mockController, context); verify(mockController).shortParam(null); assertTrue(validation.hasViolation("param1")); }
@Test public void primShortParamShouldBeParsedToShort() throws Exception { when(context.getParameter("param1")).thenReturn("20"); create("primShortParam").invoke(mockController, context); verify(mockController).primShortParam((short) 20); }
@Test public void primShortParamShouldHandleNull() throws Exception { create("primShortParam").invoke(mockController, context); verify(mockController).primShortParam((short) 0); assertFalse(validation.hasViolations()); }
@Test public void primShortValidationShouldWork() throws Exception { when(context.getParameter("param1")).thenReturn("blah"); create("primShortParam").invoke(mockController, context); verify(mockController).primShortParam((short) 0); assertTrue(validation.hasViolation("param1")); }
@Test public void characterParamShouldBeParsedToCharacter() throws Exception { when(context.getParameter("param1")).thenReturn("ABC"); create("characterParam").invoke(mockController, context); verify(mockController).characterParam('A'); }
@Test public void characterParamShouldHandleNull() throws Exception { create("characterParam").invoke(mockController, context); verify(mockController).characterParam(null); assertFalse(validation.hasViolations()); }
@Test public void charParamShouldBeParsedToCharacter() throws Exception { when(context.getParameter("param1")).thenReturn("ABC"); create("charParam").invoke(mockController, context); verify(mockController).charParam('A'); }
@Test public void charParamShouldHandleNull() throws Exception { create("charParam").invoke(mockController, context); verify(mockController).charParam('\0'); assertFalse(validation.hasViolations()); }
@Test public void byteParamShouldBeParsedToByte() throws Exception { when(context.getParameter("param1")).thenReturn("20"); create("byteParam").invoke(mockController, context); verify(mockController).byteParam((byte) 20); }
@Test public void byteParamShouldHandleNull() throws Exception { create("byteParam").invoke(mockController, context); verify(mockController).byteParam(null); assertFalse(validation.hasViolations()); }
@Test public void byteValidationShouldWork() throws Exception { when(context.getParameter("param1")).thenReturn("blah"); create("byteParam").invoke(mockController, context); verify(mockController).byteParam(null); assertTrue(validation.hasViolation("param1")); }
@Test public void primByteParamShouldBeParsedToByte() throws Exception { when(context.getParameter("param1")).thenReturn("20"); create("primByteParam").invoke(mockController, context); verify(mockController).primByteParam((byte) 20); }
@Test public void primByteParamShouldHandleNull() throws Exception { create("primByteParam").invoke(mockController, context); verify(mockController).primByteParam((byte) 0); assertFalse(validation.hasViolations()); }
@Test public void primByteValidationShouldWork() throws Exception { when(context.getParameter("param1")).thenReturn("blah"); create("primByteParam").invoke(mockController, context); verify(mockController).primByteParam((byte) 0); assertTrue(validation.hasViolation("param1")); }
@Test public void booleanParamShouldBeParsedToBoolean() throws Exception { when(context.getParameter("param1")).thenReturn("true"); create("booleanParam").invoke(mockController, context); verify(mockController).booleanParam(true); }
@Test public void booleanParamShouldHandleNull() throws Exception { create("booleanParam").invoke(mockController, context); verify(mockController).booleanParam(null); assertFalse(validation.hasViolations()); }
@Test(expected = BadRequestException.class) public void booleanParamShouldHandleNullInStrictMode() throws Exception { when(context.getParameter("param1")).thenReturn(null); when(ninjaProperties.getBooleanWithDefault(NinjaConstant.NINJA_STRICT_ARGUMENT_EXTRACTORS, false)).thenReturn(true); create("booleanParam").invoke(mockController, context); }
@Test(expected = BadRequestException.class) public void booleanParamShouldHandleWrongInputForBooleanInStrictMode() throws Exception { when(context.getParameter("param1")).thenReturn("test"); when(ninjaProperties.getBooleanWithDefault(NinjaConstant.NINJA_STRICT_ARGUMENT_EXTRACTORS, false)).thenReturn(true); create("booleanParam").invoke(mockController, context); }
@Test public void primBooleanParamShouldBeParsedToBoolean() throws Exception { when(context.getParameter("param1")).thenReturn("true"); create("primBooleanParam").invoke(mockController, context); verify(mockController).primBooleanParam(true); }
@Test public void primBooleanParamShouldHandleNull() throws Exception { create("primBooleanParam").invoke(mockController, context); verify(mockController).primBooleanParam(false); assertFalse(validation.hasViolations()); }
@Test public void booleanParamWithOptionalShouldHandleWrongInputForBooleanInStrictMode() throws Exception { when(context.getParameter("param1")).thenReturn("test"); when(ninjaProperties.getBooleanWithDefault(NinjaConstant.NINJA_STRICT_ARGUMENT_EXTRACTORS, false)).thenReturn(true); create("booleanParamWithOptional").invoke(mockController, context); verify(mockController).booleanParamWithOptional(Optional.empty()); }
@Test public void booleanParamWithOptionalShouldHandleTrueInStrictMode() throws Exception { when(context.getParameter("param1")).thenReturn("true"); when(ninjaProperties.getBooleanWithDefault(NinjaConstant.NINJA_STRICT_ARGUMENT_EXTRACTORS, false)).thenReturn(true); create("booleanParamWithOptional").invoke(mockController, context); verify(mockController).booleanParamWithOptional(Optional.of(Boolean.TRUE)); }
@Test public void booleanParamWithOptionalShouldHandleFalseInStrictMode() throws Exception { when(context.getParameter("param1")).thenReturn("false"); when(ninjaProperties.getBooleanWithDefault(NinjaConstant.NINJA_STRICT_ARGUMENT_EXTRACTORS, false)).thenReturn(true); create("booleanParamWithOptional").invoke(mockController, context); verify(mockController).booleanParamWithOptional(Optional.of(Boolean.FALSE)); }
@Test public void longParamShouldBeParsedToLong() throws Exception { when(context.getParameter("param1")).thenReturn("20"); create("longParam").invoke(mockController, context); verify(mockController).longParam(20l); }
@Test public void longParamShouldHandleNull() throws Exception { create("longParam").invoke(mockController, context); verify(mockController).longParam(null); assertFalse(validation.hasViolations()); }
@Test public void longValidationShouldWork() throws Exception { when(context.getParameter("param1")).thenReturn("blah"); create("longParam").invoke(mockController, context); verify(mockController).longParam(null); assertTrue(validation.hasViolation("param1")); }
@Test public void primLongParamShouldBeParsedToLong() throws Exception { when(context.getParameter("param1")).thenReturn("20"); create("primLongParam").invoke(mockController, context); verify(mockController).primLongParam(20); }
@Test public void primLongParamShouldHandleNull() throws Exception { create("primLongParam").invoke(mockController, context); verify(mockController).primLongParam(0); assertFalse(validation.hasViolations()); }
@Test public void primLongValidationShouldWork() throws Exception { when(context.getParameter("param1")).thenReturn("blah"); create("primLongParam").invoke(mockController, context); verify(mockController).primLongParam(0L); assertTrue(validation.hasViolation("param1")); }
@Test public void floatParamShouldBeParsedToFloat() throws Exception { when(context.getParameter("param1")).thenReturn("3.14"); create("floatParam").invoke(mockController, context); verify(mockController).floatParam(3.14f); }
@Test public void floatParamShouldHandleNull() throws Exception { create("floatParam").invoke(mockController, context); verify(mockController).floatParam(null); assertFalse(validation.hasViolations()); }
@Test public void floatValidationShouldWork() throws Exception { when(context.getParameter("param1")).thenReturn("blah"); create("floatParam").invoke(mockController, context); verify(mockController).floatParam(null); assertTrue(validation.hasViolation("param1")); }
@Test public void primFloatParamShouldBeParsedToFloat() throws Exception { when(context.getParameter("param1")).thenReturn("3.14"); create("primFloatParam").invoke(mockController, context); verify(mockController).primFloatParam(3.14f); }
@Test public void primFloatParamShouldHandleNull() throws Exception { create("primFloatParam").invoke(mockController, context); verify(mockController).primFloatParam(0); assertFalse(validation.hasViolations()); }
@Test public void primFloatValidationShouldWork() throws Exception { when(context.getParameter("param1")).thenReturn("blah"); create("primFloatParam").invoke(mockController, context); verify(mockController).primFloatParam(0); assertTrue(validation.hasViolation("param1")); }
@Test public void doubleParamShouldBeParsedToDouble() throws Exception { when(context.getParameter("param1")).thenReturn("3.14"); create("doubleParam").invoke(mockController, context); verify(mockController).doubleParam(3.14); }
@Test public void doubleParamShouldHandleNull() throws Exception { create("doubleParam").invoke(mockController, context); verify(mockController).doubleParam(null); assertFalse(validation.hasViolations()); }
@Test public void doubleValidationShouldWork() throws Exception { when(context.getParameter("param1")).thenReturn("blah"); create("doubleParam").invoke(mockController, context); verify(mockController).doubleParam(null); assertTrue(validation.hasViolation("param1")); }
@Test public void primDoubleParamShouldBeParsedToDouble() throws Exception { when(context.getParameter("param1")).thenReturn("3.14"); create("primDoubleParam").invoke(mockController, context); verify(mockController).primDoubleParam(3.14); }
@Test public void primDoubleParamShouldHandleNull() throws Exception { create("primDoubleParam").invoke(mockController, context); verify(mockController).primDoubleParam(0); assertFalse(validation.hasViolations()); }
@Test public void primDoubleValidationShouldWork() throws Exception { when(context.getParameter("param1")).thenReturn("blah"); create("primDoubleParam").invoke(mockController, context); verify(mockController).primDoubleParam(0); assertTrue(validation.hasViolation("param1")); }
@Test public void enumParamShouldBeParsedToEnumCaseSensitive() throws Exception { when(context.getParameter("param1")).thenReturn("Red"); create("enumParam").invoke(mockController, context); verify(mockController).enumParam(Rainbow.Red); }
@Test public void enumParamShouldBeParsedToEnumCaseInsensitive() throws Exception { when(context.getParameter("param1")).thenReturn("red"); create("enumParam").invoke(mockController, context); verify(mockController).enumParam(Rainbow.Red); }
@Test public void enumParamShouldHandleNull() throws Exception { create("enumParam").invoke(mockController, context); verify(mockController).enumParam(null); assertFalse(validation.hasViolations()); }
@Test public void enumParamValidationShouldWork() throws Exception { when(context.getParameter("param1")).thenReturn("blah"); create("enumParam").invoke(mockController, context); verify(mockController).enumParam(null); assertTrue(validation.hasViolation("param1")); }
@Test public void enumCsvParamSingleShouldBeParsed() throws Exception { when(context.getParameter("param1")).thenReturn("Red"); create("enumCsvParam").invoke(mockController, context); verify(mockController).enumCsvParam(new Rainbow[]{Rainbow.Red}); }
@Test public void enumCsvParamMultipleShouldBeParsed() throws Exception { when(context.getParameter("param1")).thenReturn("Red,Orange,Yellow"); create("enumCsvParam").invoke(mockController, context); verify(mockController).enumCsvParam(new Rainbow[]{Rainbow.Red, Rainbow.Orange, Rainbow.Yellow}); }
@Test public void enumCsvParamShouldReturnNull() throws Exception { when(context.getParameter("param1")).thenReturn(""); create("enumCsvParam").invoke(mockController, context); verify(mockController).enumCsvParam(null); assertFalse(validation.hasViolation("param1")); }
@Test public void enumCsvParamValidationShouldWork() throws Exception { when(context.getParameter("param1")).thenReturn("White,Black"); create("enumCsvParam").invoke(mockController, context); verify(mockController).enumCsvParam(null); assertTrue(validation.hasViolation("param1")); }
@Test public void enumArrayParamSingleShouldBeParsed() throws Exception { when(context.getParameterValues("param1")).thenReturn(Arrays.asList("Blue")); create("enumArrayParam").invoke(mockController, context); verify(mockController).enumArrayParam(new Rainbow[]{Rainbow.Blue}); }
@Test public void enumArrayParamMultipleShouldBeParsed() throws Exception { when(context.getParameterValues("param1")).thenReturn(Arrays.asList("Blue", "Indigo", "Violet")); create("enumArrayParam").invoke(mockController, context); verify(mockController).enumArrayParam(new Rainbow[]{Rainbow.Blue, Rainbow.Indigo, Rainbow.Violet}); }
@Test public void enumArrayParamShouldReturnNull() throws Exception { when(context.getParameterValues("param1")).thenReturn(new ArrayList<String>()); create("enumArrayParam").invoke(mockController, context); verify(mockController).enumArrayParam(null); assertFalse(validation.hasViolation("param1")); }
@Test public void enumArrayParamValidationShouldWork() throws Exception { when(context.getParameterValues("param1")).thenReturn(Arrays.asList("White", "Black")); create("enumArrayParam").invoke(mockController, context); verify(mockController).enumArrayParam(null); assertTrue(validation.hasViolation("param1")); }
@Test public void customDateFormatParamShouldBeParsedToDate() throws Exception { when(context.getParameter("param1")).thenReturn("15/01/2015"); create("dateParam", DateParamParser.class).invoke(mockController, context); verify(mockController).dateParam(new LocalDateTime(2015, 1, 15, 0, 0).toDate()); }
@Test public void customDateFormatParamWithOptionalShouldBeParsedToDate() throws Exception { when(context.getParameter("param1")).thenReturn("15/01/2015"); create("dateParamWithOptional", DateParamParser.class).invoke(mockController, context); verify(mockController).dateParamWithOptional(Optional.of(new LocalDateTime(2015, 1, 15, 0, 0).toDate())); }
@Test public void customDateFormatParamShouldHandleNull() throws Exception { create("dateParam", DateParamParser.class).invoke(mockController, context); verify(mockController).dateParam(null); assertFalse(validation.hasViolations()); }
@Test public void customDateFormatParamWithOptionalShouldHandleEmpty() throws Exception { create("dateParamWithOptional", DateParamParser.class).invoke(mockController, context); verify(mockController).dateParamWithOptional(Optional.empty()); assertFalse(validation.hasViolations()); }
@Test public void customDateFormatValidationShouldWork() throws Exception { when(context.getParameter("param1")).thenReturn("blah"); create("dateParam", DateParamParser.class).invoke(mockController, context); verify(mockController).dateParam(null); assertTrue(validation.hasViolation("param1")); }
@Test(expected = RoutingException.class) public void needingInjectionParamParserNotBinded() throws Exception { create("needingInjectionParamParser").invoke(mockController, context); verify(mockController).needingInjectionParamParser(new Dep("hello_hello")); }
@Test public void needingInjectionParamParser() throws Exception { when(context.getParameter("param1")).thenReturn("hello"); create("needingInjectionParamParser", NeedingInjectionParamParser.class).invoke(mockController, context); verify(mockController).needingInjectionParamParser(new Dep("hello_hello")); }
@Test public void needingInjectionParamParserArray() throws Exception { when(context.getParameterValues("param1")).thenReturn(Arrays.asList("hello1", "hello2")); create("needingInjectionParamParserArray", NeedingInjectionParamParser.class).invoke(mockController, context); verify(mockController).needingInjectionParamParserArray(new Dep[] { new Dep("hello_hello1"), new Dep("hello_hello2") }); }
@Test public void customArgumentExtractorWithNoArgsShouldBeInstantiated() { create("noArgArgumentExtractor").invoke(mockController, context); verify(mockController).noArgArgumentExtractor("noargs"); }
@Test public void customArgumentExtractorWithClassArgShouldBeInstantiated() { create("classArgArgumentExtractor").invoke(mockController, context); verify(mockController).classArgArgumentExtractor("java.lang.String"); }
@Test public void customArgumentExtractorWithGuiceShouldBeInstantiated() { create("guiceArgumentExtractor", new Dep("dep")).invoke(mockController, context); verify(mockController).guiceArgumentExtractor("dep:bar:java.lang.String"); }
@Test public void customArgumentExtractorWithOptionalAndGuiceShouldBeInstantiated() { create("guiceArgumentExtractorWithOptional", new Dep("dep")).invoke(mockController, context); verify(mockController).guiceArgumentExtractorWithOptional(Optional.of("dep:bar:java.lang.String")); }
@Test public void multipleDifferentExtractorsShouldWorkFine() { when(context.getParameter("param1")).thenReturn("value"); when(context.getPathParameter("param2")).thenReturn("20"); create("multiple").invoke(mockController, context); verify(mockController).multiple("value", 20, context, session); }
@Test public void validationShouldFailWhenBadRequest() { create("required").invoke(mockController, context); verify(mockController).required(null); assertTrue(validation.hasViolation("param1")); }
@Test public void validationShouldPassWhenGoodRequest() { when(context.getParameter("param1")).thenReturn("value"); create("required").invoke(mockController, context); verify(mockController).required("value"); assertFalse(validation.hasViolations()); }
@Test public void optionalSessionParam() { when(session.get("param1")).thenReturn("value"); create("optionalSessionParam").invoke(mockController, context); verify(mockController).optionalSessionParam(Optional.of("value")); }
@Test public void optionalSessionParamEmpty() { when(session.get("param1")).thenReturn(null); create("optionalSessionParam").invoke(mockController, context); verify(mockController).optionalSessionParam(Optional.empty()); }
@Test(expected = BadRequestException.class) public void sessionParamStrictModeWorks() { when(ninjaProperties.getBooleanWithDefault(NinjaConstant.NINJA_STRICT_ARGUMENT_EXTRACTORS, false)).thenReturn(true); when(session.get("param1")).thenReturn(null); create("sessionParam").invoke(mockController, context); }
@Test public void optionalAttribute() { Dep dep = new Dep("dep"); when(context.getAttribute("param1", Dep.class)).thenReturn(dep); create("optionalAttribute").invoke(mockController, context); verify(mockController).optionalAttribute(Optional.of(dep)); }
@Test public void optionalAttributeEmpty() { when(context.getAttribute("param1", Dep.class)).thenReturn(null); create("optionalAttribute").invoke(mockController, context); verify(mockController).optionalAttribute(Optional.empty()); }
@Test(expected = BadRequestException.class) public void attributeStrictModeWorks() { when(ninjaProperties.getBooleanWithDefault(NinjaConstant.NINJA_STRICT_ARGUMENT_EXTRACTORS, false)).thenReturn(true); when(context.getAttribute("param1", Dep.class)).thenReturn(null); create("attribute").invoke(mockController, context); }
@Test public void optionalHeader() { when(context.getHeader("param1")).thenReturn("value"); create("optionalHeader").invoke(mockController, context); verify(mockController).optionalHeader(Optional.of("value")); }
@Test public void optionalHeaderEmpty() { when(context.getHeader("param1")).thenReturn(null); create("optionalHeader").invoke(mockController, context); verify(mockController).optionalHeader(Optional.empty()); }
@Test(expected = BadRequestException.class) public void headerStrictModeWorks() { when(ninjaProperties.getBooleanWithDefault(NinjaConstant.NINJA_STRICT_ARGUMENT_EXTRACTORS, false)).thenReturn(true); when(context.getHeader("param1")).thenReturn(null); create("header").invoke(mockController, context); }
@Test public void optionalParam() { when(context.getParameter("param1")).thenReturn("value"); create("optionalParam").invoke(mockController, context); verify(mockController).optionalParam(Optional.of("value")); }
@Test public void optionalParamEmpty() { when(context.getParameter("param1")).thenReturn(null); create("optionalParam").invoke(mockController, context); verify(mockController).optionalParam(Optional.empty()); }
@Test(expected = BadRequestException.class) public void paramStrictModeWorks() { when(ninjaProperties.getBooleanWithDefault(NinjaConstant.NINJA_STRICT_ARGUMENT_EXTRACTORS, false)).thenReturn(true); when(context.getParameter("param1")).thenReturn(null); create("param").invoke(mockController, context); }
@Test public void optionalIntegerParam() { when(context.getParameter("param1")).thenReturn("1"); create("optionalIntegerParam").invoke(mockController, context); verify(mockController).optionalIntegerParam(Optional.of(1)); }
@Test public void optionalIntegerParamEmpty() { when(context.getParameter("param1")).thenReturn(null); create("optionalIntegerParam").invoke(mockController, context); verify(mockController).optionalIntegerParam(Optional.empty()); }
@Test(expected = BadRequestException.class) public void integerParamStrictModeWorks() { when(ninjaProperties.getBooleanWithDefault(NinjaConstant.NINJA_STRICT_ARGUMENT_EXTRACTORS, false)).thenReturn(true); when(context.getParameter("param1")).thenReturn(null); create("integerParam").invoke(mockController, context); }
@Test public void optionalLongParam() { when(context.getParameter("param1")).thenReturn("1"); create("optionalLongParam").invoke(mockController, context); verify(mockController).optionalLongParam(Optional.of(1L)); }
@Test public void optionalLongParamEmpty() { when(context.getParameter("param1")).thenReturn(null); create("optionalLongParam").invoke(mockController, context); verify(mockController).optionalLongParam(Optional.empty()); }
@Test(expected = BadRequestException.class) public void longParamEmptyStrictModeWorks() { when(ninjaProperties.getBooleanWithDefault(NinjaConstant.NINJA_STRICT_ARGUMENT_EXTRACTORS, false)).thenReturn(true); when(context.getParameter("param1")).thenReturn(null); create("longParam").invoke(mockController, context); }
@Test public void optionalShortParam() { when(context.getParameter("param1")).thenReturn("1"); create("optionalShortParam").invoke(mockController, context); verify(mockController).optionalShortParam(Optional.of(new Short("1"))); }
@Test public void optionalShortParamEmpty() { when(context.getParameter("param1")).thenReturn(null); create("optionalShortParam").invoke(mockController, context); verify(mockController).optionalShortParam(Optional.empty()); }
@Test(expected = BadRequestException.class) public void shortParamEmptyStrictModeWorks() { when(ninjaProperties.getBooleanWithDefault(NinjaConstant.NINJA_STRICT_ARGUMENT_EXTRACTORS, false)).thenReturn(true); when(context.getParameter("param1")).thenReturn(null); create("shortParam").invoke(mockController, context); }
@Test public void optionalEnumParam() { when(context.getParameter("param1")).thenReturn("red"); create("optionalEnumParam").invoke(mockController, context); verify(mockController).optionalEnumParam(Optional.of(Rainbow.Red)); }
@Test public void optionalEnumParamEmpty() { when(context.getParameter("param1")).thenReturn(null); create("optionalEnumParam").invoke(mockController, context); verify(mockController).optionalEnumParam(Optional.empty()); }
@Test(expected = BadRequestException.class) public void rainbowParamStrictModeWorks() { when(ninjaProperties.getBooleanWithDefault(NinjaConstant.NINJA_STRICT_ARGUMENT_EXTRACTORS, false)).thenReturn(true); when(context.getParameter("param1")).thenReturn(null); create("enumParam").invoke(mockController, context); }
@Test public void optionalEnumArrayParam() { when(context.getParameter("param1")).thenReturn("Red,Orange,Yellow"); create("optionalEnumArrayParam").invoke(mockController, context); verify(mockController).optionalEnumArrayParam(argumentCaptor.capture()); Rainbow [] rainbows = argumentCaptor.getValue().get(); assertThat(rainbows, Matchers.arrayContaining(Rainbow.Red, Rainbow.Orange, Rainbow.Yellow)); }
@Test public void optionalEnumArrayParamEmpty() { when(context.getParameter("param1")).thenReturn(null); create("optionalEnumArrayParam").invoke(mockController, context); verify(mockController).optionalEnumArrayParam(Optional.empty()); }
@Test(expected = BadRequestException.class) public void rainbowArrayParamEmptyStrictModeWorks() { when(ninjaProperties.getBooleanWithDefault(NinjaConstant.NINJA_STRICT_ARGUMENT_EXTRACTORS, false)).thenReturn(true); create("enumArrayParam").invoke(mockController, context); }
@Test public void optionalDateParam() { when(context.getParameter("param1")).thenReturn("15/01/2015"); create("optionalDateParam", DateParamParser.class).invoke(mockController, context); verify(mockController).optionalDateParam(Optional.of(new LocalDateTime(2015, 1, 15, 0, 0).toDate())); }
@Test public void optionalDateParamEmpty() { when(context.getParameter("param1")).thenReturn(null); create("optionalDateParam", DateParamParser.class).invoke(mockController, context); verify(mockController).optionalDateParam(Optional.empty()); }
@Test(expected = BadRequestException.class) public void dateParamStrictModeWorks() { when(ninjaProperties.getBooleanWithDefault(NinjaConstant.NINJA_STRICT_ARGUMENT_EXTRACTORS, false)).thenReturn(true); when(context.getParameter("param1")).thenReturn(null); create("dateParam", DateParamParser.class).invoke(mockController, context); }
@Test public void optionalBody() { Object body = new Object(); when(context.parseBody(Object.class)).thenReturn(body); create("optionalBody").invoke(mockController, context); verify(mockController).optionalBody(Optional.of(body)); }
@Test public void optionalBodyEmpty() { when(context.parseBody(Object.class)).thenReturn(null); create("optionalBody").invoke(mockController, context); verify(mockController).optionalBody(Optional.empty()); }
@Test(expected = BadRequestException.class) public void bodyEmptyStrictModeWorks() { when(ninjaProperties.getBooleanWithDefault(NinjaConstant.NINJA_STRICT_ARGUMENT_EXTRACTORS, false)).thenReturn(true); when(context.parseBody(Object.class)).thenReturn(null); create("body").invoke(mockController, context); }
@Test public void validationShouldBeAppliedInCorrectOrderPreFail() { create("requiredInt").invoke(mockController, context); verify(mockController).requiredInt(0); assertTrue(validation.hasViolation("param1")); assertEquals(1, validation.getViolations("param1").size()); assertEquals("validation.required.violation", validation.getViolations("param1").get(0).getMessageKey()); }
@Test public void validationWithOptionalShouldBeAppliedInCorrectOrderPreFail() { create("requiredIntWithOptional").invoke(mockController, context); verify(mockController).requiredIntWithOptional(Optional.empty()); assertTrue(validation.hasViolation("param1")); }
@Test public void validationShouldBeAppliedInCorrectOrderPostFail() { when(context.getParameter("param1")).thenReturn("5"); create("requiredInt").invoke(mockController, context); verify(mockController).requiredInt(5); assertTrue(validation.hasViolation("param1")); assertEquals(1, validation.getViolations("param1").size()); assertEquals("validation.number.min.violation", validation.getViolations("param1").get(0).getMessageKey()); }
@Test public void validationWithOptionalShouldBeAppliedInCorrectOrderPostFail() { when(context.getParameter("param1")).thenReturn("5"); create("requiredIntWithOptional").invoke(mockController, context); verify(mockController).requiredIntWithOptional(Optional.of(5)); assertTrue(validation.hasViolation("param1")); }
@Test public void validationShouldBeAppliedInCorrectOrderPass() { when(context.getParameter("param1")).thenReturn("20"); create("requiredInt").invoke(mockController, context); verify(mockController).requiredInt(20); assertFalse(validation.hasViolations()); }
@Test public void validationWithOptionalShouldBeAppliedInCorrectOrderPass() { when(context.getParameter("param1")).thenReturn("20"); create("requiredIntWithOptional").invoke(mockController, context); verify(mockController).requiredIntWithOptional(Optional.of(20)); assertFalse(validation.hasViolations()); }
@Test(expected = RoutingException.class) public void invalidValidatorShouldBeFlagged() { create("badValidator").invoke(mockController, context); }
@Test(expected = RoutingException.class) public void invalidValidatorWithOptionalShouldBeFlagged() { create("badValidatorWithOptional").invoke(mockController, context); }
@Test(expected = RoutingException.class) public void tooManyBodiesShouldBeFlagged() { create("tooManyBodies").invoke(mockController, context); }
@Test public void bodyShouldBeParsedIntoLeftOverParameter() { Object body = new Object(); when(context.parseBody(Object.class)).thenReturn(body); create("body").invoke(mockController, context); verify(mockController).body(body); }
@Test public void bodyWithOptionalShouldBeParsedIntoLeftOverParameter() { Object body = new Object(); when(context.parseBody(Object.class)).thenReturn(body); create("bodyWithOptional").invoke(mockController, context); verify(mockController).bodyWithOptional(Optional.of(body)); }
@Test public void bodyWithOptionalShouldBeEmptyIfNoBodyPresent() { when(context.parseBody(Object.class)).thenReturn(null); create("bodyWithOptional").invoke(mockController, context); verify(mockController).bodyWithOptional(Optional.empty()); }