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