Example usage for org.springframework.mock.web MockHttpServletResponse getContentAsByteArray

List of usage examples for org.springframework.mock.web MockHttpServletResponse getContentAsByteArray

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletResponse getContentAsByteArray.

Prototype

public byte[] getContentAsByteArray() 

Source Link

Usage

From source file:org.geoserver.wps.gs.download.DownloadMapProcessTest.java

public void testExecutMultiLayerKmz(String mime) throws Exception {
    String request = IOUtils.toString(getClass().getResourceAsStream("mapMultiLayer.xml"));
    request = request.replaceAll("image/png", mime);
    MockHttpServletResponse response = postAsServletResponse("wps", request);
    assertEquals(KMZMapOutputFormat.MIME_TYPE, response.getContentType());
    assertEquals("inline; filename=result.kmz", response.getHeader("Content-disposition"));

    ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(response.getContentAsByteArray()));
    try {// ww  w  . j a v  a  2  s  .  com
        // first entry, the kml document itself
        ZipEntry entry = zis.getNextEntry();
        assertEquals("wms.kml", entry.getName());
        byte[] data = IOUtils.toByteArray(zis);
        Document dom = dom(new ByteArrayInputStream(data));
        // print(dom);
        assertXpathEvaluatesTo("1", "count(//kml:Folder/kml:GroundOverlay)", dom);
        String href = XMLUnit.newXpathEngine().evaluate("//kml:Folder/kml:GroundOverlay/kml:Icon/kml:href",
                dom);
        assertEquals("image.png", href);
        zis.closeEntry();

        // the ground overlay for the raster layer
        entry = zis.getNextEntry();
        assertEquals("image.png", entry.getName());
        BufferedImage image = ImageIO.read(zis);
        zis.closeEntry();
        assertNull(zis.getNextEntry());

        // check the output, same as mapMultiName
        ImageAssert.assertEquals(new File(SAMPLES + "mapMultiName.png"), image, 100);
    } finally {
        zis.close();
    }
}

From source file:org.geoserver.wps.gs.download.DownloadMapProcessTest.java

@Test
public void testTimeFilter() throws Exception {
    String xml = IOUtils.toString(getClass().getResourceAsStream("mapTimeFilter.xml"));
    MockHttpServletResponse response = postAsServletResponse("wps", xml);
    assertEquals("image/png", response.getContentType());
    BufferedImage image = ImageIO.read(new ByteArrayInputStream(response.getContentAsByteArray()));

    // same test as DimensionRasterGetMapTest#testTime
    assertPixel(image, 36, 31, new Color(246, 246, 255));
    assertPixel(image, 68, 72, new Color(255, 181, 181));

    // making extra sure
    ImageAssert.assertEquals(new File(SAMPLES + "mapTimeFilter.png"), image, 100);
}

From source file:org.geoserver.wps.gs.download.DownloadMapProcessTest.java

@Test
public void testTimeFilterTimestamped() throws Exception {
    String xml = IOUtils.toString(getClass().getResourceAsStream("mapTimeFilterTimestamped.xml"));
    MockHttpServletResponse response = postAsServletResponse("wps", xml);
    assertEquals("image/png", response.getContentType());
    BufferedImage image = ImageIO.read(new ByteArrayInputStream(response.getContentAsByteArray()));
    ImageAssert.assertEquals(new File(SAMPLES + "mapTimeFilterTimestamped.png"), image, 200);
}

From source file:org.geoserver.wps.gs.download.DownloadMapProcessTest.java

@Test
public void testTimeFilterFormattedTimestamp() throws Exception {
    String xml = IOUtils.toString(getClass().getResourceAsStream("mapTimeFilterFormattedTimestamp.xml"));
    MockHttpServletResponse response = postAsServletResponse("wps", xml);
    assertEquals("image/png", response.getContentType());
    BufferedImage image = ImageIO.read(new ByteArrayInputStream(response.getContentAsByteArray()));
    ImageAssert.assertEquals(new File(SAMPLES + "mapTimeFilterFormattedTimestamp.png"), image, 200);
}

From source file:org.geoserver.wps.gs.download.DownloadMapProcessTest.java

@Test
public void downloadMapGif() throws Exception {
    String request = IOUtils.toString(getClass().getResourceAsStream("mapSimple.xml"));
    request = request.replaceAll("image/png", "image/gif");
    MockHttpServletResponse response = postAsServletResponse("wps", request);
    assertEquals("image/gif", response.getContentType());
    BufferedImage image = ImageIO.read(new ByteArrayInputStream(response.getContentAsByteArray()));
    ImageAssert.assertEquals(new File(SAMPLES + "mapSimple.png"), image, 200);
}

