]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
87a7a909588298e0ca8bd767c22d11edded68b58
[tmp/jakarta-migration.git] /
1 /**
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:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright © 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
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.
22  */
23 package org.collectionspace.services.client.test;
24
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.ws.rs.core.Response;
31
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;
51
52 /**
53  * OrganizationAuthRefDocsTest, carries out tests against a
54  * deployed and running Organization Service.
55  *
56  * $LastChangedRevision: 1327 $
57  * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
58  */
59 public class OrganizationAuthRefDocsTest extends BaseServiceTest<AbstractCommonList> {
60
61     private final String CLASS_NAME = OrganizationAuthRefDocsTest.class.getName();
62     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
63
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();
81
82         @Override
83         protected String getServiceName() {
84                 throw new UnsupportedOperationException(); //FIXME: REM - See http://issues.collectionspace.org/browse/CSPACE-3498
85         }
86     
87     /* (non-Javadoc)
88      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
89      */
90     @Override
91     protected CollectionSpaceClient getClientInstance() {
92         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
93     }
94
95     @Override
96         protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
97         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
98         }
99     
100     /* (non-Javadoc)
101      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
102      */
103     @Override
104         protected AbstractCommonList getCommonList(Response response) {
105         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
106     }
107
108     // ---------------------------------------------------------------
109     // CRUD tests : CREATE tests
110     // ---------------------------------------------------------------
111     // Success outcomes
112     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
113     public void createIntakeWithAuthRefs(String testName) throws Exception {
114         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
115
116         // Submit the request to the service and store the response.
117         String identifier = createIdentifier();
118         
119         // Create all the organization refs and entities
120         createOrgRefs();
121
122         IntakeClient intakeClient = new IntakeClient();
123         PoxPayloadOut intakePayload = createIntakeInstance(
124                 "entryNumber-" + identifier,
125                 CURRENT_DATE_UTC,
126                 currentOwnerRefName,
127                 // Use currentOwnerRefName twice to test fix for CSPACE-2863
128                 currentOwnerRefName,    //depositorRefName,
129                 conditionCheckerAssessorRefName,
130                 insurerRefName,
131                 valuerRefName );
132
133         Response res = intakeClient.create(intakePayload);
134         try {
135                 int statusCode = res.getStatus();
136         
137                 // Check the status code of the response: does it match
138                 // the expected response(s)?
139                 //
140                 // Specifically:
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);
145                 }
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.");
151
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);
158                     }
159                 }
160                 
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);
164         } finally {
165                 res.close();
166         }
167
168     }
169     
170     /**
171      * Creates the organization refs.
172      * @throws Exception 
173      */
174     protected void createOrgRefs() throws Exception{
175         OrganizationClient orgAuthClient = new OrganizationClient();
176         //orgAuthRefName = 
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);
181         try {
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.");
188         } finally {
189                 res.close();
190         }
191         
192                 currentOwnerOrgCSID = createOrganization("olivierOwnerCompany", "Olivier Owner Company", "Olivier Owner Company");
193         orgIdsCreated.add(currentOwnerOrgCSID);
194         currentOwnerRefName = OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, currentOwnerOrgCSID, orgAuthClient);
195         
196                 String newOrgCSID =
197                         createOrganization("debbieDepositorAssocs", "Debbie Depositor & Associates", "Debbie Depositor & Associates");
198         depositorRefName = 
199                 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
200         orgIdsCreated.add(newOrgCSID);
201         
202                 newOrgCSID = createOrganization("andrewCheckerAssessorLtd", "Andrew Checker-Assessor Ltd.", "Andrew Checker-Assessor Ltd.");
203                 conditionCheckerAssessorRefName = 
204                 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
205         orgIdsCreated.add(newOrgCSID);
206         
207                 newOrgCSID = createOrganization("ingridInsurerBureau", "Ingrid Insurer Bureau", "Ingrid Insurer Bureau");
208                 insurerRefName = 
209                 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
210         orgIdsCreated.add(newOrgCSID);
211         
212                 newOrgCSID = createOrganization("vinceValuerLLC", "Vince Valuer LLC", "Vince Valuer LLC");
213                 valuerRefName = 
214                 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
215         orgIdsCreated.add(newOrgCSID);
216     }
217
218     protected String createOrganization(String shortId, String shortName, String longName) throws Exception {
219         String result = null;
220         
221         OrganizationClient orgAuthClient = new OrganizationClient();
222         Map<String, String> orgInfo = new HashMap<String,String>();
223         orgInfo.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
224         
225         List<OrgTermGroup> orgTerms = new ArrayList<OrgTermGroup>();
226         OrgTermGroup term = new OrgTermGroup();
227         term.setTermDisplayName(shortName);
228         term.setTermName(shortName);
229         term.setMainBodyName(longName);
230         orgTerms.add(term);
231         PoxPayloadOut multipart =
232                 OrgAuthorityClientUtils.createOrganizationInstance(null, //orgAuthRefName
233                 orgInfo, orgTerms, orgAuthClient.getItemCommonPartName());
234
235         Response res = orgAuthClient.createItem(orgAuthCSID, multipart);
236         try {
237                 int statusCode = res.getStatus();
238         
239                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
240                         invalidStatusCodeMessage(testRequestType, statusCode));
241                 Assert.assertEquals(statusCode, STATUS_CREATED);
242                 result = extractId(res);
243         } finally {
244                 res.close();
245         }
246         
247         return result;
248     }
249
250     // Success outcomes
251     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
252         dependsOnMethods = {"createIntakeWithAuthRefs"})
253     public void readAndCheckAuthRefDocs(String testName) throws Exception {
254         // Perform setup.
255         testSetup(STATUS_OK, ServiceRequestType.READ);
256         
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;
261        try {
262            assertStatusCode(refDocListResp, testName);
263            list = refDocListResp.readEntity(AuthorityRefDocList.class);
264            Assert.assertNotNull(list);
265        } finally {
266            if (refDocListResp != null) {
267                    refDocListResp.close();
268            }
269        }
270
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();
277             int i = 0;
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())) {
287                         nIntakesFound++;
288                 }
289                 i++;
290             }
291             //
292             Assert.assertTrue((nIntakesFound==2), "Did not find Intake (twice) with authref!");
293         }
294     }
295
296
297     // ---------------------------------------------------------------
298     // Cleanup of resources created during testing
299     // ---------------------------------------------------------------
300
301     /**
302      * Deletes all resources created by tests, after all tests have been run.
303      *
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.
308      * @throws Exception 
309      */
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 ...");
316             }
317             return;
318         }
319         if (logger.isDebugEnabled()) {
320             logger.debug("Cleaning up temporary resources created for testing ...");
321         }
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();
326         }
327         // Delete persons before PersonAuth
328         OrganizationClient personAuthClient = new OrganizationClient();
329         for (String resourceId : orgIdsCreated) {
330             personAuthClient.deleteItem(orgAuthCSID, resourceId).close();
331         }
332         if (orgAuthCSID != null) {
333                 personAuthClient.delete(orgAuthCSID).close();
334         }
335     }
336
337     // ---------------------------------------------------------------
338     // Utility methods used by tests above
339     // ---------------------------------------------------------------
340     @Override
341     public String getServicePathComponent() {
342         return SERVICE_PATH_COMPONENT;
343     }
344
345    private PoxPayloadOut createIntakeInstance(String entryNumber,
346                 String entryDate,
347                                 String currentOwner,
348                                 String depositor,
349                                 String conditionCheckerAssessor,
350                                 String insurer,
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);
358
359         ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
360         List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
361         checkersOrAssessors.add(conditionCheckerAssessor);
362         intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
363
364         InsurerList insurerList = new InsurerList();
365         List<String> insurers = insurerList.getInsurer();
366         insurers.add(insurer);
367         intake.setInsurers(insurerList);
368
369         PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
370         PayloadOutputPart commonPart =
371             multipart.addPart(new IntakeClient().getCommonPartName(), intake);
372
373         if(logger.isDebugEnabled()){
374             logger.debug("to be created, intake common");
375             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
376         }
377
378         return multipart;
379     }
380 }