]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a4d6df42be8338421e69d94da13255bc550d566a
[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.PersonJAXBSchema;
34 import org.collectionspace.services.client.CollectionSpaceClient;
35 import org.collectionspace.services.client.ObjectExitClient;
36 import org.collectionspace.services.client.PersonAuthorityClient;
37 import org.collectionspace.services.client.PersonAuthorityClientUtils;
38 import org.collectionspace.services.common.authorityref.AuthorityRefList;
39 import org.collectionspace.services.jaxb.AbstractCommonList;
40 import org.collectionspace.services.objectexit.ObjectexitCommon;
41
42 import org.jboss.resteasy.client.ClientResponse;
43
44 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
45 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
46 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
47 import org.testng.Assert;
48 import org.testng.annotations.AfterClass;
49 import org.testng.annotations.Test;
50
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 /**
55  * ObjectExitAuthRefsTest, carries out Authority References tests against a deployed and running ObjectExit (aka Loans Out) Service.
56  * $LastChangedRevision:  $
57  * $LastChangedDate:  $
58  */
59 public class ObjectExitAuthRefsTest extends BaseServiceTest {
60
61     private final String CLASS_NAME = ObjectExitAuthRefsTest.class.getName();
62     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
63     final String SERVICE_PATH_COMPONENT = "objectexit";
64     final String PERSON_AUTHORITY_NAME = "ObjectexitPersonAuth";
65     private String knownResourceId = null;
66     private List<String> objectexitIdsCreated = new ArrayList<String>();
67     private List<String> personIdsCreated = new ArrayList<String>();
68     private String personAuthCSID = null;
69     private String depositorRefName = null;
70     private String exitDate = null;
71     private String exitNumber = null;
72
73     @Override
74     protected CollectionSpaceClient getClientInstance() {
75         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
76     }
77
78     @Override
79     protected AbstractCommonList getAbstractCommonList(ClientResponse<AbstractCommonList> response) {
80         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
81     }
82
83     @Override
84     public String getServicePathComponent() {
85         return SERVICE_PATH_COMPONENT;
86     }
87
88     private MultipartOutput createObjectExitInstance(String depositorRefName, String exitNumber, String exitDate) {
89         this.exitDate = exitDate;
90         this.exitNumber = exitNumber;
91         this.depositorRefName = depositorRefName;
92         ObjectexitCommon objectexit = new ObjectexitCommon();
93         objectexit.setDepositor(depositorRefName);
94         objectexit.setExitNumber(exitNumber);
95         objectexit.setExitDate(exitDate);
96
97         MultipartOutput multipart = new MultipartOutput();
98         OutputPart commonPart = multipart.addPart(objectexit, MediaType.APPLICATION_XML_TYPE);
99         commonPart.getHeaders().add("label", new ObjectExitClient().getCommonPartName());
100         logger.debug("to be created, objectexit common: " + objectAsXmlString(objectexit, ObjectexitCommon.class));
101         return multipart;
102     }
103
104     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
105     public void createWithAuthRefs(String testName) throws Exception {
106         logger.debug(testBanner(testName, CLASS_NAME));
107         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
108         String identifier = createIdentifier(); // Submit the request to the service and store the response.
109         createPersonRefs();// Create all the person refs and entities
110         // Create a new Loans In resource. One or more fields in this resource will be PersonAuthority
111         //    references, and will refer to Person resources by their refNames.
112         ObjectExitClient objectexitClient = new ObjectExitClient();
113         MultipartOutput multipart = createObjectExitInstance(depositorRefName, "exitNumber-" + identifier, "exitDate-" + identifier);
114         ClientResponse<Response> res = objectexitClient.create(multipart);
115         assertStatusCode(res, testName);
116         if (knownResourceId == null) {// Store the ID returned from the first resource created for additional tests below.
117             knownResourceId = extractId(res);
118         }
119         objectexitIdsCreated.add(extractId(res));// Store the IDs from every resource created; delete on cleanup
120     }
121
122     protected void createPersonRefs() {
123         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
124         // Create a temporary PersonAuthority resource, and its corresponding refName by which it can be identified.
125         MultipartOutput multipart = PersonAuthorityClientUtils.createPersonAuthorityInstance(PERSON_AUTHORITY_NAME, PERSON_AUTHORITY_NAME, personAuthClient.getCommonPartName());
126         ClientResponse<Response> res = personAuthClient.create(multipart);
127         assertStatusCode(res, "createPersonRefs (not a surefire test)");
128         personAuthCSID = extractId(res);
129         String authRefName = PersonAuthorityClientUtils.getAuthorityRefName(personAuthCSID, null);
130         // Create temporary Person resources, and their corresponding refNames by which they can be identified.
131         String csid = "";
132
133         csid = createPerson("Owen the Cur", "Owner", "owenCurOwner", authRefName);
134         personIdsCreated.add(csid);
135         depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
136
137         csid = createPerson("Davenport", "Depositor", "davenportDepositor", authRefName);
138         personIdsCreated.add(csid);
139         depositorRefName = PersonAuthorityClientUtils.getPersonRefName(personAuthCSID, csid, null);
140     }
141
142     protected String createPerson(String firstName, String surName, String shortId, String authRefName) {
143         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
144         Map<String, String> personInfo = new HashMap<String, String>();
145         personInfo.put(PersonJAXBSchema.FORE_NAME, firstName);
146         personInfo.put(PersonJAXBSchema.SUR_NAME, surName);
147         personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId);
148         MultipartOutput multipart = PersonAuthorityClientUtils.createPersonInstance(personAuthCSID, authRefName, personInfo, personAuthClient.getItemCommonPartName());
149         ClientResponse<Response> res = personAuthClient.createItem(personAuthCSID, multipart);
150         assertStatusCode(res, "createPerson (not a surefire test)");
151         return extractId(res);
152     }
153
154     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"createWithAuthRefs"})
155     public void readAndCheckAuthRefs(String testName) throws Exception {
156         logger.debug(testBanner(testName, CLASS_NAME));
157         testSetup(STATUS_OK, ServiceRequestType.READ);
158         ObjectExitClient objectexitClient = new ObjectExitClient();
159         ClientResponse<MultipartInput> res = objectexitClient.read(knownResourceId);
160         assertStatusCode(res, testName);
161         MultipartInput input = (MultipartInput) res.getEntity();
162         ObjectexitCommon objectexit = (ObjectexitCommon) extractPart(input, objectexitClient.getCommonPartName(), ObjectexitCommon.class);
163         Assert.assertNotNull(objectexit);
164         logger.debug(objectAsXmlString(objectexit, ObjectexitCommon.class));
165
166         // Check a couple of fields
167         Assert.assertEquals(objectexit.getDepositor(), depositorRefName);
168         Assert.assertEquals(objectexit.getExitDate(), exitDate);
169         Assert.assertEquals(objectexit.getExitNumber(), exitNumber);
170
171         // Get the auth refs and check them
172         ClientResponse<AuthorityRefList> res2 = objectexitClient.getAuthorityRefs(knownResourceId);
173         assertStatusCode(res2, testName);
174         AuthorityRefList list = res2.getEntity();
175         List<AuthorityRefList.AuthorityRefItem> items = list.getAuthorityRefItem();
176         int numAuthRefsFound = items.size();
177         logger.debug("Authority references, found " + numAuthRefsFound);
178         //Assert.assertEquals(numAuthRefsFound, NUM_AUTH_REFS_EXPECTED,
179         //                    "Did not find all expected authority references! " +
180         //                    "Expected " + NUM_AUTH_REFS_EXPECTED + ", found " + numAuthRefsFound);
181         if (logger.isDebugEnabled()) {
182             int i = 0;
183             for (AuthorityRefList.AuthorityRefItem item : items) {
184                 logger.debug(testName + ": list-item[" + i + "] Field:" + item.getSourceField() + "= " + item.getAuthDisplayName() + item.getItemDisplayName());
185                 logger.debug(testName + ": list-item[" + i + "] refName=" + item.getRefName());
186                 logger.debug(testName + ": list-item[" + i + "] URI=" + item.getUri());
187                 i++;
188             }
189         }
190     }
191
192     /**
193      * Deletes all resources created by tests, after all tests have been run.
194      * <p/>
195      * This cleanup method will always be run, even if one or more tests fail.
196      * For this reason, it attempts to remove all resources created
197      * at any point during testing, even if some of those resources
198      * may be expected to be deleted by certain tests.
199      */
200     @AfterClass(alwaysRun = true)
201     public void cleanUp() {
202         String noTest = System.getProperty("noTestCleanup");
203         if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
204             logger.debug("Skipping Cleanup phase ...");
205             return;
206         }
207         logger.debug("Cleaning up temporary resources created for testing ...");
208         PersonAuthorityClient personAuthClient = new PersonAuthorityClient();
209         // Delete Person resource(s) (before PersonAuthority resources).
210         for (String resourceId : personIdsCreated) {
211             // Note: Any non-success responses are ignored and not reported.
212             personAuthClient.deleteItem(personAuthCSID, resourceId);
213         }
214         // Delete PersonAuthority resource(s).
215         // Note: Any non-success response is ignored and not reported.
216         if (personAuthCSID != null) {
217             personAuthClient.delete(personAuthCSID);
218             // Delete Loans In resource(s).
219             ObjectExitClient objectexitClient = new ObjectExitClient();
220             for (String resourceId : objectexitIdsCreated) {
221                 // Note: Any non-success responses are ignored and not reported.
222                 objectexitClient.delete(resourceId);
223             }
224         }
225     }
226
227 }