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.OrganizationClient;
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;
46 import org.testng.Assert;
47 import org.testng.annotations.AfterClass;
48 import org.testng.annotations.Test;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 * OrganizationAuthRefDocsTest, carries out tests against a
54 * deployed and running Organization Service.
56 * $LastChangedRevision: 1327 $
57 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
59 public class OrganizationAuthRefDocsTest extends BaseServiceTest<AbstractCommonList> {
61 private final String CLASS_NAME = OrganizationAuthRefDocsTest.class.getName();
62 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
64 // Instance variables specific to this test.
65 final String SERVICE_PATH_COMPONENT = "intakes";
66 final String ORGANIZATION_AUTHORITY_NAME = "TestOrganizationAuth";
67 private String knownIntakeId = null;
68 private List<String> intakeIdsCreated = new ArrayList<String>();
69 private List<String> orgIdsCreated = new ArrayList<String>();
70 private String orgAuthCSID = null;
71 //private String orgAuthRefName = null;
72 private String currentOwnerOrgCSID = null;
73 private String currentOwnerRefName = null;
74 private String depositorRefName = null;
75 private String conditionCheckerAssessorRefName = null;
76 private String insurerRefName = null;
77 private String valuerRefName = null;
78 private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;
79 private final static String CURRENT_DATE_UTC =
80 GregorianCalendarDateTimeUtils.currentDateUTC();
83 protected String getServiceName() {
84 throw new UnsupportedOperationException(); //FIXME: REM - See http://issues.collectionspace.org/browse/CSPACE-3498
88 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
91 protected CollectionSpaceClient getClientInstance() {
92 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
96 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
97 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
101 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
104 protected AbstractCommonList getCommonList(Response response) {
105 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
108 // ---------------------------------------------------------------
109 // CRUD tests : CREATE tests
110 // ---------------------------------------------------------------
112 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
113 public void createIntakeWithAuthRefs(String testName) throws Exception {
114 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
116 // Submit the request to the service and store the response.
117 String identifier = createIdentifier();
119 // Create all the organization refs and entities
122 IntakeClient intakeClient = new IntakeClient();
123 PoxPayloadOut intakePayload = createIntakeInstance(
124 "entryNumber-" + identifier,
127 // Use currentOwnerRefName twice to test fix for CSPACE-2863
128 currentOwnerRefName, //depositorRefName,
129 conditionCheckerAssessorRefName,
133 Response res = intakeClient.create(intakePayload);
135 int statusCode = res.getStatus();
137 // Check the status code of the response: does it match
138 // the expected response(s)?
141 // Does it fall within the set of valid status codes?
142 // Does it exactly match the expected status code?
143 if(logger.isDebugEnabled()){
144 logger.debug(testName + ": status = " + statusCode);
146 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
147 invalidStatusCodeMessage(testRequestType, statusCode));
148 Assert.assertEquals(statusCode, testExpectedStatusCode);
149 String newIntakeId = extractId(res);
150 Assert.assertNotNull(newIntakeId, "Could not create a new Intake record.");
152 // Store the ID returned from the first resource created
153 // for additional tests below.
154 if (knownIntakeId == null) {
155 knownIntakeId = newIntakeId;
156 if (logger.isDebugEnabled()) {
157 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
161 // Store the IDs from every resource created by tests,
162 // so they can be deleted after tests have been run.
163 intakeIdsCreated.add(newIntakeId);
171 * Creates the organization refs.
174 protected void createOrgRefs() throws Exception{
175 OrganizationClient orgAuthClient = new OrganizationClient();
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) throws Exception {
219 String result = null;
221 OrganizationClient orgAuthClient = new OrganizationClient();
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 OrganizationClient orgAuthClient = new OrganizationClient();
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.
310 @AfterClass(alwaysRun=true)
311 public void cleanUp() throws Exception {
312 String noTest = System.getProperty("noTestCleanup");
313 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
314 if (logger.isDebugEnabled()) {
315 logger.debug("Skipping Cleanup phase ...");
319 if (logger.isDebugEnabled()) {
320 logger.debug("Cleaning up temporary resources created for testing ...");
322 IntakeClient intakeClient = new IntakeClient();
323 // Note: Any non-success responses are ignored and not reported.
324 for (String resourceId : intakeIdsCreated) {
325 intakeClient.delete(resourceId).close();
327 // Delete persons before PersonAuth
328 OrganizationClient personAuthClient = new OrganizationClient();
329 for (String resourceId : orgIdsCreated) {
330 personAuthClient.deleteItem(orgAuthCSID, resourceId).close();
332 if (orgAuthCSID != null) {
333 personAuthClient.delete(orgAuthCSID).close();
337 // ---------------------------------------------------------------
338 // Utility methods used by tests above
339 // ---------------------------------------------------------------
341 public String getServicePathComponent() {
342 return SERVICE_PATH_COMPONENT;
345 private PoxPayloadOut createIntakeInstance(String entryNumber,
349 String conditionCheckerAssessor,
351 String Valuer ) throws Exception {
352 IntakesCommon intake = new IntakesCommon();
353 intake.setEntryNumber(entryNumber);
354 intake.setEntryDate(entryDate);
355 intake.setCurrentOwner(currentOwner);
356 intake.setDepositor(depositor);
357 intake.setValuer(Valuer);
359 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
360 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
361 checkersOrAssessors.add(conditionCheckerAssessor);
362 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
364 InsurerList insurerList = new InsurerList();
365 List<String> insurers = insurerList.getInsurer();
366 insurers.add(insurer);
367 intake.setInsurers(insurerList);
369 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
370 PayloadOutputPart commonPart =
371 multipart.addPart(new IntakeClient().getCommonPartName(), intake);
373 if(logger.isDebugEnabled()){
374 logger.debug("to be created, intake common");
375 logger.debug(objectAsXmlString(intake, IntakesCommon.class));