@Test
 public void testGetWrongGranule() throws Exception {
   // Parameters for the request
   String ws = "wcs";
   String cs = "watertemp";
   String g = "notThere.html";
   // Request path
   String requestPath =
       "/rest/workspaces/"
           + ws
           + "/coveragestores/"
           + cs
           + "/coverages/"
           + cs
           + "/index/granules/"
           + g;
   // Exception path
   String exception = "Could not find a granule with id " + g + " in coveage " + ws + ":" + cs;
   // First request should thrown an exception
   MockHttpServletResponse response = getAsServletResponse(requestPath);
   assertEquals(404, response.getStatusCode());
   assertTrue(response.getOutputStreamContent().contains(exception));
   // Same request with ?quietOnNotFound should not throw an exception
   response = getAsServletResponse(requestPath + "?quietOnNotFound=true");
   assertEquals(404, response.getStatusCode());
   assertFalse(response.getOutputStreamContent().contains(exception));
   // No exception thrown
   assertTrue(response.getOutputStreamContent().isEmpty());
 }
  @Test
  public void testUploadImageMosaic() throws Exception {
    URL zip = MockData.class.getResource("watertemp.zip");
    InputStream is = null;
    byte[] bytes;
    try {
      is = zip.openStream();
      bytes = IOUtils.toByteArray(is);
    } finally {
      IOUtils.closeQuietly(is);
    }

    MockHttpServletResponse response =
        putAsServletResponse(
            "/rest/workspaces/gs/coveragestores/watertemp/file.imagemosaic",
            bytes,
            "application/zip");
    assertEquals(201, response.getStatusCode());

    // check the response contents
    String content = response.getOutputStreamContent();
    Document d = dom(new ByteArrayInputStream(content.getBytes()));

    XMLAssert.assertXpathEvaluatesTo("watertemp", "//coverageStore/name", d);
    XMLAssert.assertXpathEvaluatesTo("ImageMosaic", "//coverageStore/type", d);

    // check the coverage is actually there
    CoverageStoreInfo storeInfo = getCatalog().getCoverageStoreByName("watertemp");
    assertNotNull(storeInfo);
    CoverageInfo ci = getCatalog().getCoverageByName("watertemp");
    assertNotNull(ci);
    assertEquals(storeInfo, ci.getStore());
  }
  public void testPlainAddition() throws Exception { // Standard Test A.4.4.3
    String xml =
        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\r\n"
            + "<wps:Execute service=\"WPS\" version=\"1.0.0\"\r\n"
            + "        xmlns:wps=\"http://www.opengis.net/wps/1.0.0\" xmlns:ows=\"http://www.opengis.net/ows/1.1\"\r\n"
            + "        xmlns:xlink=\"http://www.w3.org/1999/xlink\">\r\n"
            + "        <ows:Identifier>gt:DoubleAddition</ows:Identifier>\r\n"
            + "        <wps:DataInputs>\r\n"
            + "                <wps:Input>\r\n"
            + "                        <ows:Identifier>input_a</ows:Identifier>\r\n"
            + "                        <wps:Data>\r\n"
            + "                                <wps:LiteralData>7</wps:LiteralData>\r\n"
            + "                        </wps:Data>\r\n"
            + "                </wps:Input>\r\n"
            + "                <wps:Input>\r\n"
            + "                        <ows:Identifier>input_b</ows:Identifier>\r\n"
            + "                        <wps:Data>\r\n"
            + "                                <wps:LiteralData>7</wps:LiteralData>\r\n"
            + "                        </wps:Data>\r\n"
            + "                </wps:Input>\r\n"
            + "        </wps:DataInputs>\r\n"
            + "        <wps:ResponseForm>\r\n"
            + "                <wps:RawDataOutput>\r\n"
            + "                        <ows:Identifier>result</ows:Identifier>\r\n"
            + "                </wps:RawDataOutput>\r\n"
            + "        </wps:ResponseForm>\r\n"
            + "</wps:Execute>";

    MockHttpServletResponse response = postAsServletResponse(root(), xml);
    assertEquals("text/plain", response.getContentType());
    assertEquals("14.0", response.getOutputStreamContent());
  }
  public void testInlineGeoJSON() throws Exception {
    String xml =
        "<wps:Execute service='WPS' version='1.0.0' xmlns:wps='http://www.opengis.net/wps/1.0.0' "
            + "xmlns:ows='http://www.opengis.net/ows/1.1'>"
            + "<ows:Identifier>gt:BufferFeatureCollection</ows:Identifier>"
            + "<wps:DataInputs>"
            + "<wps:Input>"
            + "<ows:Identifier>features</ows:Identifier>"
            + "<wps:Data>"
            + "<wps:ComplexData mimeType=\"application/json\"><![CDATA["
            + readFileIntoString("states-FeatureCollection.json")
            + "]]></wps:ComplexData>"
            + "</wps:Data>"
            + "</wps:Input>"
            + "<wps:Input>"
            + "<ows:Identifier>buffer</ows:Identifier>"
            + "<wps:Data>"
            + "<wps:LiteralData>10</wps:LiteralData>"
            + "</wps:Data>"
            + "</wps:Input>"
            + "</wps:DataInputs>"
            + "<wps:ResponseForm>"
            + "<wps:RawDataOutput mimeType=\"application/json\">"
            + "<ows:Identifier>result</ows:Identifier>"
            + "</wps:RawDataOutput>"
            + "</wps:ResponseForm>"
            + "</wps:Execute>";

    MockHttpServletResponse r = postAsServletResponse("wps", xml);
    assertEquals("application/json", r.getContentType());
    // System.out.println(r.getOutputStreamContent());
    FeatureCollection fc = new FeatureJSON().readFeatureCollection(r.getOutputStreamContent());
    assertEquals(2, fc.size());
  }
