FormParameterResolver implements ParameterResolver<FormParam> { @Override public Object resolve(org.everrest.core.Parameter parameter, ApplicationContext context) throws Exception { String param = this.formParam.value(); TypeProducer typeProducer = typeProducerFactory.createTypeProducer(parameter.getParameterClass(), parameter.getGenericType()); MultivaluedMap<String, String> form = readForm(context, !parameter.isEncoded()); return typeProducer.createValue(param, form, parameter.getDefaultValue()); } FormParameterResolver(FormParam formParam, TypeProducerFactory typeProducerFactory); @Override Object resolve(org.everrest.core.Parameter parameter, ApplicationContext context);  }
@Test public void resolvesDecodedFormParamAsString() throws Exception { when(parameter.isEncoded()).thenReturn(false); when(typeProducer.createValue("foo", decodedForm, null)).thenReturn(decodedForm.getFirst("foo")); Object resolved = formParameterResolver.resolve(parameter, applicationContext); assertEquals(decodedForm.getFirst("foo"), resolved); assertThatMessageBodyWriterWasCalled(); }
@Test public void resolvesEncodedFormParamAsString() throws Exception { when(parameter.isEncoded()).thenReturn(true); when(typeProducer.createValue("foo", encodedForm, null)).thenReturn(encodedForm.getFirst("foo")); Object resolved = formParameterResolver.resolve(parameter, applicationContext); assertEquals(encodedForm.getFirst("foo"), resolved); assertThatMessageBodyWriterWasCalled(); }
@Test public void createsObjectFromDefaultValueWhenFormParamNotFound() throws Exception { when(parameter.isEncoded()).thenReturn(false); when(parameter.getDefaultValue()).thenReturn("default value"); when(typeProducer.createValue("foo", decodedForm, "default value")).thenReturn("default value"); Object resolved = formParameterResolver.resolve(parameter, applicationContext); assertEquals("default value", resolved); assertThatMessageBodyWriterWasCalled(); }
@Test public void throwsIllegalStateExceptionWhenFormMessageBodyReaderIsNotAvailable() throws Exception { Class<MultivaluedMap> type = MultivaluedMap.class; ParameterizedType genericType = newParameterizedType(type, String.class, String.class); when(applicationContext.getProviders().getMessageBodyReader(eq(type), eq(genericType), any(), eq(APPLICATION_FORM_URLENCODED_TYPE))) .thenReturn(null); thrown.expect(IllegalStateException.class); formParameterResolver.resolve(parameter, applicationContext); }