use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument in project application by collectionspace.
the class TestService method testPersonContactPostViaCSIDs.
@Test
public void testPersonContactPostViaCSIDs() throws Exception {
String filename = "";
String partname = "";
ReturnedURL url = null;
Map<String, Document> parts = new HashMap<String, Document>();
StringBuilder serviceurl = new StringBuilder("");
ReturnedMultipartDocument rdocs = null;
ReturnedDocument rdoc = null;
int status = 0;
Document doc = null;
String text = "";
String xpath = "";
// POST (Create) a person authority
serviceurl.append("personauthorities/");
partname = "personauthorities_common";
filename = "personAuth.xml";
log.info("Testing create at " + serviceurl + " with " + filename + " and partname=" + partname);
parts.put(partname, getDocument(filename));
url = conn.getMultipartURL(RequestMethod.POST, serviceurl.toString(), parts, creds, cache);
assertEquals(201, url.getStatus());
String authUrl = url.getURL();
String authId = url.getURLTail();
// Test creation with a GET
if (partname != null) {
rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, authUrl, null, creds, cache);
status = rdocs.getStatus();
doc = rdocs.getDocument(partname);
}
assertEquals(200, status);
assertNotNull(doc);
log.info("CREATED PERSONAUTHORITY AT " + authUrl);
// POST (Create) a person item within the person authority
serviceurl.append(authId + "/items/");
partname = "persons_common";
String partname1 = "relations-common-list";
filename = "personItem.xml";
String filename1 = "relationshipItem.xml";
log.info("Testing create at " + serviceurl + " with " + filename + " and partname=" + partname);
if (partname != null) {
parts = new HashMap<String, Document>();
parts.put(partname, getDocument(filename));
// parts.put(partname1, getDocument(filename1));
url = conn.getMultipartURL(RequestMethod.POST, serviceurl.toString(), parts, creds, cache);
}
assertEquals(201, url.getStatus());
String itemUrl = url.getURL();
String itemId = url.getURLTail();
// Test creation with a GET
if (partname != null) {
rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, itemUrl, null, creds, cache);
status = rdocs.getStatus();
doc = rdocs.getDocument(partname);
}
assertEquals(200, status);
assertNotNull(doc);
// Test that the parent authority lists this item as a child
String parentUrl = authUrl + "/items/";
log.info("LIST from " + parentUrl);
rdoc = conn.getXMLDocument(RequestMethod.GET, parentUrl, null, creds, cache);
status = rdoc.getStatus();
doc = rdoc.getDocument();
assertEquals(200, status);
log.info(doc.asXML());
xpath = "//totalItems";
Node n = doc.selectSingleNode(xpath);
assertNotNull(n);
text = n.getText();
assertNotNull(text);
log.info("Value of XPath expression '" + xpath + "' = " + text);
assert (!text.trim().equals("0"));
xpath = "//list-item/csid";
List<Node> nodes = doc.selectNodes(xpath);
assertNotNull(nodes);
assert (nodes.size() > 0);
boolean foundItemInAuthority = false;
for (Node node : nodes) {
log.info("found '" + node.getText().trim() + "' comparing to " + itemId);
if (node.getText().trim().equals(itemId)) {
foundItemInAuthority = true;
}
}
assert (foundItemInAuthority);
log.info("CREATED PERSON AT " + itemUrl);
// POST (Create) a contact sub-resource within the person item
// and perform a full POST, GET, DELETE cycle on that contact
serviceurl.append(itemId + "/contacts");
partname = "contacts_common";
filename = "personItemContact.xml";
log.info("ADDING CONTACT USING THIS URL " + serviceurl);
testPostGetDelete(serviceurl.toString(), partname, filename, "contacts_common/emailGroupList/emailGroup/email", "test@example.com");
// DELETE (Delete) the person item within the person authority
status = conn.getNone(RequestMethod.DELETE, itemUrl, null, creds, cache);
assertEquals(200, status);
// Now try to delete non-existent (make sure CSPACE-73 hasn't regressed)
status = conn.getNone(RequestMethod.DELETE, itemUrl, null, creds, cache);
assertEquals(404, status);
// GET once more to make sure it isn't there
if (partname != null) {
rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, itemUrl, null, creds, cache);
status = rdocs.getStatus();
doc = rdocs.getDocument(partname);
}
// ensures CSPACE-209 hasn't regressed
assertEquals(404, status);
assertNull(doc);
log.info("DELETED PERSON");
// DELETE (Delete) the person authority
status = conn.getNone(RequestMethod.DELETE, authUrl, null, creds, cache);
assertEquals(200, status);
// Now try to delete non-existent (make sure CSPACE-73 hasn't regressed)
status = conn.getNone(RequestMethod.DELETE, authUrl, null, creds, cache);
assertEquals(404, status);
// GET once more to make sure it isn't there
if (partname != null) {
rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, authUrl, null, creds, cache);
status = rdocs.getStatus();
doc = rdocs.getDocument(partname);
}
// ensures CSPACE-209 hasn't regressed
assertEquals(404, status);
assertNull(doc);
log.info("DELETED PERSON AUTHORITY");
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument in project application by collectionspace.
the class TestService method testOrgContact.
// @Test
public void testOrgContact() throws Exception {
String serviceurl = "orgauthorities/urn:cspace:name(organization)/items";
String filename = "orgItem.xml";
String partname = "organizations_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());
// doesn't work because name urn gets translated to id
// assertTrue(url.getURL().startsWith("/"+serviceurl)); // ensures e.g.
// CSPACE-305 hasn't regressed
log.info("CREATE ORG" + url.getURL());
// create contact person
String serviceurlContact = "orgauthorities/urn:cspace:name(organization)/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 ORG");
// 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.ReturnedMultipartDocument in project application by collectionspace.
the class TestService method testNewPersonAuthority.
@Test
public void testNewPersonAuthority() throws Exception {
String personAuthFile = "personAuth.xml";
String personAuthItemFile = "personAuthItem.xml";
String personAuthpartname = "personauthorities_common";
String personAuthServiceUrl = "personauthorities/";
Map<String, Document> parts = new HashMap<String, Document>();
parts.put(personAuthpartname, getDocument(personAuthFile));
log.info("Testing create at " + personAuthServiceUrl + " with " + personAuthFile + " and partname=" + personAuthpartname);
ReturnedURL url = conn.getMultipartURL(RequestMethod.POST, personAuthServiceUrl, parts, creds, cache);
assertEquals(201, url.getStatus());
String id = url.getURLTail();
log.info(url.getURL());
String personAuthUrl = url.getURL();
// Test creation with a GET
ReturnedMultipartDocument rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, personAuthUrl, null, creds, cache);
int status = rdocs.getStatus();
assertEquals(200, status);
Document doc = rdocs.getDocument(personAuthpartname);
log.info(doc.asXML());
// DELETE
conn.getNone(RequestMethod.DELETE, "personauthorities/" + id, null, creds, cache);
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument in project application by collectionspace.
the class TestService method testObjectsPut.
@Test
public void testObjectsPut() throws Exception {
Map<String, Document> parts = new HashMap<String, Document>();
parts.put("collectionobjects_common", getDocument("obj1.xml"));
ReturnedURL url = conn.getMultipartURL(RequestMethod.POST, "collectionobjects/", parts, creds, cache);
assertEquals(201, url.getStatus());
ReturnedMultipartDocument doc = conn.getMultipartXMLDocument(RequestMethod.PUT, url.getURL(), buildObject("32", "obj2.xml", "collectionobjects_common"), creds, cache);
// 201?
assertEquals(201, url.getStatus());
doc = conn.getMultipartXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
assertEquals(200, doc.getStatus());
String num = doc.getDocument("collectionobjects_common").selectSingleNode("collectionobjects_common/objectNumber").getText();
assertEquals("32", num);
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument in project application by collectionspace.
the class AuthorizationStorage method simpleRetrieveJSONFullPath.
public JSONObject simpleRetrieveJSONFullPath(CSPRequestCredentials creds, CSPRequestCache cache, String filePath, Record thisr) throws ExistException, UnimplementedException, UnderlyingStorageException {
try {
JSONObject out = new JSONObject();
if (thisr.isMultipart()) {
ReturnedMultipartDocument doc = conn.getMultipartXMLDocument(RequestMethod.GET, filePath, null, creds, cache);
if ((doc.getStatus() < 200 || doc.getStatus() >= 300))
throw new UnderlyingStorageException("Does not exist ", doc.getStatus(), filePath);
for (String section : thisr.getServicesRecordPathKeys()) {
String path = thisr.getServicesRecordPath(section);
String[] parts = path.split(":", 2);
convertToJson(out, doc.getDocument(parts[0]), thisr, "GET", section, "", "");
}
} else {
ReturnedDocument doc = conn.getXMLDocument(RequestMethod.GET, filePath, null, creds, cache);
if ((doc.getStatus() < 200 || doc.getStatus() >= 300)) {
if (doc.getStatus() == 401) {
throw new UnauthorizedException("Username and/or password are invalid.", doc.getStatus(), filePath);
} else if (doc.getStatus() == 409) {
throw new ConflictException("Conflict with request. The user's tenant may be disabled. Contact your CollectionSpace administrator.", doc.getStatus(), filePath);
} else {
String status = Integer.toString(doc.getStatus());
throw new UnderlyingStorageException("Does not exist ", doc.getStatus(), filePath);
}
}
convertToJson(out, doc.getDocument(), thisr, "GET", "common", "");
}
return out;
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
} catch (JSONException e) {
throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e);
}
}
Aggregations