Search in sources :

Example 16 with UserOperation

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

the class ResourceUtils method getOperationFromElement.

private static UserOperation getOperationFromElement(Element e) {
    UserOperation op = new UserOperation();
    op.setName(e.getAttribute("name"));
    op.setVerb(e.getAttribute("verb"));
    op.setPath(e.getAttribute("path"));
    op.setOneway(Boolean.parseBoolean(e.getAttribute("oneway")));
    op.setConsumes(e.getAttribute("consumes"));
    op.setProduces(e.getAttribute("produces"));
    List<Element> paramEls = DOMUtils.findAllElementsByTagNameNS(e, "http://cxf.apache.org/jaxrs", "param");
    List<Parameter> params = new ArrayList<>(paramEls.size());
    for (int i = 0; i < paramEls.size(); i++) {
        Element paramEl = paramEls.get(i);
        Parameter p = new Parameter(paramEl.getAttribute("type"), i, paramEl.getAttribute("name"));
        p.setEncoded(Boolean.valueOf(paramEl.getAttribute("encoded")));
        p.setDefaultValue(paramEl.getAttribute("defaultValue"));
        String pClass = paramEl.getAttribute("class");
        if (!StringUtils.isEmpty(pClass)) {
            try {
                p.setJavaType(ClassLoaderUtils.loadClass(pClass, ResourceUtils.class));
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
        params.add(p);
    }
    op.setParameters(params);
    return op;
}
Also used : UserOperation(org.apache.cxf.jaxrs.model.UserOperation) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Parameter(org.apache.cxf.jaxrs.model.Parameter) JAXBException(javax.xml.bind.JAXBException)

Example 17 with UserOperation

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

the class AbstractSwagger2ServiceDescriptionTest method doTestApiListingIsProperlyReturnedJSON.

protected static void doTestApiListingIsProperlyReturnedJSON(final WebClient client, boolean useXForwarded) throws Exception {
    if (useXForwarded) {
        client.header("USE_XFORWARDED", true);
    }
    try {
        String swaggerJson = client.get(String.class);
        UserApplication ap = SwaggerParseUtils.getUserApplicationFromJson(swaggerJson);
        assertNotNull(ap);
        assertEquals(useXForwarded ? "/reverse" : "/", ap.getBasePath());
        List<UserResource> urs = ap.getResources();
        assertNotNull(urs);
        assertEquals(1, urs.size());
        UserResource r = urs.get(0);
        String basePath = "";
        if (!"/".equals(r.getPath())) {
            basePath = r.getPath();
        }
        Map<String, UserOperation> map = r.getOperationsAsMap();
        assertEquals(3, map.size());
        UserOperation getBooksOp = map.get("getBooks");
        assertEquals(HttpMethod.GET, getBooksOp.getVerb());
        assertEquals("/bookstore", basePath + getBooksOp.getPath());
        assertEquals(MediaType.APPLICATION_JSON, getBooksOp.getProduces());
        List<Parameter> getBooksOpParams = getBooksOp.getParameters();
        assertEquals(1, getBooksOpParams.size());
        assertEquals(ParameterType.QUERY, getBooksOpParams.get(0).getType());
        UserOperation getBookOp = map.get("getBook");
        assertEquals(HttpMethod.GET, getBookOp.getVerb());
        assertEquals("/bookstore/{id}", basePath + getBookOp.getPath());
        assertEquals(MediaType.APPLICATION_JSON, getBookOp.getProduces());
        List<Parameter> getBookOpParams = getBookOp.getParameters();
        assertEquals(1, getBookOpParams.size());
        assertEquals(ParameterType.PATH, getBookOpParams.get(0).getType());
        UserOperation deleteOp = map.get("delete");
        assertEquals(HttpMethod.DELETE, deleteOp.getVerb());
        assertEquals("/bookstore/{id}", basePath + deleteOp.getPath());
        List<Parameter> delOpParams = deleteOp.getParameters();
        assertEquals(1, delOpParams.size());
        assertEquals(ParameterType.PATH, delOpParams.get(0).getType());
        assertThat(swaggerJson, CoreMatchers.containsString(CONTACT));
        assertThat(swaggerJson, CoreMatchers.containsString(TITLE));
        assertThat(swaggerJson, CoreMatchers.containsString(DESCRIPTION));
        assertThat(swaggerJson, CoreMatchers.containsString(LICENSE));
        assertThat(swaggerJson, CoreMatchers.containsString(LICENSE_URL));
        assertThat(swaggerJson, CoreMatchers.containsString(SECURITY_DEFINITION_NAME));
    } finally {
        client.close();
    }
}
Also used : UserApplication(org.apache.cxf.jaxrs.model.UserApplication) UserOperation(org.apache.cxf.jaxrs.model.UserOperation) UserResource(org.apache.cxf.jaxrs.model.UserResource) Parameter(org.apache.cxf.jaxrs.model.Parameter) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Aggregations

UserOperation (org.apache.cxf.jaxrs.model.UserOperation)17 Parameter (org.apache.cxf.jaxrs.model.Parameter)13 UserResource (org.apache.cxf.jaxrs.model.UserResource)12 UserApplication (org.apache.cxf.jaxrs.model.UserApplication)7 Test (org.junit.Test)7 HashMap (java.util.HashMap)4 Map (java.util.Map)3 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 JAXBElement (javax.xml.bind.JAXBElement)2 JsonMapObjectReaderWriter (org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter)2 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)2 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)2 ParameterType (org.apache.cxf.jaxrs.model.ParameterType)2 BookStore (org.apache.cxf.jaxrs.resources.BookStore)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2