use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.
the class JAXRSUtilsTest method testBeanParamsWithBooleanConverter.
@Test
public void testBeanParamsWithBooleanConverter() throws Exception {
Class<?>[] argType = { Customer.CustomerBean.class };
Method m = Customer.class.getMethod("testBeanParam", argType);
Message messageImpl = createMessage();
messageImpl.put(Message.REQUEST_URI, "/bar");
// The converter converts any Boolean to null
ProviderFactory.getInstance(messageImpl).registerUserProvider(new MyBoolParamConverterProvider());
MultivaluedMap<String, String> headers = new MetadataMap<>();
headers.putSingle("Content-Type", MediaType.APPLICATION_FORM_URLENCODED);
messageImpl.put(Message.PROTOCOL_HEADERS, headers);
String body = "value=true";
messageImpl.setContent(InputStream.class, new ByteArrayInputStream(body.getBytes()));
final ClassResourceInfo cri = new ClassResourceInfo(Customer.class);
final MethodDispatcher md = new MethodDispatcher();
final OperationResourceInfo ori = new OperationResourceInfo(m, cri);
md.bind(ori, m);
cri.setMethodDispatcher(md);
cri.initBeanParamInfo(ServerProviderFactory.getInstance(messageImpl));
List<Object> params = JAXRSUtils.processParameters(ori, null, messageImpl);
assertEquals("Bean should be created", 1, params.size());
Customer.CustomerBean cb = (Customer.CustomerBean) params.get(0);
assertNotNull(cb);
assertNull(cb.getBool());
}
use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.
the class JAXRSUtilsTest method testLocaleParameter.
@Test
public void testLocaleParameter() throws Exception {
Message messageImpl = createMessage();
ProviderFactory.getInstance(messageImpl).registerUserProvider(new LocaleParameterHandler());
Class<?>[] argType = { Locale.class };
Method m = Customer.class.getMethod("testLocaleParam", argType);
messageImpl.put(Message.QUERY_STRING, "p1=en_us");
List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl);
assertEquals(1, params.size());
Locale l = (Locale) params.get(0);
assertEquals("en", l.getLanguage());
assertEquals("US", l.getCountry());
}
use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.
the class JAXRSUtilsTest method testMatrixParameters.
@SuppressWarnings("unchecked")
@Test
public void testMatrixParameters() throws Exception {
Class<?>[] argType = { String.class, String.class, String.class, String.class, List.class, String.class };
Method m = Customer.class.getMethod("testMatrixParam", argType);
Message messageImpl = createMessage();
messageImpl.put(Message.REQUEST_URI, "/foo;p4=0;p3=3/bar;p1=1;p2=/baz;p4=4;p4=5;p5");
List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl);
assertEquals("5 Matrix params should've been identified", 6, params.size());
assertEquals("First Matrix Parameter not matched correctly", "1", params.get(0));
assertEquals("Second Matrix Parameter was not matched correctly", "", params.get(1));
assertEquals("Third Matrix Parameter was not matched correctly", "3", params.get(2));
assertEquals("Fourth Matrix Parameter was not matched correctly", "0", params.get(3));
List<String> list = (List<String>) params.get(4);
assertEquals(3, list.size());
assertEquals("0", list.get(0));
assertEquals("4", list.get(1));
assertEquals("5", list.get(2));
assertNull("Sixth Matrix Parameter was not matched correctly", params.get(5));
}
use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.
the class JAXRSUtilsTest method testConversion.
@Test
public void testConversion() throws Exception {
ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethod("testConversion", new Class[] { PathSegmentImpl.class, SimpleFactory.class }), cri);
ori.setHttpMethod("GET");
ori.setURITemplate(new URITemplate("{id1}/{id2}"));
MultivaluedMap<String, String> values = new MetadataMap<>();
values.putSingle("id1", "1");
values.putSingle("id2", "2");
Message m = createMessage();
List<Object> params = JAXRSUtils.processParameters(ori, values, m);
PathSegment ps = (PathSegment) params.get(0);
assertEquals("1", ps.getPath());
SimpleFactory sf = (SimpleFactory) params.get(1);
assertEquals(2, sf.getId());
}
use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.
the class JAXRSUtilsTest method testFormParametersAndMap.
@SuppressWarnings("unchecked")
@Test
public void testFormParametersAndMap() throws Exception {
Class<?>[] argType = { MultivaluedMap.class, String.class, List.class };
Method m = Customer.class.getMethod("testMultivaluedMapAndFormParam", argType);
final Message messageImpl = createMessage();
String body = "p1=1&p2=2&p2=3";
messageImpl.put(Message.REQUEST_URI, "/foo");
messageImpl.put("Content-Type", MediaType.APPLICATION_FORM_URLENCODED);
messageImpl.setContent(InputStream.class, new ByteArrayInputStream(body.getBytes()));
ProviderFactory.getInstance(messageImpl).registerUserProvider(new FormEncodingProvider<Object>() {
@Override
protected void persistParamsOnMessage(MultivaluedMap<String, String> params) {
messageImpl.put(FormUtils.FORM_PARAM_MAP, params);
}
});
List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), new MetadataMap<String, String>(), messageImpl);
assertEquals("3 params should've been identified", 3, params.size());
MultivaluedMap<String, String> map = (MultivaluedMap<String, String>) params.get(0);
assertEquals(2, map.size());
assertEquals(1, map.get("p1").size());
assertEquals("First map parameter not matched correctly", "1", map.getFirst("p1"));
assertEquals(2, map.get("p2").size());
assertEquals("2", map.get("p2").get(0));
assertEquals("3", map.get("p2").get(1));
assertEquals("First Form Parameter not matched correctly", "1", params.get(1));
List<String> list = (List<String>) params.get(2);
assertEquals(2, list.size());
assertEquals("2", list.get(0));
assertEquals("3", list.get(1));
}
Aggregations