LangImpl implements Lang { @Override public Optional<String> getLanguage(Context context, Optional<Result> result) { Cookie defaultCookie = generateNinjaLanguageCookie(); if (result.isPresent()) { Cookie cookie = result.get().getCookie(defaultCookie.getName()); if (cookie != null) { if (cookie.getValue() != null && !cookie.getValue().isEmpty()) { return Optional.of(cookie.getValue()); } } } Cookie cookie = context.getCookie(defaultCookie.getName()); if (cookie != null) { if (cookie.getValue() != null && !cookie.getValue().isEmpty()) { return Optional.of(cookie.getValue()); } } String acceptLanguage = context.getAcceptLanguage(); if (acceptLanguage == null) { return Optional.empty(); } Iterable<String> languages = Splitter.on(",").trimResults().split(acceptLanguage); for (String language: languages){ if (language.contains(";")){ language = language.split(";")[0]; return Optional.of(language); } else { return Optional.of(language); } } return Optional.empty(); } @Inject  LangImpl(NinjaProperties ninjaProperties); @Override void clearLanguage(Result result); @Override Result setLanguage(String locale, Result result); @Override Optional<String> getLanguage(Context context, Optional<Result> result); @Override boolean isLanguageDirectlySupportedByThisApplication(String language); @Override Locale getLocaleFromStringOrDefault(Optional<String> language);  }
@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); Result result = Results.ok(); Optional<String> language = lang.getLanguage(context, Optional.of(result)); assertEquals("de", language.get()); 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()); }