Esempio n. 5
0
  public void testDispatchWithNamespace() throws Exception {
    URL url = getClass().getResource("applicationContextNamespace.xml");
    FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(url.toString());

    Dispatcher dispatcher = (Dispatcher) context.getBean("dispatcher");
    MockHttpServletRequest request =
        new MockHttpServletRequest() {
          String encoding;

          public int getServerPort() {
            return 8080;
          }

          public String getCharacterEncoding() {
            return encoding;
          }

          public void setCharacterEncoding(String encoding) {
            this.encoding = encoding;
          }
        };

    request.setScheme("http");
    request.setServerName("localhost");

    request.setContextPath("/geoserver");
    request.setMethod("POST");

    MockHttpServletResponse response = new MockHttpServletResponse();

    request.setContentType("application/xml");
    request.setBodyContent(
        "<h:Hello service='hello' message='Hello world!' xmlns:h='http://hello.org' />");
    request.setRequestURI("http://localhost/geoserver/hello");

    dispatcher.handleRequest(request, response);
    assertEquals("Hello world!", response.getOutputStreamContent());

    request.setBodyContent(
        "<h:Hello service='hello' message='Hello world!' xmlns:h='http://hello.org/v2' />");

    response = new MockHttpServletResponse();
    dispatcher.handleRequest(request, response);
    assertEquals("Hello world!:V2", response.getOutputStreamContent());
  }
