diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/sebconfig/impl/ExamConfigXMLParser.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/sebconfig/impl/ExamConfigXMLParser.java index 57ce32da..46a84455 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/sebconfig/impl/ExamConfigXMLParser.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/sebconfig/impl/ExamConfigXMLParser.java @@ -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, diff --git a/src/test/java/ch/ethz/seb/sebserver/webservice/servicelayer/sebconfig/impl/ExamConfigImportHandlerTest.java b/src/test/java/ch/ethz/seb/sebserver/webservice/servicelayer/sebconfig/impl/ExamConfigImportHandlerTest.java index 7e689211..1d4ca175 100644 --- a/src/test/java/ch/ethz/seb/sebserver/webservice/servicelayer/sebconfig/impl/ExamConfigImportHandlerTest.java +++ b/src/test/java/ch/ethz/seb/sebserver/webservice/servicelayer/sebconfig/impl/ExamConfigImportHandlerTest.java @@ -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 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) {