package io.dropwizard.views.mustache; import com.codahale.metrics.MetricRegistry; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import io.dropwizard.logging.BootstrapLogging; import io.dropwizard.views.ViewMessageBodyWriter; import io.dropwizard.views.ViewRenderExceptionMapper; import io.dropwizard.views.ViewRenderer; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; import org.glassfish.jersey.test.TestProperties; import org.junit.Test; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Application; import javax.ws.rs.core.MediaType; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown; /** * Test class for {@link MustacheViewRenderer} configured to load Mustache * templates from the file system. * * @since 1.1.0 */ public class MustacheViewRendererFileSystemTest extends JerseyTest { static { BootstrapLogging.bootstrap(); } @Path("/test/") @Produces(MediaType.TEXT_HTML) public static class ExampleResource { @GET @Path("/absolute") public AbsoluteView showAbsolute() { return new AbsoluteView("yay"); } @GET @Path("/relative") public RelativeView showRelative() { return new RelativeView(); } @GET @Path("/bad") public BadView showBad() { return new BadView(); } @GET @Path("/error") public ErrorView showError() { return new ErrorView(); } } @Override protected Application configure() { forceSet(TestProperties.CONTAINER_PORT, "0"); ResourceConfig config = new ResourceConfig(); final ViewRenderer renderer = new MustacheViewRenderer(); renderer.configure(ImmutableMap.of("fileRoot", "src/test/resources")); config.register(new ViewMessageBodyWriter(new MetricRegistry(), ImmutableList.of(renderer))); config.register(new ViewRenderExceptionMapper()); config.register(new ExampleResource()); return config; } @Test public void rendersViewsWithAbsoluteTemplatePaths() throws Exception { final String response = target("/test/absolute").request().get(String.class); assertThat(response).isEqualTo("Woop woop. yay\n"); } @Test public void rendersViewsWithRelativeTemplatePaths() throws Exception { final String response = target("/test/relative").request().get(String.class); assertThat(response).isEqualTo("Ok.\n"); } @Test public void returnsA500ForViewsWithBadTemplatePaths() throws Exception { try { target("/test/bad").request().get(String.class); failBecauseExceptionWasNotThrown(WebApplicationException.class); } catch (WebApplicationException e) { assertThat(e.getResponse().getStatus()) .isEqualTo(500); assertThat(e.getResponse().readEntity(String.class)) .isEqualTo(ViewRenderExceptionMapper.TEMPLATE_ERROR_MSG); } } @Test public void returnsA500ForViewsThatCantCompile() throws Exception { try { target("/test/error").request().get(String.class); failBecauseExceptionWasNotThrown(WebApplicationException.class); } catch (WebApplicationException e) { assertThat(e.getResponse().getStatus()) .isEqualTo(500); assertThat(e.getResponse().readEntity(String.class)) .isEqualTo(ViewRenderExceptionMapper.TEMPLATE_ERROR_MSG); } } }