Search in sources :

Example 11 with UserOperation

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

the class SwaggerParseUtilsTest method testConvertSimpleDocTagAndPathMismatchToUserApp.

@Test
public void testConvertSimpleDocTagAndPathMismatchToUserApp() {
    UserApplication ap = SwaggerParseUtils.getUserApplication("/swagger20TagsPathsMismatch.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("/sayHi/{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("/sayHi2/{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 12 with UserOperation

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

the class SwaggerParseUtilsTest method testConvertSimpleDocNoTagsToUserApp.

@Test
public void testConvertSimpleDocNoTagsToUserApp() {
    UserApplication ap = SwaggerParseUtils.getUserApplication("/swagger20NoTags.json");
    assertNotNull(ap);
    assertEquals("/services/helloservice", ap.getBasePath());
    Map<String, UserResource> map = ap.getResourcesAsMap();
    assertEquals(1, map.size());
    UserResource ur = map.get("");
    assertNotNull(ur);
    assertEquals("/", ur.getPath());
    assertEquals(2, 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());
    UserOperation op2 = ur.getOperations().get(1);
    assertEquals("sayHello2", 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 13 with UserOperation

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

the class ResourceUtilsTest method testClassResourceInfoUserResource.

@Test
public void testClassResourceInfoUserResource() throws Exception {
    UserResource ur = new UserResource();
    ur.setName(HashMap.class.getName());
    ur.setPath("/hashmap");
    UserOperation op = new UserOperation();
    op.setPath("/key/{id}");
    op.setName("get");
    op.setVerb("POST");
    op.setParameters(Collections.singletonList(new Parameter(ParameterType.PATH, "id")));
    ur.setOperations(Collections.singletonList(op));
    Map<String, UserResource> resources = new HashMap<>();
    resources.put(ur.getName(), ur);
    ClassResourceInfo cri = ResourceUtils.createClassResourceInfo(resources, ur, null, true, true, BusFactory.getDefaultBus());
    assertNotNull(cri);
    assertEquals("/hashmap", cri.getURITemplate().getValue());
    Method method = HashMap.class.getMethod("get", new Class[] { Object.class });
    OperationResourceInfo ori = cri.getMethodDispatcher().getOperationResourceInfo(method);
    assertNotNull(ori);
    assertEquals("/key/{id}", ori.getURITemplate().getValue());
    List<Parameter> params = ori.getParameters();
    assertNotNull(params);
    Parameter p = params.get(0);
    assertEquals("id", p.getName());
}
Also used : HashMap(java.util.HashMap) UserOperation(org.apache.cxf.jaxrs.model.UserOperation) UserResource(org.apache.cxf.jaxrs.model.UserResource) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Parameter(org.apache.cxf.jaxrs.model.Parameter) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 14 with UserOperation

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

the class ResourceUtilsTest method testUserResourceFromFile.

@Test
public void testUserResourceFromFile() throws Exception {
    List<UserResource> list = ResourceUtils.getUserResources("classpath:/resources.xml");
    assertNotNull(list);
    assertEquals(1, list.size());
    UserResource resource = list.get(0);
    assertEquals("java.util.Map", resource.getName());
    assertEquals("map", resource.getPath());
    assertEquals("application/xml", resource.getProduces());
    assertEquals("application/json", resource.getConsumes());
    UserOperation oper = resource.getOperations().get(0);
    assertEquals("putAll", oper.getName());
    assertEquals("/putAll", oper.getPath());
    assertEquals("PUT", oper.getVerb());
    assertEquals("application/json", oper.getProduces());
    assertEquals("application/xml", oper.getConsumes());
    Parameter p = oper.getParameters().get(0);
    assertEquals("map", p.getName());
    assertEquals("emptyMap", p.getDefaultValue());
    assertTrue(p.isEncoded());
    assertEquals("REQUEST_BODY", p.getType().toString());
}
Also used : 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 15 with UserOperation

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

the class ResourceUtils method createServiceClassResourceInfo.

public static ClassResourceInfo createServiceClassResourceInfo(Map<String, UserResource> resources, UserResource model, Class<?> sClass, boolean isRoot, boolean enableStatic, Bus bus) {
    if (model == null) {
        throw new RuntimeException("Resource class " + sClass.getName() + " has no model info");
    }
    ClassResourceInfo cri = new ClassResourceInfo(sClass, sClass, isRoot, enableStatic, true, model.getConsumes(), model.getProduces(), bus);
    URITemplate t = URITemplate.createTemplate(model.getPath());
    cri.setURITemplate(t);
    MethodDispatcher md = new MethodDispatcher();
    Map<String, UserOperation> ops = model.getOperationsAsMap();
    Method defaultMethod = null;
    Map<String, Method> methodNames = new HashMap<>();
    for (Method m : cri.getServiceClass().getMethods()) {
        if (m.getAnnotation(DefaultMethod.class) != null) {
            // if needed we can also support multiple default methods
            defaultMethod = m;
        }
        methodNames.put(m.getName(), m);
    }
    for (Map.Entry<String, UserOperation> entry : ops.entrySet()) {
        UserOperation op = entry.getValue();
        Method actualMethod = methodNames.get(op.getName());
        if (actualMethod == null) {
            actualMethod = defaultMethod;
        }
        if (actualMethod == null) {
            continue;
        }
        OperationResourceInfo ori = new OperationResourceInfo(actualMethod, cri, URITemplate.createTemplate(op.getPath()), op.getVerb(), op.getConsumes(), op.getProduces(), op.getParameters(), op.isOneway());
        String rClassName = actualMethod.getReturnType().getName();
        if (op.getVerb() == null) {
            if (resources.containsKey(rClassName)) {
                ClassResourceInfo subCri = rClassName.equals(model.getName()) ? cri : createServiceClassResourceInfo(resources, resources.get(rClassName), actualMethod.getReturnType(), false, enableStatic, bus);
                if (subCri != null) {
                    cri.addSubClassResourceInfo(subCri);
                    md.bind(ori, actualMethod);
                }
            }
        } else {
            md.bind(ori, actualMethod);
        }
    }
    cri.setMethodDispatcher(md);
    return checkMethodDispatcher(cri) ? cri : null;
}
Also used : HashMap(java.util.HashMap) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) URITemplate(org.apache.cxf.jaxrs.model.URITemplate) Method(java.lang.reflect.Method) DefaultMethod(org.apache.cxf.jaxrs.ext.DefaultMethod) UserOperation(org.apache.cxf.jaxrs.model.UserOperation) DefaultMethod(org.apache.cxf.jaxrs.ext.DefaultMethod) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MethodDispatcher(org.apache.cxf.jaxrs.model.MethodDispatcher) Map(java.util.Map) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

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