|
| 1 | +package org.mapfish.print.processor.map; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.URI; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.concurrent.CancellationException; |
| 8 | +import java.util.concurrent.TimeUnit; |
| 9 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 10 | +import java.util.function.Function; |
| 11 | +import org.junit.Assert; |
| 12 | +import org.junit.Test; |
| 13 | +import org.junit.jupiter.api.Timeout; |
| 14 | +import org.mapfish.print.AbstractMapfishSpringTest; |
| 15 | +import org.mapfish.print.PrintException; |
| 16 | +import org.mapfish.print.TestHttpClientFactory; |
| 17 | +import org.mapfish.print.config.Configuration; |
| 18 | +import org.mapfish.print.config.ConfigurationFactory; |
| 19 | +import org.mapfish.print.output.AbstractJasperReportOutputFormat; |
| 20 | +import org.mapfish.print.output.OutputFormat; |
| 21 | +import org.mapfish.print.wrapper.json.PJsonObject; |
| 22 | +import org.springframework.beans.factory.annotation.Autowired; |
| 23 | +import org.springframework.http.HttpMethod; |
| 24 | +import org.springframework.http.HttpStatus; |
| 25 | +import org.springframework.mock.http.client.MockClientHttpRequest; |
| 26 | +import org.springframework.mock.http.client.MockClientHttpResponse; |
| 27 | +import org.springframework.test.annotation.DirtiesContext; |
| 28 | + |
| 29 | +/** |
| 30 | + * Test that verify that forceFailOnError apply the failOnError parameter on all requested layers |
| 31 | + */ |
| 32 | +public class CreateMapPagesForceFailOnErrorProcessorTest extends AbstractMapfishSpringTest { |
| 33 | + public static final String BASE_DIR = "paging_processor_force_fail_on_error_test/"; |
| 34 | + @Autowired private ConfigurationFactory configurationFactory; |
| 35 | + @Autowired private TestHttpClientFactory requestFactory; |
| 36 | + @Autowired private Map<String, OutputFormat> outputFormat; |
| 37 | + |
| 38 | + private static PJsonObject loadJsonRequestData() throws IOException { |
| 39 | + return parseJSONObjectFromFile( |
| 40 | + CreateMapPagesForceFailOnErrorProcessorTest.class, BASE_DIR + "requestData.json"); |
| 41 | + } |
| 42 | + |
| 43 | + private static final AtomicBoolean hasFailOnce = new AtomicBoolean(false); |
| 44 | + |
| 45 | + /** File handler that will fail on the first request. */ |
| 46 | + protected TestHttpClientFactory.Handler createFailingFileHandler(Function<URI, String> filename) { |
| 47 | + return new TestHttpClientFactory.Handler() { |
| 48 | + @Override |
| 49 | + public MockClientHttpRequest handleRequest(URI uri, HttpMethod httpMethod) throws Exception { |
| 50 | + if (hasFailOnce.compareAndSet(false, true)) { |
| 51 | + MockClientHttpRequest request = new MockClientHttpRequest(httpMethod, uri); |
| 52 | + MockClientHttpResponse response = |
| 53 | + new MockClientHttpResponse(new byte[0], HttpStatus.REQUEST_TIMEOUT); |
| 54 | + request.setResponse(response); |
| 55 | + return request; |
| 56 | + } |
| 57 | + byte[] bytes = getFileBytes(filename.apply(uri)); |
| 58 | + return ok(uri, bytes, httpMethod); |
| 59 | + } |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + @DirtiesContext |
| 65 | + @Timeout(value = 1, unit = TimeUnit.MINUTES) |
| 66 | + public void testExecute() throws Exception { |
| 67 | + final String host = "paging_processor_force_fail_on_error_test"; |
| 68 | + Configuration config = configurationFactory.getConfig(getFile(BASE_DIR + "config.yaml")); |
| 69 | + PJsonObject requestData = loadJsonRequestData(); |
| 70 | + final AbstractJasperReportOutputFormat format = |
| 71 | + (AbstractJasperReportOutputFormat) this.outputFormat.get("pngOutputFormat"); |
| 72 | + |
| 73 | + // .wms will fail once |
| 74 | + requestFactory.registerHandler( |
| 75 | + input -> (input.getAuthority() != null && input.getAuthority().contains(host + ".wms")), |
| 76 | + createFailingFileHandler(uri -> "/map-data/tiger-ny.png")); |
| 77 | + requestFactory.registerHandler( |
| 78 | + input -> (input.getAuthority() != null && input.getAuthority().contains(host + ".wmts")), |
| 79 | + createFileHandler(uri -> "/map-data/tiger-ny.png")); |
| 80 | + testPrint(config, requestData, format, true); |
| 81 | + config.setForceFailOnError(false); |
| 82 | + testPrint(config, requestData, format, false); |
| 83 | + |
| 84 | + // .wmts will fail once and create a PrintException error |
| 85 | + config.setForceFailOnError(true); |
| 86 | + requestFactory.resetHandlers(); |
| 87 | + hasFailOnce.set(false); |
| 88 | + requestFactory.registerHandler( |
| 89 | + input -> (input.getAuthority() != null && input.getAuthority().contains(host + ".wms")), |
| 90 | + createFileHandler(uri -> "/map-data/tiger-ny.png")); |
| 91 | + requestFactory.registerHandler( |
| 92 | + input -> (input.getAuthority() != null && input.getAuthority().contains(host + ".wmts")), |
| 93 | + createFailingFileHandler(uri -> "/map-data/tiger-ny.png")); |
| 94 | + testPrint(config, requestData, format, true); |
| 95 | + config.setForceFailOnError(false); |
| 96 | + testPrint(config, requestData, format, false); |
| 97 | + } |
| 98 | + |
| 99 | + private void testPrint( |
| 100 | + Configuration config, |
| 101 | + PJsonObject requestData, |
| 102 | + AbstractJasperReportOutputFormat format, |
| 103 | + boolean shouldFail) { |
| 104 | + try { |
| 105 | + format.getJasperPrint( |
| 106 | + new HashMap<>(), requestData, config, config.getDirectory(), getTaskDirectory()); |
| 107 | + if (shouldFail) { |
| 108 | + Assert.fail("Generation was not canceled"); |
| 109 | + } |
| 110 | + } catch (Exception e) { |
| 111 | + if (!shouldFail) { |
| 112 | + Assert.fail("Generation was canceled"); |
| 113 | + } |
| 114 | + // WMS interrupt cause is CancellationException or RuntimeException |
| 115 | + // WMTS interrupt cause is PrintException |
| 116 | + Assert.assertTrue( |
| 117 | + String.format( |
| 118 | + "Exception cause should be CancellationException or PrintException with message that" |
| 119 | + + " contains 'Failed to compute Coverage Task' or RuntimeException with message" |
| 120 | + + " that contains 'Request Timeout' but was %s, exception : %s", |
| 121 | + e.getCause(), e), |
| 122 | + e.getCause() instanceof CancellationException |
| 123 | + || (e.getCause() instanceof PrintException |
| 124 | + && e.getMessage().contains("Failed to compute Coverage Task")) |
| 125 | + || (e.getCause() instanceof RuntimeException |
| 126 | + && e.getMessage().contains("Request Timeout"))); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments