<xs:element name="inAuthority" type="xs:string" />
<xs:element name="refName" type="xs:string"/>
<xs:element name="displayName" type="xs:string"/>
-<!-- <xs:element name="displayNameComputed" type="xs:boolean"/> -->
+ <xs:element name="displayNameComputed" type="xs:boolean"/>
<xs:element name="shortName" type="xs:string"/>
<xs:element name="longName" type="xs:string"/>
<xs:element name="nameAdditions" type="xs:string"/>
return multipart;\r
}\r
\r
+ public static String createItemInAuthority(String vcsid, \r
+ String orgAuthorityRefName, Map<String, String> orgInfo,\r
+ OrgAuthorityClient client) {\r
+ // Expected status code: 201 Created\r
+ int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();\r
+ // Type of service request being tested\r
+ ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;\r
+ String displayName = orgInfo.get(OrganizationJAXBSchema.DISPLAY_NAME);\r
+ String displayNameComputedStr = orgInfo.get(OrganizationJAXBSchema.DISPLAY_NAME_COMPUTED);\r
+ boolean displayNameComputed = (displayNameComputedStr==null) || displayNameComputedStr.equalsIgnoreCase("true");\r
+ if( displayName == null ) {\r
+ if(!displayNameComputed) {\r
+ throw new RuntimeException(\r
+ "CreateItem: Must supply a displayName if displayNameComputed is set to false.");\r
+ }\r
+ displayName = prepareDefaultDisplayName(\r
+ orgInfo.get(OrganizationJAXBSchema.SHORT_NAME ), \r
+ orgInfo.get(OrganizationJAXBSchema.FOUNDING_PLACE ));\r
+ }\r
+ String refName = createOrganizationRefName( orgAuthorityRefName, displayName, true);\r
+\r
+ if(logger.isDebugEnabled()){\r
+ logger.debug("Import: Create Item: \""+displayName\r
+ +"\" in orgAuthorityulary: \"" + orgAuthorityRefName +"\"");\r
+ }\r
+ MultipartOutput multipart =\r
+ createOrganizationInstance( vcsid, refName, orgInfo, client.getItemCommonPartName() );\r
+ ClientResponse<Response> res = client.createItem(vcsid, multipart);\r
+\r
+ int statusCode = res.getStatus();\r
+\r
+ if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
+ throw new RuntimeException("Could not create Item: \""+displayName\r
+ +"\" in orgAuthority: \"" + orgAuthorityRefName\r
+ +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
+ }\r
+ if(statusCode != EXPECTED_STATUS_CODE) {\r
+ throw new RuntimeException("Unexpected Status when creating Item: \""+displayName\r
+ +"\" in orgAuthority: \"" + orgAuthorityRefName +"\", Status:"+ statusCode);\r
+ }\r
+\r
+ return extractId(res);\r
+ }\r
+\r
public static MultipartOutput createOrganizationInstance(String inAuthority, \r
String orgRefName, Map<String, String> orgInfo, String headerLabel){\r
OrganizationsCommon organization = new OrganizationsCommon();\r
organization.setInAuthority(inAuthority);\r
organization.setRefName(orgRefName);\r
String value = null;\r
+ value = orgInfo.get(OrganizationJAXBSchema.DISPLAY_NAME_COMPUTED);\r
+ boolean displayNameComputed = (value==null) || value.equalsIgnoreCase("true"); \r
+ organization.setDisplayNameComputed(displayNameComputed);\r
+ if((value = (String)orgInfo.get(OrganizationJAXBSchema.DISPLAY_NAME))!=null)\r
+ organization.setDisplayName(value);\r
if((value = (String)orgInfo.get(OrganizationJAXBSchema.SHORT_NAME))!=null)\r
organization.setShortName(value);\r
if((value = (String)orgInfo.get(OrganizationJAXBSchema.LONG_NAME))!=null)\r
return refName;\r
}\r
\r
+ /**\r
+ * Produces a default displayName from the basic name and foundingPlace fields.\r
+ * @see OrgAuthorityDocumentModelHandler.prepareDefaultDisplayName() which\r
+ * duplicates this logic, until we define a service-general utils package\r
+ * that is neither client nor service specific.\r
+ * @param shortName\r
+ * @param foundingPlace\r
+ * @return\r
+ * @throws Exception\r
+ */\r
+ public static String prepareDefaultDisplayName(\r
+ String shortName, String foundingPlace ) {\r
+ StringBuilder newStr = new StringBuilder();\r
+ final String sep = " ";\r
+ boolean firstAdded = false;\r
+ if(null != shortName ) {\r
+ newStr.append(shortName);\r
+ firstAdded = true;\r
+ }\r
+ // Now we add the place\r
+ if(null != foundingPlace ) {\r
+ if(firstAdded) {\r
+ newStr.append(sep);\r
+ }\r
+ newStr.append(foundingPlace);\r
+ }\r
+ return newStr.toString();\r
+ }\r
+ \r
}\r
private OrgAuthorityClient client = new OrgAuthorityClient();
final String SERVICE_PATH_COMPONENT = "orgauthorities";
final String ITEM_SERVICE_PATH_COMPONENT = "items";
+ private final String TEST_ORG_SHORTNAME = "Test Org";
+ private final String TEST_ORG_FOUNDING_PLACE = "Anytown, USA";
private String knownResourceId = null;
private String lastOrgAuthId = null;
private String knownResourceRefName = null;
public void createItem(String testName) {
setupCreate(testName);
- knownItemResourceId = createItemInAuthority(lastOrgAuthId);
+ knownItemResourceId = createItemInAuthority(lastOrgAuthId, knownResourceRefName);
if(logger.isDebugEnabled()){
logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
}
}
- private String createItemInAuthority(String vcsid) {
+ private String createItemInAuthority(String vcsid, String orgAuthorityRefName) {
final String testName = "createItemInAuthority";
if(logger.isDebugEnabled()){
String identifier = createIdentifier();
String refName = OrgAuthorityClientUtils.createOrganizationRefName(knownResourceRefName, identifier, true);
Map<String, String> testOrgMap = new HashMap<String,String>();
- testOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, "Test Org");
+ testOrgMap.put(OrganizationJAXBSchema.SHORT_NAME, TEST_ORG_SHORTNAME);
testOrgMap.put(OrganizationJAXBSchema.LONG_NAME, "The real official test organization");
testOrgMap.put(OrganizationJAXBSchema.CONTACT_NAME, "joe@test.org");
testOrgMap.put(OrganizationJAXBSchema.FOUNDING_DATE, "May 26, 1907");
- testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, "Anytown, USA");
+ testOrgMap.put(OrganizationJAXBSchema.FOUNDING_PLACE, TEST_ORG_FOUNDING_PLACE);
testOrgMap.put(OrganizationJAXBSchema.FUNCTION, "For testing");
- MultipartOutput multipart =
- OrgAuthorityClientUtils.createOrganizationInstance(vcsid,
- refName, testOrgMap, client.getItemCommonPartName() );
- ClientResponse<Response> res = client.createItem(vcsid, multipart);
- int statusCode = res.getStatus();
-
- // 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);
-
+ String newID = OrgAuthorityClientUtils.createItemInAuthority(
+ vcsid, orgAuthorityRefName, testOrgMap, client);
// Store the ID returned from the first item resource created
// for additional tests below.
if (knownItemResourceId == null){
- knownItemResourceId = extractId(res);
+ knownItemResourceId = newID;
if (logger.isDebugEnabled()) {
logger.debug(testName + ": knownItemResourceId=" + knownItemResourceId);
}
//
// Item resource IDs are unique, so these are used as keys;
// the non-unique IDs of their parents are stored as associated values.
- allResourceItemIdsCreated.put(extractId(res), vcsid);
+ allResourceItemIdsCreated.put(newID, vcsid);
- return extractId(res);
+ return newID;
}
@Override
Assert.assertEquals(organization.getInAuthority(), knownResourceId);
}
+ @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
+ dependsOnMethods = {"readItem", "updateItem"})
+ public void verifyItemDisplayName(String testName) throws Exception {
+
+ // Perform setup.
+ setupRead(testName);
+
+ // Submit the request to the service and store the response.
+ ClientResponse<MultipartInput> res = client.readItem(knownResourceId, knownItemResourceId);
+ int statusCode = res.getStatus();
+
+ // 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);
+
+ // Check whether organization has expected displayName.
+ MultipartInput input = (MultipartInput) res.getEntity();
+ OrganizationsCommon organization = (OrganizationsCommon) extractPart(input,
+ client.getItemCommonPartName(), OrganizationsCommon.class);
+ Assert.assertNotNull(organization);
+ String displayName = organization.getDisplayName();
+ // Make sure displayName matches computed form
+ String expectedDisplayName =
+ OrgAuthorityClientUtils.prepareDefaultDisplayName(
+ TEST_ORG_SHORTNAME, TEST_ORG_FOUNDING_PLACE);
+ Assert.assertNotNull(displayName, expectedDisplayName);
+
+ // Update the shortName and verify the computed name is updated.
+ organization.setDisplayNameComputed(true);
+ organization.setShortName("updated-" + TEST_ORG_SHORTNAME);
+ expectedDisplayName =
+ OrgAuthorityClientUtils.prepareDefaultDisplayName(
+ "updated-" + TEST_ORG_SHORTNAME, TEST_ORG_FOUNDING_PLACE);
+
+ // Submit the updated resource to the service and store the response.
+ MultipartOutput output = new MultipartOutput();
+ OutputPart commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
+ commonPart.getHeaders().add("label", client.getItemCommonPartName());
+ res = client.updateItem(knownResourceId, knownItemResourceId, output);
+ statusCode = res.getStatus();
+
+ // Check the status code of the response: does it match the expected response(s)?
+ if(logger.isDebugEnabled()){
+ logger.debug("updateItem: status = " + statusCode);
+ }
+ Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
+ Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
+
+ // Retrieve the updated resource and verify that its contents exist.
+ input = (MultipartInput) res.getEntity();
+ OrganizationsCommon updatedOrganization =
+ (OrganizationsCommon) extractPart(input,
+ client.getItemCommonPartName(), OrganizationsCommon.class);
+ Assert.assertNotNull(updatedOrganization);
+
+ // Verify that the updated resource received the correct data.
+ Assert.assertEquals(updatedOrganization.getShortName(),
+ organization.getShortName(),
+ "Updated ShortName in Organization did not match submitted data.");
+ // Verify that the updated resource computes the right displayName.
+ Assert.assertEquals(updatedOrganization.getDisplayName(),
+ expectedDisplayName,
+ "Updated ShortName in Organization not reflected in computed DisplayName.");
+
+ // Now Update the displayName, not computed and verify the computed name is overriden.
+ organization.setDisplayNameComputed(false);
+ expectedDisplayName = "TestName";
+ organization.setDisplayName(expectedDisplayName);
+
+ // Submit the updated resource to the service and store the response.
+ output = new MultipartOutput();
+ commonPart = output.addPart(organization, MediaType.APPLICATION_XML_TYPE);
+ commonPart.getHeaders().add("label", client.getItemCommonPartName());
+ res = client.updateItem(knownResourceId, knownItemResourceId, output);
+ statusCode = res.getStatus();
+
+ // Check the status code of the response: does it match the expected response(s)?
+ if(logger.isDebugEnabled()){
+ logger.debug("updateItem: status = " + statusCode);
+ }
+ Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
+ Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
+
+ // Retrieve the updated resource and verify that its contents exist.
+ input = (MultipartInput) res.getEntity();
+ updatedOrganization =
+ (OrganizationsCommon) extractPart(input,
+ client.getItemCommonPartName(), OrganizationsCommon.class);
+ Assert.assertNotNull(updatedOrganization);
+
+ // Verify that the updated resource received the correct data.
+ Assert.assertEquals(updatedOrganization.isDisplayNameComputed(), false,
+ "Updated displayNameComputed in Organization did not match submitted data.");
+ // Verify that the updated resource computes the right displayName.
+ Assert.assertEquals(updatedOrganization.getDisplayName(),
+ expectedDisplayName,
+ "Updated DisplayName (not computed) in Organization not stored.");
+ }
+
// Failure outcomes
@Override
@Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
@Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
dependsOnMethods = {"createItem", "readItemList", "testItemSubmitRequest",
- "updateItem"})
+ "updateItem", "verifyItemDisplayName"})
public void deleteItem(String testName) throws Exception {
// Perform setup.
+newOrgAuthorityId );
}
for(Map<String,String> orgInfo : orgInfos){
- createItemInAuthority(newOrgAuthorityId,
- baseOrgAuthRefName, orgInfo );
+ OrgAuthorityClientUtils.createItemInAuthority(
+ newOrgAuthorityId, baseOrgAuthRefName, orgInfo, client);
}
}
- private String createItemInAuthority(String vcsid,
- String orgAuthorityRefName, Map<String, String> orgInfo) {
- // Expected status code: 201 Created
- int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
- // Type of service request being tested
- ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
- String shortName = orgInfo.get(OrganizationJAXBSchema.SHORT_NAME);
- String refName = OrgAuthorityClientUtils.createOrganizationRefName(
- orgAuthorityRefName, shortName, true);
-
- if(logger.isDebugEnabled()){
- logger.debug("Import: Create Item: \""+shortName
- +"\" in orgAuthorityulary: \"" + orgAuthorityRefName +"\"");
- }
- MultipartOutput multipart =
- OrgAuthorityClientUtils.createOrganizationInstance( vcsid,
- refName, orgInfo, client.getItemCommonPartName() );
- ClientResponse<Response> res = client.createItem(vcsid, multipart);
-
- int statusCode = res.getStatus();
-
- if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
- throw new RuntimeException("Could not create Item: \""+shortName
- +"\" in orgAuthority: \"" + orgAuthorityRefName
- +"\" "+ OrgAuthorityClientUtils.invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
- }
- if(statusCode != EXPECTED_STATUS_CODE) {
- throw new RuntimeException("Unexpected Status when creating Item: \""+shortName
- +"\" in orgAuthority: \"" + orgAuthorityRefName +"\", Status:"+ statusCode);
- }
-
- return OrgAuthorityClientUtils.extractId(res);
- }
-
// ---------------------------------------------------------------
// Utility methods used by methods above
// ---------------------------------------------------------------
final static String CSID = "csid";\r
final static String IN_AUTHORITY = "inAuthority";\r
final static String DISPLAY_NAME = "displayName";\r
+ final static String DISPLAY_NAME_COMPUTED = "displayNameComputed";\r
final static String SHORT_NAME = "shortName";\r
final static String REF_NAME = "refName";\r
final static String LONG_NAME = "longName";\r
<xs:element name="inAuthority" type="xs:string" />
<xs:element name="refName" type="xs:string"/>
<xs:element name="displayName" type="xs:string"/>
-<!-- <xs:element name="displayNameComputed" type="xs:boolean"/> -->
+ <xs:element name="displayNameComputed" type="xs:boolean"/>
<xs:element name="shortName" type="xs:string"/>
<xs:element name="longName" type="xs:string"/>
<xs:element name="nameAdditions" type="xs:string"/>
import java.util.List;
import org.collectionspace.services.OrganizationJAXBSchema;
+import org.collectionspace.services.common.context.MultipartServiceContext;
import org.collectionspace.services.common.document.DocumentWrapper;
import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
import org.collectionspace.services.nuxeo.util.NuxeoUtils;
public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
// first fill all the parts of the document
super.handleCreate(wrapDoc);
- handleGetDisplayName(wrapDoc.getWrappedObject());
- }
-
- private String prepareDefaultDisplayName(DocumentModel docModel) throws Exception {
- String result = null;
-
- result = (String) docModel.getProperty(getServiceContext().getCommonPartLabel("organizations"),
- OrganizationJAXBSchema.SHORT_NAME);
-
- return result;
+ handleDisplayName(wrapDoc.getWrappedObject());
}
- /**
- * Handle get display name.
- *
- * @param docModel the doc model
- *
- * @return the string
- *
- * @throws Exception the exception
- */
- private String handleGetDisplayName(DocumentModel docModel) throws Exception {
- return handleGetDisplayName(docModel, true);
+ @Override
+ public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
+ super.handleUpdate(wrapDoc);
+ handleDisplayName(wrapDoc.getWrappedObject());
}
-
+
/**
- * Handle get display name.
- *
- * @param wrapDoc the wrap doc
+ * Check the logic around the computed displayName
*
- * @return the string
+ * @param docModel
*
* @throws Exception the exception
*/
- private String handleGetDisplayName(DocumentModel docModel, boolean updateDocModel) throws Exception {
- String displayName = (String) docModel.getProperty(getServiceContext().getCommonPartLabel("organizations"),
- OrganizationJAXBSchema.DISPLAY_NAME);
- if (displayName == null) {
- displayName = prepareDefaultDisplayName(docModel);
- if (updateDocModel == true) {
- docModel.setProperty(getServiceContext().getCommonPartLabel(
- "organizations"), OrganizationJAXBSchema.DISPLAY_NAME,
+ private void handleDisplayName(DocumentModel docModel) throws Exception {
+ String commonPartLabel = getServiceContext().getCommonPartLabel("organizations");
+ Boolean displayNameComputed = (Boolean) docModel.getProperty(commonPartLabel,
+ OrganizationJAXBSchema.DISPLAY_NAME_COMPUTED);
+ if (displayNameComputed) {
+ String displayName = prepareDefaultDisplayName(
+ (String) docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.SHORT_NAME),
+ (String) docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.FOUNDING_PLACE));
+ docModel.setProperty(commonPartLabel, OrganizationJAXBSchema.DISPLAY_NAME,
displayName);
- }
+ } else if(null ==
+ docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.DISPLAY_NAME)) {
+ throw new IllegalArgumentException("Must provide "+OrganizationJAXBSchema.DISPLAY_NAME
+ +" if " + OrganizationJAXBSchema.DISPLAY_NAME_COMPUTED+" is declared false.");
}
-
- return displayName;
}
- /* Override handleGet so we can deal with defaulting the displayName
- * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
+ /**
+ * Produces a default displayName from the basic name and foundingPlace fields.
+ * @see OrgAuthorityClientUtils.prepareDefaultDisplayName() which
+ * duplicates this logic, until we define a service-general utils package
+ * that is neither client nor service specific.
+ * @param shortName
+ * @param foundingPlace
+ * @return
+ * @throws Exception
*/
- @Override
- public void handleGet(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
- handleGetDisplayName(wrapDoc.getWrappedObject());
- super.handleGet(wrapDoc);
+ private static String prepareDefaultDisplayName(
+ String shortName, String foundingPlace ) throws Exception {
+ StringBuilder newStr = new StringBuilder();
+ final String sep = " ";
+ boolean firstAdded = false;
+ if(null != shortName ) {
+ newStr.append(shortName);
+ firstAdded = true;
+ }
+ // Now we add the place
+ if(null != foundingPlace ) {
+ if(firstAdded) {
+ newStr.append(sep);
+ }
+ newStr.append(foundingPlace);
+ }
+ return newStr.toString();
}
-
+
/**
* getCommonPart get associated organization
* @return
//FIXME: iterating over a long list of documents is not a long term
//strategy...need to change to more efficient iterating in future
Iterator<DocumentModel> iter = docList.iterator();
+ String commonPartLabel = getServiceContext().getCommonPartLabel("organizations");
while(iter.hasNext()){
DocumentModel docModel = iter.next();
OrganizationListItem ilistItem = new OrganizationListItem();
- // We look for a set display name, and fall back to the short name if there is none
- String displayName = handleGetDisplayName(docModel, false);
- ilistItem.setDisplayName(displayName);
- ilistItem.setRefName((String) docModel.getProperty(getServiceContext().getCommonPartLabel(
- "organizations"), OrganizationJAXBSchema.REF_NAME));
- /*
- * These are not currently included in the listing - only in the details
- ilistItem.setLongName(
- (String) docModel.getProperty(getServiceContext().getCommonPartLabel("organizations"),
- OrganizationJAXBSchema.LONG_NAME));
- ilistItem.setDescription(
- (String) docModel.getProperty(getServiceContext().getCommonPartLabel("organizations"),
- OrganizationJAXBSchema.DESCRIPTION));
- */
- String id = NuxeoUtils.extractId(docModel.getPathAsString());
+ ilistItem.setDisplayName((String)
+ docModel.getProperty(commonPartLabel,OrganizationJAXBSchema.DISPLAY_NAME ));
+ ilistItem.setRefName((String)
+ docModel.getProperty(commonPartLabel, OrganizationJAXBSchema.REF_NAME));
+ String id = NuxeoUtils.extractId(docModel.getPathAsString());
ilistItem.setUri("/orgauthorities/"+inAuthority+"/items/" + id);
ilistItem.setCsid(id);
list.add(ilistItem);