From 17a78c4dad39ba3fd3524c1627347825d27a8e6a Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Tue, 27 Apr 2010 22:44:35 +0000 Subject: [PATCH] CSPACE-1615: Organization service now supports retrieving all authority terms used in an Organization record. --- services/organization/client/pom.xml | 5 + .../services/client/OrgAuthorityClient.java | 7 +- .../services/client/OrgAuthorityProxy.java | 10 +- .../client/test/OrgAuthorityAuthRefsTest.java | 339 ++++++++++++++++++ .../organization/OrgAuthorityResource.java | 57 ++- 5 files changed, 413 insertions(+), 5 deletions(-) create mode 100644 services/organization/client/src/test/java/org/collectionspace/services/client/test/OrgAuthorityAuthRefsTest.java diff --git a/services/organization/client/pom.xml b/services/organization/client/pom.xml index 78e322700..3c370a1f5 100644 --- a/services/organization/client/pom.xml +++ b/services/organization/client/pom.xml @@ -41,6 +41,11 @@ org.collectionspace.services.contact.client ${project.version} + + org.collectionspace.services + org.collectionspace.services.person.client + ${project.version} + org.testng diff --git a/services/organization/client/src/main/java/org/collectionspace/services/client/OrgAuthorityClient.java b/services/organization/client/src/main/java/org/collectionspace/services/client/OrgAuthorityClient.java index b4c071c7a..3e8ed0a7e 100644 --- a/services/organization/client/src/main/java/org/collectionspace/services/client/OrgAuthorityClient.java +++ b/services/organization/client/src/main/java/org/collectionspace/services/client/OrgAuthorityClient.java @@ -3,6 +3,7 @@ package org.collectionspace.services.client; import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; +import org.collectionspace.services.common.authorityref.AuthorityRefList; import org.collectionspace.services.contact.ContactsCommonList; import org.collectionspace.services.organization.OrgauthoritiesCommonList; import org.collectionspace.services.organization.OrganizationsCommonList; @@ -145,6 +146,10 @@ public class OrgAuthorityClient extends AbstractServiceClientImpl { return orgAuthorityProxy.readItemListForNamedAuthority(specifier); } + public ClientResponse getItemAuthorityRefs(String parentcsid, String csid) { + return orgAuthorityProxy.getItemAuthorityRefs(parentcsid, csid); + } + /** * @param csid * @return @@ -183,7 +188,7 @@ public class OrgAuthorityClient extends AbstractServiceClientImpl { return orgAuthorityProxy.deleteItem(vcsid, csid); } - public ClientResponse createContact(String parentcsid, + public ClientResponse createContact(String parentcsid, String itemcsid, MultipartOutput multipart) { return orgAuthorityProxy.createContact(parentcsid, itemcsid, multipart); } diff --git a/services/organization/client/src/main/java/org/collectionspace/services/client/OrgAuthorityProxy.java b/services/organization/client/src/main/java/org/collectionspace/services/client/OrgAuthorityProxy.java index 74d88d01f..60332c1bc 100644 --- a/services/organization/client/src/main/java/org/collectionspace/services/client/OrgAuthorityProxy.java +++ b/services/organization/client/src/main/java/org/collectionspace/services/client/OrgAuthorityProxy.java @@ -10,6 +10,7 @@ import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; +import org.collectionspace.services.common.authorityref.AuthorityRefList; import org.collectionspace.services.contact.ContactsCommonList; import org.collectionspace.services.organization.OrgauthoritiesCommonList; import org.collectionspace.services.organization.OrganizationsCommonList; @@ -66,6 +67,14 @@ public interface OrgAuthorityProxy { @Path("/urn:cspace:name({specifier})/items/") ClientResponse readItemListForNamedAuthority(@PathParam("specifier") String specifier); + // List Item Authority References + @GET + @Produces({"application/xml"}) + @Path("/{parentcsid}/items/{itemcsid}/authorityrefs/") + public ClientResponse getItemAuthorityRefs( + @PathParam("parentcsid") String parentcsid, + @PathParam("itemcsid") String itemcsid); + //(C)reate Item @POST @Path("/{vcsid}/items/") @@ -127,5 +136,4 @@ public interface OrgAuthorityProxy { @PathParam("itemcsid") String itemcsid, @PathParam("csid") String csid); - } diff --git a/services/organization/client/src/test/java/org/collectionspace/services/client/test/OrgAuthorityAuthRefsTest.java b/services/organization/client/src/test/java/org/collectionspace/services/client/test/OrgAuthorityAuthRefsTest.java new file mode 100644 index 000000000..9fb22eb66 --- /dev/null +++ b/services/organization/client/src/test/java/org/collectionspace/services/client/test/OrgAuthorityAuthRefsTest.java @@ -0,0 +1,339 @@ +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + * + * http://www.collectionspace.org + * http://wiki.collectionspace.org + * + * Copyright © 2009 Regents of the University of California + * + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + * + * You may obtain a copy of the ECL 2.0 License at + * https://source.collectionspace.org/collection-space/LICENSE.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.collectionspace.services.client.test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.ws.rs.core.Response; + +import org.collectionspace.services.OrganizationJAXBSchema; +import org.collectionspace.services.PersonJAXBSchema; +import org.collectionspace.services.client.OrgAuthorityClient; +import org.collectionspace.services.client.OrgAuthorityClientUtils; +import org.collectionspace.services.client.PersonAuthorityClient; +import org.collectionspace.services.client.PersonAuthorityClientUtils; +import org.collectionspace.services.common.authorityref.AuthorityRefList; +import org.collectionspace.services.common.authorityref.AuthorityRefList.AuthorityRefItem; +import org.collectionspace.services.organization.OrgauthoritiesCommon; +import org.collectionspace.services.organization.OrganizationsCommon; + +import org.jboss.resteasy.client.ClientResponse; + +import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; +import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput; +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.Test; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * LoaninAuthRefsTest, carries out Authority References tests against a + * deployed and running Loanin (aka Loans In) Service. + * + * $LastChangedRevision: 1327 $ + * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $ + */ +public class OrgAuthorityAuthRefsTest extends BaseServiceTest { + + private final Logger logger = + LoggerFactory.getLogger(OrgAuthorityAuthRefsTest.class); + + // Instance variables specific to this test. + final String SERVICE_PATH_COMPONENT = "orgauthorities"; + final String PERSON_AUTHORITY_NAME = "TestPersonAuth"; + private String knownResourceRefName = null; + private String knownAuthResourceId = null; + private String knownItemId = null; + private List allResourceIdsCreated = new ArrayList(); + private Map allItemResourceIdsCreated = + new HashMap(); + private List personIdsCreated = new ArrayList(); + private int CREATED_STATUS = Response.Status.CREATED.getStatusCode(); + private int OK_STATUS = Response.Status.OK.getStatusCode(); + private String personAuthCSID = null; + private String organizationContactPersonRefName = null; + private final int NUM_AUTH_REFS_EXPECTED = 1; + + // --------------------------------------------------------------- + // CRUD tests : CREATE tests + // --------------------------------------------------------------- + // Success outcomes + @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class) + public void createWithAuthRefs(String testName) throws Exception { + + testSetup(CREATED_STATUS, ServiceRequestType.CREATE,testName); + + // Create a new Organization Authority resource. + OrgAuthorityClient orgAuthClient = new OrgAuthorityClient(); + String identifier = createIdentifier(); + String displayName = "TestOrgAuth-" + identifier; + boolean createWithDisplayName = false; + knownResourceRefName = + OrgAuthorityClientUtils.createOrgAuthRefName(displayName, createWithDisplayName); + MultipartOutput multipart = + OrgAuthorityClientUtils.createOrgAuthorityInstance( + displayName, knownResourceRefName, new OrgAuthorityClient().getCommonPartName()); + + // Submit the request to the service and store the response. + ClientResponse res = orgAuthClient.create(multipart); + int statusCode = res.getStatus(); + + // Check the status code of the response: does it match + // the expected response(s)? + // + // Specifically: + // Does it fall within the set of valid status codes? + // Does it exactly match the expected status code? + if(logger.isDebugEnabled()){ + logger.debug(testName + ": status = " + statusCode); + } + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + + // Store the IDs from every resource created by tests, + // so they can be deleted after tests have been run. + knownAuthResourceId = extractId(res); + allResourceIdsCreated.add(knownAuthResourceId); + + // Create all the person refs and entities + createPersonRefs(); + + // Initialize values for a new Organization item, to be created within + // the newly-created Organization Authority resource. + // + // One or more fields in the Organization item record will + // contain references to Persons, via their refNames, as + // per the initialization(s) below. + Map testOrgMap = new HashMap(); + testOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, + "Test Organization-" + identifier); + testOrgMap.put(OrganizationJAXBSchema.LONG_NAME, "Test Organization Name"); + testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, "Anytown, USA"); + testOrgMap.put(OrganizationJAXBSchema.CONTACT_NAME, organizationContactPersonRefName); + + // Finishing creating the new Organization item, then + // submit the request to the service and store the response. + knownItemId = OrgAuthorityClientUtils.createItemInAuthority( + knownAuthResourceId, knownResourceRefName, testOrgMap, orgAuthClient); + + // Check the status code of the response: does it match + // the expected response(s)? + if(logger.isDebugEnabled()){ + logger.debug(testName + ": status = " + statusCode); + } + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + + // Store the IDs from every item created by tests, + // so they can be deleted after tests have been run. + allItemResourceIdsCreated.put(knownItemId, knownAuthResourceId); + + } + + protected void createPersonRefs(){ + + PersonAuthorityClient personAuthClient = new PersonAuthorityClient(); + // Create a temporary PersonAuthority resource, and its corresponding + // refName by which it can be identified. + String authRefName = + PersonAuthorityClientUtils.createPersonAuthRefName(PERSON_AUTHORITY_NAME, false); + MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance( + PERSON_AUTHORITY_NAME, authRefName, personAuthClient.getCommonPartName()); + ClientResponse res = personAuthClient.create(multipart); + int statusCode = res.getStatus(); + + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, CREATED_STATUS); + personAuthCSID = extractId(res); + + // Create a temporary Person resource, and its corresponding refName + // by which it can be identified. + organizationContactPersonRefName = + PersonAuthorityClientUtils.createPersonRefName(authRefName, "Charlie Orgcontact", true); + personIdsCreated.add(createPerson("Charlie", "Orgcontact", organizationContactPersonRefName)); + + } + + protected String createPerson(String firstName, String surName, String refName ) { + PersonAuthorityClient personAuthClient = new PersonAuthorityClient(); + Map personInfo = new HashMap(); + personInfo.put(PersonJAXBSchema.FORE_NAME, firstName); + personInfo.put(PersonJAXBSchema.SUR_NAME, surName); + MultipartOutput multipart = + PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, + refName, personInfo, personAuthClient.getItemCommonPartName()); + ClientResponse res = personAuthClient.createItem(personAuthCSID, multipart); + int statusCode = res.getStatus(); + + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, CREATED_STATUS); + return extractId(res); + } + + // Success outcomes + @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class, + dependsOnMethods = {"createWithAuthRefs"}) + public void readAndCheckAuthRefs(String testName) throws Exception { + + // Perform setup. + testSetup(OK_STATUS, ServiceRequestType.READ,testName); + + // Submit the request to the service and store the response. + OrgAuthorityClient orgAuthClient = new OrgAuthorityClient(); + ClientResponse res = + orgAuthClient.readItem(knownAuthResourceId, knownItemId); + int statusCode = res.getStatus(); + + // Check the status code of the response: does it match + // the expected response(s)? + if(logger.isDebugEnabled()){ + logger.debug(testName + ".read: status = " + statusCode); + } + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + + MultipartInput input = (MultipartInput) res.getEntity(); + OrganizationsCommon organization = (OrganizationsCommon) extractPart(input, + orgAuthClient.getItemCommonPartName(), OrganizationsCommon.class); + Assert.assertNotNull(organization); + if(logger.isDebugEnabled()){ + logger.debug(objectAsXmlString(organization, OrganizationsCommon.class)); + } + // Check one or more of the authority fields in the Organization item + Assert.assertEquals(organization.getContactName(), organizationContactPersonRefName); + + // Get the auth refs and check them + // FIXME - need to create this method in the client + // and get the ID for the organization item + ClientResponse res2 = + orgAuthClient.getItemAuthorityRefs(knownAuthResourceId, knownItemId); + statusCode = res2.getStatus(); + + if(logger.isDebugEnabled()){ + logger.debug(testName + ".getAuthorityRefs: status = " + statusCode); + } + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + AuthorityRefList list = res2.getEntity(); + + // Optionally output additional data about list members for debugging. + boolean iterateThroughList = true; + if(iterateThroughList && logger.isDebugEnabled()){ + List items = + list.getAuthorityRefItem(); + int i = 0; + for(AuthorityRefList.AuthorityRefItem item : items){ + logger.debug(testName + ": list-item[" + i + "] Field:" + + item.getSourceField() + "=" + + item.getItemDisplayName()); + logger.debug(testName + ": list-item[" + i + "] refName=" + + item.getRefName()); + logger.debug(testName + ": list-item[" + i + "] URI=" + + item.getUri()); + i++; + } + Assert.assertEquals(i, NUM_AUTH_REFS_EXPECTED, "Did not find all authrefs!"); + } + } + + // --------------------------------------------------------------- + // Cleanup of resources created during testing + // --------------------------------------------------------------- + + /** + * Deletes all resources created by tests, after all tests have been run. + * + * This cleanup method will always be run, even if one or more tests fail. + * For this reason, it attempts to remove all resources created + * at any point during testing, even if some of those resources + * may be expected to be deleted by certain tests. + */ + @AfterClass(alwaysRun=true) + public void cleanUp() { + String noTest = System.getProperty("noTestCleanup"); + if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) { + if (logger.isDebugEnabled()) { + logger.debug("Skipping Cleanup phase ..."); + } + return; + } + if (logger.isDebugEnabled()) { + logger.debug("Cleaning up temporary resources created for testing ..."); + } + PersonAuthorityClient personAuthClient = new PersonAuthorityClient(); + // Delete Person resource(s) (before PersonAuthority resources). + ClientResponse res; + for (String resourceId : personIdsCreated) { + // Note: Any non-success responses are ignored and not reported. + res = personAuthClient.deleteItem(personAuthCSID, resourceId); + } + // Delete PersonAuthority resource(s). + // Note: Any non-success response is ignored and not reported. + res = personAuthClient.delete(personAuthCSID); + String parentResourceId; + String itemResourceId; + OrgAuthorityClient client = new OrgAuthorityClient(); + // Clean up item resources. + for (Map.Entry entry : allItemResourceIdsCreated.entrySet()) { + itemResourceId = entry.getKey(); + parentResourceId = entry.getValue(); + // Note: Any non-success responses from the delete operation + // below are ignored and not reported. + res = client.deleteItem(parentResourceId, itemResourceId); + } + // Clean up parent resources. + for (String resourceId : allResourceIdsCreated) { + // Note: Any non-success responses from the delete operation + // below are ignored and not reported. + res = client.delete(resourceId); + } + } + + // --------------------------------------------------------------- + // Utility methods used by tests above + // --------------------------------------------------------------- + @Override + public String getServicePathComponent() { + return SERVICE_PATH_COMPONENT; + } + + private MultipartOutput createOrgAuthorityInstance(String identifier) { + String displayName = "displayName-" + identifier; + String refName = OrgAuthorityClientUtils.createOrgAuthRefName(displayName, true); + return OrgAuthorityClientUtils.createOrgAuthorityInstance( + displayName, refName, + new OrgAuthorityClient().getCommonPartName()); + } + +} diff --git a/services/organization/service/src/main/java/org/collectionspace/services/organization/OrgAuthorityResource.java b/services/organization/service/src/main/java/org/collectionspace/services/organization/OrgAuthorityResource.java index 3b45ff4e6..8edf38210 100644 --- a/services/organization/service/src/main/java/org/collectionspace/services/organization/OrgAuthorityResource.java +++ b/services/organization/service/src/main/java/org/collectionspace/services/organization/OrgAuthorityResource.java @@ -23,6 +23,8 @@ */ package org.collectionspace.services.organization; +import java.util.List; + import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; @@ -44,13 +46,17 @@ import org.collectionspace.services.OrganizationJAXBSchema; import org.collectionspace.services.common.AbstractMultiPartCollectionSpaceResourceImpl; import org.collectionspace.services.common.ClientType; import org.collectionspace.services.common.ServiceMain; +import org.collectionspace.services.common.authorityref.AuthorityRefList; import org.collectionspace.services.common.context.MultipartServiceContext; +import org.collectionspace.services.common.context.MultipartServiceContextImpl; //import org.collectionspace.services.common.context.MultipartServiceContextFactory; +import org.collectionspace.services.common.context.ServiceBindingUtils; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.document.BadRequestException; import org.collectionspace.services.common.document.DocumentFilter; import org.collectionspace.services.common.document.DocumentHandler; import org.collectionspace.services.common.document.DocumentNotFoundException; +import org.collectionspace.services.common.document.DocumentWrapper; import org.collectionspace.services.common.query.IQueryManager; import org.collectionspace.services.contact.ContactResource; import org.collectionspace.services.contact.ContactsCommon; @@ -58,10 +64,12 @@ import org.collectionspace.services.contact.ContactsCommonList; import org.collectionspace.services.contact.ContactJAXBSchema; import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler; import org.collectionspace.services.common.security.UnauthorizedException; +import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl; import org.collectionspace.services.organization.nuxeo.OrganizationDocumentModelHandler; import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput; import org.jboss.resteasy.util.HttpResponseCodes; +import org.nuxeo.ecm.core.api.DocumentModel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -563,10 +571,10 @@ public class OrgAuthorityResource extends /** * Gets the organization. * - * @param parentcsid the parentcsid - * @param itemcsid the itemcsid + * @param csid The organization authority (parent) CSID. + * @param itemcsid The organization item CSID. * - * @return the organization + * @return the organization. */ @GET @Path("{csid}/items/{itemcsid}") @@ -627,6 +635,49 @@ public class OrgAuthorityResource extends return result; } + /** + * Gets the authority refs for an Organization item. + * + * @param csid The organization authority (parent) CSID. + * @param itemcsid The organization item CSID. + * + * @return the authority refs for the Organization item. + */ + @GET + @Path("{csid}/items/{itemcsid}/authorityrefs") + @Produces("application/xml") + public AuthorityRefList getOrganizationAuthorityRefs( + @PathParam("csid") String parentcsid, + @PathParam("itemcsid") String itemcsid, + @Context UriInfo ui) { + AuthorityRefList authRefList = null; + try { + MultivaluedMap queryParams = ui.getQueryParameters(); + ServiceContext ctx = + createServiceContext(getItemServiceName(), queryParams); + RemoteDocumentModelHandlerImpl handler = + (RemoteDocumentModelHandlerImpl) createItemDocumentHandler(ctx, parentcsid); + DocumentWrapper docWrapper = + getRepositoryClient(ctx).getDoc(ctx, itemcsid); + List authRefFields = + ((MultipartServiceContextImpl)ctx).getCommonPartPropertyValues( + ServiceBindingUtils.AUTH_REF_PROP, ServiceBindingUtils.QUALIFIED_PROP_NAMES); + authRefList = handler.getAuthorityRefs(docWrapper, authRefFields); + } catch (UnauthorizedException ue) { + Response response = Response.status( + Response.Status.UNAUTHORIZED).entity("Failed to retrieve authority references: reason " + ue.getErrorReason()).type("text/plain").build(); + throw new WebApplicationException(response); + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception in getAuthorityRefs", e); + } + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity("Failed to retrieve authority references").type("text/plain").build(); + throw new WebApplicationException(response); + } + return authRefList; + } + /** * Gets the organization list. * -- 2.47.3