use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.
the class JAXRSServiceFactoryBeanTest method testSubResources.
@Test
public void testSubResources() throws Exception {
JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
sf.setEnableStaticResolution(true);
sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStore.class);
sf.create();
List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
assertEquals(1, resources.size());
// Verify root ClassResourceInfo: BookStore
ClassResourceInfo rootCri = resources.get(0);
assertNotNull(rootCri.getURITemplate());
URITemplate template = rootCri.getURITemplate();
MultivaluedMap<String, String> values = new MetadataMap<>();
assertTrue(template.match("/bookstore/books/123", values));
assertTrue(rootCri.hasSubResources());
MethodDispatcher md = rootCri.getMethodDispatcher();
assertEquals(7, md.getOperationResourceInfos().size());
for (OperationResourceInfo ori : md.getOperationResourceInfos()) {
if ("getDescription".equals(ori.getMethodToInvoke().getName())) {
assertEquals("GET", ori.getHttpMethod());
assertEquals("/path", ori.getURITemplate().getValue());
assertEquals("text/bar", ori.getProduceTypes().get(0).toString());
assertEquals("text/foo", ori.getConsumeTypes().get(0).toString());
assertFalse(ori.isSubResourceLocator());
} else if ("getAuthor".equals(ori.getMethodToInvoke().getName())) {
assertEquals("GET", ori.getHttpMethod());
assertEquals("/path2", ori.getURITemplate().getValue());
assertEquals("text/bar2", ori.getProduceTypes().get(0).toString());
assertEquals("text/foo2", ori.getConsumeTypes().get(0).toString());
assertFalse(ori.isSubResourceLocator());
} else if ("getBook".equals(ori.getMethodToInvoke().getName())) {
assertNull(ori.getHttpMethod());
assertNotNull(ori.getURITemplate());
assertTrue(ori.isSubResourceLocator());
} else if ("getNewBook".equals(ori.getMethodToInvoke().getName())) {
assertNull(ori.getHttpMethod());
assertNotNull(ori.getURITemplate());
assertTrue(ori.isSubResourceLocator());
} else if ("addBook".equals(ori.getMethodToInvoke().getName())) {
assertEquals("POST", ori.getHttpMethod());
assertNotNull(ori.getURITemplate());
assertFalse(ori.isSubResourceLocator());
} else if ("updateBook".equals(ori.getMethodToInvoke().getName())) {
assertEquals("PUT", ori.getHttpMethod());
assertNotNull(ori.getURITemplate());
assertFalse(ori.isSubResourceLocator());
} else if ("deleteBook".equals(ori.getMethodToInvoke().getName())) {
assertEquals("DELETE", ori.getHttpMethod());
assertNotNull(ori.getURITemplate());
assertFalse(ori.isSubResourceLocator());
} else {
fail("unexpected OperationResourceInfo" + ori.getMethodToInvoke().getName());
}
}
// Verify sub-resource ClassResourceInfo: Book
assertEquals(1, rootCri.getSubResources().size());
ClassResourceInfo subCri = rootCri.getSubResources().iterator().next();
assertNull(subCri.getURITemplate());
assertEquals(org.apache.cxf.jaxrs.resources.Book.class, subCri.getResourceClass());
MethodDispatcher subMd = subCri.getMethodDispatcher();
assertEquals(2, subMd.getOperationResourceInfos().size());
// getChapter method
OperationResourceInfo subOri = subMd.getOperationResourceInfos().iterator().next();
assertEquals("GET", subOri.getHttpMethod());
assertNotNull(subOri.getURITemplate());
// getState method
OperationResourceInfo subOri2 = subMd.getOperationResourceInfos().iterator().next();
assertEquals("GET", subOri2.getHttpMethod());
assertNotNull(subOri2.getURITemplate());
}
use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.
the class SelectMethodCandidatesTest method testSelectUsingQualityFactors.
@Test
public void testSelectUsingQualityFactors() throws Exception {
JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
sf.setResourceClasses(org.apache.cxf.jaxrs.resources.TestResource.class);
sf.create();
List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
String contentTypes = "*/*";
String acceptContentTypes = "application/xml;q=0.5,application/json";
MetadataMap<String, String> values = new MetadataMap<>();
OperationResourceInfo ori = findTargetResourceClass(resources, createMessage(), "/1/2/3/d/resource1", "GET", values, contentTypes, sortMediaTypes(acceptContentTypes));
assertNotNull(ori);
assertEquals("jsonResource needs to be selected", "jsonResource", ori.getMethodToInvoke().getName());
}
use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.
the class SelectMethodCandidatesTest method testFindFromAbstractGenericImpl4.
@Test
public void testFindFromAbstractGenericImpl4() throws Exception {
JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
sf.setResourceClasses(GenericEntityImpl4.class);
sf.create();
List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
String contentTypes = "text/xml";
String acceptContentTypes = "text/xml";
Message m = new MessageImpl();
m.put(Message.CONTENT_TYPE, "text/xml");
Exchange ex = new ExchangeImpl();
ex.setInMessage(m);
m.setExchange(ex);
Endpoint e = mockEndpoint();
ex.put(Endpoint.class, e);
MetadataMap<String, String> values = new MetadataMap<>();
OperationResourceInfo ori = findTargetResourceClass(resources, m, "/books", "POST", values, contentTypes, sortMediaTypes(acceptContentTypes));
assertNotNull(ori);
assertEquals("resourceMethod needs to be selected", "postEntity", ori.getMethodToInvoke().getName());
String value = "<Books><Book><name>The Book</name><id>2</id></Book></Books>";
m.setContent(InputStream.class, new ByteArrayInputStream(value.getBytes()));
List<Object> params = JAXRSUtils.processParameters(ori, values, m);
assertEquals(1, params.size());
List<?> books = (List<?>) params.get(0);
assertEquals(1, books.size());
Book book = (Book) books.get(0);
assertNotNull(book);
assertEquals(2L, book.getId());
assertEquals("The Book", book.getName());
}
use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.
the class SelectMethodCandidatesTest method doTestFindTargetSubResource.
public void doTestFindTargetSubResource(String path, String method, boolean setKeepSubProp) throws Exception {
JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
sf.setResourceClasses(org.apache.cxf.jaxrs.resources.TestResource.class);
sf.create();
List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
String contentTypes = "*/*";
String acceptContentTypes = "text/xml,*/*";
MetadataMap<String, String> values = new MetadataMap<>();
OperationResourceInfo ori = findTargetResourceClass(resources, createMessage(), path, "GET", values, contentTypes, sortMediaTypes(acceptContentTypes), setKeepSubProp);
assertNotNull(ori);
assertEquals("resourceMethod needs to be selected", method, ori.getMethodToInvoke().getName());
}
use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.
the class SelectMethodCandidatesTest method testFindFromAbstractGenericClass3.
@Test
public void testFindFromAbstractGenericClass3() throws Exception {
JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
sf.setResourceClasses(BookEntity.class);
sf.create();
List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
String contentTypes = "text/xml";
String acceptContentTypes = "text/xml";
Message m = new MessageImpl();
m.put(Message.CONTENT_TYPE, "text/xml");
Exchange ex = new ExchangeImpl();
ex.setInMessage(m);
m.setExchange(ex);
Endpoint e = mockEndpoint();
ex.put(Endpoint.class, e);
MetadataMap<String, String> values = new MetadataMap<>();
OperationResourceInfo ori = findTargetResourceClass(resources, m, "/books", "PUT", values, contentTypes, sortMediaTypes(acceptContentTypes));
assertNotNull(ori);
assertEquals("resourceMethod needs to be selected", "putEntity", ori.getMethodToInvoke().getName());
String value = "<Chapter><title>The Book</title><id>2</id></Chapter>";
m.setContent(InputStream.class, new ByteArrayInputStream(value.getBytes()));
List<Object> params = JAXRSUtils.processParameters(ori, values, m);
assertEquals(1, params.size());
Chapter c = (Chapter) params.get(0);
assertNotNull(c);
assertEquals(2L, c.getId());
assertEquals("The Book", c.getTitle());
}
Aggregations