mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-16 14:33:09 +00:00
[CST-5587] Added tests for SimpleMapConverter
This commit is contained in:
@@ -1,115 +0,0 @@
|
||||
/**
|
||||
* The contents of this file are subject to the license and copyright
|
||||
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||
* tree and available online at
|
||||
*
|
||||
* http://www.dspace.org/license/
|
||||
*/
|
||||
package org.dspace.submit.lookup;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.dspace.services.ConfigurationService;
|
||||
|
||||
/**
|
||||
* @author Andrea Bollini
|
||||
* @author Kostas Stamatis
|
||||
* @author Luigi Andrea Pascarelli
|
||||
* @author Panagiotis Koutsourakis
|
||||
*/
|
||||
public class MapConverterModifier {
|
||||
|
||||
protected String mappingFile; // The properties absolute filename
|
||||
|
||||
protected String converterNameFile; // The properties filename
|
||||
|
||||
protected ConfigurationService configurationService;
|
||||
|
||||
protected Map<String, String> mapping;
|
||||
|
||||
protected String defaultValue = "";
|
||||
|
||||
protected List<String> fieldKeys;
|
||||
|
||||
protected Map<String, String> regexConfig = new HashMap<String, String>();
|
||||
|
||||
public final String REGEX_PREFIX = "regex.";
|
||||
|
||||
public void init() {
|
||||
this.mappingFile = configurationService.getProperty(
|
||||
"dspace.dir") + File.separator + "config" + File.separator + "crosswalks" + File.separator +
|
||||
converterNameFile;
|
||||
|
||||
this.mapping = new HashMap<String, String>();
|
||||
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
fis = new FileInputStream(new File(mappingFile));
|
||||
Properties mapConfig = new Properties();
|
||||
mapConfig.load(fis);
|
||||
fis.close();
|
||||
for (Object key : mapConfig.keySet()) {
|
||||
String keyS = (String) key;
|
||||
if (keyS.startsWith(REGEX_PREFIX)) {
|
||||
String regex = keyS.substring(REGEX_PREFIX.length());
|
||||
String regReplace = mapping.get(keyS);
|
||||
if (regReplace == null) {
|
||||
regReplace = "";
|
||||
} else if (regReplace.equalsIgnoreCase("@ident@")) {
|
||||
regReplace = "$0";
|
||||
}
|
||||
regexConfig.put(regex, regReplace);
|
||||
}
|
||||
if (mapConfig.getProperty(keyS) != null) {
|
||||
mapping.put(keyS, mapConfig.getProperty(keyS));
|
||||
} else {
|
||||
mapping.put(keyS, "");
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("", e);
|
||||
} finally {
|
||||
if (fis != null) {
|
||||
try {
|
||||
fis.close();
|
||||
} catch (IOException ioe) {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
}
|
||||
for (String keyS : mapping.keySet()) {
|
||||
if (keyS.startsWith(REGEX_PREFIX)) {
|
||||
String regex = keyS.substring(REGEX_PREFIX.length());
|
||||
String regReplace = mapping.get(keyS);
|
||||
if (regReplace == null) {
|
||||
regReplace = "";
|
||||
} else if (regReplace.equalsIgnoreCase("@ident@")) {
|
||||
regReplace = "$0";
|
||||
}
|
||||
regexConfig.put(regex, regReplace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setFieldKeys(List<String> fieldKeys) {
|
||||
this.fieldKeys = fieldKeys;
|
||||
}
|
||||
|
||||
public void setDefaultValue(String defaultValue) {
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
public void setConverterNameFile(String converterNameFile) {
|
||||
this.converterNameFile = converterNameFile;
|
||||
}
|
||||
|
||||
public void setConfigurationService(ConfigurationService configurationService) {
|
||||
this.configurationService = configurationService;
|
||||
}
|
||||
}
|
@@ -7,41 +7,101 @@
|
||||
*/
|
||||
package org.dspace.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.dspace.submit.lookup.MapConverterModifier;
|
||||
import org.dspace.services.ConfigurationService;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Class that parse a properties file present in the crosswalks directory and
|
||||
* allows to get its values given a key.
|
||||
*
|
||||
* @author Mykhaylo Boychuk (mykhaylo.boychuk at 4science.it)
|
||||
* @author Andrea Bollini
|
||||
* @author Kostas Stamatis
|
||||
* @author Luigi Andrea Pascarelli
|
||||
* @author Panagiotis Koutsourakis
|
||||
* @author Luca Giamminonni
|
||||
*/
|
||||
public class SimpleMapConverter extends MapConverterModifier {
|
||||
public class SimpleMapConverter {
|
||||
|
||||
public String getValue(String key) {
|
||||
boolean matchEmpty = false;
|
||||
String stringValue = key;
|
||||
private String converterNameFile; // The properties filename
|
||||
|
||||
String tmp = "";
|
||||
if (mapping.containsKey(stringValue)) {
|
||||
tmp = mapping.get(stringValue);
|
||||
} else {
|
||||
tmp = defaultValue;
|
||||
for (String regex : regexConfig.keySet()) {
|
||||
if (stringValue != null && stringValue.matches(regex)) {
|
||||
tmp = stringValue.replaceAll(regex, regexConfig.get(regex));
|
||||
if (StringUtils.isBlank(tmp)) {
|
||||
matchEmpty = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
private ConfigurationService configurationService;
|
||||
|
||||
private Map<String, String> mapping;
|
||||
|
||||
private String defaultValue = "";
|
||||
|
||||
/**
|
||||
* Parse the configured property file.
|
||||
*/
|
||||
public void init() {
|
||||
|
||||
Assert.notNull(converterNameFile, "No properties file name provided");
|
||||
Assert.notNull(configurationService, "No configuration service provided");
|
||||
|
||||
String mappingFile = configurationService.getProperty(
|
||||
"dspace.dir") + File.separator + "config" + File.separator + "crosswalks" + File.separator +
|
||||
converterNameFile;
|
||||
|
||||
try (FileInputStream fis = new FileInputStream(new File(mappingFile))) {
|
||||
|
||||
Properties mapConfig = new Properties();
|
||||
mapConfig.load(fis);
|
||||
|
||||
this.mapping = parseProperties(mapConfig);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("An error occurs parsing " + mappingFile, e);
|
||||
}
|
||||
|
||||
if ("@@ident@@".equals(tmp)) {
|
||||
return stringValue;
|
||||
} else if (StringUtils.isNotBlank(tmp) || (StringUtils.isBlank(tmp) && matchEmpty)) {
|
||||
return tmp;
|
||||
}
|
||||
|
||||
return stringValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value related to the given key. If the given key is not found the
|
||||
* incoming value is returned.
|
||||
*
|
||||
* @param key the key to search for a value
|
||||
* @return the value
|
||||
*/
|
||||
public String getValue(String key) {
|
||||
|
||||
String value = mapping.getOrDefault(key, defaultValue);
|
||||
|
||||
if (StringUtils.isBlank(value)) {
|
||||
return key;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
private Map<String, String> parseProperties(Properties properties) {
|
||||
|
||||
Map<String, String> mapping = new HashMap<String, String>();
|
||||
|
||||
for (Object key : properties.keySet()) {
|
||||
String keyString = (String) key;
|
||||
mapping.put(keyString, properties.getProperty(keyString, ""));
|
||||
}
|
||||
|
||||
return mapping;
|
||||
|
||||
}
|
||||
|
||||
public void setDefaultValue(String defaultValue) {
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
public void setConverterNameFile(String converterNameFile) {
|
||||
this.converterNameFile = converterNameFile;
|
||||
}
|
||||
|
||||
public void setConfigurationService(ConfigurationService configurationService) {
|
||||
this.configurationService = configurationService;
|
||||
}
|
||||
}
|
@@ -0,0 +1,167 @@
|
||||
/**
|
||||
* The contents of this file are subject to the license and copyright
|
||||
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||
* tree and available online at
|
||||
*
|
||||
* http://www.dspace.org/license/
|
||||
*/
|
||||
package org.dspace.util;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.dspace.services.ConfigurationService;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link SimpleMapConverter}.
|
||||
*
|
||||
* @author Luca Giamminonni (luca.giamminonni at 4science.it)
|
||||
*
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class SimpleMapConverterTest {
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder folder = new TemporaryFolder();
|
||||
|
||||
@Mock
|
||||
private ConfigurationService configurationService;
|
||||
|
||||
private File dspaceDir;
|
||||
|
||||
private File crosswalksDir;
|
||||
|
||||
@Before
|
||||
public void before() throws IOException {
|
||||
dspaceDir = folder.getRoot();
|
||||
crosswalksDir = folder.newFolder("config", "crosswalks");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertiesParsing() throws IOException {
|
||||
|
||||
when(configurationService.getProperty("dspace.dir")).thenReturn(dspaceDir.getAbsolutePath());
|
||||
createFileInFolder(crosswalksDir, "test.properties", "key1=value1\nkey2=value2\nkey3=value3");
|
||||
|
||||
SimpleMapConverter simpleMapConverter = new SimpleMapConverter();
|
||||
simpleMapConverter.setConfigurationService(configurationService);
|
||||
simpleMapConverter.setConverterNameFile("test.properties");
|
||||
|
||||
simpleMapConverter.init();
|
||||
|
||||
assertThat(simpleMapConverter.getValue("key1"), is("value1"));
|
||||
assertThat(simpleMapConverter.getValue("key2"), is("value2"));
|
||||
assertThat(simpleMapConverter.getValue("key3"), is("value3"));
|
||||
assertThat(simpleMapConverter.getValue(""), is(""));
|
||||
assertThat(simpleMapConverter.getValue(null), nullValue());
|
||||
|
||||
assertThat(simpleMapConverter.getValue("key4"), is("key4"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertiesParsingWithDefaultValue() throws IOException {
|
||||
|
||||
when(configurationService.getProperty("dspace.dir")).thenReturn(dspaceDir.getAbsolutePath());
|
||||
createFileInFolder(crosswalksDir, "test.properties", "key1=value1\nkey2=value2\nkey3=value3");
|
||||
|
||||
SimpleMapConverter simpleMapConverter = new SimpleMapConverter();
|
||||
simpleMapConverter.setConfigurationService(configurationService);
|
||||
simpleMapConverter.setConverterNameFile("test.properties");
|
||||
simpleMapConverter.setDefaultValue("default");
|
||||
|
||||
simpleMapConverter.init();
|
||||
|
||||
assertThat(simpleMapConverter.getValue("key1"), is("value1"));
|
||||
assertThat(simpleMapConverter.getValue("key2"), is("value2"));
|
||||
assertThat(simpleMapConverter.getValue("key3"), is("value3"));
|
||||
assertThat(simpleMapConverter.getValue(""), is("default"));
|
||||
assertThat(simpleMapConverter.getValue(null), is("default"));
|
||||
|
||||
assertThat(simpleMapConverter.getValue("key4"), is("default"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertiesParsingWithAnUnexistingFile() throws IOException {
|
||||
|
||||
when(configurationService.getProperty("dspace.dir")).thenReturn(dspaceDir.getAbsolutePath());
|
||||
|
||||
SimpleMapConverter simpleMapConverter = new SimpleMapConverter();
|
||||
simpleMapConverter.setConfigurationService(configurationService);
|
||||
simpleMapConverter.setConverterNameFile("test.properties");
|
||||
|
||||
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
|
||||
() -> simpleMapConverter.init());
|
||||
|
||||
assertThat(exception.getMessage(),
|
||||
is("An error occurs parsing " + dspaceDir.getAbsolutePath() + "/config/crosswalks/test.properties"));
|
||||
|
||||
Throwable cause = exception.getCause();
|
||||
assertThat(cause, notNullValue());
|
||||
assertThat(cause, instanceOf(FileNotFoundException.class));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertiesParsingWithCorruptedFile() throws IOException {
|
||||
|
||||
when(configurationService.getProperty("dspace.dir")).thenReturn(dspaceDir.getAbsolutePath());
|
||||
createFileInFolder(crosswalksDir, "test.properties", "key1=value1\nkey2\nkey3=value3");
|
||||
|
||||
SimpleMapConverter simpleMapConverter = new SimpleMapConverter();
|
||||
simpleMapConverter.setConfigurationService(configurationService);
|
||||
simpleMapConverter.setConverterNameFile("test.properties");
|
||||
|
||||
simpleMapConverter.init();
|
||||
|
||||
assertThat(simpleMapConverter.getValue("key1"), is("value1"));
|
||||
assertThat(simpleMapConverter.getValue("key2"), is("key2"));
|
||||
assertThat(simpleMapConverter.getValue("key3"), is("value3"));
|
||||
|
||||
assertThat(simpleMapConverter.getValue("key4"), is("key4"));
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertiesParsingWithEmptyFile() throws IOException {
|
||||
|
||||
when(configurationService.getProperty("dspace.dir")).thenReturn(dspaceDir.getAbsolutePath());
|
||||
createFileInFolder(crosswalksDir, "test.properties", "");
|
||||
|
||||
SimpleMapConverter simpleMapConverter = new SimpleMapConverter();
|
||||
simpleMapConverter.setConfigurationService(configurationService);
|
||||
simpleMapConverter.setConverterNameFile("test.properties");
|
||||
|
||||
simpleMapConverter.init();
|
||||
|
||||
assertThat(simpleMapConverter.getValue("key1"), is("key1"));
|
||||
assertThat(simpleMapConverter.getValue("key2"), is("key2"));
|
||||
|
||||
}
|
||||
|
||||
private void createFileInFolder(File folder, String name, String content) throws IOException {
|
||||
File file = new File(folder, name);
|
||||
FileUtils.write(file, content, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user