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
125 conditionCheckerAssessorRefName,
\r
129 ClientResponse<Response> res = intakeClient.create(multipart);
\r
131 int statusCode = res.getStatus();
\r
133 // Check the status code of the response: does it match
\r
134 // the expected response(s)?
\r
137 // Does it fall within the set of valid status codes?
\r
138 // Does it exactly match the expected status code?
\r
139 if(logger.isDebugEnabled()){
\r
140 logger.debug(testName + ": status = " + statusCode);
\r
142 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
\r
143 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
144 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
\r
146 res.releaseConnection();
\r
149 // Store the ID returned from the first resource created
\r
150 // for additional tests below.
\r
151 if (knownIntakeId == null){
\r
152 knownIntakeId = extractId(res);
\r
153 if (logger.isDebugEnabled()) {
\r
154 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
\r
158 // Store the IDs from every resource created by tests,
\r
159 // so they can be deleted after tests have been run.
\r
160 intakeIdsCreated.add(extractId(res));
\r
164 * Creates the organization refs.
\r
166 protected void createOrgRefs(){
\r
167 OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
\r
169 OrgAuthorityClientUtils.createOrgAuthRefName(ORGANIZATION_AUTHORITY_NAME, null);
\r
170 MultipartOutput multipart = OrgAuthorityClientUtils.createOrgAuthorityInstance(
\r
171 ORGANIZATION_AUTHORITY_NAME, ORGANIZATION_AUTHORITY_NAME, orgAuthClient.getCommonPartName());
\r
172 ClientResponse<Response> res = orgAuthClient.create(multipart);
\r
173 int statusCode = res.getStatus();
\r
175 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
\r
176 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
177 Assert.assertEquals(statusCode, STATUS_CREATED);
\r
178 orgAuthCSID = extractId(res);
\r
180 currentOwnerOrgCSID = createOrganization("olivierOwnerCompany", "Olivier Owner Company", "Olivier Owner Company");
\r
181 orgIdsCreated.add(currentOwnerOrgCSID);
\r
182 currentOwnerRefName = OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, currentOwnerOrgCSID, orgAuthClient);
\r
184 String newOrgCSID =
\r
185 createOrganization("debbieDepositorAssocs", "Debbie Depositor & Associates", "Debbie Depositor & Associates");
\r
186 depositorRefName =
\r
187 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
\r
188 orgIdsCreated.add(newOrgCSID);
\r
190 newOrgCSID = createOrganization("andrewCheckerAssessorLtd", "Andrew Checker-Assessor Ltd.", "Andrew Checker-Assessor Ltd.");
\r
191 conditionCheckerAssessorRefName =
\r
192 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
\r
193 orgIdsCreated.add(newOrgCSID);
\r
195 newOrgCSID = createOrganization("ingridInsurerBureau", "Ingrid Insurer Bureau", "Ingrid Insurer Bureau");
\r
197 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
\r
198 orgIdsCreated.add(newOrgCSID);
\r
200 newOrgCSID = createOrganization("vinceValuerLLC", "Vince Valuer LLC", "Vince Valuer LLC");
\r
202 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
\r
203 orgIdsCreated.add(newOrgCSID);
\r
206 protected String createOrganization(String shortId, String shortName, String longName) {
\r
207 OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
\r
208 Map<String, String> orgInfo = new HashMap<String,String>();
\r
209 orgInfo.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
\r
210 orgInfo.put(OrganizationJAXBSchema.SHORT_NAME, shortName);
\r
211 orgInfo.put(OrganizationJAXBSchema.LONG_NAME, longName);
\r
212 MultipartOutput multipart =
\r
213 OrgAuthorityClientUtils.createOrganizationInstance(orgAuthRefName,
\r
214 orgInfo, orgAuthClient.getItemCommonPartName());
\r
215 ClientResponse<Response> res = orgAuthClient.createItem(orgAuthCSID, multipart);
\r
216 int statusCode = res.getStatus();
\r
218 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
\r
219 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
220 Assert.assertEquals(statusCode, STATUS_CREATED);
\r
221 return extractId(res);
\r
224 // Success outcomes
\r
225 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
\r
226 dependsOnMethods = {"createIntakeWithAuthRefs"})
\r
227 public void readAndCheckAuthRefDocs(String testName) throws Exception {
\r
229 if (logger.isDebugEnabled()) {
\r
230 logger.debug(testBanner(testName, CLASS_NAME));
\r
233 testSetup(STATUS_OK, ServiceRequestType.READ);
\r
235 // Get the auth ref docs and check them
\r
236 OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
\r
237 ClientResponse<AuthorityRefDocList> refDocListResp =
\r
238 orgAuthClient.getReferencingObjects(orgAuthCSID, currentOwnerOrgCSID);
\r
240 int statusCode = refDocListResp.getStatus();
\r
242 if(logger.isDebugEnabled()){
\r
243 logger.debug(testName + ".getReferencingObjects: status = " + statusCode);
\r
245 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
\r
246 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
247 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
\r
248 AuthorityRefDocList list = refDocListResp.getEntity();
\r
250 // Optionally output additional data about list members for debugging.
\r
251 boolean iterateThroughList = true;
\r
252 boolean fFoundIntake = false;
\r
253 if(iterateThroughList && logger.isDebugEnabled()){
\r
254 List<AuthorityRefDocList.AuthorityRefDocItem> items =
\r
255 list.getAuthorityRefDocItem();
\r
257 logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
\r
258 for(AuthorityRefDocList.AuthorityRefDocItem item : items){
\r
259 logger.debug(testName + ": list-item[" + i + "] " +
\r
260 item.getDocType() + "(" +
\r
261 item.getDocId() + ") Name:[" +
\r
262 item.getDocName() + "] Number:[" +
\r
263 item.getDocNumber() + "] in field:[" +
\r
264 item.getSourceField() + "]");
\r
265 if(!fFoundIntake && knownIntakeId.equalsIgnoreCase(item.getDocId())) {
\r
266 fFoundIntake = true;
\r
270 Assert.assertTrue(fFoundIntake, "Did not find Intake with authref!");
\r
275 // ---------------------------------------------------------------
\r
276 // Cleanup of resources created during testing
\r
277 // ---------------------------------------------------------------
\r
280 * Deletes all resources created by tests, after all tests have been run.
\r
282 * This cleanup method will always be run, even if one or more tests fail.
\r
283 * For this reason, it attempts to remove all resources created
\r
284 * at any point during testing, even if some of those resources
\r
285 * may be expected to be deleted by certain tests.
\r
287 @AfterClass(alwaysRun=true)
\r
288 public void cleanUp() {
\r
289 String noTest = System.getProperty("noTestCleanup");
\r
290 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
\r
291 if (logger.isDebugEnabled()) {
\r
292 logger.debug("Skipping Cleanup phase ...");
\r
296 if (logger.isDebugEnabled()) {
\r
297 logger.debug("Cleaning up temporary resources created for testing ...");
\r
299 IntakeClient intakeClient = new IntakeClient();
\r
300 // Note: Any non-success responses are ignored and not reported.
\r
301 for (String resourceId : intakeIdsCreated) {
\r
302 ClientResponse<Response> res = intakeClient.delete(resourceId);
\r
303 res.releaseConnection();
\r
305 // Delete persons before PersonAuth
\r
306 OrgAuthorityClient personAuthClient = new OrgAuthorityClient();
\r
307 for (String resourceId : orgIdsCreated) {
\r
308 ClientResponse<Response> res = personAuthClient.deleteItem(orgAuthCSID, resourceId);
\r
309 res.releaseConnection();
\r
311 if (orgAuthCSID != null) {
\r
312 personAuthClient.delete(orgAuthCSID).releaseConnection();
\r
316 // ---------------------------------------------------------------
\r
317 // Utility methods used by tests above
\r
318 // ---------------------------------------------------------------
\r
320 public String getServicePathComponent() {
\r
321 return SERVICE_PATH_COMPONENT;
\r
324 private MultipartOutput createIntakeInstance(String entryNumber,
\r
326 String currentOwner,
\r
328 String conditionCheckerAssessor,
\r
331 IntakesCommon intake = new IntakesCommon();
\r
332 intake.setEntryNumber(entryNumber);
\r
333 intake.setEntryDate(entryDate);
\r
334 intake.setCurrentOwner(currentOwner);
\r
335 intake.setDepositor(depositor);
\r
336 intake.setValuer(Valuer);
\r
338 ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
\r
339 List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
\r
340 checkersOrAssessors.add(conditionCheckerAssessor);
\r
341 intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
\r
343 InsurerList insurerList = new InsurerList();
\r
344 List<String> insurers = insurerList.getInsurer();
\r
345 insurers.add(insurer);
\r
346 intake.setInsurers(insurerList);
\r
348 MultipartOutput multipart = new MultipartOutput();
\r
349 OutputPart commonPart =
\r
350 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
\r
351 commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
\r
353 if(logger.isDebugEnabled()){
\r
354 logger.debug("to be created, intake common");
\r
355 logger.debug(objectAsXmlString(intake, IntakesCommon.class));
\r