Esempio n. 6
0
  /**
   * Tests mixed get/post situations for cases in which there is no kvp parser
   *
   * @throws Exception
   */
  public void testHelloOperationMixed() throws Exception {
    URL url = getClass().getResource("applicationContextOnlyXml.xml");

    FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(url.toString());

    Dispatcher dispatcher = (Dispatcher) context.getBean("dispatcher");

    final String body = "<Hello service=\"hello\" message=\"Hello world!\" version=\"1.0.0\" />";

    MockHttpServletRequest request =
        new MockHttpServletRequest() {
          String encoding;

          public int getServerPort() {
            return 8080;
          }

          public String getCharacterEncoding() {
            return encoding;
          }

          public void setCharacterEncoding(String encoding) {
            this.encoding = encoding;
          }

          public ServletInputStream getInputStream() throws IOException {
            final ServletInputStream stream = super.getInputStream();
            return new ServletInputStream() {
              public int read() throws IOException {
                return stream.read();
              }

              public int available() {
                return body.length();
              }
            };
          }
        };

    request.setScheme("http");
    request.setServerName("localhost");
    request.setContextPath("/geoserver");
    request.setMethod("POST");
    request.setRequestURI("http://localhost/geoserver/ows");
    request.setContentType("application/xml");
    request.setBodyContent(body);

    MockHttpServletResponse response = new MockHttpServletResponse();

    request.setupAddParameter("strict", "true");

    dispatcher.handleRequest(request, response);
    assertEquals("Hello world!", response.getOutputStreamContent());
  }
  public void testPlainAdditionKVP() throws Exception { // Standard Test A.4.4.3
    String request =
        "wps?service=WPS&version=1.0.0&request=Execute&Identifier=gt:DoubleAddition"
            + "&DataInputs="
            + urlEncode("input_a=7;input_b=7")
            + "&RawDataOutput=result";

    MockHttpServletResponse response = getAsServletResponse(request);
    assertEquals("text/plain", response.getContentType());
    assertEquals("14.0", response.getOutputStreamContent());
  }
  /**
   * Checks the bounds process returned the expected envelope
   *
   * @param request
   * @param id
   * @throws Exception
   */
  void executeState1BoundsTest(String request, String id) throws Exception {
    if (!RemoteOWSTestSupport.isRemoteWMSStatesAvailable(LOGGER)) {
      LOGGER.warning("Remote OWS tests disabled, skipping test with " + id + " reference source");
      return;
    }

    MockHttpServletResponse resp = postAsServletResponse(root(), request);
    ReferencedEnvelope re = toEnvelope(resp.getOutputStreamContent());
    assertEquals(-91.516129, re.getMinX(), 0.001);
    assertEquals(36.986771, re.getMinY(), 0.001);
    assertEquals(-87.507889, re.getMaxX(), 0.001);
    assertEquals(42.509361, re.getMaxY(), 0.001);
  }
 public void testWKTInlineKVPRawOutput() throws Exception {
   String request =
       "wps?service=WPS&version=1.0.0&request=Execute&Identifier=gt:buffer"
           + "&DataInputs="
           + urlEncode(
               "geom1=POLYGON((1 1, 2 1, 2 2, 1 2, 1 1))@mimetype=application/wkt;buffer=1")
           + "&RawDataOutput="
           + urlEncode("result=@mimetype=application/wkt");
   MockHttpServletResponse response = getAsServletResponse(request);
   // System.out.println(response.getOutputStreamContent());
   assertEquals("application/wkt", response.getContentType());
   Geometry g = new WKTReader().read(response.getOutputStreamContent());
   assertTrue(g instanceof Polygon);
 }
Esempio n. 10
0
  public void testWKTInlineRawOutput() throws Exception { // Standard Test A.4.4.3
    String xml =
        "<wps:Execute service='WPS' version='1.0.0' xmlns:wps='http://www.opengis.net/wps/1.0.0' "
            + "xmlns:ows='http://www.opengis.net/ows/1.1'>"
            + "<ows:Identifier>gt:buffer</ows:Identifier>"
            + "<wps:DataInputs>"
            + "<wps:Input>"
            + "<ows:Identifier>geom1</ows:Identifier>"
            + "<wps:Data>"
            + "<wps:ComplexData mimeType=\"application/wkt\">"
            + "<![CDATA[POLYGON((1 1, 2 1, 2 2, 1 2, 1 1))]]>"
            + "</wps:ComplexData>"
            + "</wps:Data>"
            + "</wps:Input>"
            + "<wps:Input>"
            + "<ows:Identifier>buffer</ows:Identifier>"
            + "<wps:Data>"
            + "<wps:LiteralData>1</wps:LiteralData>"
            + "</wps:Data>"
            + "</wps:Input>"
            + "</wps:DataInputs>"
            + "<wps:ResponseForm>"
            + "    <wps:RawDataOutput mimeType=\"application/wkt\">"
            + "        <ows:Identifier>result</ows:Identifier>"
            + "    </wps:RawDataOutput>"
            + "  </wps:ResponseForm>"
            + "</wps:Execute>";

    // print(dom(new StringInputStream("<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n" + xml)));

    MockHttpServletResponse response = postAsServletResponse("wps", xml);
    System.out.println(response.getOutputStreamContent());
    assertEquals("application/wkt", response.getContentType());
    Geometry g = new WKTReader().read(response.getOutputStreamContent());
    assertTrue(g instanceof Polygon);
  }
