use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL in project application by collectionspace.
the class TestService method testCRUD.
// TODO merge this method with testPostGetDelete - this is Chris's temporary
// fork to help testing repeatable fields
/**
* Test Create, Update, Read and Delete for a record type
*
* @param serviceurl
* @param partname
* @param Createfilename
* @param Updatefilename
* @param xpath
* @param expected
*/
private void testCRUD(String serviceurl, String partname, String Createfilename, String Updatefilename, String xpath, String expected) throws Exception {
ReturnedURL url;
log.info("Testing " + serviceurl + " with " + Createfilename + " and partname=" + partname);
// POST (Create)
if (partname != null) {
Map<String, Document> parts = new HashMap<String, Document>();
parts.put(partname, getDocument(Createfilename));
url = conn.getMultipartURL(RequestMethod.POST, serviceurl, parts, creds, cache);
} else {
url = conn.getURL(RequestMethod.POST, serviceurl, getDocument(Createfilename), creds, cache);
}
assertEquals(201, url.getStatus());
// ensures e.g.
assertTrue(url.getURL().startsWith("/" + serviceurl));
// CSPACE-305
// hasn't
// regressed
// 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(200, getStatus);
assertNotNull(doc);
Node n = doc.selectSingleNode(xpath);
assertNotNull(n);
String text = n.getText();
assertEquals(expected, text);
// Update
if (partname != null) {
Map<String, Document> parts = new HashMap<String, Document>();
parts.put(partname, getDocument(Updatefilename));
conn.getMultipartXMLDocument(RequestMethod.PUT, url.getURL(), parts, creds, cache);
ReturnedMultipartDocument rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
getStatus = rdocs.getStatus();
doc = rdocs.getDocument(partname);
} else {
conn.getXMLDocument(RequestMethod.PUT, url.getURL(), getDocument(Updatefilename), creds, cache);
ReturnedDocument rdoc = conn.getXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
getStatus = rdoc.getStatus();
doc = rdoc.getDocument();
}
assertEquals(200, getStatus);
assertNotNull(doc);
n = doc.selectSingleNode(xpath);
assertNotNull(n);
text = n.getText();
assertEquals(expected, text);
// log.info(doc.asXML());
// Get
// 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);
// 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();
}
// ensures CSPACE-209 hasn't regressed
assertEquals(404, getStatus);
assertNull(doc);
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL 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.ReturnedURL in project application by collectionspace.
the class TestService method testAuthorityCreateUpdateDelete.
/*
* @Ignore public void testRolePermissionsPost() throws Exception { //
* ReturnedURLurl=conn.getURL(RequestMethod.POST,
* "authorization/roles/cbdb4f45-2fac-461b-93ef-6fec21a2ad97/permroles"
* ,getDocument("rolepermissions.xml"),creds,cache); //
* assertEquals(201,url.getStatus());
*
*
*
* // TODO check whether should be commented back in - Chris was debugging
* permissions // NOTE this test is more complex than PostGetDelete and
* perhaps should remain as a separate test? //create a permission //
* Map<String,Document> parts=new HashMap<String,Document>(); // ReturnedURL
* url
* =conn.getURL(RequestMethod.POST,"authorization/permissions/",getDocument
* ("permissions.xml"),creds,cache); // assertEquals(201,url.getStatus());
*
* //create permissionRole for the permission above // url =
* conn.getURL(RequestMethod.POST,
* "authorization/permissions/"+url.getURLTail()+"/permroles",
* getDocument("rolepermissions.xml"), creds, cache); // assertEquals(201,
* url.getStatus()); //delete the permissionRole // int
* status=conn.getNone(RequestMethod.DELETE,url.getURL(),null,creds,cache);
* // assertEquals(200,status); // XXX CSPACE-73, should be 404
*
* //delete the permission //
* status=conn.getNone(RequestMethod.DELETE,url.getURL(),null,creds,cache);
* // assertEquals(200,status); // XXX CSPACE-73, should be 404 }
*/
@Test
public void testAuthorityCreateUpdateDelete() throws Exception {
Map<String, Document> parts = new HashMap<String, Document>();
parts.put("personauthorities_common", getDocument("personAuth.xml"));
Map<String, Document> parts1 = new HashMap<String, Document>();
parts1.put("personauthorities_common", getDocument("personAuth.xml"));
String id;
// CREATE
ReturnedURL url = conn.getMultipartURL(RequestMethod.POST, "personauthorities/", parts, creds, cache);
assertEquals(201, url.getStatus());
id = url.getURLTail();
// UPDATE
ReturnedMultipartDocument doc = conn.getMultipartXMLDocument(RequestMethod.PUT, "personauthorities/" + id, parts1, creds, cache);
// XXX shouldn't this be 201?
assertEquals(200, doc.getStatus());
// DELETE
conn.getNone(RequestMethod.DELETE, "personauthorities/" + id, null, creds, cache);
assertEquals(200, doc.getStatus());
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL 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.ReturnedURL 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));
}
Aggregations