Loading time-series-view/src/test/java/es/redmic/test/timeseriesview/integration/controller/translation/ExceptionsTranslationTest.java 0 → 100644 +98 −0 Original line number Diff line number Diff line package es.redmic.test.timeseriesview.integration.controller.translation; import java.text.MessageFormat; import java.util.Locale; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.MessageSource; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import es.redmic.exception.common.PatternUtils; import es.redmic.exception.data.ItemNotFoundException; import es.redmic.timeseriesview.TimeSeriesViewApplication; @SpringBootTest(classes = { TimeSeriesViewApplication.class }) @RunWith(SpringJUnit4ClassRunner.class) @ActiveProfiles("test") public class ExceptionsTranslationTest { @Autowired MessageSource messageSource; protected static final String resourcePathSpanish = "classpath*:i18n/messages_es_ES.properties", resourcePathEnglish = "classpath*:i18n/messages_en_EN.properties"; @Test public void getEnglishMessage_returnI18nMessageInDefaultLocale_WhenCodePropertyExist() { try { throw new ItemNotFoundException("id", "2"); } catch (ItemNotFoundException e) { String code = e.getCode().toString(); String[] fields = (String[]) e.getFieldErrors().toArray(); String mess = messageSource.getMessage(code, fields, new Locale("en", "EN")); Assert.assertEquals(getMessage(code, fields, resourcePathEnglish), mess); } } @Test public void getSpanishMessage_returnI18nMessage_WhenCodePropertyExist() { try { throw new ItemNotFoundException("id", "2"); } catch (ItemNotFoundException e) { String code = e.getCode().toString(); String[] fields = (String[]) e.getFieldErrors().toArray(); String mess = messageSource.getMessage(code, fields, new Locale("es", "ES")); Assert.assertEquals(getMessage(code, fields, resourcePathSpanish), mess); } } @Test public void getRussianLanguageMessage_returnDefaultI18nMessage_WhenI18nFileNotExist() { try { throw new ItemNotFoundException("id", "2"); } catch (ItemNotFoundException e) { String code = e.getCode().toString(); String[] fields = (String[]) e.getFieldErrors().toArray(); String mess = messageSource.getMessage(code, fields, new Locale("ru", "RU")); Assert.assertEquals(getMessage(code, fields, resourcePathSpanish), mess); } } @Test public void getMessage_returnCode_WhenCodeNotInI18nFile() { String code = "CodeNotFound"; String mess = messageSource.getMessage(code, null, new Locale("es", "ES")); Assert.assertEquals(code, mess); } private String getMessage(String code, String[] fields, String resourcePath) { String message = PatternUtils.getPattern(code, resourcePath); MessageFormat format = new MessageFormat(message); return format.format(fields); } } Loading
time-series-view/src/test/java/es/redmic/test/timeseriesview/integration/controller/translation/ExceptionsTranslationTest.java 0 → 100644 +98 −0 Original line number Diff line number Diff line package es.redmic.test.timeseriesview.integration.controller.translation; import java.text.MessageFormat; import java.util.Locale; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.MessageSource; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import es.redmic.exception.common.PatternUtils; import es.redmic.exception.data.ItemNotFoundException; import es.redmic.timeseriesview.TimeSeriesViewApplication; @SpringBootTest(classes = { TimeSeriesViewApplication.class }) @RunWith(SpringJUnit4ClassRunner.class) @ActiveProfiles("test") public class ExceptionsTranslationTest { @Autowired MessageSource messageSource; protected static final String resourcePathSpanish = "classpath*:i18n/messages_es_ES.properties", resourcePathEnglish = "classpath*:i18n/messages_en_EN.properties"; @Test public void getEnglishMessage_returnI18nMessageInDefaultLocale_WhenCodePropertyExist() { try { throw new ItemNotFoundException("id", "2"); } catch (ItemNotFoundException e) { String code = e.getCode().toString(); String[] fields = (String[]) e.getFieldErrors().toArray(); String mess = messageSource.getMessage(code, fields, new Locale("en", "EN")); Assert.assertEquals(getMessage(code, fields, resourcePathEnglish), mess); } } @Test public void getSpanishMessage_returnI18nMessage_WhenCodePropertyExist() { try { throw new ItemNotFoundException("id", "2"); } catch (ItemNotFoundException e) { String code = e.getCode().toString(); String[] fields = (String[]) e.getFieldErrors().toArray(); String mess = messageSource.getMessage(code, fields, new Locale("es", "ES")); Assert.assertEquals(getMessage(code, fields, resourcePathSpanish), mess); } } @Test public void getRussianLanguageMessage_returnDefaultI18nMessage_WhenI18nFileNotExist() { try { throw new ItemNotFoundException("id", "2"); } catch (ItemNotFoundException e) { String code = e.getCode().toString(); String[] fields = (String[]) e.getFieldErrors().toArray(); String mess = messageSource.getMessage(code, fields, new Locale("ru", "RU")); Assert.assertEquals(getMessage(code, fields, resourcePathSpanish), mess); } } @Test public void getMessage_returnCode_WhenCodeNotInI18nFile() { String code = "CodeNotFound"; String mess = messageSource.getMessage(code, null, new Locale("es", "ES")); Assert.assertEquals(code, mess); } private String getMessage(String code, String[] fields, String resourcePath) { String message = PatternUtils.getPattern(code, resourcePath); MessageFormat format = new MessageFormat(message); return format.format(fields); } }