]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
46892ce556c0631e2682978d96460efb6a8804b8
[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.io.File;
26 import java.net.URL;
27 import java.util.List;
28 import java.util.UUID;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31 import org.collectionspace.services.client.CollectionSpaceClient;
32 import org.collectionspace.services.client.PayloadOutputPart;
33 import org.collectionspace.services.client.PoxPayloadOut;
34 import org.collectionspace.services.client.RestrictedMediaClient;
35 import org.collectionspace.services.jaxb.AbstractCommonList;
36 import org.collectionspace.services.restrictedmedia.LanguageList;
37 import org.collectionspace.services.restrictedmedia.RestrictedMediaCommon;
38 import org.collectionspace.services.restrictedmedia.SubjectList;
39 import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataOutput;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.testng.Assert;
43 import org.testng.annotations.Test;
44
45 /**
46  * RestrictedMediaServiceTest, carries out tests against a deployed and running Restricted Media Service.
47  */
48 public class RestrictedMediaServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, RestrictedMediaCommon> {
49
50     private final Logger logger = LoggerFactory.getLogger(RestrictedMediaServiceTest.class);
51     private static final String PUBLIC_URL_DECK = "https://farm8.staticflickr.com/7231/6962564226_4bdfc17599_k_d.jpg";
52
53     private boolean mediaCleanup = true;
54
55     /**
56      * Sets up create tests.
57      */
58     @Override
59     protected void setupCreate() {
60         super.setupCreate();
61         String noMediaCleanup = System.getProperty(NO_MEDIA_CLEANUP);
62         if (Boolean.parseBoolean(noMediaCleanup)) {
63             // Don't delete the blobs that we created during the test cycle
64             this.mediaCleanup = false;
65         }
66     }
67
68     private boolean isMediaCleanup() {
69         return mediaCleanup;
70     }
71
72     @Override
73     public String getServicePathComponent() {
74         return RestrictedMediaClient.SERVICE_PATH_COMPONENT;
75     }
76
77     @Override
78     protected String getServiceName() {
79         return RestrictedMediaClient.SERVICE_NAME;
80     }
81
82     @Override
83     protected CollectionSpaceClient getClientInstance() throws Exception {
84         return new RestrictedMediaClient();
85     }
86
87     @Override
88     protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
89         return new RestrictedMediaClient(clientPropertiesFilename);
90     }
91
92     @Override
93     protected AbstractCommonList getCommonList(Response response) {
94         return response.readEntity(AbstractCommonList.class);
95     }
96
97     /**
98      * Looks in the .../src/test/resources/blobs directory for files from which to create Blob
99      * instances.
100      *
101      * @param testName the test name
102      * @param fromUri - if 'true' then send the service a URI from which to create the blob.
103      * @param fromUri - if 'false' then send the service a multipart/form-data POST from which to create the blob.
104      * @throws Exception the exception
105      */
106     public void createBlob(String testName, boolean fromUri) throws Exception {
107         setupCreate();
108
109         // First create a restricted media record
110         RestrictedMediaClient client = new RestrictedMediaClient();
111         PoxPayloadOut multipart = createMediaInstance(createIdentifier());
112         Response mediaRes = client.create(multipart);
113         String mediaCsid = null;
114         try {
115             assertStatusCode(mediaRes, testName);
116             mediaCsid = extractId(mediaRes);
117         } finally {
118             if (mediaRes != null) {
119                 mediaRes.close();
120             }
121         }
122
123         String currentDir = getResourceDir();
124         File blobsDir = new File(currentDir, BLOBS_DIR);
125         File blob = findBlobForMedia(blobsDir);
126         if (blob != null) {
127             createBlob(blob, fromUri, testName, mediaCsid);
128         } else {
129             logger.warn("Could not find blobbable file in {}", blobsDir);
130         }
131     }
132
133     /**
134      * Iterate a directory for a file which matches isBlobbable
135      *
136      * @param blobsDir the directory to iterate
137      * @return a blob or null
138      */
139     public File findBlobForMedia(File blobsDir) {
140         if (blobsDir.exists() && blobsDir.canRead()) {
141             File[] children = blobsDir.listFiles();
142             if (children != null && children.length > 0) {
143                 // Since Media records can have only a single associated
144                 // blob, we'll stop after we find a valid candidate.
145                 for (File child : children) {
146                     if (isBlobbable(child)) {
147                         return child;
148                     }
149                 }
150             } else {
151                 logger.warn("Directory: {} is empty or cannot be read.", blobsDir);
152             }
153         } else {
154             logger.warn("Directory: {} is missing or cannot be read.", blobsDir);
155         }
156
157         return null;
158     }
159
160     public void createBlob(File blobFile, boolean fromUri, String testName, String mediaCsid) throws Exception {
161         logger.debug("Processing file URI: {}", blobFile.getAbsolutePath());
162
163         String mimeType = getMimeType(blobFile);
164         logger.debug("MIME type is: {}", mimeType);
165
166         Response res;
167         RestrictedMediaClient client = new RestrictedMediaClient();
168         if (fromUri) {
169             URL childUrl = blobFile.toURI().toURL();
170             res = client.createBlobFromUri(mediaCsid, childUrl.toString());
171         } else {
172             MultipartFormDataOutput formData = new MultipartFormDataOutput();
173             formData.addFormData("file", blobFile, MediaType.valueOf(mimeType));
174             res = client.createBlobFromFormData(mediaCsid, formData);
175         }
176
177         try {
178             assertStatusCode(res, testName);
179             String blobCsid = extractId(res);
180             if (isMediaCleanup()) {
181                 allResourceIdsCreated.add(blobCsid);
182                 allResourceIdsCreated.add(mediaCsid);
183             }
184         } finally {
185             if (res != null) {
186                 res.close();
187             }
188         }
189     }
190
191     @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
192     public void createWithBlobUri(String testName) throws Exception {
193         createBlob(testName, true /*with URI*/);
194     }
195
196     @Test(dataProvider = "testName", dependsOnMethods = {"createWithBlobUri"})
197     public void createMediaAndBlobWithUri(String testName) throws Exception {
198         RestrictedMediaClient client = new RestrictedMediaClient();
199         PoxPayloadOut multipart = createMediaInstance(createIdentifier());
200
201         // purge the original
202         Response mediaRes = client.createMediaAndBlobWithUri(multipart, PUBLIC_URL_DECK, true);
203         String mediaCsid = null;
204         try {
205             assertStatusCode(mediaRes, testName);
206             mediaCsid = extractId(mediaRes);
207             if (isMediaCleanup()) {
208                 allResourceIdsCreated.add(mediaCsid);
209             }
210         } finally {
211             if (mediaRes != null) {
212                 mediaRes.close();
213             }
214         }
215     }
216
217     @Test(dataProvider = "testName", dependsOnMethods = {"createWithBlobUri"})
218     public void createWithBlobPost(String testName) throws Exception {
219         createBlob(testName, false /*with POST*/);
220     }
221
222     // ---------------------------------------------------------------
223     // Utility tests : tests of code used in tests above
224     // ---------------------------------------------------------------
225
226     @Override
227     protected PoxPayloadOut createInstance(String identifier) throws Exception {
228         return createMediaInstance(identifier);
229     }
230
231     // ---------------------------------------------------------------
232     // Utility methods used by tests above
233     // ---------------------------------------------------------------
234     private PoxPayloadOut createMediaInstance(String title) throws Exception {
235         String identifier = "media.title-" + title;
236         RestrictedMediaCommon media = new RestrictedMediaCommon();
237         media.setTitle(identifier);
238         media.setIdentificationNumber(UUID.randomUUID().toString());
239         media.setContributor("Joe-bob briggs");
240         media.setCoverage("Lots of stuff");
241         media.setPublisher("Ludicrum Enterprises");
242         SubjectList subjects = new SubjectList();
243         List<String> subjList = subjects.getSubject();
244         subjList.add("Pints of blood");
245         subjList.add("Much skin");
246         media.setSubjectList(subjects);
247         LanguageList languages = new LanguageList();
248         List<String> langList = languages.getLanguage();
249         langList.add("English");
250         langList.add("German");
251         media.setLanguageList(languages);
252         PoxPayloadOut multipart = new PoxPayloadOut(RestrictedMediaClient.SERVICE_PAYLOAD_NAME);
253         PayloadOutputPart commonPart = multipart.addPart(media, MediaType.APPLICATION_XML_TYPE);
254         commonPart.setLabel(new RestrictedMediaClient().getCommonPartName());
255
256         logger.debug("to be created, media common");
257         logger.debug(objectAsXmlString(media, RestrictedMediaCommon.class));
258
259         return multipart;
260     }
261
262     @Override
263     protected PoxPayloadOut createInstance(String commonPartName, String identifier) throws Exception {
264         return createMediaInstance(identifier);
265     }
266
267     @Override
268     protected RestrictedMediaCommon updateInstance(final RestrictedMediaCommon original) {
269         RestrictedMediaCommon result = new RestrictedMediaCommon();
270         result.setTitle("updated-" + original.getTitle());
271         return result;
272     }
273
274     @Override
275     protected void compareUpdatedInstances(RestrictedMediaCommon original, RestrictedMediaCommon updated) {
276         Assert.assertEquals(updated.getTitle(), original.getTitle());
277     }
278
279     @Override
280     @Test(dataProvider = "testName",
281           dependsOnMethods = {"org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
282     public void CRUDTests(String testName) {}
283 }