package org.mapfish.print.processor.map.scalebar;
import com.google.common.base.Predicate;
import com.google.common.io.Files;
import jsr166y.ForkJoinPool;
import org.junit.Test;
import org.mapfish.print.AbstractMapfishSpringTest;
import org.mapfish.print.TestHttpClientFactory;
import org.mapfish.print.config.Configuration;
import org.mapfish.print.config.ConfigurationFactory;
import org.mapfish.print.config.Template;
import org.mapfish.print.output.Values;
import org.mapfish.print.parser.MapfishParser;
import org.mapfish.print.test.util.ImageSimilarity;
import org.mapfish.print.wrapper.json.PJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.mock.http.client.MockClientHttpRequest;
import org.springframework.test.annotation.DirtiesContext;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* Basic test of the Scalebar processor.
*/
public class CreateScaleBarProcessorFixedScaleCenterOsmTest extends AbstractMapfishSpringTest {
public static final String BASE_DIR = "center_osm_fixedscale/";
@Autowired
private ConfigurationFactory configurationFactory;
@Autowired
private ForkJoinPool forkJoinPool;
@Autowired
private TestHttpClientFactory requestFactory;
@Autowired
private MapfishParser parser;
@Test
@DirtiesContext
public void testExecute() throws Exception {
final String host = "center_osm_fixedscale";
requestFactory.registerHandler(
new Predicate<URI>() {
@Override
public boolean apply(URI input) {
return (("" + input.getHost()).contains(host + ".osm")) || input.getAuthority().contains(host + ".osm");
}
}, new TestHttpClientFactory.Handler() {
@Override
public MockClientHttpRequest handleRequest(URI uri, HttpMethod httpMethod) throws Exception {
try {
byte[] bytes = Files.toByteArray(getFile("/map-data/osm" + uri.getPath()));
return ok(uri, bytes, httpMethod);
} catch (AssertionError e) {
return error404(uri, httpMethod);
}
}
}
);
requestFactory.registerHandler(
new Predicate<URI>() {
@Override
public boolean apply(URI input) {
return (("" + input.getHost()).contains(host + ".json")) || input.getAuthority().contains(host + ".json");
}
}, new TestHttpClientFactory.Handler() {
@Override
public MockClientHttpRequest handleRequest(URI uri, HttpMethod httpMethod) throws Exception {
try {
byte[] bytes = Files.toByteArray(getFile("/map-data" + uri.getPath()));
return ok(uri, bytes, httpMethod);
} catch (AssertionError e) {
return error404(uri, httpMethod);
}
}
}
);
final Configuration config = configurationFactory.getConfig(getFile(BASE_DIR + "config.yaml"));
final Template template = config.getTemplate("main");
PJsonObject requestData = loadJsonRequestData();
Values values = new Values(requestData, template, this.parser, getTaskDirectory(), this.requestFactory, new File("."));
this.forkJoinPool.invoke(template.getProcessorGraph().createTask(values));
@SuppressWarnings("unchecked")
List<URI> layerGraphics = (List<URI>) values.getObject("layerGraphics", List.class);
assertEquals(2, layerGraphics.size());
final BufferedImage referenceImage = ImageSimilarity.mergeImages(layerGraphics, 780, 330);
new ImageSimilarity(referenceImage, 2)
.assertSimilarity(getFile(BASE_DIR + "expectedSimpleImage.png"), 60);
String scalebarGraphic = values.getObject("scalebarGraphic", String.class);
new ImageSimilarity(new File(new URI(scalebarGraphic)), 4).assertSimilarity(getFile(BASE_DIR + "expectedScalebar.png"), 250);
assertNotNull(values.getObject("scalebarSubReport", String.class));
// now without a subreport
final Configuration config_noreport = configurationFactory.getConfig(getFile(BASE_DIR + "config-no-report.yaml"));
final Template template_noreport = config_noreport.getTemplate("main");
Values values_noreport = new Values(requestData, template_noreport, this.parser, getTaskDirectory(), this.requestFactory, new File("."));
this.forkJoinPool.invoke(template.getProcessorGraph().createTask(values_noreport));
assertNull(values_noreport.getObject("scalebarSubReport", String.class));
}
private static PJsonObject loadJsonRequestData() throws IOException {
return parseJSONObjectFromFile(CreateScaleBarProcessorFixedScaleCenterOsmTest.class, BASE_DIR + "requestData.json");
}
}