addressed review feedback
parent
0ee4ee2f58
commit
011bf8adb8
|
@ -16,6 +16,18 @@
|
||||||
|
|
||||||
package org.mitre.openid.connect.config;
|
package org.mitre.openid.connect.config;
|
||||||
|
|
||||||
|
import com.google.common.base.Splitter;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonIOException;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
|
import com.google.gson.JsonSyntaxException;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.support.AbstractMessageSource;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -28,19 +40,6 @@ import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.support.AbstractMessageSource;
|
|
||||||
import org.springframework.core.io.Resource;
|
|
||||||
|
|
||||||
import com.google.common.base.Splitter;
|
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import com.google.gson.JsonIOException;
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import com.google.gson.JsonParser;
|
|
||||||
import com.google.gson.JsonSyntaxException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author jricher
|
* @author jricher
|
||||||
*/
|
*/
|
||||||
|
@ -54,12 +53,8 @@ public class JsonMessageSource extends AbstractMessageSource {
|
||||||
|
|
||||||
private Map<Locale, List<JsonObject>> languageMaps = new HashMap<>();
|
private Map<Locale, List<JsonObject>> languageMaps = new HashMap<>();
|
||||||
|
|
||||||
private ConfigurationPropertiesBean config;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public JsonMessageSource(ConfigurationPropertiesBean config) {
|
private ConfigurationPropertiesBean config;
|
||||||
this.config = config;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected MessageFormat resolveCode(String code, Locale locale) {
|
protected MessageFormat resolveCode(String code, Locale locale) {
|
||||||
|
@ -86,9 +81,6 @@ public class JsonMessageSource extends AbstractMessageSource {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a value from the set of maps, taking the first match in order
|
* Get a value from the set of maps, taking the first match in order
|
||||||
* @param code
|
|
||||||
* @param langs
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
private String getValue(String code, List<JsonObject> langs) {
|
private String getValue(String code, List<JsonObject> langs) {
|
||||||
if (langs == null || langs.isEmpty()) {
|
if (langs == null || langs.isEmpty()) {
|
||||||
|
@ -110,9 +102,6 @@ public class JsonMessageSource extends AbstractMessageSource {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a value from a single map
|
* Get a value from a single map
|
||||||
* @param code
|
|
||||||
* @param lang
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
private String getValue(String code, JsonObject lang) {
|
private String getValue(String code, JsonObject lang) {
|
||||||
|
|
||||||
|
@ -157,7 +146,7 @@ public class JsonMessageSource extends AbstractMessageSource {
|
||||||
* @param locale
|
* @param locale
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
List<JsonObject> getLanguageMap(Locale locale) {
|
private List<JsonObject> getLanguageMap(Locale locale) {
|
||||||
|
|
||||||
if (!languageMaps.containsKey(locale)) {
|
if (!languageMaps.containsKey(locale)) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -1,49 +1,40 @@
|
||||||
package org.mitre.openid.connect.config;
|
package org.mitre.openid.connect.config;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Spy;
|
||||||
|
import org.mockito.runners.MockitoJUnitRunner;
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
|
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class TestJsonMessageSource {
|
public class TestJsonMessageSource {
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
private JsonMessageSource jsonMessageSource;
|
private JsonMessageSource jsonMessageSource;
|
||||||
|
|
||||||
|
@Spy
|
||||||
|
private ConfigurationPropertiesBean config;
|
||||||
|
|
||||||
private Locale localeThatHasAFile = new Locale("en");
|
private Locale localeThatHasAFile = new Locale("en");
|
||||||
|
|
||||||
private Locale localeThatDoesNotHaveAFile = new Locale("xx");
|
private Locale localeThatDoesNotHaveAFile = new Locale("xx");
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
ConfigurationPropertiesBean config = new ConfigurationPropertiesBean();
|
|
||||||
jsonMessageSource = new JsonMessageSource(config);
|
|
||||||
|
|
||||||
//test message files are located in test/resources/js/locale/
|
//test message files are located in test/resources/js/locale/
|
||||||
Resource resource = new ClassPathResource("/resources/js/locale/");
|
Resource resource = new ClassPathResource("/resources/js/locale/");
|
||||||
jsonMessageSource.setBaseDirectory(resource);
|
jsonMessageSource.setBaseDirectory(resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void verifyWhenLocaleExists_languageMapIsLoaded() {
|
|
||||||
List<JsonObject> languageMap = jsonMessageSource.getLanguageMap(localeThatHasAFile);
|
|
||||||
assertNotNull(languageMap);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void verifyWhenLocaleDoesNotExist_languageMapIsNotLoaded() {
|
|
||||||
List<JsonObject> languageMap = jsonMessageSource.getLanguageMap(localeThatDoesNotHaveAFile);
|
|
||||||
assertNull(languageMap);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyWhenLocaleExists_canResolveCode() {
|
public void verifyWhenLocaleExists_canResolveCode() {
|
||||||
MessageFormat mf = jsonMessageSource.resolveCode("testAttribute", localeThatHasAFile);
|
MessageFormat mf = jsonMessageSource.resolveCode("testAttribute", localeThatHasAFile);
|
||||||
|
|
Loading…
Reference in New Issue