added config attribute (primitive) type check on import
This commit is contained in:
		
							parent
							
								
									cc543f79da
								
							
						
					
					
						commit
						61a232630b
					
				
					 2 changed files with 66 additions and 4 deletions
				
			
		|  | @ -413,7 +413,7 @@ public class ExamConfigXMLParser extends DefaultHandler { | ||||||
|                 name, |                 name, | ||||||
|                 attribute, |                 attribute, | ||||||
|                 listIndex, |                 listIndex, | ||||||
|                 value); |                 checkValueType(value, attribute)); | ||||||
| 
 | 
 | ||||||
|         if (configurationValue != null) { |         if (configurationValue != null) { | ||||||
|             if (log.isDebugEnabled()) { |             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( |     private ConfigurationValue createConfigurationValue( | ||||||
|             final String name, |             final String name, | ||||||
|             final ConfigurationAttribute attribute, |             final ConfigurationAttribute attribute, | ||||||
|  |  | ||||||
|  | @ -15,20 +15,21 @@ import java.util.List; | ||||||
| import java.util.function.Consumer; | import java.util.function.Consumer; | ||||||
| import java.util.function.Function; | import java.util.function.Function; | ||||||
| 
 | 
 | ||||||
| import ch.ethz.seb.sebserver.gbl.util.Cryptor; |  | ||||||
| import org.junit.Test; | 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.AttributeType; | ||||||
| import ch.ethz.seb.sebserver.gbl.model.sebconfig.ConfigurationAttribute; | import ch.ethz.seb.sebserver.gbl.model.sebconfig.ConfigurationAttribute; | ||||||
| import ch.ethz.seb.sebserver.gbl.model.sebconfig.ConfigurationValue; | import ch.ethz.seb.sebserver.gbl.model.sebconfig.ConfigurationValue; | ||||||
| import org.mockito.Mockito; | import ch.ethz.seb.sebserver.gbl.util.Cryptor; | ||||||
| 
 | 
 | ||||||
| public class ExamConfigImportHandlerTest { | public class ExamConfigImportHandlerTest { | ||||||
| 
 | 
 | ||||||
|     private static final Function<String, ConfigurationAttribute> attributeResolver = |     private static final Function<String, ConfigurationAttribute> attributeResolver = | ||||||
|             name -> new ConfigurationAttribute( |             name -> new ConfigurationAttribute( | ||||||
|                     getId(name), |                     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); |                     null); | ||||||
| 
 | 
 | ||||||
|     private static final Long getId(final String name) { |     private static final Long getId(final String name) { | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 anhefti
						anhefti