import java.util.Date;
import java.util.Locale;
+import java.util.MissingResourceException;
import org.sonar.api.platform.Server;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
public class IndexAction implements WsAction {
private static final String LOCALE_PARAM = "locale";
+ private static final String INVALID_LANGUAGE_TAG_MESSAGE = "Locale cannot be parsed as a BCP47 language tag";
private static final String TS_PARAM = "ts";
private final DefaultI18n i18n;
private final Server server;
}
String localeParam = request.mandatoryParam(LOCALE_PARAM);
Locale locale = Locale.forLanguageTag(localeParam);
- checkArgument(!locale.getISO3Language().isEmpty(), "Locale cannot be parsed as a BCP47 language tag");
+ try {
+ checkArgument(!locale.getISO3Language().isEmpty(), INVALID_LANGUAGE_TAG_MESSAGE);
+ } catch (MissingResourceException e) {
+ throw new IllegalArgumentException(INVALID_LANGUAGE_TAG_MESSAGE, e);
+ }
try (JsonWriter json = response.newJsonWriter()) {
json.beginObject();
*/
package org.sonar.server.platform.ws;
-import com.google.common.collect.ImmutableSet;
import java.net.HttpURLConnection;
import java.util.Date;
+import java.util.Set;
import javax.annotation.Nullable;
import org.junit.Test;
import org.sonar.api.platform.Server;
import static org.sonar.test.JsonAssert.assertJson;
public class IndexActionTest {
-
private static final String KEY_1 = "key1";
private static final String KEY_2 = "key2";
private static final String KEY_3 = "key3";
+ private final DefaultI18n i18n = mock(DefaultI18n.class);
+ private final Server server = mock(Server.class);
- private DefaultI18n i18n = mock(DefaultI18n.class);
- private Server server = mock(Server.class);
-
- private IndexAction underTest = new IndexAction(i18n, server);
+ private final IndexAction underTest = new IndexAction(i18n, server);
- private WsActionTester ws = new WsActionTester(underTest);
+ private final WsActionTester ws = new WsActionTester(underTest);
@Test
- public void allow_client_to_cache_messages() {
+ public void execute_shouldAllowClientToCacheMessages() {
Date now = new Date();
Date aBitLater = new Date(now.getTime() + 1000);
when(server.getStartedAt()).thenReturn(now);
}
@Test
- public void return_all_l10n_messages_using_accept_header_with_cache_expired() {
+ public void execute_shouldReturnAllL10nMessages_whenUsingAcceptHeaderWithCacheExpired() {
Date now = new Date();
Date aBitEarlier = new Date(now.getTime() - 1000);
when(server.getStartedAt()).thenReturn(now);
- when(i18n.getPropertyKeys()).thenReturn(ImmutableSet.of(KEY_1, KEY_2, KEY_3));
+ when(i18n.getPropertyKeys()).thenReturn(Set.of(KEY_1, KEY_2, KEY_3));
when(i18n.message(PRC, KEY_1, KEY_1)).thenReturn(KEY_1);
when(i18n.message(PRC, KEY_2, KEY_2)).thenReturn(KEY_2);
when(i18n.message(PRC, KEY_3, KEY_3)).thenReturn(KEY_3);
}
@Test
- public void default_locale_is_english() {
+ public void execute_shouldReturnEnglishMessages_whenDefaultLocaleProvided() {
String key1 = "key1";
String key2 = "key2";
String key3 = "key3";
- when(i18n.getPropertyKeys()).thenReturn(ImmutableSet.of(key1, key2, key3));
+ when(i18n.getPropertyKeys()).thenReturn(Set.of(key1, key2, key3));
when(i18n.message(ENGLISH, key1, key1)).thenReturn(key1);
when(i18n.message(ENGLISH, key2, key2)).thenReturn(key2);
when(i18n.message(ENGLISH, key3, key3)).thenReturn(key3);
}
@Test
- public void support_BCP47_formatted_language_tags() {
+ public void execute_shouldReturnMessages_whenProvidedSupportedBCP47FormattedLanguageTags() {
String key1 = "key1";
- when(i18n.getPropertyKeys()).thenReturn(ImmutableSet.of(key1));
+ when(i18n.getPropertyKeys()).thenReturn(Set.of(key1));
when(i18n.message(UK, key1, key1)).thenReturn(key1);
when(i18n.getEffectiveLocale(UK)).thenReturn(UK);
}
@Test
- public void fail_when_java_formatted_language_tags() {
+ public void execute_shouldFail_whenJavaFormattedLanguageTags() {
String key1 = "key1";
- when(i18n.getPropertyKeys()).thenReturn(ImmutableSet.of(key1));
+ when(i18n.getPropertyKeys()).thenReturn(Set.of(key1));
when(i18n.message(UK, key1, key1)).thenReturn(key1);
when(i18n.getEffectiveLocale(UK)).thenReturn(UK);
.hasMessageContaining("Locale cannot be parsed as a BCP47 language tag");
}
+ @Test
+ public void execute_shouldFail_whenUnknownBCP47Tag() {
+ String key1 = "key1";
+ when(i18n.getPropertyKeys()).thenReturn(Set.of(key1));
+ when(i18n.message(UK, key1, key1)).thenReturn(key1);
+ when(i18n.getEffectiveLocale(UK)).thenReturn(UK);
+
+ assertThatThrownBy(() -> call("ABCD", null))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("Locale cannot be parsed as a BCP47 language tag");
+ }
+
private TestResponse call(@Nullable String locale, @Nullable String timestamp) {
TestRequest request = ws.newRequest();
if (locale != null) {