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());
}
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());
}
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());
}
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());
}
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;
}
Aggregations