I added a service method call to Boostrap.groovy
in my Grails 4.0.1 application:
@GrailsCompileStatic
class BootStrap {
GrailsApplication grailsApplication
SelfAssessmentRatingService selfAssessmentRatingService
def init = { servletContext ->
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
...
SelfAssessmentRating rating = SelfAssessmentRating.first()
if (!rating) {
selfAssessmentRatingService.createRatingsFromConfig()
}
}
def destroy = {
}
}
In the service method, an i18n message is fetched for each object I have specified in setup.selfAssessmentRatings
in the application config.
@GrailsCompileStatic
@Slf4j
@Transactional
class SelfAssessmentRatingService implements GrailsConfigurationAware {
MessageSource messageSource
List<Map> setupAssessmentRatings
@Override
void setConfiguration(Config co) {
List selfAssessmentRatings = co.getProperty('setup.selfAssessmentRatings', List, null)
setupAssessmentRatings = selfAssessmentRatings as List<Map>
}
/**
* Saves a SelfAssessmentRating record for each one specified in the application config.
* This method gets called if there are no SelfAssessmentRating records saved.
*/
def createRatingsFromConfig() {
if (setupAssessmentRatings == null || setupAssessmentRatings.empty) {
log.info("Skipping creating SelfAssessmentRatings because none were specified " +
"in the config: 'setup.selfAssessmentRatings'")
return
}
log.info("Saving ${setupAssessmentRatings.size()} new SelfAssessmentRating records")
for (Map map in setupAssessmentRatings) {
// SelfAssessmentRating is a domain class
SelfAssessmentRating newRating = new SelfAssessmentRating()
newRating.rating = map.rating as Integer
newRating.englishText = map.englishText
newRating.translationKey = map.translationKey
newRating.save(failOnError: true)
// Verify we have that translation. This will throw an exception if the translation is not present.
messageSource.getMessage(newRating.translationKey, [].toArray(), Locale.default)
}
}
}
Here is the default config value for setup.selfAssessmentRatings
; this is in the root of application.yml
so it applies to all Grails environments:
setup.selfAssessmentRatings:
- {rating: 1, translationKey: "example.selfAssessment.noExperience", englishText: "No Experience or Knowledge"}
- {rating: 2, translationKey: "example.selfAssessment.someExperience", englishText: "Some Experience or Knowledge"}
- {rating: 3, translationKey: "example.selfAssessment.functionIndependently", englishText: "Able to function independently in this area"}
And I have those three messages defined in grails-app/i18n/messages.properties
:
example.selfAssessment.noExperience=No Experience or Knowledge
example.selfAssessment.someExperience=Some Experience or Knowledge
example.selfAssessment.functionIndependently=Able to function independently in this area
My integration tests all pass in CircleCI (./gradlew -Dgrails.env=circleci integrationTest
), but on my local machine I get the following error for each integration test:
org.springframework.context.NoSuchMessageException: No message found under code 'example.selfAssessment.noExperience' for locale 'en_US'.
at org.springframework.context.support.AbstractMessageSource.getMessage(AbstractMessageSource.java:161)
at com.hclabs.ojt.step.SelfAssessmentRatingService.$tt__createRatingsFromConfig(SelfAssessmentRatingService.groovy:46)
Does anyone know how I can get my integration tests to pass again locally ?
Is there an argument I could add or an environment variable to set when calling ./gradlew integrationTest
on my local machine so that Grails can find the messages in grails-app/i18n/messages.properties
?
Aucun commentaire:
Enregistrer un commentaire