From 467af0afff52db6e70b10ab611da43e7d04e359f Mon Sep 17 00:00:00 2001 From: Richard Millet Date: Thu, 24 Feb 2011 20:55:12 +0000 Subject: [PATCH] CSPACE-3598: Media and Blob tests needed to create example media data in the "Nightly" build --- .../services/client/test/BlobServiceTest.java | 66 ++++++++----------- .../client/test/AbstractServiceTestImpl.java | 16 +++++ .../services/client/MediaProxy.java | 4 +- .../client/test/MediaServiceTest.java | 55 ++++++++++++---- .../services/media/MediaResource.java | 38 ++++++----- 5 files changed, 112 insertions(+), 67 deletions(-) diff --git a/services/blob/client/src/test/java/org/collectionspace/services/client/test/BlobServiceTest.java b/services/blob/client/src/test/java/org/collectionspace/services/client/test/BlobServiceTest.java index d766f4549..e78a1ca66 100644 --- a/services/blob/client/src/test/java/org/collectionspace/services/client/test/BlobServiceTest.java +++ b/services/blob/client/src/test/java/org/collectionspace/services/client/test/BlobServiceTest.java @@ -115,9 +115,16 @@ public class BlobServiceTest extends AbstractServiceTestImpl { allResourceIdsCreated.add(extractId(res)); // Store the IDs from every resource created by tests so they can be deleted after tests have been run. } - @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"create"}) - public void createBlobFromURI(String testName) throws Exception { - logger.debug(testBanner(testName, CLASS_NAME)); + /** + * Looks in the .../src/test/resources/blobs directory for files from which to create Blob + * instances. + * + * @param testName the test name + * @param fromUri - if 'true' then send the service a URI from which to create the blob. + * @param fromUri - if 'false' then send the service a multipart/form-data POST from which to create the blob. + * @throws Exception the exception + */ + protected void createBlob(String testName, boolean fromUri) throws Exception { setupCreate(); BlobClient client = new BlobClient(); @@ -128,11 +135,18 @@ public class BlobServiceTest extends AbstractServiceTestImpl { File[] children = blobsDir.listFiles(); if (children != null && children.length > 0) { for (File child : children) { - if (child.isHidden() == false) { + if (isBlobbable(child) == true) { + ClientResponse res = null; String mimeType = this.getMimeType(child); logger.debug("Processing file URI: " + child.getAbsolutePath()); logger.debug("MIME type is: " + mimeType); - ClientResponse res = client.createBlobFromURI(child.getAbsolutePath()); + if (fromUri == true) { + res = client.createBlobFromURI(child.getAbsolutePath()); + } else { + MultipartFormDataOutput form = new MultipartFormDataOutput(); + OutputPart outputPart = form.addFormData("file", child, MediaType.valueOf(mimeType)); + res = client.createBlobFromFormData(form); + } assertStatusCode(res, testName); if (isBlobCleanup() == true) { allResourceIdsCreated.add(extractId(res)); @@ -144,42 +158,20 @@ public class BlobServiceTest extends AbstractServiceTestImpl { } } else { logger.debug("Directory: " + blobsDirPath + " is missing or cannot be read."); - } + } + } + + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"create"}) + public void createBlobWithURI(String testName) throws Exception { + logger.debug(testBanner(testName, CLASS_NAME)); + createBlob(testName, true /*with URI*/); } @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, - dependsOnMethods = {"createBlobFromURI"}) - public void createBlobwithPost(String testName) throws Exception { + dependsOnMethods = {"createBlobWithURI"}) + public void createBlobWithPost(String testName) throws Exception { logger.debug(testBanner(testName, CLASS_NAME)); - setupCreate(); - BlobClient client = new BlobClient(); - - String currentDir = this.getResourceDir(); - String blobsDirPath = currentDir + File.separator + BLOBS_DIR; - File blobsDir = new File(blobsDirPath); - if (blobsDir != null && blobsDir.exists()) { - File[] children = blobsDir.listFiles(); - if (children != null && children.length > 0) { - for (File child : children) { - if (child.isHidden() == false) { - String mimeType = this.getMimeType(child); - logger.debug("Posting file: " + child.getAbsolutePath()); - logger.debug("MIME type is: " + mimeType); - MultipartFormDataOutput form = new MultipartFormDataOutput(); - OutputPart outputPart = form.addFormData("file", child, MediaType.valueOf(mimeType)); - ClientResponse res = client.createBlobFromFormData(form); - assertStatusCode(res, testName); - if (blobCleanup == true) { - allResourceIdsCreated.add(extractId(res)); - } - } - } - } else { - logger.debug("Directory: " + blobsDirPath + " is empty or cannot be read."); - } - } else { - logger.debug("Directory: " + blobsDirPath + " is missing or cannot be read."); - } + createBlob(testName, false /*with POST*/); } @Override diff --git a/services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTestImpl.java b/services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTestImpl.java index 42be818d1..c871478f8 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTestImpl.java +++ b/services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTestImpl.java @@ -108,6 +108,7 @@ public abstract class AbstractServiceTestImpl extends BaseServiceTest implements } + // --------------------------------------------------------------- // CRUD tests : CREATE tests // @@ -130,6 +131,21 @@ public abstract class AbstractServiceTestImpl extends BaseServiceTest implements REQUEST_TYPE = ServiceRequestType.CREATE; testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE); } + + /** + * Checks if 'theFile' is something we can turn into a Blob instance. It can't + * be read-protected, hidden, or a directory. + * + * @param theFile the the file + * @return true, if is blobable + */ + protected boolean isBlobbable(File theFile) { + boolean result = true; + if (theFile.isDirectory() || theFile.isHidden() || !theFile.canRead()) { + result = false; + } + return result; + } /* (non-Javadoc) * @see org.collectionspace.services.client.test.ServiceTest#createList(java.lang.String) diff --git a/services/media/client/src/main/java/org/collectionspace/services/client/MediaProxy.java b/services/media/client/src/main/java/org/collectionspace/services/client/MediaProxy.java index 89bd4580b..ed931d8d9 100644 --- a/services/media/client/src/main/java/org/collectionspace/services/client/MediaProxy.java +++ b/services/media/client/src/main/java/org/collectionspace/services/client/MediaProxy.java @@ -34,11 +34,13 @@ public interface MediaProxy extends CollectionSpaceProxy { @POST @Path("/{csid}") @Consumes("multipart/form-data") - ClientResponse createBlobFromFormData(String csid, + ClientResponse createBlobFromFormData(@PathParam("csid") String csid, MultipartFormDataOutput formDataOutput); @POST @Path("/{csid}") + @Produces("application/xml") + @Consumes("application/xml") ClientResponsecreateBlobFromUri(@PathParam("csid") String csid, @QueryParam(BlobClient.BLOB_URI_PARAM) String blobUri); diff --git a/services/media/client/src/test/java/org/collectionspace/services/client/test/MediaServiceTest.java b/services/media/client/src/test/java/org/collectionspace/services/client/test/MediaServiceTest.java index ca8c97794..62f228584 100644 --- a/services/media/client/src/test/java/org/collectionspace/services/client/test/MediaServiceTest.java +++ b/services/media/client/src/test/java/org/collectionspace/services/client/test/MediaServiceTest.java @@ -36,6 +36,8 @@ import org.collectionspace.services.jaxb.AbstractCommonList; import org.collectionspace.services.media.MediaCommon; import org.jboss.resteasy.client.ClientResponse; +import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataOutput; +import org.jboss.resteasy.plugins.providers.multipart.OutputPart; import org.testng.Assert; import org.testng.annotations.Test; @@ -112,10 +114,16 @@ public class MediaServiceTest extends AbstractServiceTestImpl { allResourceIdsCreated.add(extractId(res)); // Store the IDs from every resource created by tests so they can be deleted after tests have been run. } - @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"create"}) - public void createWithBlobUri(String testName) throws Exception { - logger.debug(testBanner(testName, CLASS_NAME)); - + /** + * Looks in the .../src/test/resources/blobs directory for files from which to create Blob + * instances. + * + * @param testName the test name + * @param fromUri - if 'true' then send the service a URI from which to create the blob. + * @param fromUri - if 'false' then send the service a multipart/form-data POST from which to create the blob. + * @throws Exception the exception + */ + public void createBlob(String testName, boolean fromUri) throws Exception { setupCreate(); MediaClient client = new MediaClient(); PoxPayloadOut multipart = createMediaInstance(createIdentifier()); @@ -130,17 +138,31 @@ public class MediaServiceTest extends AbstractServiceTestImpl { File[] children = blobsDir.listFiles(); if (children != null && children.length > 0) { File blobFile = null; + // + // Since Media records can have only a single associated blob, + // we'll stop after we find a valid candidate + // for (File child : children) { - if (child.isHidden() == false) { + if (isBlobbable(child) == true) { blobFile = child; break; } } + // + // If we found a good blob candidate file, then try to create the blob record + // if (blobFile != null) { + ClientResponse res = null; String mimeType = this.getMimeType(blobFile); logger.debug("Processing file URI: " + blobFile.getAbsolutePath()); logger.debug("MIME type is: " + mimeType); - ClientResponse res = client.createBlobFromUri(mediaCsid, blobFile.getAbsolutePath()); + if (fromUri == true) { + res = client.createBlobFromUri(mediaCsid, blobFile.getAbsolutePath()); + } else { + MultipartFormDataOutput formData = new MultipartFormDataOutput(); + OutputPart outputPart = formData.addFormData("file", blobFile, MediaType.valueOf(mimeType)); + res = client.createBlobFromFormData(mediaCsid, formData); + } assertStatusCode(res, testName); if (isMediaCleanup() == true) { allResourceIdsCreated.add(extractId(res)); @@ -157,13 +179,19 @@ public class MediaServiceTest extends AbstractServiceTestImpl { } } -// String noTest = System.getProperty("noTestCleanup"); -// if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) { -// if (logger.isDebugEnabled()) { -// logger.debug("Skipping Cleanup phase ..."); -// } -// return; -// } + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, + dependsOnMethods = {"create"}) + public void createWithBlobUri(String testName) throws Exception { + logger.debug(testBanner(testName, CLASS_NAME)); + createBlob(testName, true /*with URI*/); + } + + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, + dependsOnMethods = {"createWithBlobUri"}) + public void createWithBlobPost(String testName) throws Exception { + logger.debug(testBanner(testName, CLASS_NAME)); + createBlob(testName, false /*with POST*/); + } // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"update"}) // public void updateWithBlob(String testName) throws Exception { @@ -178,7 +206,6 @@ public class MediaServiceTest extends AbstractServiceTestImpl { // // allResourceIdsCreated.add(extractId(res)); // Store the IDs from every resource created by tests so they can be deleted after tests have been run. // } - @Override @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"create"}) diff --git a/services/media/service/src/main/java/org/collectionspace/services/media/MediaResource.java b/services/media/service/src/main/java/org/collectionspace/services/media/MediaResource.java index 5532da820..df74272a5 100644 --- a/services/media/service/src/main/java/org/collectionspace/services/media/MediaResource.java +++ b/services/media/service/src/main/java/org/collectionspace/services/media/MediaResource.java @@ -117,6 +117,8 @@ public class MediaResource extends ResourceBase { @POST @Path("{csid}") + @Consumes("application/xml") + @Produces("application/xml") public Response createBlobWithUri(@PathParam("csid") String csid, @QueryParam(BlobClient.BLOB_URI_PARAM) String blobUri) { Response response = null; @@ -143,25 +145,31 @@ public class MediaResource extends ResourceBase { @POST @Path("{csid}") @Consumes("multipart/form-data") + @Produces("application/xml") public Response createBlob(@Context HttpServletRequest req, @PathParam("csid") String csid, - @QueryParam(BlobClient.BLOB_URI_PARAM) String blobUri) { //FIXME: REM - Do we really need the blobUri query param here? + @QueryParam(BlobClient.BLOB_URI_PARAM) String blobUri) { PoxPayloadIn input = null; - Response response = null; + Response response = null; try { - // - // First, create the blob - // - ServiceContext blobContext = createServiceContext(BlobUtil.BLOB_RESOURCE_NAME, input); - BlobInput blobInput = BlobUtil.getBlobInput(blobContext); - blobInput.createBlobFile(req, blobUri); - response = this.create(input, blobContext); - // - // Next, update the Media record to be linked to the blob - // - ServiceContext mediaContext = createServiceContext(); - BlobUtil.setBlobInput(mediaContext, blobInput); //and put the blobInput into the Media context - this.update(csid, input, mediaContext); + if (blobUri == null) { + // + // First, create the blob + // + ServiceContext blobContext = createServiceContext(BlobUtil.BLOB_RESOURCE_NAME, input); + BlobInput blobInput = BlobUtil.getBlobInput(blobContext); + blobInput.createBlobFile(req, null); + response = this.create(input, blobContext); + // + // Next, update the Media record to be linked to the blob + // + ServiceContext mediaContext = createServiceContext(); + BlobUtil.setBlobInput(mediaContext, blobInput); //and put the blobInput into the Media context + this.update(csid, input, mediaContext); + } else { + //A URI query param overrides the incoming multipart/form-data payload in the request + response = createBlobWithUri(csid, blobUri); + } } catch (Exception e) { throw bigReThrow(e, ServiceMessages.CREATE_FAILED); } -- 2.47.3