public void testDownload() throws Exception {

    final Long processId = issueProcessAndWaitForTermination();

    // zip extension is not required but should be handled
    final String request = RESTLET_PATH + "/" + processId.longValue() + ".zip";

    final MockHttpServletResponse response = getAsServletResponse(request);
    assertEquals(Status.SUCCESS_OK, Status.valueOf(response.getStatus()));
    assertEquals(MediaType.APPLICATION_ZIP, MediaType.valueOf(response.getContentType()));
    assertEquals(
        "attachment; filename=\"test map.zip\"", response.getHeader("Content-Disposition"));

    final ByteArrayInputStream responseStream = getBinaryInputStream(response);
    final ZipInputStream zipIn = new ZipInputStream(responseStream);

    Set<String> expectedFiles = new HashSet<String>();
    expectedFiles.add("README.txt");
    expectedFiles.add(VECTOR_LAYER.getLocalPart() + ".shp");
    expectedFiles.add(VECTOR_LAYER.getLocalPart() + ".cst");
    expectedFiles.add(VECTOR_LAYER.getLocalPart() + ".prj");
    expectedFiles.add(VECTOR_LAYER.getLocalPart() + ".dbf");
    expectedFiles.add(VECTOR_LAYER.getLocalPart() + ".shx");
    // TODO: change this expectation once we normalize the raster file name
    expectedFiles.add(RASTER_LAYER.getPrefix() + ":" + RASTER_LAYER.getLocalPart() + ".tiff");

    Set<String> archivedFiles = new HashSet<String>();

    ZipEntry nextEntry;
    while ((nextEntry = zipIn.getNextEntry()) != null) {
      archivedFiles.add(nextEntry.getName());
    }

    assertEquals(expectedFiles, archivedFiles);
  }
  public void testCoverageContents() throws Exception {
    final Long processId = issueProcessAndWaitForTermination();

    final String request = RESTLET_PATH + "/" + processId.longValue();

    final MockHttpServletResponse response = getAsServletResponse(request);
    assertEquals(Status.SUCCESS_OK, Status.valueOf(response.getStatus()));
    assertEquals(MediaType.APPLICATION_ZIP, MediaType.valueOf(response.getContentType()));

    final ByteArrayInputStream responseStream = getBinaryInputStream(response);
    File dataDirectoryRoot = super.getTestData().getDataDirectoryRoot();
    File file = new File(dataDirectoryRoot, "testCoverageContents.zip");
    super.getTestData().copyTo(responseStream, file.getName());

    ZipFile zipFile = new ZipFile(file);
    try {
      // TODO: change this expectation once we normalize the raster file name
      String rasterName = RASTER_LAYER.getPrefix() + ":" + RASTER_LAYER.getLocalPart() + ".tiff";
      ZipEntry nextEntry = zipFile.getEntry(rasterName);
      assertNotNull(nextEntry);
      InputStream coverageInputStream = zipFile.getInputStream(nextEntry);
      // Use a file, geotiffreader might not work well reading out of a plain input stream
      File covFile = new File(file.getParentFile(), "coverage.tiff");
      IOUtils.copy(coverageInputStream, covFile);
      GeoTiffReader geoTiffReader = new GeoTiffReader(covFile);
      GridCoverage2D coverage = geoTiffReader.read(null);
      CoordinateReferenceSystem crs = coverage.getCoordinateReferenceSystem();
      assertEquals(CRS.decode("EPSG:4326", true), crs);
    } finally {
      zipFile.close();
    }
  }
  private static void completeOperationThrows(
      CollectInfo collectInfo,
      Operation operation,
      MethodAnnotationInfo mai,
      List<? extends IntrospectionHelper> introspectionHelper) {
    Class<?>[] thrownClasses = mai.getJavaMethod().getExceptionTypes();
    if (thrownClasses != null) {
      for (Class<?> thrownClass : thrownClasses) {
        ThrowableAnnotationInfo throwableAnnotationInfo =
            AnnotationUtils.getInstance().getThrowableAnnotationInfo(thrownClass);
        if (throwableAnnotationInfo != null) {
          int statusCode = throwableAnnotationInfo.getStatus().getCode();
          Response response = new Response();
          response.setCode(statusCode);
          response.setName(Status.valueOf(statusCode).getReasonPhrase());
          response.setMessage("Status " + statusCode);

          Class<?> outputPayloadType =
              throwableAnnotationInfo.isSerializable() ? thrownClass : StatusInfo.class;
          TypeInfo outputTypeInfo = null;
          try {
            outputTypeInfo = Types.getTypeInfo(outputPayloadType, null);
          } catch (UnsupportedTypeException e) {
            LOGGER.warning(
                "Could not add output payload for exception "
                    + thrownClass
                    + " throws by method "
                    + mai.getJavaMethod()
                    + ". "
                    + e.getMessage());
            continue;
          }

          RepresentationCollector.addRepresentation(
              collectInfo, outputTypeInfo, introspectionHelper);

          PayLoad outputPayLoad = new PayLoad();
          outputPayLoad.setType(outputTypeInfo.getRepresentationName());
          response.setOutputPayLoad(outputPayLoad);
          operation.getResponses().add(response);
        }
      }
    }
  }
 public void testNonExistentProcess() throws Exception {
   String request = RESTLET_PATH + "/10000";
   MockHttpServletResponse r = getAsServletResponse(request);
   assertEquals(Status.CLIENT_ERROR_NOT_FOUND, Status.valueOf(r.getStatus()));
 }
 public void testInvalidProcessId() throws Exception {
   String request = RESTLET_PATH + "/notAProcessId";
   MockHttpServletResponse r = getAsServletResponse(request);
   assertEquals(Status.CLIENT_ERROR_BAD_REQUEST, Status.valueOf(r.getStatus()));
 }
 public void testHTTPMethod() throws Exception {
   MockHttpServletResponse r = postAsServletResponse(RESTLET_PATH, "");
   assertEquals(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED, Status.valueOf(r.getStatus()));
 }