Search in sources :

Example 26 with OperationResourceInfo

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

the class JAXRSUtilsTest method verifyParametersBean.

private void verifyParametersBean(Method m, MultivaluedMap<String, String> simpleValues, Message simpleMessageImpl, MultivaluedMap<String, String> complexValues, Message complexMessageImpl) throws Exception {
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), simpleValues, simpleMessageImpl);
    assertEquals("Bean should be created", 1, params.size());
    Customer.CustomerBean cb = (Customer.CustomerBean) params.get(0);
    assertNotNull(cb);
    assertEquals("aValue", cb.getA());
    assertEquals(Long.valueOf(123), cb.getB());
    params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), complexValues, complexMessageImpl);
    assertEquals("Bean should be created", 1, params.size());
    Customer.CustomerBean cb1 = (Customer.CustomerBean) params.get(0);
    assertNotNull(cb1);
    assertEquals("A", cb1.getA());
    assertEquals(Long.valueOf(123), cb1.getB());
    List<String> list1 = cb1.getC();
    assertEquals(3, list1.size());
    assertEquals("1", list1.get(0));
    assertEquals("2", list1.get(1));
    assertEquals("3", list1.get(2));
    Customer.CustomerBean cb2 = cb1.getD();
    assertNotNull(cb2);
    assertEquals("B", cb2.getA());
    assertEquals(Long.valueOf(456), cb2.getB());
    List<String> list2 = cb2.getC();
    assertEquals(3, list2.size());
    assertEquals("4", list2.get(0));
    assertEquals("5", list2.get(1));
    assertEquals("6", list2.get(2));
    List<Customer.CustomerBean> cb2List = cb1.e;
    assertEquals(2, cb2List.size());
    int idx = 1;
    for (Customer.CustomerBean cb2E : cb2List) {
        assertNotNull(cb2E);
        assertEquals("B" + idx, cb2E.getA());
        assertEquals(Long.valueOf(456 + idx), cb2E.getB());
        // ensure C was stripped properly since lists within lists are not supported
        assertNull(cb2E.getC());
        assertNull(cb2E.getD());
        assertNull(cb2E.e);
        idx++;
    }
    Customer.CustomerBean cb3 = cb2.getD();
    assertNotNull(cb3);
    assertEquals("C", cb3.getA());
    assertEquals(Long.valueOf(789), cb3.getB());
    List<String> list3 = cb3.getC();
    assertEquals(3, list3.size());
    assertEquals("7", list3.get(0));
    assertEquals("8", list3.get(1));
    assertEquals("9", list3.get(2));
    List<Customer.CustomerBean> cb3List = cb2.e;
    assertEquals(2, cb3List.size());
    idx = 1;
    for (Customer.CustomerBean cb3E : cb3List) {
        assertNotNull(cb3E);
        assertEquals("C" + idx, cb3E.getA());
        assertEquals(Long.valueOf(789 + idx), cb3E.getB());
        // ensure C was stripped properly since lists within lists are not supported
        assertNull(cb3E.getC());
        assertNull(cb3E.getD());
        assertNull(cb3E.e);
        idx++;
    }
}
Also used : Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Endpoint(org.apache.cxf.endpoint.Endpoint)

Example 27 with OperationResourceInfo

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

the class JAXRSUtilsTest method testXmlAdapterString.

@Test
public void testXmlAdapterString() throws Exception {
    Method m = Customer.class.getMethod("testXmlAdapter4", String.class);
    Message messageImpl = createMessage();
    messageImpl.put(Message.QUERY_STRING, "a=3");
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl);
    assertEquals(1, params.size());
    String ret = (String) params.get(0);
    assertEquals("Val: 3", ret);
}
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 28 with OperationResourceInfo

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

the class JAXRSUtilsTest method testParamAnnotationOnField.

@Test
public void testParamAnnotationOnField() 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();
    MultivaluedMap<String, String> headers = new MetadataMap<>();
    headers.add("AHeader", "theAHeader");
    m.put(Message.PROTOCOL_HEADERS, headers);
    m.put(Message.QUERY_STRING, "b=bValue");
    JAXRSUtils.injectParameters(ori, c, m);
    assertEquals("bValue", c.getB());
    assertEquals("theAHeader", c.getAHeader());
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) 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 29 with OperationResourceInfo

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

the class JAXRSUtilsTest method invokeCustomerMethod.

private void invokeCustomerMethod(ClassResourceInfo cri, Customer customer, Server server) throws Exception {
    OperationResourceInfo ori = cri.getMethodDispatcher().getOperationResourceInfo(Customer.class.getMethod("test", new Class[] {}));
    JAXRSInvoker invoker = new JAXRSInvoker();
    Exchange exc = new ExchangeImpl();
    exc.put(Endpoint.class, server.getEndpoint());
    Message inMessage = new MessageImpl();
    exc.setInMessage(inMessage);
    exc.put(OperationResourceInfo.class, ori);
    invoker.invoke(exc, Collections.emptyList(), customer);
}
Also used : Exchange(org.apache.cxf.message.Exchange) Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MessageImpl(org.apache.cxf.message.MessageImpl) JAXRSInvoker(org.apache.cxf.jaxrs.JAXRSInvoker) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

Example 30 with OperationResourceInfo

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

the class JAXRSUtilsTest method testContextResolverParam.

@Test
public void testContextResolverParam() throws Exception {
    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethod("testContextResolvers", new Class[] { ContextResolver.class }), cri);
    ori.setHttpMethod("GET");
    Message m = createMessage();
    ContextResolver<JAXBContext> cr = new JAXBContextProvider();
    ProviderFactory.getInstance(m).registerUserProvider(cr);
    m.put(Message.BASE_PATH, "/");
    List<Object> params = JAXRSUtils.processParameters(ori, new MetadataMap<String, String>(), m);
    assertEquals("1 parameters expected", 1, params.size());
    assertSame(cr.getClass(), params.get(0).getClass());
}
Also used : Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) JAXBContext(javax.xml.bind.JAXBContext) JAXBContextProvider(org.apache.cxf.jaxrs.JAXBContextProvider) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) ContextResolver(javax.ws.rs.ext.ContextResolver) Test(org.junit.Test)

Aggregations

OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)130 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)105 Message (org.apache.cxf.message.Message)72 Test (org.junit.Test)70 Method (java.lang.reflect.Method)53 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)40 Customer (org.apache.cxf.jaxrs.Customer)22 ArrayList (java.util.ArrayList)13 List (java.util.List)13 Endpoint (org.apache.cxf.endpoint.Endpoint)13 MessageImpl (org.apache.cxf.message.MessageImpl)13 ByteArrayInputStream (java.io.ByteArrayInputStream)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 MethodDispatcher (org.apache.cxf.jaxrs.model.MethodDispatcher)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)7