-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
97 additions
and
9 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
75 changes: 75 additions & 0 deletions
75
core/src/test/java/org/mapfish/print/processor/jasper/IconTaskTest.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.processor.jasper; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
import java.awt.image.BufferedImage; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.util.HashMap; | ||
import org.junit.Test; | ||
import org.mapfish.print.http.MfClientHttpRequestFactory; | ||
import org.mapfish.print.processor.AbstractProcessor; | ||
import org.mapfish.print.processor.Processor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.mock.http.client.MockClientHttpRequest; | ||
import org.springframework.mock.http.client.MockClientHttpResponse; | ||
|
||
public class IconTaskTest { | ||
|
||
@Test | ||
public void call_iconImageDoesNotExist_returnsMissingImageWhenOk() throws Exception { | ||
int statusCode = HttpStatus.OK.value(); | ||
BufferedImage missingImage = new BufferedImage(24, 24, BufferedImage.TYPE_INT_RGB); | ||
|
||
LegendProcessor.IconTask iconTask = prepareTest(missingImage, statusCode); | ||
|
||
Object[] result = iconTask.call(); | ||
|
||
assertEquals(missingImage, result[1]); | ||
} | ||
|
||
@Test | ||
public void call_iconImageDoesNotExist_returnsMissingImageWithUnsupportedStatus() | ||
throws Exception { | ||
int statusCode = 999; | ||
BufferedImage missingImage = new BufferedImage(24, 24, BufferedImage.TYPE_INT_RGB); | ||
|
||
LegendProcessor.IconTask iconTask = prepareTest(missingImage, statusCode); | ||
|
||
Object[] result = iconTask.call(); | ||
|
||
assertEquals(missingImage, result[1]); | ||
} | ||
|
||
private static LegendProcessor.IconTask prepareTest(BufferedImage missingImage, int statusCode) | ||
throws URISyntaxException, IOException { | ||
LegendProcessor legendProcessorMock = spy(LegendProcessor.class); | ||
when(legendProcessorMock.getMissingImage()).thenReturn(missingImage); | ||
Processor.ExecutionContext context = new AbstractProcessor.Context(new HashMap<>()); | ||
URL icon = mock(URL.class); | ||
when(icon.toURI()).thenReturn(new URI("http://localhost/icon.png")); | ||
when(icon.getProtocol()).thenReturn("http"); | ||
MfClientHttpRequestFactory requestFactoryMock = mock(MfClientHttpRequestFactory.class); | ||
MockClientHttpRequest requestMock = new MockClientHttpRequest(); | ||
when(requestFactoryMock.createRequest(any(URI.class), any())).thenReturn(requestMock); | ||
|
||
byte[] responseBody = "Image bytes".getBytes(); | ||
MockClientHttpResponse responseMock = new MockClientHttpResponse(responseBody, statusCode); | ||
responseMock.getHeaders().setContentType(MediaType.IMAGE_PNG); | ||
when(requestFactoryMock.createRequest( | ||
new URI("testUriString"), org.springframework.http.HttpMethod.GET)) | ||
.thenReturn(requestMock); | ||
requestMock.setResponse(responseMock); | ||
|
||
return legendProcessorMock | ||
.new IconTask(icon, 96, context, 1, null, requestFactoryMock, new MetricRegistry()); | ||
} | ||
} |