added config attribute (primitive) type check on import

This commit is contained in:
anhefti 2020-07-06 09:41:00 +02:00
parent cc543f79da
commit 61a232630b
2 changed files with 66 additions and 4 deletions

View file

@ -413,7 +413,7 @@ public class ExamConfigXMLParser extends DefaultHandler {
name,
attribute,
listIndex,
value);
checkValueType(value, attribute));
if (configurationValue != null) {
if (log.isDebugEnabled()) {
@ -424,6 +424,67 @@ public class ExamConfigXMLParser extends DefaultHandler {
}
}
private String checkValueType(final String value, final ConfigurationAttribute attribute) {
if (attribute == null) {
return value;
}
if (attribute.type == null) {
log.warn(
"Invalid attribute type detected. Name: {} type: {} value: {} : import with default value for this attribute",
attribute.name,
attribute.type,
value);
return attribute.defaultValue;
}
switch (attribute.type) {
case CHECKBOX: {
try {
Boolean.parseBoolean(value);
return value;
} catch (final Exception e) {
log.warn(
"Invalid attribute value detected. Name: {} type: {} value: {} : import with default value for this attribute",
attribute.name,
attribute.type,
value);
return attribute.defaultValue;
}
}
case INTEGER:
case RADIO_SELECTION:
case SINGLE_SELECTION: {
try {
Integer.parseInt(value);
return value;
} catch (final Exception e) {
log.warn(
"Invalid attribute value detected. Name: {} type: {} value: {} : import with default value for this attribute",
attribute.name,
attribute.type,
value);
return attribute.defaultValue;
}
}
case DECIMAL: {
try {
Double.parseDouble(value);
return value;
} catch (final Exception e) {
log.warn(
"Invalid attribute value detected. Name: {} type: {} value: {} : import with default value for this attribute",
attribute.name,
attribute.type,
value);
return attribute.defaultValue;
}
}
default:
return value;
}
}
private ConfigurationValue createConfigurationValue(
final String name,
final ConfigurationAttribute attribute,

View file

@ -15,20 +15,21 @@ import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import ch.ethz.seb.sebserver.gbl.util.Cryptor;
import org.junit.Test;
import org.mockito.Mockito;
import ch.ethz.seb.sebserver.gbl.model.sebconfig.AttributeType;
import ch.ethz.seb.sebserver.gbl.model.sebconfig.ConfigurationAttribute;
import ch.ethz.seb.sebserver.gbl.model.sebconfig.ConfigurationValue;
import org.mockito.Mockito;
import ch.ethz.seb.sebserver.gbl.util.Cryptor;
public class ExamConfigImportHandlerTest {
private static final Function<String, ConfigurationAttribute> attributeResolver =
name -> new ConfigurationAttribute(
getId(name),
null, name, (name.contains("array")) ? AttributeType.MULTI_SELECTION : null, null, null, null,
null, name, (name.contains("array")) ? AttributeType.MULTI_SELECTION : AttributeType.TEXT_FIELD,
null, null, null,
null);
private static final Long getId(final String name) {