]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
be59cc2522ef4d805bed565043469a97d46e6de5
[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.MediaType;
31 import javax.ws.rs.core.Response;
32
33 import org.collectionspace.services.OrganizationJAXBSchema;
34 import org.collectionspace.services.client.CollectionSpaceClient;
35 import org.collectionspace.services.client.IntakeClient;
36 import org.collectionspace.services.client.OrgAuthorityClient;
37 import org.collectionspace.services.client.OrgAuthorityClientUtils;
38 import org.collectionspace.services.client.PayloadOutputPart;
39 import org.collectionspace.services.client.PoxPayloadOut;
40 import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;
41 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
42 import org.collectionspace.services.intake.ConditionCheckerOrAssessorList;
43 import org.collectionspace.services.intake.IntakesCommon;
44 import org.collectionspace.services.intake.InsurerList;
45 import org.collectionspace.services.jaxb.AbstractCommonList;
46 import org.collectionspace.services.organization.OrgTermGroup;
47
48 import org.jboss.resteasy.client.ClientResponse;
49
50 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
51 //import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
52 //import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
53 import org.testng.Assert;
54 import org.testng.annotations.AfterClass;
55 import org.testng.annotations.Test;
56
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60 /**
61  * OrganizationAuthRefDocsTest, carries out tests against a
62  * deployed and running Organization Service.
63  *
64  * $LastChangedRevision: 1327 $
65  * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
66  */
67 public class OrganizationAuthRefDocsTest extends BaseServiceTest<AbstractCommonList> {
68
69     private final String CLASS_NAME = OrganizationAuthRefDocsTest.class.getName();
70     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
71
72     // Instance variables specific to this test.
73     final String SERVICE_PATH_COMPONENT = "intakes";
74     final String ORGANIZATION_AUTHORITY_NAME = "TestOrganizationAuth";
75     private String knownIntakeId = null;
76     private List<String> intakeIdsCreated = new ArrayList<String>();
77     private List<String> orgIdsCreated = new ArrayList<String>();
78     private String orgAuthCSID = null; 
79     //private String orgAuthRefName = null; 
80     private String currentOwnerOrgCSID = null; 
81     private String currentOwnerRefName = null;
82     private String depositorRefName = null;
83     private String conditionCheckerAssessorRefName = null;
84     private String insurerRefName = null;
85     private String valuerRefName = null;
86     private final int NUM_AUTH_REF_DOCS_EXPECTED = 1;
87     private final static String CURRENT_DATE_UTC =
88             GregorianCalendarDateTimeUtils.currentDateUTC();
89
90         @Override
91         protected String getServiceName() {
92                 throw new UnsupportedOperationException(); //FIXME: REM - See http://issues.collectionspace.org/browse/CSPACE-3498
93         }
94     
95     /* (non-Javadoc)
96      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
97      */
98     @Override
99     protected CollectionSpaceClient getClientInstance() {
100         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
101     }
102     
103     /* (non-Javadoc)
104      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
105      */
106     @Override
107         protected AbstractCommonList getCommonList(
108                         ClientResponse<AbstractCommonList> response) {
109         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
110     }
111
112     // ---------------------------------------------------------------
113     // CRUD tests : CREATE tests
114     // ---------------------------------------------------------------
115     // Success outcomes
116     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
117     public void createIntakeWithAuthRefs(String testName) throws Exception {
118         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
119
120         // Submit the request to the service and store the response.
121         String identifier = createIdentifier();
122         
123         // Create all the organization refs and entities
124         createOrgRefs();
125
126         IntakeClient intakeClient = new IntakeClient();
127         PoxPayloadOut multipart = createIntakeInstance(
128                 "entryNumber-" + identifier,
129                 CURRENT_DATE_UTC,
130                 currentOwnerRefName,
131                 // Use currentOwnerRefName twice to test fix for CSPACE-2863
132                 currentOwnerRefName,    //depositorRefName,
133                 conditionCheckerAssessorRefName,
134                 insurerRefName,
135                 valuerRefName );
136
137         ClientResponse<Response> res = intakeClient.create(multipart);
138         try {
139                 int statusCode = res.getStatus();
140         
141                 // Check the status code of the response: does it match
142                 // the expected response(s)?
143                 //
144                 // Specifically:
145                 // Does it fall within the set of valid status codes?
146                 // Does it exactly match the expected status code?
147                 if(logger.isDebugEnabled()){
148                     logger.debug(testName + ": status = " + statusCode);
149                 }
150                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
151                         invalidStatusCodeMessage(testRequestType, statusCode));
152                 Assert.assertEquals(statusCode, testExpectedStatusCode);
153         } finally {
154                 res.releaseConnection();
155         }
156
157         // Store the ID returned from the first resource created
158         // for additional tests below.
159         if (knownIntakeId == null){
160             knownIntakeId = extractId(res);
161             if (logger.isDebugEnabled()) {
162                 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
163             }
164         }
165         
166         // Store the IDs from every resource created by tests,
167         // so they can be deleted after tests have been run.
168         intakeIdsCreated.add(extractId(res));
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         ClientResponse<Response> res = orgAuthClient.create(multipart);
181         int statusCode = res.getStatus();
182
183         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
184                 invalidStatusCodeMessage(testRequestType, statusCode));
185         Assert.assertEquals(statusCode, STATUS_CREATED);
186         orgAuthCSID = extractId(res);
187         
188                 currentOwnerOrgCSID = createOrganization("olivierOwnerCompany", "Olivier Owner Company", "Olivier Owner Company");
189         orgIdsCreated.add(currentOwnerOrgCSID);
190         currentOwnerRefName = OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, currentOwnerOrgCSID, orgAuthClient);
191         
192                 String newOrgCSID =
193                         createOrganization("debbieDepositorAssocs", "Debbie Depositor & Associates", "Debbie Depositor & Associates");
194         depositorRefName = 
195                 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
196         orgIdsCreated.add(newOrgCSID);
197         
198                 newOrgCSID = createOrganization("andrewCheckerAssessorLtd", "Andrew Checker-Assessor Ltd.", "Andrew Checker-Assessor Ltd.");
199                 conditionCheckerAssessorRefName = 
200                 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
201         orgIdsCreated.add(newOrgCSID);
202         
203                 newOrgCSID = createOrganization("ingridInsurerBureau", "Ingrid Insurer Bureau", "Ingrid Insurer Bureau");
204                 insurerRefName = 
205                 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
206         orgIdsCreated.add(newOrgCSID);
207         
208                 newOrgCSID = createOrganization("vinceValuerLLC", "Vince Valuer LLC", "Vince Valuer LLC");
209                 valuerRefName = 
210                 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
211         orgIdsCreated.add(newOrgCSID);
212     }
213
214     protected String createOrganization(String shortId, String shortName, String longName) {
215         OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
216         Map<String, String> orgInfo = new HashMap<String,String>();
217         orgInfo.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
218         
219         List<OrgTermGroup> orgTerms = new ArrayList<OrgTermGroup>();
220         OrgTermGroup term = new OrgTermGroup();
221         term.setTermDisplayName(shortName);
222         term.setTermName(shortName);
223         term.setMainBodyName(longName);
224         orgTerms.add(term);
225         PoxPayloadOut multipart =
226                 OrgAuthorityClientUtils.createOrganizationInstance(null, //orgAuthRefName
227                 orgInfo, orgTerms, orgAuthClient.getItemCommonPartName());
228
229         ClientResponse<Response> res = orgAuthClient.createItem(orgAuthCSID, multipart);
230         int statusCode = res.getStatus();
231
232         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
233                 invalidStatusCodeMessage(testRequestType, statusCode));
234         Assert.assertEquals(statusCode, STATUS_CREATED);
235         return extractId(res);
236     }
237
238     // Success outcomes
239     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
240         dependsOnMethods = {"createIntakeWithAuthRefs"})
241     public void readAndCheckAuthRefDocs(String testName) throws Exception {
242         // Perform setup.
243         testSetup(STATUS_OK, ServiceRequestType.READ);
244         
245         // Get the auth ref docs and check them
246        OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
247        ClientResponse<AuthorityRefDocList> refDocListResp =
248                 orgAuthClient.getReferencingObjects(orgAuthCSID, currentOwnerOrgCSID);
249        AuthorityRefDocList list = null;
250        try {
251            assertStatusCode(refDocListResp, testName);
252            list = refDocListResp.getEntity();
253            Assert.assertNotNull(list);
254        } finally {
255            if (refDocListResp != null) {
256                    refDocListResp.releaseConnection();
257            }
258        }
259
260         // Optionally output additional data about list members for debugging.
261         boolean iterateThroughList = true;
262         int nIntakesFound = 0;
263         if(iterateThroughList && logger.isDebugEnabled()){
264             List<AuthorityRefDocList.AuthorityRefDocItem> items =
265                     list.getAuthorityRefDocItem();
266             int i = 0;
267             logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
268             for(AuthorityRefDocList.AuthorityRefDocItem item : items){
269                 logger.debug(testName + ": list-item[" + i + "] " +
270                                 item.getDocType() + "(" +
271                                 item.getDocId() + ") Name:[" +
272                                 item.getDocName() + "] Number:[" +
273                                 item.getDocNumber() + "] in field:[" +
274                                 item.getSourceField() + "]");
275                 if(knownIntakeId.equalsIgnoreCase(item.getDocId())) {
276                         nIntakesFound++;
277                 }
278                 i++;
279             }
280             //
281             Assert.assertTrue((nIntakesFound==2), "Did not find Intake (twice) with authref!");
282         }
283     }
284
285
286     // ---------------------------------------------------------------
287     // Cleanup of resources created during testing
288     // ---------------------------------------------------------------
289
290     /**
291      * Deletes all resources created by tests, after all tests have been run.
292      *
293      * This cleanup method will always be run, even if one or more tests fail.
294      * For this reason, it attempts to remove all resources created
295      * at any point during testing, even if some of those resources
296      * may be expected to be deleted by certain tests.
297      */
298     @AfterClass(alwaysRun=true)
299     public void cleanUp() {
300         String noTest = System.getProperty("noTestCleanup");
301         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
302             if (logger.isDebugEnabled()) {
303                 logger.debug("Skipping Cleanup phase ...");
304             }
305             return;
306         }
307         if (logger.isDebugEnabled()) {
308             logger.debug("Cleaning up temporary resources created for testing ...");
309         }
310         IntakeClient intakeClient = new IntakeClient();
311         // Note: Any non-success responses are ignored and not reported.
312         for (String resourceId : intakeIdsCreated) {
313             ClientResponse<Response> res = intakeClient.delete(resourceId);
314             res.releaseConnection();
315         }
316         // Delete persons before PersonAuth
317         OrgAuthorityClient personAuthClient = new OrgAuthorityClient();
318         for (String resourceId : orgIdsCreated) {
319             ClientResponse<Response> res = personAuthClient.deleteItem(orgAuthCSID, resourceId);
320             res.releaseConnection();
321         }
322         if (orgAuthCSID != null) {
323                 personAuthClient.delete(orgAuthCSID).releaseConnection();
324         }
325     }
326
327     // ---------------------------------------------------------------
328     // Utility methods used by tests above
329     // ---------------------------------------------------------------
330     @Override
331     public String getServicePathComponent() {
332         return SERVICE_PATH_COMPONENT;
333     }
334
335    private PoxPayloadOut createIntakeInstance(String entryNumber,
336                 String entryDate,
337                                 String currentOwner,
338                                 String depositor,
339                                 String conditionCheckerAssessor,
340                                 String insurer,
341                                 String Valuer ) {
342         IntakesCommon intake = new IntakesCommon();
343         intake.setEntryNumber(entryNumber);
344         intake.setEntryDate(entryDate);
345         intake.setCurrentOwner(currentOwner);
346         intake.setDepositor(depositor);
347         intake.setValuer(Valuer);
348
349         ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
350         List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
351         checkersOrAssessors.add(conditionCheckerAssessor);
352         intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
353
354         InsurerList insurerList = new InsurerList();
355         List<String> insurers = insurerList.getInsurer();
356         insurers.add(insurer);
357         intake.setInsurers(insurerList);
358
359         PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
360         PayloadOutputPart commonPart =
361             multipart.addPart(new IntakeClient().getCommonPartName(), intake);
362
363         if(logger.isDebugEnabled()){
364             logger.debug("to be created, intake common");
365             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
366         }
367
368         return multipart;
369     }
370 }