use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument in project application by collectionspace.
the class TestService method testReporting.
// @Test
// remove test as never know if all the bits for the report are there to test
public void testReporting() throws Exception {
ReturnedURL url;
int getStatus;
Document doc;
String serviceurl = "acquisitions/";
String partname = "acquisitions_common";
String filename = "acquisitionXMLJSON.xml";
String xpath = "acquisitions_common/accessionDate";
String expected = "April 1, 2010";
// POST (Create Acquisition Record)
if (partname != null) {
Map<String, Document> parts = new HashMap<String, Document>();
parts.put(partname, getDocument(filename));
url = conn.getMultipartURL(RequestMethod.POST, serviceurl, parts, creds, cache);
} else {
url = conn.getURL(RequestMethod.POST, serviceurl, getDocument(filename), creds, cache);
}
assertEquals("Failed to receive 201 status code on create", 201, url.getStatus());
// find report
ReturnedDocument doc3 = conn.getXMLDocument(RequestMethod.GET, "reports?doctype=Acquisition", null, creds, cache);
assertEquals(200, doc3.getStatus());
Set<String> csids = new HashSet<String>();
String reportcsid = "";
for (Node n : (List<Node>) doc3.getDocument().selectNodes("abstract-common-list/list-item/csid")) {
reportcsid = n.getText();
}
assertFalse("No Acquisition report to test with", reportcsid.equals(""));
if (!reportcsid.equals("")) {
// only runs if there is a report to run
String reportsurl = "reports/" + reportcsid;
String csid = url.getURLTail();
Document report = getDocument("reportrun.xml");
report.selectSingleNode("invocationContext/singleCSID").setText(csid);
// DO REPORT
// run report
ReturnUnknown doc2 = conn.getReportDocument(RequestMethod.POST, reportsurl, report, creds, cache);
JSONObject out = new JSONObject();
out.put("getByteBody", doc2.getBytes());
out.put("contenttype", doc2.getContentType());
assertEquals("Failed to receive 200 status code on create", 200, doc2.getStatus());
}
// DELETE (Delete Acquisition)
int status = conn.getNone(RequestMethod.DELETE, url.getURL(), null, creds, cache);
assertEquals("Failed to receive expected 200 status code on delete", 200, status);
// Now try to delete non-existent (make sure CSPACE-73 hasn't regressed)
status = conn.getNone(RequestMethod.DELETE, url.getURL(), null, creds, cache);
assertEquals("Failed to receive expected 404 status code on repeated delete of same record", 404, status);
log.info("DELETE");
// GET once more to make sure it isn't there
if (partname != null) {
ReturnedMultipartDocument rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
getStatus = rdocs.getStatus();
doc = rdocs.getDocument(partname);
} else {
ReturnedDocument rdoc = conn.getXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
getStatus = rdoc.getStatus();
doc = rdoc.getDocument();
}
assertEquals("Failed to receive expected 404 status code on repeated delete of same record", 404, // ensures CSPACE-209 hasn't regressed
getStatus);
assertNull("Contents of deleted record were unexpectedly not null", doc);
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument in project application by collectionspace.
the class TestService method testPostGetDelete.
private void testPostGetDelete(String serviceurl, String partname, String filename, String xpath, String expected) throws Exception {
ReturnedURL url;
log.info("Testing " + serviceurl + " with " + filename + " and partname=" + partname);
// POST (Create)
if (partname != null) {
Map<String, Document> parts = new HashMap<String, Document>();
parts.put(partname, getDocument(filename));
url = conn.getMultipartURL(RequestMethod.POST, serviceurl, parts, creds, cache);
} else {
url = conn.getURL(RequestMethod.POST, serviceurl, getDocument(filename), creds, cache);
}
assertEquals("Failed to receive 201 status code on create", 201, url.getStatus());
// assertTrue(url.getURL().startsWith("/"+serviceurl)); // ensures e.g.
// CSPACE-305 hasn't regressed
log.info("CREATED RECORD " + url.getURL());
// GET (Read)
int getStatus;
Document doc;
if (partname != null) {
ReturnedMultipartDocument rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
getStatus = rdocs.getStatus();
doc = rdocs.getDocument(partname);
} else {
ReturnedDocument rdoc = conn.getXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
getStatus = rdoc.getStatus();
doc = rdoc.getDocument();
}
assertEquals("Failed to receive expected 200 status code on read", 200, getStatus);
log.trace("RETRIEVED RECORD " + doc.asXML());
assertNotNull("Record received on read was unexpectedly null", doc);
Node n = doc.selectSingleNode(xpath);
assertNotNull("Expected XPath expression was not found in record", n);
String text = n.getText();
assertEquals("Expected value was not found in record", expected, text);
// List
log.info("LIST from " + serviceurl);
ReturnedDocument rdoc1 = conn.getXMLDocument(RequestMethod.GET, "/" + serviceurl, null, creds, cache);
getStatus = rdoc1.getStatus();
doc = rdoc1.getDocument();
assertEquals("Failed to receive expected 200 status code on list read", 200, getStatus);
log.trace("LISTLISTLIST");
log.trace(doc.asXML());
log.trace("LISTLISTLIST");
// DELETE (Delete)
int status = conn.getNone(RequestMethod.DELETE, url.getURL(), null, creds, cache);
assertEquals("Failed to receive expected 200 status code on delete", 200, status);
// Now try to delete non-existent (make sure CSPACE-73 hasn't regressed)
status = conn.getNone(RequestMethod.DELETE, url.getURL(), null, creds, cache);
assertEquals("Failed to receive expected 404 status code on repeated delete of same record", 404, status);
log.info("DELETE");
// GET once more to make sure it isn't there
if (partname != null) {
ReturnedMultipartDocument rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
getStatus = rdocs.getStatus();
doc = rdocs.getDocument(partname);
} else {
ReturnedDocument rdoc = conn.getXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
getStatus = rdoc.getStatus();
doc = rdoc.getDocument();
}
assertEquals("Failed to receive expected 404 status code on repeated delete of same record", 404, // ensures CSPACE-209 hasn't regressed
getStatus);
assertNull("Contents of deleted record were unexpectedly not null", doc);
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument in project application by collectionspace.
the class TestService method testSearch.
@SuppressWarnings("unchecked")
@Test
public void testSearch() throws Exception {
// Insert one non-aardvark
Map<String, Document> parts = new HashMap<String, Document>();
Document doc1 = getDocument("obj1.xml");
parts.put("collectionobjects_common", doc1);
ReturnedURL url = conn.getMultipartURL(RequestMethod.POST, "collectionobjects/", parts, creds, cache);
assertEquals(201, url.getStatus());
String uid1 = url.getURL();
String non = url.getURLTail();
// Insert one aardvark
parts = new HashMap<String, Document>();
Document doc2 = getDocument("obj-search.xml");
parts.put("collectionobjects_common", doc2);
url = conn.getMultipartURL(RequestMethod.POST, "collectionobjects/", parts, creds, cache);
String uid2 = url.getURL();
assertEquals(201, url.getStatus());
String good = url.getURLTail();
// search for aardvark
ReturnedDocument doc = conn.getXMLDocument(RequestMethod.GET, "collectionobjects?kw=aardvark", null, creds, cache);
assertEquals(200, doc.getStatus());
Set<String> csids = new HashSet<String>();
for (Node n : (List<Node>) doc.getDocument().selectNodes("abstract-common-list/list-item/csid")) {
csids.add(n.getText());
}
// delete non-aadvark and aadvark
int status = conn.getNone(RequestMethod.DELETE, uid1, null, creds, cache);
// XXX CSPACE-73, should be 404
assertEquals(200, status);
status = conn.getNone(RequestMethod.DELETE, uid2, null, creds, cache);
// XXX CSPACE-73, should be 404
assertEquals(200, status);
// test
assertFalse(csids.size() == 0);
assertTrue(csids.contains(good));
assertFalse(csids.contains(non));
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument in project application by collectionspace.
the class TestService method testPersonContact.
// @Test
public void testPersonContact() throws Exception {
String serviceurl = "personauthorities/urn:cspace:name(person)/items";
String filename = "personItem.xml";
String partname = "persons_common";
ReturnedURL url;
log.info("Testing " + serviceurl + " with " + filename + " and partname=" + partname);
// POST (Create)
if (partname != null) {
Map<String, Document> parts = new HashMap<String, Document>();
parts.put(partname, getDocument(filename));
url = conn.getMultipartURL(RequestMethod.POST, serviceurl, parts, creds, cache);
} else {
url = conn.getURL(RequestMethod.POST, serviceurl, getDocument(filename), creds, cache);
}
assertEquals(201, url.getStatus());
// log.info("CREATE PERSON" + url.getURL());
// assertTrue(url.getURL().startsWith("/"+serviceurl)); // ensures e.g.
// CSPACE-305 hasn't regressed
log.info("CREATE PERSON" + url.getURL());
// create contact person
String serviceurlContact = "personauthorities/urn:cspace:name(person)/items/" + url.getURLTail() + "/contacts";
String filenameContact = "personItemContact.xml";
String partnameContact = "contacts_common";
log.info("ADD CONTACT USING THIS URL " + serviceurlContact);
testPostGetDelete(serviceurlContact, partnameContact, filenameContact, "contacts_common/email", "email@example.com");
// DELETE (Delete)
int status = conn.getNone(RequestMethod.DELETE, url.getURL(), null, creds, cache);
assertEquals(200, status);
// Now try to delete non-existent (make sure CSPACE-73 hasn't regressed)
status = conn.getNone(RequestMethod.DELETE, url.getURL(), null, creds, cache);
assertEquals(404, status);
log.info("DELETE PERSON");
// GET once more to make sure it isn't there
int getStatus;
Document doc;
if (partname != null) {
ReturnedMultipartDocument rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
getStatus = rdocs.getStatus();
doc = rdocs.getDocument(partname);
} else {
ReturnedDocument rdoc = conn.getXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
getStatus = rdoc.getStatus();
doc = rdoc.getDocument();
}
// ensures CSPACE-209 hasn't regressed
assertEquals(404, getStatus);
assertNull(doc);
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument in project application by collectionspace.
the class AuthorizationStorage method refViewRetrieveJSON.
@Override
public JSONObject refViewRetrieveJSON(ContextualisedStorage storage, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException, JSONException {
try {
JSONObject out = new JSONObject();
// not all the records need a reference, look in cspace-config.xml for which that don't
if (r.hasTermsUsed()) {
String path = r.getServicesURL() + "/" + filePath + "/authorityrefs";
ReturnedDocument all = conn.getXMLDocument(RequestMethod.GET, path, null, creds, cache);
if (all.getStatus() != 200)
throw new ConnectionException("Bad request problem in AuthorizationStorage/refViewRetrieve: status not 200", all.getStatus(), path);
Document list = all.getDocument();
for (Object node : list.selectNodes("authority-ref-list/authority-ref-item")) {
if (!(node instanceof Element))
continue;
String key = ((Element) node).selectSingleNode("sourceField").getText();
String uri = ((Element) node).selectSingleNode("uri").getText();
String refname = ((Element) node).selectSingleNode("refName").getText();
if (uri != null && uri.startsWith("/"))
uri = uri.substring(1);
JSONObject data = miniForURI(storage, creds, cache, refname, uri, restrictions);
out.put(key, data);
}
}
return out;
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Connection problem in AuthorizationStorage:" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
}
}
Aggregations