List of usage examples for org.springframework.mock.web MockHttpServletRequest setContent
public void setContent(@Nullable byte[] content)
From source file:org.geoserver.rest.catalog.CoverageStoreFileUploadTest.java
@Test public void testHarvestExternalImageMosaic() throws Exception { // Check if an already existing directory called "mosaic" is present URL resource = getClass().getResource("test-data/mosaic"); if (resource != null) { File oldDir = DataUtilities.urlToFile(resource); if (oldDir.exists()) { FileUtils.deleteDirectory(oldDir); }/* ww w. j a v a2 s . c o m*/ } // reading of the mosaic directory Resource mosaic = readMosaic(); // Creation of the builder for building a new CoverageStore CatalogBuilder builder = new CatalogBuilder(getCatalog()); // Definition of the workspace associated to the coverage WorkspaceInfo ws = getCatalog().getWorkspaceByName("gs"); // Creation of a CoverageStore CoverageStoreInfo store = builder.buildCoverageStore("watertemp4"); store.setURL(DataUtilities.fileToURL(Resources.find(mosaic)).toExternalForm()); store.setWorkspace(ws); ImageMosaicFormat imageMosaicFormat = new ImageMosaicFormat(); store.setType((imageMosaicFormat.getName())); // Addition to the catalog getCatalog().add(store); builder.setStore(store); // Input reader used for reading the mosaic folder GridCoverage2DReader reader = null; // Reader used for checking if the mosaic has been configured correctly StructuredGridCoverage2DReader reader2 = null; try { // Selection of the reader to use for the mosaic reader = imageMosaicFormat.getReader(DataUtilities.fileToURL(Resources.find(mosaic))); // configure the coverage configureCoverageInfo(builder, store, reader); // check the coverage is actually there CoverageStoreInfo storeInfo = getCatalog().getCoverageStoreByName("watertemp4"); assertNotNull(storeInfo); CoverageInfo ci = getCatalog().getCoverageByName("mosaic"); assertNotNull(ci); assertEquals(storeInfo, ci.getStore()); // Harvesting of the Mosaic URL zipHarvest = MockData.class.getResource("harvesting.zip"); // Extract a Byte array from the zip file InputStream is = null; byte[] bytes; try { is = zipHarvest.openStream(); bytes = IOUtils.toByteArray(is); } finally { IOUtils.closeQuietly(is); } // Create the POST request MockHttpServletRequest request = createRequest( RestBaseController.ROOT_PATH + "/workspaces/gs/coveragestores/watertemp4/file.imagemosaic"); request.setMethod("POST"); request.setContentType("application/zip"); request.setContent(bytes); request.addHeader("Content-type", "application/zip"); // Get The response MockHttpServletResponse response = dispatch(request); // Get the Mosaic Reader reader2 = (StructuredGridCoverage2DReader) storeInfo.getGridCoverageReader(null, GeoTools.getDefaultHints()); // Test if all the TIME DOMAINS are present String[] metadataNames = reader2.getMetadataNames(); assertNotNull(metadataNames); assertEquals("true", reader2.getMetadataValue("HAS_TIME_DOMAIN")); assertEquals("2008-10-31T00:00:00.000Z,2008-11-01T00:00:00.000Z,2008-11-02T00:00:00.000Z", reader2.getMetadataValue(metadataNames[0])); // Removal of all the data associated to the mosaic reader2.delete(true); } finally { // Reader disposal if (reader != null) { try { reader.dispose(); } catch (Throwable t) { // Does nothing } } if (reader2 != null) { try { reader2.dispose(); } catch (Throwable t) { // Does nothing } } } }
From source file:org.geoserver.rest.catalog.CoverageStoreFileUploadTest.java
private StructuredGridCoverage2DReader uploadGeotiffAndCheck(CoverageStoreInfo storeInfo, byte[] bytes, String filename) throws Exception { StructuredGridCoverage2DReader reader2; // Create the POST request MockHttpServletRequest request = createRequest(RestBaseController.ROOT_PATH + "/workspaces/gs/coveragestores/watertemp5/file.imagemosaic?filename=" + filename); request.setMethod("POST"); request.setContentType("image/tiff"); request.setContent(bytes); request.addHeader("Content-type", "image/tiff"); // Get The response assertEquals(202, dispatch(request).getStatus()); // Get the Mosaic Reader reader2 = (StructuredGridCoverage2DReader) storeInfo.getGridCoverageReader(null, GeoTools.getDefaultHints()); // Test if all the TIME DOMAINS are present String[] metadataNames = reader2.getMetadataNames(); assertNotNull(metadataNames);/*from w ww . j a v a2s .co m*/ assertEquals("true", reader2.getMetadataValue("HAS_TIME_DOMAIN")); assertEquals("2008-10-31T00:00:00.000Z,2008-11-01T00:00:00.000Z,2008-11-02T00:00:00.000Z", reader2.getMetadataValue(metadataNames[0])); return reader2; }
From source file:org.geoserver.rest.catalog.StyleControllerTest.java
/** * TODO I had to put this here BECAUSE://from w w w .ja v a2 s. com * * - The testPutSLDPackage test uses a *.zip URL * - BUT, put style does not support ZIP responses * - Spring interprets the .zip extension on the path as being a request for a zip response * - This fails, because there is no actual handler for a zip response on a style endpoint * - Unfortunately Spring only considers one of the Accept header or the path * - So the handler is never found * * this leaves us with a few options * * 1) Configure spring to prefer the accept header over the path. This would: * * - Force future clients who depended on put/posting to zip endpoints to make sure their * Accept header is correct. * - Maybe more importantly it could potentially break other end points that depend on preferring * the path extension. * * 2) Continue letting Spring prefer the path (which is really the right behavior for a REST api) * * - Future clients would not be able to use an endpoint like .zip * - But this is more REST-y * * 3) Write our own content negotiation strategy that allows for both. * * - This is a pain in the ass. * - Potentially difficult to recreate all default behavior + behavior needed to fix this test * case * * @param path * @param body * @param contentType * @return * @throws Exception */ protected MockHttpServletResponse putAsServletResponse(String path, byte[] body, String contentType, String accepts) throws Exception { MockHttpServletRequest request = createRequest(path); request.setMethod("PUT"); request.setContentType(contentType); request.setContent(body); request.addHeader("Accept", accepts); request.addHeader("Content-type", contentType); return dispatch(request); }
From source file:org.geoserver.rest.resources.ResourceControllerTest.java
public MockHttpServletResponse headAsServletResponse(String path) throws Exception { MockHttpServletRequest request = createRequest(path); request.setMethod("HEAD"); request.setContent(new byte[] {}); return dispatch(request, null); }
From source file:org.geoserver.restupload.ResumableUploadTest.java
@Test public void testUploadFull() throws Exception { String uploadId = sendPostRequest(); File uploadedFile = getTempPath(uploadId); assertTrue(uploadedFile.exists());//from w w w . j a va 2s. co m MockHttpServletRequest request = createRequest("/rest/resumableupload/" + uploadId); request.setMethod("PUT"); request.setContentType("application/octet-stream"); byte[] bigFile = generateFileAsBytes(); request.setContent(bigFile); request.addHeader("Content-type", "application/octet-stream"); request.addHeader("Content-Length", String.valueOf(bigFile.length)); MockHttpServletResponse response = dispatch(request); assertEquals(Status.SUCCESS_OK.getCode(), response.getStatus()); assertFalse(uploadedFile.exists()); File destinationFile = new File(FilenameUtils.concat(root, fileName.replaceAll("^/", ""))); assertTrue(destinationFile.exists()); assertEquals(bigFile.length, destinationFile.length()); // Check uploaded file byte by byte boolean checkBytes = Arrays.equals(bigFile, toBytes(new FileInputStream(destinationFile))); assertTrue(checkBytes); // Check response content String restUrl = response.getContentAsString(); assertEquals(fileName.replaceAll("^/", ""), restUrl); }
From source file:org.geoserver.restupload.ResumableUploadTest.java
@Test public void testPartialUpload() throws Exception { String uploadId = sendPostRequest(); MockHttpServletRequest request = createRequest("/rest/resumableupload/" + uploadId); request.setMethod("PUT"); request.setContentType("application/octet-stream"); byte[] bigFile = generateFileAsBytes(); byte[] partialFile = ArrayUtils.subarray(bigFile, 0, (int) partialSize); request.setContent(partialFile); request.addHeader("Content-type", "application/octet-stream"); request.addHeader("Content-Length", String.valueOf(bigFile.length)); MockHttpServletResponse response = dispatch(request); assertEquals(ResumableUploadCatalogResource.RESUME_INCOMPLETE.getCode(), response.getStatus()); assertEquals(null, response.getHeader("Content-Length")); assertEquals("0-" + (partialSize - 1), response.getHeader("Range")); File uploadedFile = getTempPath(uploadId); assertTrue(uploadedFile.exists());//from w w w . ja v a2s. co m assertEquals(partialSize, uploadedFile.length()); boolean checkBytes = Arrays.equals(partialFile, toBytes(new FileInputStream(uploadedFile))); assertTrue(checkBytes); }
From source file:org.geoserver.restupload.ResumableUploadTest.java
@Test public void testUploadPartialResume() throws Exception { String uploadId = sendPostRequest(); byte[] bigFile = generateFileAsBytes(); byte[] partialFile1 = ArrayUtils.subarray(bigFile, 0, (int) partialSize); // First upload MockHttpServletRequest request = createRequest("/rest/resumableupload/" + uploadId); request.setMethod("PUT"); request.setContentType("application/octet-stream"); request.setContent(partialFile1); request.addHeader("Content-type", "application/octet-stream"); request.addHeader("Content-Length", String.valueOf(bigFile.length)); dispatch(request);/*from w w w. j ava 2 s.co m*/ // Resume upload request = createRequest("/rest/resumableupload/" + uploadId); request.setMethod("PUT"); request.setContentType("application/octet-stream"); byte[] partialFile2 = ArrayUtils.subarray(bigFile, (int) partialSize, (int) partialSize * 2); request.setContent(partialFile2); request.addHeader("Content-type", "application/octet-stream"); request.addHeader("Content-Length", String.valueOf(partialFile2.length)); request.addHeader("Content-Range", "bytes " + partialSize + "-" + partialSize * 2 + "/" + bigFile.length); MockHttpServletResponse response = dispatch(request); assertEquals(ResumableUploadCatalogResource.RESUME_INCOMPLETE.getCode(), response.getStatus()); assertEquals(null, response.getHeader("Content-Length")); assertEquals("0-" + (partialSize * 2 - 1), response.getHeader("Range")); File uploadedFile = getTempPath(uploadId); assertTrue(uploadedFile.exists()); assertEquals(partialSize * 2, uploadedFile.length()); // Check uploaded file byte by byte boolean checkBytes = Arrays.equals(ArrayUtils.addAll(partialFile1, partialFile2), toBytes(new FileInputStream(uploadedFile))); assertTrue(checkBytes); }
From source file:org.geoserver.restupload.ResumableUploadTest.java
@Test public void testUploadFullResume() throws Exception { String uploadId = sendPostRequest(); byte[] bigFile = generateFileAsBytes(); byte[] partialFile1 = ArrayUtils.subarray(bigFile, 0, (int) partialSize); // First upload MockHttpServletRequest request = createRequest("/rest/resumableupload/" + uploadId); request.setMethod("PUT"); request.setContentType("application/octet-stream"); request.setContent(partialFile1); request.addHeader("Content-type", "application/octet-stream"); request.addHeader("Content-Length", String.valueOf(bigFile.length)); dispatch(request);/*from w w w. jav a2 s. com*/ // Resume upload request = createRequest("/rest/resumableupload/" + uploadId); request.setMethod("PUT"); request.setContentType("application/octet-stream"); byte[] partialFile2 = ArrayUtils.subarray(bigFile, (int) partialSize, bigFile.length); request.setContent(partialFile2); request.addHeader("Content-type", "application/octet-stream"); request.addHeader("Content-Length", String.valueOf(partialFile2.length)); request.addHeader("Content-Range", "bytes " + partialSize + "-" + bigFile.length + "/" + bigFile.length); MockHttpServletResponse response = dispatch(request); assertEquals(Status.SUCCESS_OK.getCode(), response.getStatus()); File uploadedFile = getTempPath(uploadId); assertFalse(uploadedFile.exists()); File destinationFile = new File(FilenameUtils.concat(root, fileName.replaceAll("^/", ""))); assertTrue(destinationFile.exists()); assertEquals(bigFile.length, destinationFile.length()); // Check uploaded file byte by byte boolean checkBytes = Arrays.equals(bigFile, toBytes(new FileInputStream(destinationFile))); assertTrue(checkBytes); // Check response content String restUrl = response.getContentAsString(); assertEquals(fileName.replaceAll("^/", ""), restUrl); }
From source file:org.geoserver.restupload.ResumableUploadTest.java
@Test public void testPartialCleanup() throws Exception { // Change cleanup expirationDelay ResumableUploadResourceCleaner cleaner = (ResumableUploadResourceCleaner) applicationContext .getBean("resumableUploadStorageCleaner"); cleaner.setExpirationDelay(1000);//from w w w . ja va 2 s . c o m // Upload file String uploadId = sendPostRequest(); byte[] bigFile = generateFileAsBytes(); byte[] partialFile = ArrayUtils.subarray(bigFile, 0, (int) partialSize); MockHttpServletRequest request = createRequest("/rest/resumableupload/" + uploadId); request.setMethod("PUT"); request.setContentType("application/octet-stream"); request.setContent(partialFile); request.addHeader("Content-type", "application/octet-stream"); request.addHeader("Content-Length", String.valueOf(bigFile.length)); dispatch(request); File uploadedFile = getTempPath(uploadId); assertTrue(uploadedFile.exists()); // Wait to cleanup, max 2 minutes long startTime = new Date().getTime(); while (uploadedFile.exists() && (new Date().getTime() - startTime) < 120000) { Thread.sleep(1000); } assertTrue(!uploadedFile.exists()); cleaner.setExpirationDelay(300000); }
From source file:org.geoserver.restupload.ResumableUploadTest.java
@Test public void testSidecarCleanup() throws Exception { // Change cleanup expirationDelay ResumableUploadResourceCleaner cleaner = (ResumableUploadResourceCleaner) applicationContext .getBean("resumableUploadStorageCleaner"); cleaner.setExpirationDelay(1000);/*from w ww . j a va 2 s .co m*/ // Upload file String uploadId = sendPostRequest(); byte[] bigFile = generateFileAsBytes(); MockHttpServletRequest request = createRequest("/rest/resumableupload/" + uploadId); request.setMethod("PUT"); request.setContentType("application/octet-stream"); request.setContent(bigFile); request.addHeader("Content-type", "application/octet-stream"); request.addHeader("Content-Length", String.valueOf(bigFile.length)); dispatch(request); File uploadedFile = getTempPath(uploadId); assertFalse(uploadedFile.exists()); File sidecarFile = new File( FilenameUtils.concat(tmpUploadFolder.dir().getCanonicalPath(), uploadId + ".sidecar")); assertTrue(sidecarFile.exists()); // Wait to cleanup, max 2 minutes long startTime = new Date().getTime(); while (sidecarFile.exists() && (new Date().getTime() - startTime) < 120000) { Thread.sleep(1000); } assertFalse(sidecarFile.exists()); // Test GET after sidecar cleanup MockHttpServletResponse response = getAsServletResponse("/rest/resumableupload/" + uploadId, "text/plain"); assertEquals(Status.CLIENT_ERROR_NOT_FOUND.getCode(), response.getStatus()); cleaner.setExpirationDelay(300000); }