2 * This document is a part of the source code and related artifacts
\r
3 * for CollectionSpace, an open source collections management system
\r
4 * for museums and related institutions:
\r
6 * http://www.collectionspace.org
\r
7 * http://wiki.collectionspace.org
\r
9 * Copyright © 2009 Regents of the University of California
\r
11 * Licensed under the Educational Community License (ECL), Version 2.0.
\r
12 * You may not use this file except in compliance with this License.
\r
14 * You may obtain a copy of the ECL 2.0 License at
\r
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
\r
17 * Unless required by applicable law or agreed to in writing, software
\r
18 * distributed under the License is distributed on an "AS IS" BASIS,
\r
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
20 * See the License for the specific language governing permissions and
\r
21 * limitations under the License.
\r
23 package org.collectionspace.services.client.test;
\r
25 import java.util.ArrayList;
\r
26 import java.util.HashMap;
\r
27 import java.util.List;
\r
28 import java.util.Map;
\r
30 import javax.ws.rs.core.MediaType;
\r
31 import javax.ws.rs.core.Response;
\r
33 import org.collectionspace.services.OrganizationJAXBSchema;
\r
34 import org.collectionspace.services.client.CollectionSpaceClient;
\r
35 import org.collectionspace.services.client.IntakeClient;
\r
36 import org.collectionspace.services.client.OrgAuthorityClient;
\r
37 import org.collectionspace.services.client.OrgAuthorityClientUtils;
\r
38 import org.collectionspace.services.client.PayloadOutputPart;
\r
39 import org.collectionspace.services.client.PoxPayloadOut;
\r
40 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
\r
41 import org.collectionspace.services.common.datetime.GregorianCalendarDateTimeUtils;
\r
42 import org.collectionspace.services.intake.ConditionCheckerOrAssessorList;
\r
43 import org.collectionspace.services.intake.IntakesCommon;
\r
44 import org.collectionspace.services.intake.InsurerList;
\r
45 import org.collectionspace.services.jaxb.AbstractCommonList;
\r
47 import org.jboss.resteasy.client.ClientResponse;
\r
49 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
\r
50 //import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
\r
51 //import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
\r
52 import org.testng.Assert;
\r
53 import org.testng.annotations.AfterClass;
\r
54 import org.testng.annotations.Test;
\r
56 import org.slf4j.Logger;
\r
57 import org.slf4j.LoggerFactory;
\r
60 * OrganizationAuthRefDocsTest, carries out tests against a
\r
61 * deployed and running Organization Service.
\r
63 * $LastChangedRevision: 1327 $
\r
64 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
\r
66 public class OrganizationAuthRefDocsTest extends BaseServiceTest {
\r
68 private final String CLASS_NAME = OrganizationAuthRefDocsTest.class.getName();
\r
69 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
\r
71 // Instance variables specific to this test.
\r
72 final String SERVICE_PATH_COMPONENT = "intakes";
\r
73 final String ORGANIZATION_AUTHORITY_NAME = "TestOrganizationAuth";
\r
74 private String knownIntakeId = null;
\r
75 private List<String> intakeIdsCreated = new ArrayList<String>();
\r
76 private List<String> orgIdsCreated = new ArrayList<String>();
\r
77 private String orgAuthCSID = null;
\r
78 //private String orgAuthRefName = null;
\r
79 private String currentOwnerOrgCSID = null;
\r
80 private String currentOwnerRefName = null;
\r
81 private String depositorRefName = null;
\r
82 private String conditionCheckerAssessorRefName = null;
\r
83 private String insurerRefName = null;
\r
84 private String valuerRefName = null;
\r
85 private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;
\r
86 private final static String CURRENT_DATE_UTC =
\r
87 GregorianCalendarDateTimeUtils.currentDateUTC();
\r
90 protected String getServiceName() {
\r
91 throw new UnsupportedOperationException(); //FIXME: REM - See http://issues.collectionspace.org/browse/CSPACE-3498
\r
95 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
\r
98 protected CollectionSpaceClient getClientInstance() {
\r
99 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
\r
103 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
\r
106 protected AbstractCommonList getAbstractCommonList(
\r
107 ClientResponse<AbstractCommonList> response) {
\r
108 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
\r
111 // ---------------------------------------------------------------
\r
112 // CRUD tests : CREATE tests
\r
113 // ---------------------------------------------------------------
\r
114 // Success outcomes
\r
115 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
\r
116 public void createIntakeWithAuthRefs(String testName) throws Exception {
\r
118 if (logger.isDebugEnabled()) {
\r
119 logger.debug(testBanner(testName, CLASS_NAME));
\r
121 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
\r
123 // Submit the request to the service and store the response.
\r
124 String identifier = createIdentifier();
\r
126 // Create all the organization refs and entities
\r
129 IntakeClient intakeClient = new IntakeClient();
\r
130 PoxPayloadOut multipart = createIntakeInstance(
\r
131 "entryNumber-" + identifier,
\r
133 currentOwnerRefName,
\r
134 // Use currentOwnerRefName twice to test fix for CSPACE-2863
\r
135 currentOwnerRefName, //depositorRefName,
\r
136 conditionCheckerAssessorRefName,
\r
140 ClientResponse<Response> res = intakeClient.create(multipart);
\r
142 int statusCode = res.getStatus();
\r
144 // Check the status code of the response: does it match
\r
145 // the expected response(s)?
\r
148 // Does it fall within the set of valid status codes?
\r
149 // Does it exactly match the expected status code?
\r
150 if(logger.isDebugEnabled()){
\r
151 logger.debug(testName + ": status = " + statusCode);
\r
153 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
\r
154 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
155 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
\r
157 res.releaseConnection();
\r
160 // Store the ID returned from the first resource created
\r
161 // for additional tests below.
\r
162 if (knownIntakeId == null){
\r
163 knownIntakeId = extractId(res);
\r
164 if (logger.isDebugEnabled()) {
\r
165 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
\r
169 // Store the IDs from every resource created by tests,
\r
170 // so they can be deleted after tests have been run.
\r
171 intakeIdsCreated.add(extractId(res));
\r
175 * Creates the organization refs.
\r
177 protected void createOrgRefs(){
\r
178 OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
\r
179 //orgAuthRefName =
\r
180 // OrgAuthorityClientUtils.createOrgAuthRefName(ORGANIZATION_AUTHORITY_NAME, null);
\r
181 PoxPayloadOut multipart = OrgAuthorityClientUtils.createOrgAuthorityInstance(
\r
182 ORGANIZATION_AUTHORITY_NAME, ORGANIZATION_AUTHORITY_NAME, orgAuthClient.getCommonPartName());
\r
183 ClientResponse<Response> res = orgAuthClient.create(multipart);
\r
184 int statusCode = res.getStatus();
\r
186 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
\r
187 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
188 Assert.assertEquals(statusCode, STATUS_CREATED);
\r
189 orgAuthCSID = extractId(res);
\r
191 currentOwnerOrgCSID = createOrganization("olivierOwnerCompany", "Olivier Owner Company", "Olivier Owner Company");
\r
192 orgIdsCreated.add(currentOwnerOrgCSID);
\r
193 currentOwnerRefName = OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, currentOwnerOrgCSID, orgAuthClient);
\r
195 String newOrgCSID =
\r
196 createOrganization("debbieDepositorAssocs", "Debbie Depositor & Associates", "Debbie Depositor & Associates");
\r
197 depositorRefName =
\r
198 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
\r
199 orgIdsCreated.add(newOrgCSID);
\r
201 newOrgCSID = createOrganization("andrewCheckerAssessorLtd", "Andrew Checker-Assessor Ltd.", "Andrew Checker-Assessor Ltd.");
\r
202 conditionCheckerAssessorRefName =
\r
203 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
\r
204 orgIdsCreated.add(newOrgCSID);
\r
206 newOrgCSID = createOrganization("ingridInsurerBureau", "Ingrid Insurer Bureau", "Ingrid Insurer Bureau");
\r
208 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
\r
209 orgIdsCreated.add(newOrgCSID);
\r
211 newOrgCSID = createOrganization("vinceValuerLLC", "Vince Valuer LLC", "Vince Valuer LLC");
\r
213 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
\r
214 orgIdsCreated.add(newOrgCSID);
\r
217 protected String createOrganization(String shortId, String shortName, String longName) {
\r
218 OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
\r
219 Map<String, String> orgInfo = new HashMap<String,String>();
\r
220 orgInfo.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
\r
221 orgInfo.put(OrganizationJAXBSchema.SHORT_NAME, shortName);
\r
222 orgInfo.put(OrganizationJAXBSchema.LONG_NAME, longName);
\r
223 PoxPayloadOut multipart =
\r
224 OrgAuthorityClientUtils.createOrganizationInstance(null, //orgAuthRefName,
\r
225 orgInfo, orgAuthClient.getItemCommonPartName());
\r
226 ClientResponse<Response> res = orgAuthClient.createItem(orgAuthCSID, multipart);
\r
227 int statusCode = res.getStatus();
\r
229 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
\r
230 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
231 Assert.assertEquals(statusCode, STATUS_CREATED);
\r
232 return extractId(res);
\r
235 // Success outcomes
\r
236 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
\r
237 dependsOnMethods = {"createIntakeWithAuthRefs"})
\r
238 public void readAndCheckAuthRefDocs(String testName) throws Exception {
\r
240 if (logger.isDebugEnabled()) {
\r
241 logger.debug(testBanner(testName, CLASS_NAME));
\r
244 testSetup(STATUS_OK, ServiceRequestType.READ);
\r
246 // Get the auth ref docs and check them
\r
247 OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
\r
248 ClientResponse<AuthorityRefDocList> refDocListResp =
\r
249 orgAuthClient.getReferencingObjects(orgAuthCSID, currentOwnerOrgCSID);
\r
251 int statusCode = refDocListResp.getStatus();
\r
253 if(logger.isDebugEnabled()){
\r
254 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
\r
256 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
\r
257 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
258 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
\r
259 AuthorityRefDocList list = refDocListResp.getEntity();
\r
261 // Optionally output additional data about list members for debugging.
\r
262 boolean iterateThroughList = true;
\r
263 int nIntakesFound = 0;
\r
264 if(iterateThroughList && logger.isDebugEnabled()){
\r
265 List<AuthorityRefDocList.AuthorityRefDocItem> items =
\r
266 list.getAuthorityRefDocItem();
\r
268 logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
\r
269 for(AuthorityRefDocList.AuthorityRefDocItem item : items){
\r
270 logger.debug(testName + ": list-item[" + i + "] " +
\r
271 item.getDocType() + "(" +
\r
272 item.getDocId() + ") Name:[" +
\r
273 item.getDocName() + "] Number:[" +
\r
274 item.getDocNumber() + "] in field:[" +
\r
275 item.getSourceField() + "]");
\r
276 if(knownIntakeId.equalsIgnoreCase(item.getDocId())) {
\r
282 Assert.assertTrue((nIntakesFound==2), "Did not find Intake (twice) with authref!");
\r
287 // ---------------------------------------------------------------
\r
288 // Cleanup of resources created during testing
\r
289 // ---------------------------------------------------------------
\r
292 * Deletes all resources created by tests, after all tests have been run.
\r
294 * This cleanup method will always be run, even if one or more tests fail.
\r
295 * For this reason, it attempts to remove all resources created
\r
296 * at any point during testing, even if some of those resources
\r
297 * may be expected to be deleted by certain tests.
\r
299 @AfterClass(alwaysRun=true)
\r
300 public void cleanUp() {
\r
301 String noTest = System.getProperty("noTestCleanup");
\r
302 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
\r
303 if (logger.isDebugEnabled()) {
\r
304 logger.debug("Skipping Cleanup phase ...");
\r
308 if (logger.isDebugEnabled()) {
\r
309 logger.debug("Cleaning up temporary resources created for testing ...");
\r
311 IntakeClient intakeClient = new IntakeClient();
\r
312 // Note: Any non-success responses are ignored and not reported.
\r
313 for (String resourceId : intakeIdsCreated) {
\r
314 ClientResponse<Response> res = intakeClient.delete(resourceId);
\r
315 res.releaseConnection();
\r
317 // Delete persons before PersonAuth
\r
318 OrgAuthorityClient personAuthClient = new OrgAuthorityClient();
\r
319 for (String resourceId : orgIdsCreated) {
\r
320 ClientResponse<Response> res = personAuthClient.deleteItem(orgAuthCSID, resourceId);
\r
321 res.releaseConnection();
\r
323 if (orgAuthCSID != null) {
\r
324 personAuthClient.delete(orgAuthCSID).releaseConnection();
\r
328 // ---------------------------------------------------------------
\r
329 // Utility methods used by tests above
\r
330 // ---------------------------------------------------------------
\r
332 public String getServicePathComponent() {
\r
333 return SERVICE_PATH_COMPONENT;
\r
336 private PoxPayloadOut createIntakeInstance(String entryNumber,
\r
338 String currentOwner,
\r
340 String conditionCheckerAssessor,
\r
343 IntakesCommon intake = new IntakesCommon();
\r
344 intake.setEntryNumber(entryNumber);
\r
345 intake.setEntryDate(entryDate);
\r
346 intake.setCurrentOwner(currentOwner);
\r
347 intake.setDepositor(depositor);
\r
348 intake.setValuer(Valuer);
\r
350 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
\r
351 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
\r
352 checkersOrAssessors.add(conditionCheckerAssessor);
\r
353 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
\r
355 InsurerList insurerList = new InsurerList();
\r
356 List<String> insurers = insurerList.getInsurer();
\r
357 insurers.add(insurer);
\r
358 intake.setInsurers(insurerList);
\r
360 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
\r
361 PayloadOutputPart commonPart =
\r
362 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
\r
363 commonPart.setLabel(new IntakeClient().getCommonPartName());
\r
365 if(logger.isDebugEnabled()){
\r
366 logger.debug("to be created, intake common");
\r
367 logger.debug(objectAsXmlString(intake, IntakesCommon.class));
\r