From source file:org.geoserver.wps.gs.download.DownloadMapProcessTest.java

@Test
public void downloadRemoteSimple11() throws Exception {
    String request = IOUtils.toString(getClass().getResourceAsStream("mapRemoteSimple11.xml"));
    String caps111 = IOUtils.toString(getClass().getResourceAsStream("caps111.xml"));
    byte[] getMapBytes = FileUtils.readFileToByteArray(new File(SAMPLES + "mapSimple.png"));
    DownloadMapProcess process = applicationContext.getBean(DownloadMapProcess.class);
    MockHttpClient client = new MockHttpClient();
    client.expectGet(//w  ww  . j  a v  a 2  s.  c  o m
            new URL("http://geoserver"
                    + ".org/geoserver/wms?service=WMS&request=GetCapabilities&version=1.1.0"),
            new MockHttpResponse(caps111, "text/xml"));
    // check it follows the links in the caps document
    client.expectGet(new URL("http://mock.test.geoserver"
            + ".org/wms11?SERVICE=WMS&LAYERS=cite:BasicPolygons&FORMAT=image%2Fpng&HEIGHT=256&TRANSPARENT=false"
            + "&REQUEST=GetMap&WIDTH=256&BBOX=-2.4,1.4,0.4,4.2&SRS=EPSG:4326&VERSION=1.1.1"),
            new MockHttpResponse(getMapBytes, "image/png"));
    // switch from the standard supplier to one using the mock client prepared above
    Supplier<HTTPClient> oldSupplier = process.getHttpClientSupplier();
    try {
        process.setHttpClientSupplier(() -> client);

        MockHttpServletResponse response = postAsServletResponse("wps", request);
        assertEquals("image/png", response.getContentType());
        BufferedImage image = ImageIO.read(new ByteArrayInputStream(response.getContentAsByteArray()));
        ImageAssert.assertEquals(new File(SAMPLES + "mapSimple.png"), image, 100);
    } finally {
        process.setHttpClientSupplier(oldSupplier);
    }
}

From source file:org.geoserver.wps.gs.download.DownloadMapProcessTest.java

@Test
public void downloadRemoteSimple13() throws Exception {
    String request = IOUtils.toString(getClass().getResourceAsStream("mapRemoteSimple13.xml"));
    String caps130 = IOUtils.toString(getClass().getResourceAsStream("caps130.xml"));
    byte[] getMapBytes = FileUtils.readFileToByteArray(new File(SAMPLES + "mapSimple.png"));
    DownloadMapProcess process = applicationContext.getBean(DownloadMapProcess.class);
    MockHttpClient client = new MockHttpClient();
    client.expectGet(/*from w w w .j  a v a 2  s  .c o m*/
            new URL("http://geoserver.org/geoserver/wms?service=WMS&request=GetCapabilities&version=1.3.0"),
            new MockHttpResponse(caps130, "text/xml"));
    // check it follows the links in the caps document and does axis flipping as required
    client.expectGet(new URL("http://mock.test.geoserver"
            + ".org/wms13?SERVICE=WMS&LAYERS=cite:BasicPolygons&FORMAT=image%2Fpng&HEIGHT=256&TRANSPARENT=false"
            + "&REQUEST=GetMap&WIDTH=256&BBOX=1.4,-2.4,4.2,0.4&CRS=EPSG:4326&VERSION=1.3.0"),
            new MockHttpResponse(getMapBytes, "image/png"));
    // switch from the standard supplier to one using the mock client prepared above
    Supplier<HTTPClient> oldSupplier = process.getHttpClientSupplier();
    try {
        process.setHttpClientSupplier(() -> client);

        MockHttpServletResponse response = postAsServletResponse("wps", request);
        assertEquals("image/png", response.getContentType());
        BufferedImage image = ImageIO.read(new ByteArrayInputStream(response.getContentAsByteArray()));
        ImageAssert.assertEquals(new File(SAMPLES + "mapSimple.png"), image, 100);
    } finally {
        process.setHttpClientSupplier(oldSupplier);
    }
}

From source file:org.geoserver.wps.gs.download.DownloadMapProcessTest.java

