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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright © 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
23 package org.collectionspace.services.client.test;
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;
46 * RestrictedMediaServiceTest, carries out tests against a deployed and running Restricted Media Service.
48 public class RestrictedMediaServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, RestrictedMediaCommon> {
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";
53 private boolean mediaCleanup = true;
56 * Sets up create tests.
59 protected void 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;
68 private boolean isMediaCleanup() {
73 public String getServicePathComponent() {
74 return RestrictedMediaClient.SERVICE_PATH_COMPONENT;
78 protected String getServiceName() {
79 return RestrictedMediaClient.SERVICE_NAME;
83 protected CollectionSpaceClient getClientInstance() throws Exception {
84 return new RestrictedMediaClient();
88 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
89 return new RestrictedMediaClient(clientPropertiesFilename);
93 protected AbstractCommonList getCommonList(Response response) {
94 return response.readEntity(AbstractCommonList.class);
98 * Looks in the .../src/test/resources/blobs directory for files from which to create Blob
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
106 public void createBlob(String testName, boolean fromUri) throws Exception {
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;
115 assertStatusCode(mediaRes, testName);
116 mediaCsid = extractId(mediaRes);
118 if (mediaRes != null) {
123 String currentDir = getResourceDir();
124 File blobsDir = new File(currentDir, BLOBS_DIR);
125 File blob = findBlobForMedia(blobsDir);
127 createBlob(blob, fromUri, testName, mediaCsid);
129 logger.warn("Could not find blobbable file in {}", blobsDir);
134 * Iterate a directory for a file which matches isBlobbable
136 * @param blobsDir the directory to iterate
137 * @return a blob or null
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)) {
151 logger.warn("Directory: {} is empty or cannot be read.", blobsDir);
154 logger.warn("Directory: {} is missing or cannot be read.", blobsDir);
160 public void createBlob(File blobFile, boolean fromUri, String testName, String mediaCsid) throws Exception {
161 logger.debug("Processing file URI: {}", blobFile.getAbsolutePath());
163 String mimeType = getMimeType(blobFile);
164 logger.debug("MIME type is: {}", mimeType);
167 RestrictedMediaClient client = new RestrictedMediaClient();
169 URL childUrl = blobFile.toURI().toURL();
170 res = client.createBlobFromUri(mediaCsid, childUrl.toString());
172 MultipartFormDataOutput formData = new MultipartFormDataOutput();
173 formData.addFormData("file", blobFile, MediaType.valueOf(mimeType));
174 res = client.createBlobFromFormData(mediaCsid, formData);
178 assertStatusCode(res, testName);
179 String blobCsid = extractId(res);
180 if (isMediaCleanup()) {
181 allResourceIdsCreated.add(blobCsid);
182 allResourceIdsCreated.add(mediaCsid);
191 @Test(dataProvider = "testName", dependsOnMethods = {"CRUDTests"})
192 public void createWithBlobUri(String testName) throws Exception {
193 createBlob(testName, true /*with URI*/);
196 @Test(dataProvider = "testName", dependsOnMethods = {"createWithBlobUri"})
197 public void createMediaAndBlobWithUri(String testName) throws Exception {
198 RestrictedMediaClient client = new RestrictedMediaClient();
199 PoxPayloadOut multipart = createMediaInstance(createIdentifier());
201 // purge the original
202 Response mediaRes = client.createMediaAndBlobWithUri(multipart, PUBLIC_URL_DECK, true);
203 String mediaCsid = null;
205 assertStatusCode(mediaRes, testName);
206 mediaCsid = extractId(mediaRes);
207 if (isMediaCleanup()) {
208 allResourceIdsCreated.add(mediaCsid);
211 if (mediaRes != null) {
217 @Test(dataProvider = "testName", dependsOnMethods = {"createWithBlobUri"})
218 public void createWithBlobPost(String testName) throws Exception {
219 createBlob(testName, false /*with POST*/);
222 // ---------------------------------------------------------------
223 // Utility tests : tests of code used in tests above
224 // ---------------------------------------------------------------
227 protected PoxPayloadOut createInstance(String identifier) throws Exception {
228 return createMediaInstance(identifier);
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());
256 logger.debug("to be created, media common");
257 logger.debug(objectAsXmlString(media, RestrictedMediaCommon.class));
263 protected PoxPayloadOut createInstance(String commonPartName, String identifier) throws Exception {
264 return createMediaInstance(identifier);
268 protected RestrictedMediaCommon updateInstance(final RestrictedMediaCommon original) {
269 RestrictedMediaCommon result = new RestrictedMediaCommon();
270 result.setTitle("updated-" + original.getTitle());
275 protected void compareUpdatedInstances(RestrictedMediaCommon original, RestrictedMediaCommon updated) {
276 Assert.assertEquals(updated.getTitle(), original.getTitle());
280 @Test(dataProvider = "testName",
281 dependsOnMethods = {"org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
282 public void CRUDTests(String testName) {}