Search in sources :

Example 1 with Result

use of ninja.Result in project ninja by ninjaframework.

the class DiagnosticErrorRendererTest method buildAndRenderDiagnosticErrorWithHTMLEntities.

@Test
public void buildAndRenderDiagnosticErrorWithHTMLEntities() throws Exception {
    NinjaDefault ninja = Mockito.spy(new NinjaDefault());
    doReturn(Boolean.TRUE).when(ninja).isDiagnosticsEnabled();
    // build context that will have HTML entites in key spots
    when(context.getMethod()).thenReturn("GET");
    when(context.getContextPath()).thenReturn("/");
    when(context.getAttributes()).thenReturn(ImmutableMap.of("TEST-ATTR", (Object) "Attribute with < > & entities"));
    Result testResult = ninja.getInternalServerErrorResult(context, new Exception("Exception message with < > & entities"), null);
    // renderable in testResult is the DiagnosticError
    Assert.assertNotNull(testResult);
    Assert.assertNotNull(testResult.getRenderable());
    Assert.assertThat(testResult.getRenderable(), instanceOf(DiagnosticError.class));
    DiagnosticError de = (DiagnosticError) testResult.getRenderable();
    DiagnosticErrorRenderer renderer = DiagnosticErrorRenderer.build(context, testResult, de);
    String out = renderer.render();
    Assert.assertThat(out, containsString("Attribute with &lt; &gt; &amp; entities"));
    Assert.assertThat(out, containsString("Exception message with &lt; &gt; &amp; entities"));
}
Also used : NinjaDefault(ninja.NinjaDefault) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Result(ninja.Result) Test(org.junit.Test)

Example 2 with Result

use of ninja.Result in project ninja by ninjaframework.

the class DiagnosticErrorRendererTest method buildAndRender401UnauthorizedDiagnosticError.

@Test
public void buildAndRender401UnauthorizedDiagnosticError() throws Exception {
    when(context.getMethod()).thenReturn("GET");
    when(context.getContextPath()).thenReturn("/");
    Result testResult = Results.unauthorized();
    DiagnosticError de = DiagnosticErrorBuilder.build401UnauthorizedDiagnosticError();
    Assert.assertEquals(-1, de.getLineNumberOfError());
    Assert.assertEquals(-1, de.getLineNumberOfSourceLines());
    Assert.assertNull(de.getThrowable());
    DiagnosticErrorRenderer renderer = DiagnosticErrorRenderer.build(context, testResult, de);
    String out = renderer.render();
    Assert.assertTrue(out.contains("Diagnostic Error"));
    Assert.assertTrue(out.contains("401"));
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Result(ninja.Result) Test(org.junit.Test)

Example 3 with Result

use of ninja.Result in project ninja by ninjaframework.

the class LangImplTest method testGetLanguage.

@Test
public void testGetLanguage() {
    Cookie cookie = Cookie.builder("NINJA_TEST" + NinjaConstant.LANG_COOKIE_SUFFIX, "de").build();
    when(ninjaProperties.getOrDie(NinjaConstant.applicationCookiePrefix)).thenReturn("NINJA_TEST");
    when(context.getCookie("NINJA_TEST" + NinjaConstant.LANG_COOKIE_SUFFIX)).thenReturn(cookie);
    Lang lang = new LangImpl(ninjaProperties);
    // 1) with context and result => but result does not have a default lang
    Result result = Results.ok();
    Optional<String> language = lang.getLanguage(context, Optional.of(result));
    assertEquals("de", language.get());
    // 2) with context and result => result has already new lang set...
    result = Results.ok();
    cookie = Cookie.builder("NINJA_TEST" + NinjaConstant.LANG_COOKIE_SUFFIX, "en").build();
    result.addCookie(cookie);
    language = lang.getLanguage(context, Optional.of(result));
    assertEquals("en", language.get());
}
Also used : Cookie(ninja.Cookie) Result(ninja.Result) Test(org.junit.Test)

Example 4 with Result

use of ninja.Result in project ninja by ninjaframework.

the class LangImplTest method testChangeLanguage.

@Test
public void testChangeLanguage() {
    Cookie cookie = Cookie.builder("NINJA_TEST" + NinjaConstant.LANG_COOKIE_SUFFIX, "de").build();
    when(ninjaProperties.getOrDie(NinjaConstant.applicationCookiePrefix)).thenReturn("NINJA_TEST");
    Lang lang = new LangImpl(ninjaProperties);
    // test with result
    Result result = Results.noContent();
    result = lang.setLanguage("to", result);
    assertEquals("to", result.getCookie(cookie.getName()).getValue());
    assertEquals(Result.SC_204_NO_CONTENT, result.getStatusCode());
}
Also used : Cookie(ninja.Cookie) Result(ninja.Result) Test(org.junit.Test)

Example 5 with Result

use of ninja.Result in project ninja by ninjaframework.

the class TemplateEngineFreemarkerI18nMethodTest method testThatKeyWithPlaceholderReturnsDefaultKeyWhenKeyCannotBeFound.

@Test
public void testThatKeyWithPlaceholderReturnsDefaultKeyWhenKeyCannotBeFound() throws Exception {
    Optional<Result> resultOptional = Optional.of(result);
    List args = new ArrayList();
    args.add(new SimpleScalar("my.message.key"));
    args.add(new SimpleScalar("1000"));
    Mockito.when(messages.get(Matchers.eq("my.message.key"), Matchers.eq(context), Matchers.eq(resultOptional), Matchers.any(Object.class))).thenReturn(Optional.<String>empty());
    TemplateModel returnValue = templateEngineFreemarkerI18nMethod.exec(args);
    assertThat(((SimpleScalar) returnValue).getAsString(), CoreMatchers.equalTo("my.message.key"));
    // There must have been logged something because we did not find
    // the value for the key...
    Mockito.verify(mockAppender).doAppend(Matchers.anyObject());
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TemplateModel(freemarker.template.TemplateModel) SimpleScalar(freemarker.template.SimpleScalar) Result(ninja.Result) Test(org.junit.Test)

Aggregations

Result (ninja.Result)37 Test (org.junit.Test)26 TemplateModel (freemarker.template.TemplateModel)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Cookie (ninja.Cookie)6 SimpleScalar (freemarker.template.SimpleScalar)4 Context (ninja.Context)3 Timed (ninja.metrics.Timed)3 ResponseStreams (ninja.utils.ResponseStreams)3 BeansWrapper (freemarker.ext.beans.BeansWrapper)2 StringModel (freemarker.ext.beans.StringModel)2 Date (java.util.Date)2 ConstraintViolation (ninja.validation.ConstraintViolation)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Before (org.junit.Before)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1