@Test
public void downloadLocalRemote() throws Exception {
    String request = IOUtils.toString(getClass().getResourceAsStream("mapLocalRemote.xml"));
    String caps111 = IOUtils.toString(getClass().getResourceAsStream("caps111.xml"));
    byte[] getMapBytes = FileUtils.readFileToByteArray(new File(SAMPLES + "lakes.png"));
    DownloadMapProcess process = applicationContext.getBean(DownloadMapProcess.class);
    MockHttpClient client = new MockHttpClient();
    client.expectGet(//  ww  w .jav  a  2  s .c  om
            new URL("http://geoserver"
                    + ".org/geoserver/wms?service=WMS&request=GetCapabilities&version=1.1.0"),
            new MockHttpResponse(caps111, "text/xml"));
    // check it follows the links in the caps document
    client.expectGet(
            new URL("http://mock.test.geoserver"
                    + ".org/wms11?SERVICE=WMS&LAYERS=cite:Lakes&FORMAT=image%2Fpng&HEIGHT=256&TRANSPARENT=true"
                    + "&REQUEST=GetMap&WIDTH=256&BBOX=0.0,-0.003,0.004,0.001&SRS=EPSG:4326&VERSION=1.1.1"),
            new MockHttpResponse(getMapBytes, "image/png"));
    // switch from the standard supplier to one using the mock client prepared above
    Supplier<HTTPClient> oldSupplier = process.getHttpClientSupplier();
    try {
        process.setHttpClientSupplier(() -> client);

        MockHttpServletResponse response = postAsServletResponse("wps", request);
        assertEquals("image/png", response.getContentType());
        BufferedImage image = ImageIO.read(new ByteArrayInputStream(response.getContentAsByteArray()));
        ImageAssert.assertEquals(new File(SAMPLES + "localRemote.png"), image, 100);
    } finally {
        process.setHttpClientSupplier(oldSupplier);
    }
}

From source file:org.geowebcache.service.wmts.WMTSRestTest.java

@Test
public void testGetTileWithStyle() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest();
    req.setPathInfo("geowebcache/service/wmts/rest/mockLayer/style-a/EPSG:4326/EPSG:4326:0/0/0");
    req.addParameter("format", "image/png");

    MockHttpServletResponse resp = dispatch(req);

    assertEquals(200, resp.getStatus());
    assertEquals("image/png", resp.getContentType());
    assertEquals("EPSG:4326", resp.getHeader("geowebcache-crs"));
    assertArrayEquals(getSampleTileContent().getContents(), resp.getContentAsByteArray());
}

From source file:org.jasig.portal.portlet.rendering.PortletRendererImplTest.java

/**
 * Mimic workflow when cached portlet data using "expiration" method is available.
 * /*from ww  w .j a va2  s.c  o  m*/
 * @throws PortletContainerException 
 * @throws IOException 
 * @throws PortletException 
 */
@Test
public void doServeResourceCachedContentExpirationMethodTest()
        throws PortletException, IOException, PortletContainerException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    Date now = new Date();
    CacheControlImpl cacheControl = new CacheControlImpl();
    cacheControl.setUseCachedContent(true);
    cacheControl.setExpirationTime(300);
    CachedPortletData cachedPortletData = new CachedPortletData();
    cachedPortletData.setContentType("application/json");
    byte[] content = "{ \"hello\": \"world\" }".getBytes();
    cachedPortletData.setByteData(content);
    cachedPortletData.setExpirationTimeSeconds(cacheControl.getExpirationTime());
    cachedPortletData.setTimeStored(now);

    setupPortletExecutionMocks(request);

    when(portletCacheControlService.getPortletResourceCacheControl(portletWindowId, request, response))
            .thenReturn(cacheControl);
    when(portletCacheControlService.getCachedPortletResourceOutput(portletWindowId, request))
            .thenReturn(cachedPortletData);

    portletRenderer.doServeResource(portletWindowId, request, response);
    // verify content matches what was in cache (no array support in Assert.assertEquals, check byte for byte)
    byte[] fromResponse = response.getContentAsByteArray();
    Assert.assertEquals(content.length, fromResponse.length);
    for (int i = 0; i < content.length; i++) {
        Assert.assertEquals(content[i], fromResponse[i]);
    }
    // verify we enter the first branch and never execute portletContainer#doServeResource
    verify(portletContainer, never()).doServeResource(isA(PortletWindow.class),
            isA(PortletHttpServletRequestWrapper.class), isA(PortletHttpServletResponseWrapper.class));
    // verify we never enter the other branch of the "should render cached output" if statement
    verify(portletCacheControlService, never()).shouldOutputBeCached(isA(CacheControl.class));
}