-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accept i18n errors #684
Merged
Merged
Accept i18n errors #684
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
48eb885
WIP Provide i18n form errors
lauraluiz 25429f4
Fix test
lauraluiz d945212
Merge branch 'pre-1.0.0' into i18n-errors
lauraluiz 982159c
Change to messages file to add more cases
lauraluiz b34ea1d
Test error formatter
lauraluiz 88127e7
Support hash args
lauraluiz 778d598
Fix test
lauraluiz 56e72e5
Remove unused error key
lauraluiz 2eec280
Change hashArgs to a more neutral name
lauraluiz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,22 +2,19 @@ | |
|
||
import com.commercetools.sunrise.common.forms.ErrorBean; | ||
import com.commercetools.sunrise.common.forms.ErrorsBean; | ||
import com.commercetools.sunrise.common.utils.ErrorFormatter; | ||
import com.google.common.collect.Maps; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
import play.data.Form; | ||
import play.data.validation.ValidationError; | ||
|
||
import java.util.*; | ||
import java.util.function.Predicate; | ||
|
||
import static java.util.Collections.singletonList; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class PlayJavaFormResolverTest { | ||
|
||
PlayJavaFormResolver formResolver = new PlayJavaFormResolver(singletonList(Locale.ENGLISH), (locales, message) -> | ||
PlayJavaFormResolver formResolver = new PlayJavaFormResolver(singletonList(Locale.ENGLISH), (locales, message, args) -> | ||
message); | ||
|
||
@Test | ||
|
@@ -35,7 +32,7 @@ public void extractErrors() throws Exception { | |
|
||
private void checkError(ErrorBean error, String field, String key, String message) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't check it in the IDE, but to me it looks the |
||
assertThat(error.getField()).isEqualTo(field); | ||
assertThat(error.getMessage()).isEqualTo(message + ": " + key); | ||
assertThat(error.getMessage()).isEqualTo(message); | ||
|
||
} | ||
|
||
|
74 changes: 74 additions & 0 deletions
74
common/test/com/commercetools/sunrise/common/utils/ErrorFormatterImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.commercetools.sunrise.common.utils; | ||
|
||
import com.commercetools.sunrise.common.template.i18n.I18nIdentifier; | ||
import com.commercetools.sunrise.common.template.i18n.I18nIdentifierFactory; | ||
import com.commercetools.sunrise.common.template.i18n.I18nResolver; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Spy; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static java.util.Collections.*; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.*; | ||
import static org.mockito.Mockito.when; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class ErrorFormatterImplTest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the new tests 👍 |
||
|
||
private static final List<Locale> LOCALES = singletonList(Locale.ENGLISH); | ||
private static final String MESSAGE_KEY = "error.required"; | ||
private static final I18nIdentifier I18N_IDENTIFIER = I18nIdentifier.of("main", MESSAGE_KEY); | ||
private static final Map<String, Object> ARGS_WITH_FIELD = singletonMap("field", "username"); | ||
|
||
@Mock | ||
private I18nResolver i18nResolver; | ||
@Spy | ||
private I18nIdentifierFactory i18nIdentifierFactory; | ||
|
||
@InjectMocks | ||
private ErrorFormatterImpl errorFormatter; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
when(i18nResolver.getOrKey(any(), any(), any())).thenCallRealMethod(); | ||
} | ||
|
||
@Test | ||
public void translatesMessageKey() throws Exception { | ||
mockI18nResolverWithError(); | ||
final String errorMessage = errorFormatter.format(LOCALES, MESSAGE_KEY, emptyMap()); | ||
assertThat(errorMessage).isEqualTo("Required field"); | ||
} | ||
|
||
@Test | ||
public void returnsMessageKeyWhenNoMatch() throws Exception { | ||
mockI18nResolverWithoutError(); | ||
final String errorMessage = errorFormatter.format(LOCALES, MESSAGE_KEY, emptyMap()); | ||
assertThat(errorMessage).isEqualTo(MESSAGE_KEY); | ||
} | ||
|
||
@Test | ||
public void returnsMessageKeyWithFieldIfProvided() throws Exception { | ||
mockI18nResolverWithError(); | ||
final String errorMessage = errorFormatter.format(LOCALES, MESSAGE_KEY, ARGS_WITH_FIELD); | ||
assertThat(errorMessage).isEqualTo("Required field: username"); | ||
} | ||
|
||
private void mockI18nResolverWithoutError() { | ||
when(i18nResolver.get(any(), eq(I18N_IDENTIFIER), anyMap())).thenReturn(Optional.empty()); | ||
} | ||
|
||
private void mockI18nResolverWithError() { | ||
when(i18nResolver.get(any(), eq(I18N_IDENTIFIER), anyMap())).thenReturn(Optional.of("Required field")); | ||
when(i18nResolver.get(any(), eq(I18N_IDENTIFIER), eq(singletonMap("field", "username")))).thenReturn(Optional.of("Required field: username")); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
common/test/com/commercetools/sunrise/common/utils/ErrorFormatterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.commercetools.sunrise.common.utils; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
import play.data.validation.ValidationError; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
|
||
import static java.util.Arrays.asList; | ||
import static java.util.Collections.*; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class ErrorFormatterTest { | ||
|
||
private static final List<Locale> LOCALES = singletonList(Locale.ENGLISH); | ||
private static final String MESSAGE_KEY = "error.required"; | ||
private static final String SOME_ERROR_MESSAGE = "errorMessage"; | ||
|
||
@Mock | ||
private ErrorFormatter errorFormatter; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
when(errorFormatter.format(any(), any(), any())).thenReturn(SOME_ERROR_MESSAGE); | ||
when(errorFormatter.format(any(), any())).thenCallRealMethod(); | ||
} | ||
|
||
@Test | ||
public void transformsToMessage() throws Exception { | ||
final ValidationError validationError = new ValidationError("", MESSAGE_KEY); | ||
test(validationError, errorFormatter -> | ||
verify(errorFormatter).format(LOCALES, MESSAGE_KEY, emptyMap())); | ||
} | ||
|
||
@Test | ||
public void transformsToMessageWithField() throws Exception { | ||
final ValidationError validationError = new ValidationError("username", MESSAGE_KEY); | ||
test(validationError, errorFormatter -> | ||
verify(errorFormatter).format(LOCALES, MESSAGE_KEY, singletonMap("field", "username"))); | ||
} | ||
|
||
@Test | ||
public void transformsToMessageWithArgs() throws Exception { | ||
final ValidationError validationError = new ValidationError("", MESSAGE_KEY, asList("arg1", "arg2", "arg3")); | ||
test(validationError, errorFormatter -> { | ||
final Map<String, Object> hashArgs = new HashMap<>(); | ||
hashArgs.put("0", "arg1"); | ||
hashArgs.put("1", "arg2"); | ||
hashArgs.put("2", "arg3"); | ||
verify(errorFormatter).format(LOCALES, MESSAGE_KEY, hashArgs); | ||
}); | ||
} | ||
|
||
@Test | ||
public void transformsToMessageWithFieldAndArgs() throws Exception { | ||
final ValidationError validationError = new ValidationError("username", MESSAGE_KEY, asList("arg1", "arg2", "arg3")); | ||
test(validationError, errorFormatter -> { | ||
final Map<String, Object> hashArgs = new HashMap<>(); | ||
hashArgs.put("field", "username"); | ||
hashArgs.put("0", "arg1"); | ||
hashArgs.put("1", "arg2"); | ||
hashArgs.put("2", "arg3"); | ||
verify(errorFormatter).format(LOCALES, MESSAGE_KEY, hashArgs); | ||
}); | ||
} | ||
|
||
private void test(final ValidationError validationError, final Consumer<ErrorFormatter> test) { | ||
final String errorMessage = errorFormatter.format(LOCALES, validationError); | ||
assertThat(errorMessage).isEqualTo(SOME_ERROR_MESSAGE); | ||
test.accept(errorFormatter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defaultError: Something went wrong, please try again | ||
checkout: | ||
payment: | ||
invalidPayment: Invalid payment | ||
addresses: | ||
invalidShippingCountry: Invalid shipping country | ||
invalidBillingCountry: Invalid billing country | ||
myAccount: | ||
logIn: | ||
invalidCredentials: Invalid credentials | ||
signUp: | ||
duplicatedEmail: A user with this email already exists | ||
notMatchingPasswords: Not matching passwords | ||
agreeToTerms: You must agree to terms | ||
recoverPassword: | ||
emailSent: A message with further instructions has been sent to your email address | ||
emailNotFound: Email not found | ||
emailNotDelivered: Email could not be delivered | ||
resetPassword: | ||
invalidToken: Reset token is not valid | ||
addressBook: | ||
invalidCountry: Invalid country | ||
confirmation: | ||
agreeToTerms: You must agree to terms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defaultError: Something went wrong, please try again | ||
checkout: | ||
payment: | ||
invalidPayment: Invalid payment | ||
addresses: | ||
invalidShippingCountry: Invalid shipping country | ||
invalidBillingCountry: Invalid billing country | ||
myAccount: | ||
logIn: | ||
invalidCredentials: Invalid credentials | ||
signUp: | ||
duplicatedEmail: A user with this email already exists | ||
notMatchingPasswords: Not matching passwords | ||
agreeToTerms: You must agree to terms | ||
recoverPassword: | ||
emailSent: A message with further instructions has been sent to your email address | ||
emailNotFound: Email not found | ||
emailNotDelivered: Email could not be delivered | ||
resetPassword: | ||
invalidToken: Reset token is not valid | ||
addressBook: | ||
invalidCountry: Invalid country | ||
confirmation: | ||
agreeToTerms: You must agree to terms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer the parameter name
args
here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I named them
hashArgs
here and inI18nResolver
because they are hash arguments. What don't you like in particular from the name?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the type of
hashArgs
is justMap
, I was wondering how useful it is to mentionhash
in the parameter name.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!