]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
271d12c8533b9d8416eb28d7f855e61fc1b1f672
[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(Response response) {
108         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
109     }
110
111     // ---------------------------------------------------------------
112     // CRUD tests : CREATE tests
113     // ---------------------------------------------------------------
114     // Success outcomes
115     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
116     public void createIntakeWithAuthRefs(String testName) throws Exception {
117         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
118
119         // Submit the request to the service and store the response.
120         String identifier = createIdentifier();
121         
122         // Create all the organization refs and entities
123         createOrgRefs();
124
125         IntakeClient intakeClient = new IntakeClient();
126         PoxPayloadOut multipart = createIntakeInstance(
127                 "entryNumber-" + identifier,
128                 CURRENT_DATE_UTC,
129                 currentOwnerRefName,
130                 // Use currentOwnerRefName twice to test fix for CSPACE-2863
131                 currentOwnerRefName,    //depositorRefName,
132                 conditionCheckerAssessorRefName,
133                 insurerRefName,
134                 valuerRefName );
135
136         Response res = intakeClient.create(multipart);
137         try {
138                 int statusCode = res.getStatus();
139         
140                 // Check the status code of the response: does it match
141                 // the expected response(s)?
142                 //
143                 // Specifically:
144                 // Does it fall within the set of valid status codes?
145                 // Does it exactly match the expected status code?
146                 if(logger.isDebugEnabled()){
147                     logger.debug(testName + ": status = " + statusCode);
148                 }
149                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
150                         invalidStatusCodeMessage(testRequestType, statusCode));
151                 Assert.assertEquals(statusCode, testExpectedStatusCode);
152         } finally {
153                 res.close();
154         }
155
156         // Store the ID returned from the first resource created
157         // for additional tests below.
158         if (knownIntakeId == null){
159             knownIntakeId = extractId(res);
160             if (logger.isDebugEnabled()) {
161                 logger.debug(testName + ": knownIntakeId=" + knownIntakeId);
162             }
163         }
164         
165         // Store the IDs from every resource created by tests,
166         // so they can be deleted after tests have been run.
167         intakeIdsCreated.add(extractId(res));
168     }
169     
170     /**
171      * Creates the organization refs.
172      */
173     protected void createOrgRefs(){
174         OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
175         //orgAuthRefName = 
176         //      OrgAuthorityClientUtils.createOrgAuthRefName(ORGANIZATION_AUTHORITY_NAME, null);
177         PoxPayloadOut multipart = OrgAuthorityClientUtils.createOrgAuthorityInstance(
178                         ORGANIZATION_AUTHORITY_NAME, ORGANIZATION_AUTHORITY_NAME, orgAuthClient.getCommonPartName());
179         Response res = orgAuthClient.create(multipart);
180         int statusCode = res.getStatus();
181
182         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
183                 invalidStatusCodeMessage(testRequestType, statusCode));
184         Assert.assertEquals(statusCode, STATUS_CREATED);
185         orgAuthCSID = extractId(res);
186         
187                 currentOwnerOrgCSID = createOrganization("olivierOwnerCompany", "Olivier Owner Company", "Olivier Owner Company");
188         orgIdsCreated.add(currentOwnerOrgCSID);
189         currentOwnerRefName = OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, currentOwnerOrgCSID, orgAuthClient);
190         
191                 String newOrgCSID =
192                         createOrganization("debbieDepositorAssocs", "Debbie Depositor & Associates", "Debbie Depositor & Associates");
193         depositorRefName = 
194                 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
195         orgIdsCreated.add(newOrgCSID);
196         
197                 newOrgCSID = createOrganization("andrewCheckerAssessorLtd", "Andrew Checker-Assessor Ltd.", "Andrew Checker-Assessor Ltd.");
198                 conditionCheckerAssessorRefName = 
199                 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
200         orgIdsCreated.add(newOrgCSID);
201         
202                 newOrgCSID = createOrganization("ingridInsurerBureau", "Ingrid Insurer Bureau", "Ingrid Insurer Bureau");
203                 insurerRefName = 
204                 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
205         orgIdsCreated.add(newOrgCSID);
206         
207                 newOrgCSID = createOrganization("vinceValuerLLC", "Vince Valuer LLC", "Vince Valuer LLC");
208                 valuerRefName = 
209                 OrgAuthorityClientUtils.getOrgRefName(orgAuthCSID, newOrgCSID, orgAuthClient);
210         orgIdsCreated.add(newOrgCSID);
211     }
212
213     protected String createOrganization(String shortId, String shortName, String longName) {
214         OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
215         Map<String, String> orgInfo = new HashMap<String,String>();
216         orgInfo.put(OrganizationJAXBSchema.SHORT_IDENTIFIER, shortId);
217         
218         List<OrgTermGroup> orgTerms = new ArrayList<OrgTermGroup>();
219         OrgTermGroup term = new OrgTermGroup();
220         term.setTermDisplayName(shortName);
221         term.setTermName(shortName);
222         term.setMainBodyName(longName);
223         orgTerms.add(term);
224         PoxPayloadOut multipart =
225                 OrgAuthorityClientUtils.createOrganizationInstance(null, //orgAuthRefName
226                 orgInfo, orgTerms, orgAuthClient.getItemCommonPartName());
227
228         Response res = orgAuthClient.createItem(orgAuthCSID, multipart);
229         int statusCode = res.getStatus();
230
231         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
232                 invalidStatusCodeMessage(testRequestType, statusCode));
233         Assert.assertEquals(statusCode, STATUS_CREATED);
234         return extractId(res);
235     }
236
237     // Success outcomes
238     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
239         dependsOnMethods = {"createIntakeWithAuthRefs"})
240     public void readAndCheckAuthRefDocs(String testName) throws Exception {
241         // Perform setup.
242         testSetup(STATUS_OK, ServiceRequestType.READ);
243         
244         // Get the auth ref docs and check them
245        OrgAuthorityClient orgAuthClient = new OrgAuthorityClient();
246        Response refDocListResp = orgAuthClient.getReferencingObjects(orgAuthCSID, currentOwnerOrgCSID);
247        AuthorityRefDocList list = null;
248        try {
249            assertStatusCode(refDocListResp, testName);
250            list = (AuthorityRefDocList)refDocListResp.getEntity();
251            Assert.assertNotNull(list);
252        } finally {
253            if (refDocListResp != null) {
254                    refDocListResp.close();
255            }
256        }
257
258         // Optionally output additional data about list members for debugging.
259         boolean iterateThroughList = true;
260         int nIntakesFound = 0;
261         if(iterateThroughList && logger.isDebugEnabled()){
262             List<AuthorityRefDocList.AuthorityRefDocItem> items =
263                     list.getAuthorityRefDocItem();
264             int i = 0;
265             logger.debug(testName + ": Docs that use: " + currentOwnerRefName);
266             for(AuthorityRefDocList.AuthorityRefDocItem item : items){
267                 logger.debug(testName + ": list-item[" + i + "] " +
268                                 item.getDocType() + "(" +
269                                 item.getDocId() + ") Name:[" +
270                                 item.getDocName() + "] Number:[" +
271                                 item.getDocNumber() + "] in field:[" +
272                                 item.getSourceField() + "]");
273                 if(knownIntakeId.equalsIgnoreCase(item.getDocId())) {
274                         nIntakesFound++;
275                 }
276                 i++;
277             }
278             //
279             Assert.assertTrue((nIntakesFound==2), "Did not find Intake (twice) with authref!");
280         }
281     }
282
283
284     // ---------------------------------------------------------------
285     // Cleanup of resources created during testing
286     // ---------------------------------------------------------------
287
288     /**
289      * Deletes all resources created by tests, after all tests have been run.
290      *
291      * This cleanup method will always be run, even if one or more tests fail.
292      * For this reason, it attempts to remove all resources created
293      * at any point during testing, even if some of those resources
294      * may be expected to be deleted by certain tests.
295      */
296     @AfterClass(alwaysRun=true)
297     public void cleanUp() {
298         String noTest = System.getProperty("noTestCleanup");
299         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
300             if (logger.isDebugEnabled()) {
301                 logger.debug("Skipping Cleanup phase ...");
302             }
303             return;
304         }
305         if (logger.isDebugEnabled()) {
306             logger.debug("Cleaning up temporary resources created for testing ...");
307         }
308         IntakeClient intakeClient = new IntakeClient();
309         // Note: Any non-success responses are ignored and not reported.
310         for (String resourceId : intakeIdsCreated) {
311             intakeClient.delete(resourceId).close();
312         }
313         // Delete persons before PersonAuth
314         OrgAuthorityClient personAuthClient = new OrgAuthorityClient();
315         for (String resourceId : orgIdsCreated) {
316             personAuthClient.deleteItem(orgAuthCSID, resourceId).close();
317         }
318         if (orgAuthCSID != null) {
319                 personAuthClient.delete(orgAuthCSID).close();
320         }
321     }
322
323     // ---------------------------------------------------------------
324     // Utility methods used by tests above
325     // ---------------------------------------------------------------
326     @Override
327     public String getServicePathComponent() {
328         return SERVICE_PATH_COMPONENT;
329     }
330
331    private PoxPayloadOut createIntakeInstance(String entryNumber,
332                 String entryDate,
333                                 String currentOwner,
334                                 String depositor,
335                                 String conditionCheckerAssessor,
336                                 String insurer,
337                                 String Valuer ) {
338         IntakesCommon intake = new IntakesCommon();
339         intake.setEntryNumber(entryNumber);
340         intake.setEntryDate(entryDate);
341         intake.setCurrentOwner(currentOwner);
342         intake.setDepositor(depositor);
343         intake.setValuer(Valuer);
344
345         ConditionCheckerOrAssessorList checkerOrAssessorList = new ConditionCheckerOrAssessorList();
346         List<String> checkersOrAssessors = checkerOrAssessorList.getConditionCheckerOrAssessor();
347         checkersOrAssessors.add(conditionCheckerAssessor);
348         intake.setConditionCheckersOrAssessors(checkerOrAssessorList);
349
350         InsurerList insurerList = new InsurerList();
351         List<String> insurers = insurerList.getInsurer();
352         insurers.add(insurer);
353         intake.setInsurers(insurerList);
354
355         PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
356         PayloadOutputPart commonPart =
357             multipart.addPart(new IntakeClient().getCommonPartName(), intake);
358
359         if(logger.isDebugEnabled()){
360             logger.debug("to be created, intake common");
361             logger.debug(objectAsXmlString(intake, IntakesCommon.class));
362         }
363
364         return multipart;
365     }
366 }