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.common.authorityref.AuthorityRefDocList;
\r
39 import org.collectionspace.services.intake.ConditionCheckerOrAssessorList;
\r
40 import org.collectionspace.services.intake.IntakesCommon;
\r
41 import org.collectionspace.services.intake.InsurerList;
\r
42 import org.collectionspace.services.jaxb.AbstractCommonList;
\r
44 import org.jboss.resteasy.client.ClientResponse;
\r
46 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
\r
47 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
\r
48 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
\r
49 import org.testng.Assert;
\r
50 import org.testng.annotations.AfterClass;
\r
51 import org.testng.annotations.Test;
\r
53 import org.slf4j.Logger;
\r
54 import org.slf4j.LoggerFactory;
\r
57 * OrganizationAuthRefDocsTest, carries out tests against a
\r
58 * deployed and running Organization Service.
\r
60 * $LastChangedRevision: 1327 $
\r
61 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
\r
63 public class OrganizationAuthRefDocsTest extends BaseServiceTest {
\r
65 private final String CLASS_NAME = OrganizationAuthRefDocsTest.class.getName();
\r
66 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
\r
68 // Instance variables specific to this test.
\r
69 final String SERVICE_PATH_COMPONENT = "intakes";
\r
70 final String ORGANIZATION_AUTHORITY_NAME = "TestOrganizationAuth";
\r
71 private String knownIntakeId = null;
\r
72 private List<String> intakeIdsCreated = new ArrayList<String>();
\r
73 private List<String> orgIdsCreated = new ArrayList<String>();
\r
74 private String orgAuthCSID = null;
\r
75 private String orgAuthRefName = null;
\r
76 private String currentOwnerOrgCSID = null;
\r
77 private String currentOwnerRefName = null;
\r
78 private String depositorRefName = null;
\r
79 private String conditionCheckerAssessorRefName = null;
\r
80 private String insurerRefName = null;
\r
81 private String valuerRefName = null;
\r
82 private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;
\r
85 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
\r
88 protected CollectionSpaceClient getClientInstance() {
\r
89 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
\r
93 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
\r
96 protected AbstractCommonList getAbstractCommonList(
\r
97 ClientResponse<AbstractCommonList> response) {
\r
98 throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
\r
101 // ---------------------------------------------------------------
\r
102 // CRUD tests : CREATE tests
\r
103 // ---------------------------------------------------------------
\r
104 // Success outcomes
\r
105 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
\r
106 public void createIntakeWithAuthRefs(String testName) throws Exception {
\r
108 if (logger.isDebugEnabled()) {
\r
109 logger.debug(testBanner(testName, CLASS_NAME));
\r
111 testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
\r
113 // Submit the request to the service and store the response.
\r
114 String identifier = createIdentifier();
\r
116 // Create all the organization refs and entities
\r
119 IntakeClient intakeClient = new IntakeClient();
\r
120 MultipartOutput multipart = createIntakeInstance(
\r
121 "entryNumber-" + identifier,
\r
122 "entryDate-" + identifier,
\r
123 currentOwnerRefName,
\r
124 // Use currentOwnerRefName twice to test fix for CSPACE-2863
\r
125 currentOwnerRefName, //depositorRefName,
\r
126 conditionCheckerAssessorRefName,
\r
130 ClientResponse<Response> res = intakeClient.create(multipart);
\r
132 int statusCode = res.getStatus();
\r
134 // Check the status code of the response: does it match
\r
135 // the expected response(s)?
\r
138 // Does it fall within the set of valid status codes?
\r
139 // Does it exactly match the expected status code?
\r
140 if(logger.isDebugEnabled()){
\r
141 logger.debug(testName + ": status = " + statusCode);
\r
143 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
\r
144 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
145 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
\r
147 res.releaseConnection();
\r
150 // Store the ID returned from the first resource created
\r
151 // for additional tests below.
\r
152 if (knownIntakeId == null){
\r
153 knownIntakeId = extractId(res);
\r
154 if (logger.isDebugEnabled()) {
\r
155 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
\r
159 // Store the IDs from every resource created by tests,
\r
160 // so they can be deleted after tests have been run.
\r
161 intakeIdsCreated.add(extractId(res));
\r
165 * Creates the organization refs.
\r
167 protected void createOrgRefs(){
\r
168 OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
\r
170 OrgAuthorityClientUtils.createOrgAuthRefName(ORGANIZATION_AUTHORITY_NAME, null);
\r
171 MultipartOutput multipart = OrgAuthorityClientUtils.createOrgAuthorityInstance(
\r
172 ORGANIZATION_AUTHORITY_NAME, ORGANIZATION_AUTHORITY_NAME, orgAuthClient.getCommonPartName());
\r
173 ClientResponse<Response> res = orgAuthClient.create(multipart);
\r
174 int statusCode = res.getStatus();
\r
176 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
\r
177 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
178 Assert.assertEquals(statusCode, STATUS_CREATED);
\r
179 orgAuthCSID = extractId(res);
\r
181 currentOwnerOrgCSID = createOrganization("olivierOwnerCompany", "Olivier Owner Company", "Olivier Owner Company");
\r
182 orgIdsCreated.add(currentOwnerOrgCSID);
\r
183 currentOwnerRefName = OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, currentOwnerOrgCSID, orgAuthClient);
\r
185 String newOrgCSID =
\r
186 createOrganization("debbieDepositorAssocs", "Debbie Depositor & Associates", "Debbie Depositor & Associates");
\r
187 depositorRefName =
\r
188 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
\r
189 orgIdsCreated.add(newOrgCSID);
\r
191 newOrgCSID = createOrganization("andrewCheckerAssessorLtd", "Andrew Checker-Assessor Ltd.", "Andrew Checker-Assessor Ltd.");
\r
192 conditionCheckerAssessorRefName =
\r
193 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
\r
194 orgIdsCreated.add(newOrgCSID);
\r
196 newOrgCSID = createOrganization("ingridInsurerBureau", "Ingrid Insurer Bureau", "Ingrid Insurer Bureau");
\r
198 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
\r
199 orgIdsCreated.add(newOrgCSID);
\r
201 newOrgCSID = createOrganization("vinceValuerLLC", "Vince Valuer LLC", "Vince Valuer LLC");
\r
203 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
\r
204 orgIdsCreated.add(newOrgCSID);
\r
207 protected String createOrganization(String shortId, String shortName, String longName) {
\r
208 OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
\r
209 Map<String, String> orgInfo = new HashMap<String,String>();
\r
210 orgInfo.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
\r
211 orgInfo.put(OrganizationJAXBSchema.SHORT_NAME, shortName);
\r
212 orgInfo.put(OrganizationJAXBSchema.LONG_NAME, longName);
\r
213 MultipartOutput multipart =
\r
214 OrgAuthorityClientUtils.createOrganizationInstance(orgAuthRefName,
\r
215 orgInfo, orgAuthClient.getItemCommonPartName());
\r
216 ClientResponse<Response> res = orgAuthClient.createItem(orgAuthCSID, multipart);
\r
217 int statusCode = res.getStatus();
\r
219 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
\r
220 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
221 Assert.assertEquals(statusCode, STATUS_CREATED);
\r
222 return extractId(res);
\r
225 // Success outcomes
\r
226 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
\r
227 dependsOnMethods = {"createIntakeWithAuthRefs"})
\r
228 public void readAndCheckAuthRefDocs(String testName) throws Exception {
\r
230 if (logger.isDebugEnabled()) {
\r
231 logger.debug(testBanner(testName, CLASS_NAME));
\r
234 testSetup(STATUS_OK, ServiceRequestType.READ);
\r
236 // Get the auth ref docs and check them
\r
237 OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
\r
238 ClientResponse<AuthorityRefDocList> refDocListResp =
\r
239 orgAuthClient.getReferencingObjects(orgAuthCSID, currentOwnerOrgCSID);
\r
241 int statusCode = refDocListResp.getStatus();
\r
243 if(logger.isDebugEnabled()){
\r
244 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
\r
246 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
\r
247 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
248 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
\r
249 AuthorityRefDocList list = refDocListResp.getEntity();
\r
251 // Optionally output additional data about list members for debugging.
\r
252 boolean iterateThroughList = true;
\r
253 int nIntakesFound = 0;
\r
254 if(iterateThroughList && logger.isDebugEnabled()){
\r
255 List<AuthorityRefDocList.AuthorityRefDocItem> items =
\r
256 list.getAuthorityRefDocItem();
\r
258 logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
\r
259 for(AuthorityRefDocList.AuthorityRefDocItem item : items){
\r
260 logger.debug(testName + ": list-item[" + i + "] " +
\r
261 item.getDocType() + "(" +
\r
262 item.getDocId() + ") Name:[" +
\r
263 item.getDocName() + "] Number:[" +
\r
264 item.getDocNumber() + "] in field:[" +
\r
265 item.getSourceField() + "]");
\r
266 if(knownIntakeId.equalsIgnoreCase(item.getDocId())) {
\r
272 Assert.assertTrue((nIntakesFound==2), "Did not find Intake (twice) with authref!");
\r
277 // ---------------------------------------------------------------
\r
278 // Cleanup of resources created during testing
\r
279 // ---------------------------------------------------------------
\r
282 * Deletes all resources created by tests, after all tests have been run.
\r
284 * This cleanup method will always be run, even if one or more tests fail.
\r
285 * For this reason, it attempts to remove all resources created
\r
286 * at any point during testing, even if some of those resources
\r
287 * may be expected to be deleted by certain tests.
\r
289 @AfterClass(alwaysRun=true)
\r
290 public void cleanUp() {
\r
291 String noTest = System.getProperty("noTestCleanup");
\r
292 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
\r
293 if (logger.isDebugEnabled()) {
\r
294 logger.debug("Skipping Cleanup phase ...");
\r
298 if (logger.isDebugEnabled()) {
\r
299 logger.debug("Cleaning up temporary resources created for testing ...");
\r
301 IntakeClient intakeClient = new IntakeClient();
\r
302 // Note: Any non-success responses are ignored and not reported.
\r
303 for (String resourceId : intakeIdsCreated) {
\r
304 ClientResponse<Response> res = intakeClient.delete(resourceId);
\r
305 res.releaseConnection();
\r
307 // Delete persons before PersonAuth
\r
308 OrgAuthorityClient personAuthClient = new OrgAuthorityClient();
\r
309 for (String resourceId : orgIdsCreated) {
\r
310 ClientResponse<Response> res = personAuthClient.deleteItem(orgAuthCSID, resourceId);
\r
311 res.releaseConnection();
\r
313 if (orgAuthCSID != null) {
\r
314 personAuthClient.delete(orgAuthCSID).releaseConnection();
\r
318 // ---------------------------------------------------------------
\r
319 // Utility methods used by tests above
\r
320 // ---------------------------------------------------------------
\r
322 public String getServicePathComponent() {
\r
323 return SERVICE_PATH_COMPONENT;
\r
326 private MultipartOutput createIntakeInstance(String entryNumber,
\r
328 String currentOwner,
\r
330 String conditionCheckerAssessor,
\r
333 IntakesCommon intake = new IntakesCommon();
\r
334 intake.setEntryNumber(entryNumber);
\r
335 intake.setEntryDate(entryDate);
\r
336 intake.setCurrentOwner(currentOwner);
\r
337 intake.setDepositor(depositor);
\r
338 intake.setValuer(Valuer);
\r
340 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
\r
341 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
\r
342 checkersOrAssessors.add(conditionCheckerAssessor);
\r
343 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
\r
345 InsurerList insurerList = new InsurerList();
\r
346 List<String> insurers = insurerList.getInsurer();
\r
347 insurers.add(insurer);
\r
348 intake.setInsurers(insurerList);
\r
350 MultipartOutput multipart = new MultipartOutput();
\r
351 OutputPart commonPart =
\r
352 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
\r
353 commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
\r
355 if(logger.isDebugEnabled()){
\r
356 logger.debug("to be created, intake common");
\r
357 logger.debug(objectAsXmlString(intake, IntakesCommon.class));
\r