-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test with unsupported HttpStatus code
- Loading branch information
Showing
10 changed files
with
473 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
core/src/test/java/org/mapfish/print/map/image/AbstractSingleImageLayerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.mapfish.print.map.image; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
import java.awt.image.BufferedImage; | ||
import java.io.IOException; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.mapfish.print.Constants; | ||
import org.mapfish.print.attribute.map.MapfishMapContext; | ||
import org.mapfish.print.http.MfClientHttpRequestFactory; | ||
import org.mapfish.print.map.AbstractLayerParams; | ||
import org.springframework.mock.http.client.MockClientHttpRequest; | ||
import org.springframework.mock.http.client.MockClientHttpResponse; | ||
|
||
public class AbstractSingleImageLayerTest { | ||
|
||
@Test | ||
public void testFetchImage() throws IOException { | ||
MapfishMapContext mapContext = new MapfishMapContext(null, null, 100, 100, false, true); | ||
MockClientHttpRequest mockClientHttpRequest = new MockClientHttpRequest(); | ||
final int unsupportedStatusCode = 999; | ||
mockClientHttpRequest.setResponse( | ||
new MockClientHttpResponse( | ||
"SomeResponse".getBytes(Constants.DEFAULT_CHARSET), unsupportedStatusCode)); | ||
AbstractLayerParams layerParams = new AbstractLayerParams(); | ||
layerParams.failOnError = true; | ||
AbstractSingleImageLayer layer = new AbstractSingleImageLayerTestImpl(layerParams); | ||
|
||
try { | ||
layer.fetchImage(mockClientHttpRequest, mapContext); | ||
Assert.fail("Did not throw exception with unsupported status code"); | ||
} catch (RuntimeException e) { | ||
assertTrue(e.getMessage().contains(String.valueOf(unsupportedStatusCode))); | ||
} | ||
} | ||
|
||
private static class AbstractSingleImageLayerTestImpl extends AbstractSingleImageLayer { | ||
|
||
public AbstractSingleImageLayerTestImpl(AbstractLayerParams layerParams) { | ||
super(null, null, layerParams, new MetricRegistry(), null); | ||
} | ||
|
||
@Override | ||
protected BufferedImage loadImage( | ||
MfClientHttpRequestFactory requestFactory, MapfishMapContext transformer) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public RenderType getRenderType() { | ||
return null; | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
core/src/test/java/org/mapfish/print/map/style/ParserPluginUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.mapfish.print.map.style; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.nio.charset.Charset; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import org.geotools.api.style.Style; | ||
import org.junit.Test; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.client.ClientHttpRequest; | ||
import org.springframework.http.client.ClientHttpRequestFactory; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
|
||
public class ParserPluginUtilsTest { | ||
|
||
@Test | ||
public void loadStyleAsURITest_ValidURI() throws IOException, URISyntaxException { | ||
int httpStatusCode = HttpStatus.OK.value(); | ||
Style style = mock(Style.class); | ||
Optional<Style> actualStyle = testLoadingStyleWithStatusCode(style, httpStatusCode); | ||
|
||
assertTrue(actualStyle.isPresent()); | ||
assertEquals(style, actualStyle.get()); | ||
} | ||
|
||
private static Optional<Style> testLoadingStyleWithStatusCode( | ||
final Style style, final int httpStatusCode) throws IOException, URISyntaxException { | ||
ClientHttpRequestFactory factory = mock(ClientHttpRequestFactory.class); | ||
ClientHttpRequest request = mock(ClientHttpRequest.class); | ||
ClientHttpResponse response = mock(ClientHttpResponse.class); | ||
|
||
Function<byte[], Optional<Style>> function = bytes -> Optional.of(style); | ||
|
||
when(factory.createRequest(new URI("http://valid.uri"), HttpMethod.GET)).thenReturn(request); | ||
when(request.execute()).thenReturn(response); | ||
when(response.getRawStatusCode()).thenReturn(httpStatusCode); | ||
when(response.getBody()) | ||
.thenReturn( | ||
new ByteArrayInputStream( | ||
"This is dummy style data".getBytes(Charset.defaultCharset()))); | ||
|
||
return ParserPluginUtils.loadStyleAsURI(factory, "http://valid.uri", function); | ||
} | ||
|
||
@Test | ||
public void loadStyleAsURITest_InvalidStatusCode() throws IOException, URISyntaxException { | ||
int httpStatusCode = 999; | ||
Style style = mock(Style.class); | ||
Optional<Style> actualStyle = testLoadingStyleWithStatusCode(style, httpStatusCode); | ||
|
||
assertFalse(actualStyle.isPresent()); | ||
} | ||
|
||
@Test | ||
public void loadStyleAsURITest_InValidURI() { | ||
ClientHttpRequestFactory factory = mock(ClientHttpRequestFactory.class); | ||
Style style = mock(Style.class); | ||
|
||
Function<byte[], Optional<Style>> function = bytes -> Optional.of(style); | ||
|
||
Optional<Style> actualStyle = | ||
ParserPluginUtils.loadStyleAsURI(factory, "invalid|uri", function); | ||
assertTrue(actualStyle.isEmpty()); | ||
} | ||
} |
Oops, something went wrong.