Search in sources :

Example 26 with Result

use of ninja.Result in project ninja by ninjaframework.

the class TemplateEngineFreemarkerI18nMethodTest method testThatKeyWithPlaceholderWorks.

@Test
public void testThatKeyWithPlaceholderWorks() 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.of("This simulates the translated message number 1000!"));
    TemplateModel returnValue = templateEngineFreemarkerI18nMethod.exec(args);
    assertThat(((SimpleScalar) returnValue).getAsString(), CoreMatchers.equalTo("This simulates the translated message number 1000!"));
    Mockito.verify(mockAppender, Mockito.never()).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)

Example 27 with Result

use of ninja.Result in project ninja by ninjaframework.

the class TemplateEngineFreemarkerI18nMethodTest method testThatSingleKeyWithMissingValueReturnsDefaultKey.

@Test
public void testThatSingleKeyWithMissingValueReturnsDefaultKey() throws Exception {
    Optional<Result> resultOptional = Optional.of(result);
    Mockito.when(messages.get("my.message.key", context, resultOptional)).thenReturn(Optional.<String>empty());
    List args = new ArrayList();
    args.add(new SimpleScalar("my.message.key"));
    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)

Example 28 with Result

use of ninja.Result in project ninja by ninjaframework.

the class TemplateEngineFreemarkerI18nMethodTest method testThatConstraintViolationWorksWithDefault.

@Test
public void testThatConstraintViolationWorksWithDefault() throws Exception {
    Optional<Result> resultOptional = Optional.of(result);
    Mockito.when(messages.get(IsInteger.KEY, context, resultOptional)).thenReturn(Optional.empty());
    ConstraintViolation violation = new ConstraintViolation(IsInteger.KEY, "theField", IsInteger.MESSAGE);
    List args = new ArrayList();
    args.add(new StringModel(violation, new BeansWrapper()));
    TemplateModel returnValue = templateEngineFreemarkerI18nMethod.exec(args);
    assertThat(((SimpleScalar) returnValue).getAsString(), CoreMatchers.equalTo("theField must be an integer"));
    Mockito.verify(mockAppender, Mockito.never()).doAppend(Matchers.anyObject());
}
Also used : ConstraintViolation(ninja.validation.ConstraintViolation) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) StringModel(freemarker.ext.beans.StringModel) BeansWrapper(freemarker.ext.beans.BeansWrapper) TemplateModel(freemarker.template.TemplateModel) Result(ninja.Result) Test(org.junit.Test)

Example 29 with Result

use of ninja.Result in project ninja by ninjaframework.

the class TemplateEngineFreemarkerI18nMethodTest method testThatConstraintViolationWorks.

@Test
public void testThatConstraintViolationWorks() throws Exception {
    Optional<Result> resultOptional = Optional.of(result);
    Mockito.when(messages.get(IsInteger.KEY, context, resultOptional)).thenReturn(Optional.of("This simulates the translated message!"));
    ConstraintViolation violation = new ConstraintViolation(IsInteger.KEY, "theField", IsInteger.MESSAGE);
    List args = new ArrayList();
    args.add(new StringModel(violation, new BeansWrapper()));
    TemplateModel returnValue = templateEngineFreemarkerI18nMethod.exec(args);
    assertThat(((SimpleScalar) returnValue).getAsString(), CoreMatchers.equalTo("This simulates the translated message!"));
    Mockito.verify(mockAppender, Mockito.never()).doAppend(Matchers.anyObject());
}
Also used : ConstraintViolation(ninja.validation.ConstraintViolation) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) StringModel(freemarker.ext.beans.StringModel) BeansWrapper(freemarker.ext.beans.BeansWrapper) TemplateModel(freemarker.template.TemplateModel) Result(ninja.Result) Test(org.junit.Test)

Example 30 with Result

use of ninja.Result in project ninja by ninjaframework.

the class LangImplTest method testClearLanguage.

@Test
public void testClearLanguage() {
    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);
    Result result = Results.ok();
    lang.clearLanguage(result);
    Cookie returnCookie = result.getCookie(cookie.getName());
    assertEquals("", returnCookie.getValue());
    assertEquals(0, returnCookie.getMaxAge());
}
Also used : Cookie(ninja.Cookie) 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