Search in sources :

Example 1 with ThymeleafReactiveView

use of org.thymeleaf.spring5.view.reactive.ThymeleafReactiveView in project thymeleaf-tests by thymeleaf.

the class AbstractSpring5ReactiveTest method createThymeleafReactiveView.

private static ThymeleafReactiveView createThymeleafReactiveView(final ISpringWebFluxTemplateEngine templateEngine, final String viewName, final Set<String> markupSelectors, final Locale locale, final int responseMaxChunkSizeBytes) {
    if (markupSelectors != null && markupSelectors.size() > 1) {
        throw new RuntimeException("Cannot execute SpringView-based test with more than 1 markup selector");
    }
    final ThymeleafReactiveView view = new ThymeleafReactiveView();
    view.setTemplateName(viewName);
    if (markupSelectors != null) {
        view.setMarkupSelector(markupSelectors.iterator().next());
    }
    view.setResponseMaxChunkSizeBytes(responseMaxChunkSizeBytes);
    try {
        thymeleafReactiveViewTemplateEngineSetter.setAccessible(true);
        thymeleafReactiveViewTemplateEngineSetter.invoke(view, templateEngine);
        thymeleafReactiveViewTemplateEngineSetter.setAccessible(false);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
    try {
        thymeleafReactiveViewLocaleSetter.setAccessible(true);
        thymeleafReactiveViewLocaleSetter.invoke(view, locale);
        thymeleafReactiveViewLocaleSetter.setAccessible(false);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
    view.setApplicationContext(applicationContext);
    return view;
}
Also used : ThymeleafReactiveView(org.thymeleaf.spring5.view.reactive.ThymeleafReactiveView) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException)

Example 2 with ThymeleafReactiveView

use of org.thymeleaf.spring5.view.reactive.ThymeleafReactiveView in project thymeleaf-tests by thymeleaf.

the class AbstractSpring5ReactiveTest method testTemplateSpringView.

private static void testTemplateSpringView(final String template, final Set<String> markupSelectors, final IContext context, final String result, final boolean sse, final int responseMaxChunkSizeBytes) throws Exception {
    if (context instanceof ISpringWebFluxContext) {
        // we can influence when directly instantiating the ThymeleafReactiveView. So we'll just skip
        return;
    }
    final String dataDriverVariableName = detectDataDriver(context);
    final boolean isDataDriven = dataDriverVariableName != null;
    final ThymeleafReactiveView thymeleafReactiveView = createThymeleafReactiveView(templateEngine, template, markupSelectors, Locale.US, responseMaxChunkSizeBytes);
    final Map<String, Object> model = new HashMap<String, Object>();
    for (final String variableName : context.getVariableNames()) {
        model.put(variableName, context.getVariable(variableName));
    }
    final ServerWebExchange serverWebExchange = new TestingServerWebExchange("testing", Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap());
    List<DataBuffer> resultBuffers = null;
    try {
        final Mono<ServerHttpResponse> responseStream = thymeleafReactiveView.render(model, (sse ? sseMediaType : htmlMediaType), serverWebExchange).then(Mono.just(serverWebExchange.getResponse()));
        final ServerHttpResponse response = responseStream.block();
        resultBuffers = ((TestingServerHttpResponse) response).getWrittenOutput();
    } catch (final Exception e) {
        throw new TemplateProcessingException("Error happened while executing reactive test for template " + template + " with markup " + "selectors " + markupSelectors + ", context with variables " + context.getVariableNames() + " and " + "response chunk size of " + responseMaxChunkSizeBytes + " bytes.", e);
    }
    if (responseMaxChunkSizeBytes != Integer.MAX_VALUE) {
        for (final DataBuffer resultBuffer : resultBuffers) {
            Assert.assertTrue("Buffer returned by stream is of size larger than " + responseMaxChunkSizeBytes, resultBuffer.readableByteCount() <= responseMaxChunkSizeBytes);
        }
    } else {
        if (!isDataDriven) {
            final int bufferCount = resultBuffers.size();
            Assert.assertTrue("No limit set on buffer size, and non-data-driven: there should only be one result buffer instead of " + bufferCount, bufferCount == 1);
        }
    }
    final String resultStr = resultBuffers.stream().map((buffer) -> ReactiveTestUtils.bufferAsString(buffer, charset)).map(// Note we NORMALIZE before joining it all
    ReactiveTestUtils::normalizeResult).collect(Collectors.joining());
    final String expected = ReactiveTestUtils.readExpectedNormalizedResults(result, charset);
    Assert.assertEquals(expected, resultStr);
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) TestingServerWebExchange(org.thymeleaf.spring5.reactive.exchange.TestingServerWebExchange) HashMap(java.util.HashMap) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) TestingServerHttpResponse(org.thymeleaf.spring5.reactive.exchange.TestingServerHttpResponse) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) ThymeleafReactiveView(org.thymeleaf.spring5.view.reactive.ThymeleafReactiveView) TestingServerWebExchange(org.thymeleaf.spring5.reactive.exchange.TestingServerWebExchange) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) ISpringWebFluxContext(org.thymeleaf.spring5.context.webflux.ISpringWebFluxContext) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Aggregations

TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)2 ThymeleafReactiveView (org.thymeleaf.spring5.view.reactive.ThymeleafReactiveView)2 HashMap (java.util.HashMap)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)1 ServerWebExchange (org.springframework.web.server.ServerWebExchange)1 ISpringWebFluxContext (org.thymeleaf.spring5.context.webflux.ISpringWebFluxContext)1 TestingServerHttpResponse (org.thymeleaf.spring5.reactive.exchange.TestingServerHttpResponse)1 TestingServerWebExchange (org.thymeleaf.spring5.reactive.exchange.TestingServerWebExchange)1