Esempio n. 11
0
  public void testProcessChaining() throws Exception {
    // chain two JTS processes
    String xml =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
            + "<wps:Execute version=\"1.0.0\" service=\"WPS\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.opengis.net/wps/1.0.0\" xmlns:wfs=\"http://www.opengis.net/wfs\" xmlns:wps=\"http://www.opengis.net/wps/1.0.0\" xmlns:ows=\"http://www.opengis.net/ows/1.1\" xmlns:gml=\"http://www.opengis.net/gml\" xmlns:ogc=\"http://www.opengis.net/ogc\" xmlns:wcs=\"http://www.opengis.net/wcs/1.1.1\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xsi:schemaLocation=\"http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsAll.xsd\">\n"
            + "  <ows:Identifier>JTS:area</ows:Identifier>\n"
            + "  <wps:DataInputs>\n"
            + "    <wps:Input>\n"
            + "      <ows:Identifier>geom</ows:Identifier>\n"
            + "      <wps:Reference mimeType=\"text/xml; subtype=gml/3.1.1\" xlink:href=\"http://geoserver/wps\" method=\"POST\">\n"
            + "        <wps:Execute>\n"
            + "          <ows:Identifier>JTS:buffer</ows:Identifier>\n"
            + "          <wps:DataInputs>\n"
            + "            <wps:Input>\n"
            + "              <ows:Identifier>geom</ows:Identifier>\n"
            + "              <wps:Data>\n"
            + "                <wps:ComplexData mimeType=\"application/wkt\"><![CDATA[POINT(0 0)]]></wps:ComplexData>\n"
            + "              </wps:Data>\n"
            + "            </wps:Input>\n"
            + "            <wps:Input>\n"
            + "              <ows:Identifier>distance</ows:Identifier>\n"
            + "              <wps:Data>\n"
            + "                <wps:LiteralData>10</wps:LiteralData>\n"
            + "              </wps:Data>\n"
            + "            </wps:Input>\n"
            + "          </wps:DataInputs>\n"
            + "          <wps:ResponseForm>\n"
            + "            <wps:RawDataOutput mimeType=\"text/xml; subtype=gml/3.1.1\">\n"
            + "              <ows:Identifier>result</ows:Identifier>\n"
            + "            </wps:RawDataOutput>\n"
            + "          </wps:ResponseForm>\n"
            + "        </wps:Execute>\n"
            + "      </wps:Reference>\n"
            + "    </wps:Input>\n"
            + "  </wps:DataInputs>\n"
            + "  <wps:ResponseForm>\n"
            + "    <wps:RawDataOutput>\n"
            + "      <ows:Identifier>result</ows:Identifier>\n"
            + "    </wps:RawDataOutput>\n"
            + "  </wps:ResponseForm>\n"
            + "</wps:Execute>";

    MockHttpServletResponse resp = postAsServletResponse(root(), xml);
    assertEquals("text/plain", resp.getContentType());
    // the result is inaccurate since the buffer is just a poor approximation of a circle
    assertTrue(resp.getOutputStreamContent().matches("312\\..*"));
  }
Esempio n. 12
0
  public void testProcessChainingKVP() throws Exception {
    String nested =
        "http://geoserver/wps?service=WPS&version=1.0.0&request=Execute&Identifier=JTS:buffer"
            + "&DataInputs="
            + urlEncode("geom=POINT(0 0)@mimetype=application/wkt;distance=10")
            + "&RawDataOutput=result";
    String request =
        "wps?service=WPS&version=1.0.0&request=Execute&Identifier=JTS:area"
            + "&DataInputs="
            + urlEncode("geom=@href=" + nested)
            + "&RawDataOutput=result";

    MockHttpServletResponse resp = getAsServletResponse(request);
    assertEquals("text/plain", resp.getContentType());
    // the result is inaccurate since the buffer is just a poor approximation of a circle
    assertTrue(resp.getOutputStreamContent().matches("312\\..*"));
  }
  /**
   * Ensures there is at most one feature at the specified location, and returns its feature id
   *
   * @param baseFeatureInfo The GetFeatureInfo request, minus x and y
   * @param x
   * @param y
   * @param layerName TODO
   * @return
   */
  Double getFeatureAt(String baseFeatureInfo, int x, int y, String layerName) throws Exception {
    MockHttpServletResponse response =
        getAsServletResponse(
            baseFeatureInfo + "&info_format=application/vnd.ogc.gml&x=" + x + "&y=" + y);
    assertEquals("application/vnd.ogc.gml", response.getContentType());
    Document doc = dom(new ByteArrayInputStream(response.getOutputStreamContent().getBytes()));
    String sCount = xpath.evaluate("count(//" + layerName + ")", doc);
    int count = Integer.valueOf(sCount);

    if (count == 0) {
      return null;
    } else if (count == 1) {
      return Double.valueOf(xpath.evaluate("//" + layerName + "/sf:GRAY_INDEX", doc));
    } else {
      fail("Found more than one feature: " + count);
      return null; // just to make the compiler happy, fail throws an unchecked exception
    }
  }
