IogiParametersProvider implements ParametersProvider { public Object[] getParametersFor(ResourceMethod method, List<Message> errors, ResourceBundle bundle) { Parameters parameters = parseParameters(servletRequest); List<Target<Object>> targets = createTargets(method); List<Object> arguments = instantiateParameters(parameters, targets, errors); return arguments.toArray(); } IogiParametersProvider(ParameterNameProvider provider, HttpServletRequest parameters, InstantiatorWithErrors instantiator); Object[] getParametersFor(ResourceMethod method, List<Message> errors, ResourceBundle bundle);  }
@Test public void returnsNullWhenInstantiatingAStringForWhichThereAreNoParameters() throws Exception { thereAreNoParameters(); final ResourceMethod method = string; Object[] params = provider.getParametersFor(method, errors, null); assertArrayEquals(new Object[] {null}, params); }
@Test public void canInjectADependencyProvidedByVraptor() throws Exception { thereAreNoParameters(); ResourceMethod resourceMethod = DefaultResourceMethod.instanceFor(OtherResource.class, OtherResource.class.getDeclaredMethod("logic", NeedsMyResource.class)); final MyResource providedInstance = new MyResource(); when(container.canProvide(MyResource.class)).thenReturn(true); when(container.instanceFor(MyResource.class)).thenReturn(providedInstance); Object[] params = provider.getParametersFor(resourceMethod, errors, null); assertThat(((NeedsMyResource)params[0]).getMyResource(), is(sameInstance(providedInstance))); }
@Test public void willCreateAnIogiParameterForEachRequestParameterValue() throws Exception { ResourceMethod anyMethod = buyA; requestParameterIs(anyMethod, "name", "a", "b"); final InstantiatorWithErrors mockInstantiator = mock(InstantiatorWithErrors.class); final Parameters expectedParamters = new Parameters( Arrays.asList(new Parameter("name", "a"), new Parameter("name", "b"))); IogiParametersProvider iogiProvider = new IogiParametersProvider(nameProvider, request, mockInstantiator); iogiProvider.getParametersFor(anyMethod, errors, null); verify(mockInstantiator).instantiate(any(Target.class), eq(expectedParamters), eq(errors)); }
@Test public void willCreateATargerForEachFormalParameterDeclaredByTheMethod() throws Exception { final ResourceMethod buyAHouse = buyA; requestParameterIs(buyAHouse, "house", ""); final InstantiatorWithErrors mockInstantiator = mock(InstantiatorWithErrors.class); IogiParametersProvider iogiProvider = new IogiParametersProvider(nameProvider, request, mockInstantiator); final Target<House> expectedTarget = Target.create(House.class, "house"); iogiProvider.getParametersFor(buyAHouse, errors, null); verify(mockInstantiator).instantiate(eq(expectedTarget), any(Parameters.class), eq(errors)); }
@Test public void shouldInjectOnlyAttributesWithSameType() throws Exception { Object result = mock(Result.class); ResourceBundle emptyBundle = mock(ResourceBundle.class); ResourceMethod method = DefaultResourceMethod.instanceFor(OtherResource.class, OtherResource.class.getDeclaredMethod("logic", String.class)); when(request.getAttribute("result")).thenReturn(result); when(request.getParameterValues("result")).thenReturn(new String[] { "buggy" }); when(request.getParameterNames()).thenReturn(enumeration(asList("result"))); when(nameProvider.parameterNamesFor(method.getMethod())).thenReturn(new String[] { "result" }); List<Message> errors = new ArrayList<Message>(); Object[] out = provider.getParametersFor(method, errors, emptyBundle); assertThat(out[0], is(not(result))); }