2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright © 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 package org.collectionspace.services.client.test;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
30 import javax.ws.rs.core.Response;
32 import org.collectionspace.services.OrganizationJAXBSchema;
33 import org.collectionspace.services.client.CollectionSpaceClient;
34 import org.collectionspace.services.client.IntakeClient;
35 import org.collectionspace.services.client.OrgAuthorityClient;
36 import org.collectionspace.services.client.OrgAuthorityClientUtils;
37 import org.collectionspace.services.client.PayloadOutputPart;
38 import org.collectionspace.services.client.PoxPayloadOut;
39 import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;
40 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
41 import org.collectionspace.services.intake.ConditionCheckerOrAssessorList;
42 import org.collectionspace.services.intake.IntakesCommon;
43 import org.collectionspace.services.intake.InsurerList;
44 import org.collectionspace.services.jaxb.AbstractCommonList;
45 import org.collectionspace.services.organization.OrgTermGroup;
47 import org.testng.Assert;
48 import org.testng.annotations.AfterClass;
49 import org.testng.annotations.Test;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 * OrganizationAuthRefDocsTest, carries out tests against a
55 * deployed and running Organization Service.
57 * $LastChangedRevision: 1327 $
58 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
60 public class OrganizationAuthRefDocsTest extends BaseServiceTest<AbstractCommonList> {
62 private final String CLASS_NAME = OrganizationAuthRefDocsTest.class.getName();
63 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
65 // Instance variables specific to this test.
66 final String SERVICE_PATH_COMPONENT = "intakes";
67 final String ORGANIZATION_AUTHORITY_NAME = "TestOrganizationAuth";
68 private String knownIntakeId = null;
69 private List<String> intakeIdsCreated = new ArrayList<String>();
70 private List<String> orgIdsCreated = new ArrayList<String>();
71 private String orgAuthCSID = null;
72 //private String orgAuthRefName = null;
73 private String currentOwnerOrgCSID = null;
74 private String currentOwnerRefName = null;
75 private String depositorRefName = null;
76 private String conditionCheckerAssessorRefName = null;
77 private String insurerRefName = null;
78 private String valuerRefName = null;
79 private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;
80 private final static String CURRENT_DATE_UTC =
81 GregorianCalendarDateTimeUtils.currentDateUTC();
84 protected String getServiceName() {
85 throw new UnsupportedOperationException(); //FIXME: REM - See http://issues.collectionspace.org/browse/CSPACE-3498
89 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
92 protected CollectionSpaceClient getClientInstance() {
93 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
97 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
98 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
102 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
105 protected AbstractCommonList getCommonList(Response response) {
106 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
109 // ---------------------------------------------------------------
110 // CRUD tests : CREATE tests
111 // ---------------------------------------------------------------
113 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
114 public void createIntakeWithAuthRefs(String testName) throws Exception {
115 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
117 // Submit the request to the service and store the response.
118 String identifier = createIdentifier();
120 // Create all the organization refs and entities
123 IntakeClient intakeClient = new IntakeClient();
124 PoxPayloadOut intakePayload = createIntakeInstance(
125 "entryNumber-" + identifier,
128 // Use currentOwnerRefName twice to test fix for CSPACE-2863
129 currentOwnerRefName, //depositorRefName,
130 conditionCheckerAssessorRefName,
134 Response res = intakeClient.create(intakePayload);
136 int statusCode = res.getStatus();
138 // Check the status code of the response: does it match
139 // the expected response(s)?
142 // Does it fall within the set of valid status codes?
143 // Does it exactly match the expected status code?
144 if(logger.isDebugEnabled()){
145 logger.debug(testName + ": status = " + statusCode);
147 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
148 invalidStatusCodeMessage(testRequestType, statusCode));
149 Assert.assertEquals(statusCode, testExpectedStatusCode);
150 String newIntakeId = extractId(res);
151 Assert.assertNotNull(newIntakeId, "Could not create a new Intake record.");
153 // Store the ID returned from the first resource created
154 // for additional tests below.
155 if (knownIntakeId == null) {
156 knownIntakeId = newIntakeId;
157 if (logger.isDebugEnabled()) {
158 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
162 // Store the IDs from every resource created by tests,
163 // so they can be deleted after tests have been run.
164 intakeIdsCreated.add(newIntakeId);
172 * Creates the organization refs.
174 protected void createOrgRefs(){
175 OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
177 // OrgAuthorityClientUtils.createOrgAuthRefName(ORGANIZATION_AUTHORITY_NAME, null);
178 PoxPayloadOut multipart = OrgAuthorityClientUtils.createOrgAuthorityInstance(
179 ORGANIZATION_AUTHORITY_NAME, ORGANIZATION_AUTHORITY_NAME, orgAuthClient.getCommonPartName());
180 Response res = orgAuthClient.create(multipart);
182 int statusCode = res.getStatus();
183 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
184 invalidStatusCodeMessage(testRequestType, statusCode));
185 Assert.assertEquals(statusCode, STATUS_CREATED);
186 orgAuthCSID = extractId(res);
187 Assert.assertNotNull(orgAuthCSID, "Could not create a new Organization authority record.");
192 currentOwnerOrgCSID = createOrganization("olivierOwnerCompany", "Olivier Owner Company", "Olivier Owner Company");
193 orgIdsCreated.add(currentOwnerOrgCSID);
194 currentOwnerRefName = OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, currentOwnerOrgCSID, orgAuthClient);
197 createOrganization("debbieDepositorAssocs", "Debbie Depositor & Associates", "Debbie Depositor & Associates");
199 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
200 orgIdsCreated.add(newOrgCSID);
202 newOrgCSID = createOrganization("andrewCheckerAssessorLtd", "Andrew Checker-Assessor Ltd.", "Andrew Checker-Assessor Ltd.");
203 conditionCheckerAssessorRefName =
204 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
205 orgIdsCreated.add(newOrgCSID);
207 newOrgCSID = createOrganization("ingridInsurerBureau", "Ingrid Insurer Bureau", "Ingrid Insurer Bureau");
209 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
210 orgIdsCreated.add(newOrgCSID);
212 newOrgCSID = createOrganization("vinceValuerLLC", "Vince Valuer LLC", "Vince Valuer LLC");
214 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
215 orgIdsCreated.add(newOrgCSID);
218 protected String createOrganization(String shortId, String shortName, String longName) {
219 String result = null;
221 OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
222 Map<String, String> orgInfo = new HashMap<String,String>();
223 orgInfo.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
225 List<OrgTermGroup> orgTerms = new ArrayList<OrgTermGroup>();
226 OrgTermGroup term = new OrgTermGroup();
227 term.setTermDisplayName(shortName);
228 term.setTermName(shortName);
229 term.setMainBodyName(longName);
231 PoxPayloadOut multipart =
232 OrgAuthorityClientUtils.createOrganizationInstance(null, //orgAuthRefName
233 orgInfo, orgTerms, orgAuthClient.getItemCommonPartName());
235 Response res = orgAuthClient.createItem(orgAuthCSID, multipart);
237 int statusCode = res.getStatus();
239 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
240 invalidStatusCodeMessage(testRequestType, statusCode));
241 Assert.assertEquals(statusCode, STATUS_CREATED);
242 result = extractId(res);
251 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
252 dependsOnMethods = {"createIntakeWithAuthRefs"})
253 public void readAndCheckAuthRefDocs(String testName) throws Exception {
255 testSetup(STATUS_OK, ServiceRequestType.READ);
257 // Get the auth ref docs and check them
258 OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
259 Response refDocListResp = orgAuthClient.getReferencingObjects(orgAuthCSID, currentOwnerOrgCSID);
260 AuthorityRefDocList list = null;
262 assertStatusCode(refDocListResp, testName);
263 list = refDocListResp.readEntity(AuthorityRefDocList.class);
264 Assert.assertNotNull(list);
266 if (refDocListResp != null) {
267 refDocListResp.close();
271 // Optionally output additional data about list members for debugging.
272 boolean iterateThroughList = true;
273 int nIntakesFound = 0;
274 if(iterateThroughList && logger.isDebugEnabled()){
275 List<AuthorityRefDocList.AuthorityRefDocItem> items =
276 list.getAuthorityRefDocItem();
278 logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
279 for(AuthorityRefDocList.AuthorityRefDocItem item : items){
280 logger.debug(testName + ": list-item[" + i + "] " +
281 item.getDocType() + "(" +
282 item.getDocId() + ") Name:[" +
283 item.getDocName() + "] Number:[" +
284 item.getDocNumber() + "] in field:[" +
285 item.getSourceField() + "]");
286 if(knownIntakeId.equalsIgnoreCase(item.getDocId())) {
292 Assert.assertTrue((nIntakesFound==2), "Did not find Intake (twice) with authref!");
297 // ---------------------------------------------------------------
298 // Cleanup of resources created during testing
299 // ---------------------------------------------------------------
302 * Deletes all resources created by tests, after all tests have been run.
304 * This cleanup method will always be run, even if one or more tests fail.
305 * For this reason, it attempts to remove all resources created
306 * at any point during testing, even if some of those resources
307 * may be expected to be deleted by certain tests.
309 @AfterClass(alwaysRun=true)
310 public void cleanUp() {
311 String noTest = System.getProperty("noTestCleanup");
312 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
313 if (logger.isDebugEnabled()) {
314 logger.debug("Skipping Cleanup phase ...");
318 if (logger.isDebugEnabled()) {
319 logger.debug("Cleaning up temporary resources created for testing ...");
321 IntakeClient intakeClient = new IntakeClient();
322 // Note: Any non-success responses are ignored and not reported.
323 for (String resourceId : intakeIdsCreated) {
324 intakeClient.delete(resourceId).close();
326 // Delete persons before PersonAuth
327 OrgAuthorityClient personAuthClient = new OrgAuthorityClient();
328 for (String resourceId : orgIdsCreated) {
329 personAuthClient.deleteItem(orgAuthCSID, resourceId).close();
331 if (orgAuthCSID != null) {
332 personAuthClient.delete(orgAuthCSID).close();
336 // ---------------------------------------------------------------
337 // Utility methods used by tests above
338 // ---------------------------------------------------------------
340 public String getServicePathComponent() {
341 return SERVICE_PATH_COMPONENT;
344 private PoxPayloadOut createIntakeInstance(String entryNumber,
348 String conditionCheckerAssessor,
351 IntakesCommon intake = new IntakesCommon();
352 intake.setEntryNumber(entryNumber);
353 intake.setEntryDate(entryDate);
354 intake.setCurrentOwner(currentOwner);
355 intake.setDepositor(depositor);
356 intake.setValuer(Valuer);
358 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
359 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
360 checkersOrAssessors.add(conditionCheckerAssessor);
361 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
363 InsurerList insurerList = new InsurerList();
364 List<String> insurers = insurerList.getInsurer();
365 insurers.add(insurer);
366 intake.setInsurers(insurerList);
368 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
369 PayloadOutputPart commonPart =
370 multipart.addPart(new IntakeClient().getCommonPartName(), intake);
372 if(logger.isDebugEnabled()){
373 logger.debug("to be created, intake common");
374 logger.debug(objectAsXmlString(intake, IntakesCommon.class));