Esempio n. 14
0
  public void testHelloOperationGet() throws Exception {
    URL url = getClass().getResource("applicationContext.xml");

    FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(url.toString());

    Dispatcher dispatcher = (Dispatcher) context.getBean("dispatcher");

    MockHttpServletRequest request =
        new MockHttpServletRequest() {
          String encoding;

          public int getServerPort() {
            return 8080;
          }

          public String getCharacterEncoding() {
            return encoding;
          }

          public void setCharacterEncoding(String encoding) {
            this.encoding = encoding;
          }
        };

    request.setScheme("http");
    request.setServerName("localhost");

    request.setContextPath("/geoserver");
    request.setMethod("GET");

    MockHttpServletResponse response = new MockHttpServletResponse();

    request.setupAddParameter("service", "hello");
    request.setupAddParameter("request", "Hello");
    request.setupAddParameter("version", "1.0.0");
    request.setupAddParameter("message", "Hello world!");

    request.setRequestURI(
        "http://localhost/geoserver/ows?service=hello&request=hello&message=HelloWorld");
    request.setQueryString("service=hello&request=hello&message=HelloWorld");
    dispatcher.handleRequest(request, response);
    assertEquals("Hello world!", response.getOutputStreamContent());
  }
  @Test
  public void testWorldImageUploadZipped() throws Exception {
    URL zip = getClass().getResource("test-data/usa.zip");
    byte[] bytes = FileUtils.readFileToByteArray(DataUtilities.urlToFile(zip));

    MockHttpServletResponse response =
        putAsServletResponse(
            "/rest/workspaces/sf/coveragestores/usa/file.worldimage", bytes, "application/zip");
    assertEquals(201, response.getStatusCode());

    String content = response.getOutputStreamContent();
    Document d = dom(new ByteArrayInputStream(content.getBytes()));
    assertEquals("coverageStore", d.getDocumentElement().getNodeName());

    CoverageStoreInfo cs = getCatalog().getCoverageStoreByName("sf", "usa");
    assertNotNull(cs);
    CoverageInfo ci = getCatalog().getCoverageByName("sf", "usa");
    assertNotNull(ci);
  }
  @Test
  public void testCreateStoredQuerySOAP() throws Exception {
    String xml =
        "<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope'> "
            + " <soap:Header/> "
            + " <soap:Body>"
            + "<wfs:CreateStoredQuery service='WFS' version='2.0.0' "
            + "   xmlns:wfs='http://www.opengis.net/wfs/2.0' "
            + "   xmlns:fes='http://www.opengis.org/fes/2.0' "
            + "   xmlns:gml='http://www.opengis.net/gml/3.2' "
            + "   xmlns:myns='http://www.someserver.com/myns' "
            + "   xmlns:sf='"
            + MockData.SF_URI
            + "'>"
            + "   <wfs:StoredQueryDefinition id='myStoredQuery'> "
            + "      <wfs:Parameter name='AreaOfInterest' type='gml:Polygon'/> "
            + "      <wfs:QueryExpressionText "
            + "           returnFeatureTypes='sf:PrimitiveGeoFeature' "
            + "           language='urn:ogc:def:queryLanguage:OGC-WFS::WFS_QueryExpression' "
            + "           isPrivate='false'> "
            + "         <wfs:Query typeNames='sf:PrimitiveGeoFeature'> "
            + "            <fes:Filter> "
            + "               <fes:Within> "
            + "                  <fes:ValueReference>pointProperty</fes:ValueReference> "
            + "                  ${AreaOfInterest} "
            + "               </fes:Within> "
            + "            </fes:Filter> "
            + "         </wfs:Query> "
            + "      </wfs:QueryExpressionText> "
            + "   </wfs:StoredQueryDefinition> "
            + "</wfs:CreateStoredQuery>"
            + " </soap:Body> "
            + "</soap:Envelope> ";

    MockHttpServletResponse resp = postAsServletResponse("wfs", xml, "application/soap+xml");
    assertEquals("application/soap+xml", resp.getContentType());

    Document dom = dom(new ByteArrayInputStream(resp.getOutputStreamContent().getBytes()));
    assertEquals("soap:Envelope", dom.getDocumentElement().getNodeName());
    assertEquals(1, dom.getElementsByTagName("wfs:CreateStoredQueryResponse").getLength());
  }
  @Test
  public void testDropStoredQuerySOAP() throws Exception {
    testCreateStoredQuery();

    String xml =
        "<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope'> "
            + " <soap:Header/> "
            + " <soap:Body>"
            + "<wfs:DropStoredQuery service='WFS' version='2.0.0' "
            + " xmlns:wfs='"
            + WFS.NAMESPACE
            + "' id='myStoredQuery'/>"
            + " </soap:Body> "
            + "</soap:Envelope> ";

    MockHttpServletResponse resp = postAsServletResponse("wfs", xml, "application/soap+xml");
    assertEquals("application/soap+xml", resp.getContentType());

    Document dom = dom(new ByteArrayInputStream(resp.getOutputStreamContent().getBytes()));
    assertEquals("soap:Envelope", dom.getDocumentElement().getNodeName());
    assertEquals(1, dom.getElementsByTagName("wfs:DropStoredQueryResponse").getLength());
  }
