Search in sources :

Example 46 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.

the class JAXRSUtilsTest method testArrayParamNoProvider.

@Test
public void testArrayParamNoProvider() throws Exception {
    Message messageImpl = createMessage();
    Class<?>[] argType = { String[].class };
    Method m = Customer.class.getMethod("testCustomerParam2", argType);
    messageImpl.put(Message.QUERY_STRING, "p1=Fred&p1=Barry");
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl);
    assertEquals(1, params.size());
    String[] values = (String[]) params.get(0);
    assertEquals("Fred", values[0]);
    assertEquals("Barry", values[1]);
}
Also used : Message(org.apache.cxf.message.Message) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 47 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.

the class JAXRSUtilsTest method doTestFormParameters.

@SuppressWarnings("unchecked")
private void doTestFormParameters(boolean useMediaType) throws Exception {
    Class<?>[] argType = { String.class, List.class };
    Method m = Customer.class.getMethod("testFormParam", argType);
    Message messageImpl = createMessage();
    String body = "p1=hello%2bworld&p2=2&p2=3";
    messageImpl.put(Message.REQUEST_URI, "/foo");
    MultivaluedMap<String, String> headers = new MetadataMap<>();
    if (useMediaType) {
        headers.putSingle("Content-Type", MediaType.APPLICATION_FORM_URLENCODED);
    }
    messageImpl.put(Message.PROTOCOL_HEADERS, headers);
    messageImpl.setContent(InputStream.class, new ByteArrayInputStream(body.getBytes()));
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl);
    assertEquals("2 form params should've been identified", 2, params.size());
    assertEquals("First Form Parameter not matched correctly", "hello+world", params.get(0));
    List<String> list = (List<String>) params.get(1);
    assertEquals(2, list.size());
    assertEquals("2", list.get(0));
    assertEquals("3", list.get(1));
}
Also used : Message(org.apache.cxf.message.Message) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Method(java.lang.reflect.Method) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ByteArrayInputStream(java.io.ByteArrayInputStream) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) List(java.util.List) ArrayList(java.util.ArrayList)

Example 48 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.

the class JAXRSUtilsTest method testDefaultValueOnField.

@Test
public void testDefaultValueOnField() throws Exception {
    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    Customer c = new Customer();
    OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethods()[0], cri);
    Message m = createMessage();
    m.put(Message.QUERY_STRING, "");
    JAXRSUtils.injectParameters(ori, c, m);
    assertEquals("bQuery", c.getB());
}
Also used : Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Test(org.junit.Test)

Example 49 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.

the class JAXRSUtilsTest method testFromStringParameters.

@Test
public void testFromStringParameters() throws Exception {
    Class<?>[] argType = { UUID.class, CustomerGender.class, CustomerGender.class };
    Method m = Customer.class.getMethod("testFromStringParam", argType);
    UUID u = UUID.randomUUID();
    Message messageImpl = createMessage();
    messageImpl.put(Message.QUERY_STRING, "p1=" + u.toString() + "&p2=1&p3=2");
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl);
    assertEquals(3, params.size());
    assertEquals("Query UUID Parameter was not matched correctly", u.toString(), params.get(0).toString());
    assertSame(CustomerGender.FEMALE, params.get(1));
    assertSame(CustomerGender.MALE, params.get(2));
}
Also used : Message(org.apache.cxf.message.Message) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Method(java.lang.reflect.Method) UUID(java.util.UUID) Test(org.junit.Test)

Example 50 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.

the class JAXRSUtilsTest method testXmlAdapterBean.

@Test
public void testXmlAdapterBean() throws Exception {
    Class<?>[] argType = { Customer.CustomerBean.class };
    Method m = Customer.class.getMethod("testXmlAdapter", argType);
    Message messageImpl = createMessage();
    messageImpl.put(Message.QUERY_STRING, "a=aValue");
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl);
    assertEquals(1, params.size());
    Customer.CustomerBean bean = (Customer.CustomerBean) params.get(0);
    assertEquals("aValue", bean.getA());
}
Also used : Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)129 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)104 Message (org.apache.cxf.message.Message)72 Test (org.junit.Test)70 Method (java.lang.reflect.Method)52 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)40 Customer (org.apache.cxf.jaxrs.Customer)22 Endpoint (org.apache.cxf.endpoint.Endpoint)13 MessageImpl (org.apache.cxf.message.MessageImpl)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ArrayList (java.util.ArrayList)12 List (java.util.List)12 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)11 Response (javax.ws.rs.core.Response)11 URITemplate (org.apache.cxf.jaxrs.model.URITemplate)11 Exchange (org.apache.cxf.message.Exchange)11 MediaType (javax.ws.rs.core.MediaType)10 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 MethodDispatcher (org.apache.cxf.jaxrs.model.MethodDispatcher)7