Search in sources :

Example 1 with UserOperation

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

the class ResourceUtils method getResourceFromElement.

private static UserResource getResourceFromElement(Element e) {
    UserResource resource = new UserResource();
    resource.setName(e.getAttribute("name"));
    resource.setPath(e.getAttribute("path"));
    resource.setConsumes(e.getAttribute("consumes"));
    resource.setProduces(e.getAttribute("produces"));
    List<Element> operEls = DOMUtils.findAllElementsByTagNameNS(e, "http://cxf.apache.org/jaxrs", "operation");
    List<UserOperation> opers = new ArrayList<>(operEls.size());
    for (Element operEl : operEls) {
        opers.add(getOperationFromElement(operEl));
    }
    resource.setOperations(opers);
    return resource;
}
Also used : UserOperation(org.apache.cxf.jaxrs.model.UserOperation) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) UserResource(org.apache.cxf.jaxrs.model.UserResource) ArrayList(java.util.ArrayList)

Example 2 with UserOperation

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

the class AbstractOpenApiServiceDescriptionTest method doTestApiListingIsProperlyReturnedJSON.

protected void doTestApiListingIsProperlyReturnedJSON(final WebClient client, boolean useXForwarded, String basePath) throws Exception {
    if (useXForwarded) {
        client.header("USE_XFORWARDED", true);
    }
    try {
        String swaggerJson = client.get(String.class);
        UserApplication ap = OpenApiParseUtils.getUserApplicationFromJson(swaggerJson);
        assertNotNull(ap);
        if (basePath == null) {
            assertEquals(useXForwarded ? "/reverse" : "/", ap.getBasePath());
        } else {
            assertEquals(basePath, ap.getBasePath());
        }
        List<UserResource> urs = ap.getResources();
        assertNotNull(urs);
        assertEquals(1, urs.size());
        UserResource r = urs.get(0);
        Map<String, UserOperation> map = r.getOperationsAsMap();
        assertEquals(3, map.size());
        UserOperation getBooksOp = map.get("getBooks");
        assertEquals(HttpMethod.GET, getBooksOp.getVerb());
        assertEquals(getApplicationPath() + "/bookstore", getBooksOp.getPath());
        // see https://github.com/swagger-api/swagger-core/issues/2646
        if (getBooksOp.getProduces() != null) {
            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(getApplicationPath() + "/bookstore/{id}", 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(getApplicationPath() + "/bookstore/{id}", 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)

Example 3 with UserOperation

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

the class JAXRSClientFactoryBeanTest method testCreateClientWithUserResource.

@Test
public void testCreateClientWithUserResource() throws Exception {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress("http://bar");
    UserResource r = new UserResource();
    r.setName(BookStore.class.getName());
    r.setPath("/");
    UserOperation op = new UserOperation();
    op.setName("getDescription");
    op.setVerb("GET");
    r.setOperations(Collections.singletonList(op));
    bean.setModelBeans(r);
    assertTrue(bean.create() instanceof BookStore);
}
Also used : BookStore(org.apache.cxf.jaxrs.resources.BookStore) UserOperation(org.apache.cxf.jaxrs.model.UserOperation) UserResource(org.apache.cxf.jaxrs.model.UserResource) Test(org.junit.Test)

Example 4 with UserOperation

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

the class SwaggerParseUtilsTest method testConvertSimpleDocToUserApp.

@Test
public void testConvertSimpleDocToUserApp() {
    UserApplication ap = SwaggerParseUtils.getUserApplication("/swagger20.json");
    assertNotNull(ap);
    assertEquals("/services/helloservice", ap.getBasePath());
    Map<String, UserResource> map = ap.getResourcesAsMap();
    assertEquals(2, map.size());
    UserResource ur = map.get("sayHello");
    assertNotNull(ur);
    assertEquals("/", ur.getPath());
    assertEquals(1, ur.getOperations().size());
    UserOperation op = ur.getOperations().get(0);
    assertEquals("sayHello", op.getName());
    assertEquals("/sayHello/{a}", op.getPath());
    assertEquals("GET", op.getVerb());
    assertEquals("text/plain", op.getProduces());
    assertEquals(1, op.getParameters().size());
    Parameter param1 = op.getParameters().get(0);
    assertEquals("a", param1.getName());
    assertEquals(ParameterType.PATH, param1.getType());
    assertEquals(String.class, param1.getJavaType());
    UserResource ur2 = map.get("sayHello2");
    assertNotNull(ur2);
    assertEquals("/", ur2.getPath());
    assertEquals(1, ur2.getOperations().size());
    UserOperation op2 = ur2.getOperations().get(0);
    assertEquals("sayHello", op2.getName());
    assertEquals("/sayHello2/{a}", op2.getPath());
    assertEquals("GET", op2.getVerb());
    assertEquals("text/plain", op2.getProduces());
    assertEquals(1, op2.getParameters().size());
    Parameter param2 = op.getParameters().get(0);
    assertEquals("a", param2.getName());
    assertEquals(ParameterType.PATH, param2.getType());
    assertEquals(String.class, param2.getJavaType());
}
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) Test(org.junit.Test)

Example 5 with UserOperation

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

the class JAXRSClientFactoryBeanTest method testCreateClientWithTwoUserResources.

@Test
public void testCreateClientWithTwoUserResources() throws Exception {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress("http://bar");
    UserResource r1 = new UserResource();
    r1.setName(BookStore.class.getName());
    r1.setPath("/store");
    UserOperation op = new UserOperation();
    op.setName("getDescription");
    op.setVerb("GET");
    r1.setOperations(Collections.singletonList(op));
    UserResource r2 = new UserResource();
    r2.setName(Book.class.getName());
    r2.setPath("/book");
    UserOperation op2 = new UserOperation();
    op2.setName("getName");
    op2.setVerb("GET");
    r2.setOperations(Collections.singletonList(op2));
    bean.setModelBeans(r1, r2);
    bean.setServiceClass(Book.class);
    assertTrue(bean.create() instanceof Book);
}
Also used : BookStore(org.apache.cxf.jaxrs.resources.BookStore) UserOperation(org.apache.cxf.jaxrs.model.UserOperation) Book(org.apache.cxf.jaxrs.resources.Book) UserResource(org.apache.cxf.jaxrs.model.UserResource) Test(org.junit.Test)

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