Esempio n. 18
0
  @Test
  public void testGetWrongWMSLayer() throws Exception {
    // Parameters for the request
    String ws = "sf";
    String wms = "demo";
    String wl = "statessssss";
    // Request path
    String requestPath = "/rest/workspaces/" + ws + "/wmslayers/" + wl + ".html";
    String requestPath2 =
        "/rest/workspaces/" + ws + "/wmsstores/" + wms + "/wmslayers/" + wl + ".html";
    // Exception path
    String exception = "No such cascaded wms: " + ws + "," + wl;
    String exception2 = "No such cascaded wms layer: " + ws + "," + wms + "," + wl;

    // CASE 1: No wmsstore set

    // First request should thrown an exception
    MockHttpServletResponse response = getAsServletResponse(requestPath);
    assertEquals(404, response.getStatusCode());
    assertTrue(response.getOutputStreamContent().contains(exception));

    // Same request with ?quietOnNotFound should not throw an exception
    response = getAsServletResponse(requestPath + "?quietOnNotFound=true");
    assertEquals(404, response.getStatusCode());
    assertFalse(response.getOutputStreamContent().contains(exception));
    // No exception thrown
    assertTrue(response.getOutputStreamContent().isEmpty());

    // CASE 2: wmsstore set

    // First request should thrown an exception
    response = getAsServletResponse(requestPath2);
    assertEquals(404, response.getStatusCode());
    assertTrue(response.getOutputStreamContent().contains(exception2));

    // Same request with ?quietOnNotFound should not throw an exception
    response = getAsServletResponse(requestPath2 + "?quietOnNotFound=true");
    assertEquals(404, response.getStatusCode());
    assertFalse(response.getOutputStreamContent().contains(exception2));
    // No exception thrown
    assertTrue(response.getOutputStreamContent().isEmpty());
  }
  @Test
  public void testHarvestImageMosaicWithDirectory() throws Exception {
    // Upload of the Mosaic via REST
    URL zip = MockData.class.getResource("watertemp.zip");
    InputStream is = null;
    byte[] bytes;
    try {
      is = zip.openStream();
      bytes = IOUtils.toByteArray(is);
    } finally {
      IOUtils.closeQuietly(is);
    }

    MockHttpServletResponse response =
        putAsServletResponse(
            "/rest/workspaces/gs/coveragestores/watertemp3/file.imagemosaic",
            bytes,
            "application/zip");
    assertEquals(201, response.getStatusCode());

    // check the response contents
    String content = response.getOutputStreamContent();
    Document d = dom(new ByteArrayInputStream(content.getBytes()));

    XMLAssert.assertXpathEvaluatesTo("watertemp3", "//coverageStore/name", d);
    XMLAssert.assertXpathEvaluatesTo("ImageMosaic", "//coverageStore/type", d);

    // check the coverage is actually there
    CoverageStoreInfo storeInfo = getCatalog().getCoverageStoreByName("watertemp3");
    assertNotNull(storeInfo);
    CoverageInfo ci = getCatalog().getCoverageByName("watertemp3");
    assertNotNull(ci);
    assertEquals(storeInfo, ci.getStore());

    // Harvesting of the Mosaic
    URL zipHarvest = getClass().getResource("test-data/harvesting.zip");
    File zipFile = DataUtilities.urlToFile(zipHarvest);
    // Creation of another zip file which is a copy of the one before
    File newZip = new File(zipFile.getParentFile(), "harvesting2.zip");
    // Copy the content of the first zip to the second
    FileUtils.copyFile(zipFile, newZip);
    File outputDirectory = new File(zipFile.getParentFile(), "harvesting");
    outputDirectory.mkdir();
    RESTUtils.unzipFile(newZip, outputDirectory);
    // Create the POST request
    MockHttpServletRequest request =
        createRequest("/rest/workspaces/gs/coveragestores/watertemp3/external.imagemosaic");
    request.setMethod("POST");
    request.setContentType("text/plain");
    request.setBodyContent("file:///" + outputDirectory.getAbsolutePath());
    request.setHeader("Content-type", "text/plain");
    // Get The response
    response = dispatch(request);
    // Get the Mosaic Reader
    GridCoverageReader reader = storeInfo.getGridCoverageReader(null, GeoTools.getDefaultHints());
    // Test if all the TIME DOMAINS are present
    String[] metadataNames = reader.getMetadataNames();
    assertNotNull(metadataNames);
    assertEquals("true", reader.getMetadataValue("HAS_TIME_DOMAIN"));
    assertEquals(
        "2008-10-31T00:00:00.000Z,2008-11-01T00:00:00.000Z,2008-11-02T00:00:00.000Z",
        reader.getMetadataValue(metadataNames[0]));
    // Removal of the temporary directory
    FileUtils.deleteDirectory(outputDirectory);
  }
  @Test
  public void testHarvestImageMosaic() throws Exception {
    // Upload of the Mosaic via REST
    URL zip = MockData.class.getResource("watertemp.zip");
    InputStream is = null;
    byte[] bytes;
    try {
      is = zip.openStream();
      bytes = IOUtils.toByteArray(is);
    } finally {
      IOUtils.closeQuietly(is);
    }

    MockHttpServletResponse response =
        putAsServletResponse(
            "/rest/workspaces/gs/coveragestores/watertemp2/file.imagemosaic",
            bytes,
            "application/zip");
    assertEquals(201, response.getStatusCode());

    // check the response contents
    String content = response.getOutputStreamContent();
    Document d = dom(new ByteArrayInputStream(content.getBytes()));

    XMLAssert.assertXpathEvaluatesTo("watertemp2", "//coverageStore/name", d);
    XMLAssert.assertXpathEvaluatesTo("ImageMosaic", "//coverageStore/type", d);

    // check the coverage is actually there
    CoverageStoreInfo storeInfo = getCatalog().getCoverageStoreByName("watertemp2");
    assertNotNull(storeInfo);
    CoverageInfo ci = getCatalog().getCoverageByName("watertemp2");
    assertNotNull(ci);
    assertEquals(storeInfo, ci.getStore());

    // Harvesting of the Mosaic
    URL zipHarvest = getClass().getResource("test-data/harvesting.zip");
    // Extract a Byte array from the zip file
    is = null;
    try {
      is = zipHarvest.openStream();
      bytes = IOUtils.toByteArray(is);
    } finally {
      IOUtils.closeQuietly(is);
    }
    // Create the POST request
    MockHttpServletRequest request =
        createRequest("/rest/workspaces/gs/coveragestores/watertemp2/file.imagemosaic");
    request.setMethod("POST");
    request.setContentType("application/zip");
    request.setBodyContent(bytes);
    request.setHeader("Content-type", "application/zip");
    // Get The response
    response = dispatch(request);
    // Get the Mosaic Reader
    GridCoverageReader reader = storeInfo.getGridCoverageReader(null, GeoTools.getDefaultHints());
    // Test if all the TIME DOMAINS are present
    String[] metadataNames = reader.getMetadataNames();
    assertNotNull(metadataNames);
    assertEquals("true", reader.getMetadataValue("HAS_TIME_DOMAIN"));
    assertEquals(
        "2008-10-31T00:00:00.000Z,2008-11-01T00:00:00.000Z,2008-11-02T00:00:00.000Z",
        reader.getMetadataValue(metadataNames[0]));
  }