private static Double convertDecimal(Attribute attr, Object value) { if (value instanceof Double) { return (Double) value; } if (value == null) { return null; } // try to convert value Double doubleValue; if (value instanceof String) { try { doubleValue = Double.valueOf((String) value); } catch (NumberFormatException e) { throw new MolgenisValidationException( new ConstraintViolation( format( "Attribute [%s] value [%s] cannot be converter to type [%s]", attr.getName(), value, Double.class.getSimpleName()))); } } else if (value instanceof Number) { doubleValue = ((Number) value).doubleValue(); } else { throw new MolgenisValidationException( new ConstraintViolation( format( "Attribute [%s] value is of type [%s] instead of [%s] or [%s]", attr.getName(), value.getClass().getSimpleName(), String.class.getSimpleName(), Number.class.getSimpleName()))); } return doubleValue; }
private static Boolean convertBool(Attribute attr, Object value) { if (value instanceof Boolean) { return (Boolean) value; } if (value == null) { return null; } Boolean booleanValue; if (value instanceof String) { String stringValue = (String) value; if (stringValue.equalsIgnoreCase(TRUE.toString())) { booleanValue = true; } else if (stringValue.equalsIgnoreCase(FALSE.toString())) { booleanValue = false; } else { throw new MolgenisValidationException( new ConstraintViolation( format( "Attribute [%s] value [%s] cannot be converter to type [%s]", attr.getName(), value, Boolean.class.getSimpleName()))); } } else { throw new MolgenisValidationException( new ConstraintViolation( format( "Attribute [%s] value is of type [%s] instead of [%s] or [%s]", attr.getName(), value.getClass().getSimpleName(), String.class.getSimpleName(), Boolean.class.getSimpleName()))); } return booleanValue; }
@BeforeTest public void createEntity() { entityType = when(mock(EntityType.class).getName()).thenReturn("Source").getMock(); Attribute idAttr = when(mock(Attribute.class).getName()).thenReturn("Identifier").getMock(); when(idAttr.getDataType()).thenReturn(INT); Attribute intAttr = when(mock(Attribute.class).getName()).thenReturn("Int").getMock(); when(intAttr.getDataType()).thenReturn(INT); Attribute stringAttr = when(mock(Attribute.class).getName()).thenReturn("String").getMock(); when(stringAttr.getDataType()).thenReturn(STRING); Attribute nonNumericStringAttr = when(mock(Attribute.class).getName()).thenReturn("NonNumericString").getMock(); when(nonNumericStringAttr.getDataType()).thenReturn(STRING); Attribute longAttr = when(mock(Attribute.class).getName()).thenReturn("Long").getMock(); when(longAttr.getDataType()).thenReturn(LONG); when(entityType.getIdAttribute()).thenReturn(idAttr); when(entityType.getAttribute("Identifier")).thenReturn(idAttr); when(entityType.getAttribute("Int")).thenReturn(intAttr); when(entityType.getAttribute("String")).thenReturn(stringAttr); when(entityType.getAttribute("NonNumericString")).thenReturn(nonNumericStringAttr); when(entityType.getAttribute("Long")).thenReturn(longAttr); entity = new DynamicEntity(entityType); entity.set("Int", 1); entity.set("String", "12"); entity.set("Long", 10L); entity.set("NonNumericString", "Hello World!"); }
private static Integer convertInt(Attribute attr, Object value) { if (value instanceof Integer) { return (Integer) value; } if (value == null) { return null; } // try to convert value Integer integerValue; if (value instanceof String) { try { integerValue = Integer.valueOf((String) value); } catch (NumberFormatException e) { throw new MolgenisValidationException( new ConstraintViolation( format( "Attribute [%s] value [%s] cannot be converter to type [%s]", attr.getName(), value, Integer.class.getSimpleName()))); } } else if (value instanceof Number) { integerValue = ((Number) value).intValue(); } else { throw new MolgenisValidationException( new ConstraintViolation( format( "Attribute [%s] value is of type [%s] instead of [%s] or [%s]", attr.getName(), value.getClass().getSimpleName(), String.class.getSimpleName(), Number.class.getSimpleName()))); } return integerValue; }
@Test public void testStringEvaluatorLookupAttributeAndConvertFromIntToLong() { Attribute amd = when(mock(Attribute.class).getName()).thenReturn("#POS").getMock(); when(amd.getDataType()).thenReturn(LONG); when(amd.getExpression()).thenReturn("Int"); assertEquals(new StringExpressionEvaluator(amd, entityType).evaluate(entity), 1L); }
private static String convertEnum(Attribute attr, Object value) { if (value == null) { return null; } String stringValue; if (value instanceof String) { stringValue = (String) value; } else if (value instanceof Enum) { stringValue = value.toString(); } else { throw new MolgenisValidationException( new ConstraintViolation( format( "Attribute [%s] value is of type [%s] instead of [%s] or [%s]", attr.getName(), value.getClass().getSimpleName(), String.class.getSimpleName(), Enum.class.getSimpleName()))); } if (!attr.getEnumOptions().contains(stringValue)) { throw new MolgenisValidationException( new ConstraintViolation( format( "Attribute [%s] value [%s] is not a valid enum option", attr.getName(), stringValue))); } return stringValue; }
private static Date convertDate(Attribute attr, Object value) { if (value instanceof Date) { return (Date) value; } if (value == null) { return null; } // try to convert value Date dateValue; if (value instanceof String) { String paramStrValue = (String) value; try { dateValue = getDateFormat().parse(paramStrValue); } catch (ParseException e) { throw new MolgenisValidationException( new ConstraintViolation( format( "Attribute [%s] value [%s] does not match date format [%s]", attr.getName(), paramStrValue, MolgenisDateFormat.getDateFormat().toPattern()))); } } else { throw new MolgenisValidationException( new ConstraintViolation( format( "Attribute [%s] value is of type [%s] instead of [%s]", attr.getName(), value.getClass().getSimpleName(), String.class.getSimpleName()))); } return dateValue; }
@Override public void setIdValue(Object id) { Attribute idAttr = entityType.getIdAttribute(); if (idAttr == null) { throw new IllegalArgumentException( format("Entity [%s] doesn't have an id attribute", entityType.getName())); } set(idAttr.getName(), id); }
@Test( expectedExceptions = ConversionFailedException.class, expectedExceptionsMessageRegExp = "Failed to convert from type \\[java.lang.String\\] to type \\[java.lang.Long\\] for value 'Hello World!'; nested exception is java.lang.NumberFormatException: For input string: \"HelloWorld!\"") public void testStringEvaluatorLookupAttributeAndConvertFromNonNumericStringToLongFails() { Attribute amd = when(mock(Attribute.class).getName()).thenReturn("#POS").getMock(); when(amd.getDataType()).thenReturn(LONG); when(amd.getExpression()).thenReturn("NonNumericString"); new StringExpressionEvaluator(amd, entityType).evaluate(entity); }
@Test public void testStringEvaluatorConstructorChecksIfAttributeHasExpression() { Attribute amd = when(mock(Attribute.class).getName()).thenReturn("#CHROM").getMock(); when(amd.getDataType()).thenReturn(STRING); try { new StringExpressionEvaluator(amd, entityType); fail("Expected NPE"); } catch (NullPointerException expected) { assertEquals(expected.getMessage(), "Attribute has no expression."); } }
private Object toQueryRuleValue(Object queryRuleValue, Attribute attr) { Object value; AttributeType attrType = attr.getDataType(); switch (attrType) { case BOOL: value = convertBool(attr, queryRuleValue); break; case EMAIL: case HTML: case HYPERLINK: case SCRIPT: case STRING: case TEXT: value = convertString(attr, queryRuleValue); break; case ENUM: value = convertEnum(attr, queryRuleValue); break; case CATEGORICAL: case XREF: case CATEGORICAL_MREF: case MREF: case ONE_TO_MANY: value = convertRef(attr, queryRuleValue); break; case DATE: value = convertDate(attr, queryRuleValue); break; case DATE_TIME: value = convertDateTime(attr, queryRuleValue); break; case DECIMAL: value = convertDecimal(attr, queryRuleValue); break; case FILE: value = convertFile(attr, queryRuleValue); break; case INT: value = convertInt(attr, queryRuleValue); break; case LONG: value = convertLong(attr, queryRuleValue); break; case COMPOUND: throw new MolgenisValidationException( new ConstraintViolation( format( "Attribute [%s] type [%s] is not allowed", attr.getName(), attrType.toString()))); default: throw new RuntimeException(format("Unknown attribute type [%s]", attrType.toString())); } return value; }
@Test public void testStringEvaluatorConstructorChecksIfExpressionIsMap() { Attribute amd = when(mock(Attribute.class).getName()).thenReturn("#CHROM").getMock(); when(amd.getDataType()).thenReturn(STRING); when(amd.getExpression()).thenReturn("{}"); try { new StringExpressionEvaluator(amd, entityType); fail("expected illegal state exception"); } catch (JsonSyntaxException expected) { } }
private Entity convertRef(Attribute attr, Object value) { if (value instanceof Entity) { return (Entity) value; } if (value == null) { return null; } // try to convert value Object idValue = toQueryRuleValue(value, attr.getRefEntity().getIdAttribute()); return entityManager.getReference(attr.getRefEntity(), idValue); }
Entity getDefaultSettings() { Entity defaultSettingsEntity = new DynamicEntity(this); for (Attribute attr : this.getAtomicAttributes()) { // default values are stored/retrieved as strings, so we convert them to the required type // here. String defaultValue = attr.getDefaultValue(); if (defaultValue != null) { Object typedDefaultValue = getTypedValue(defaultValue, attr, entityManager); defaultSettingsEntity.set(attr.getName(), typedDefaultValue); } } return defaultSettingsEntity; }
@Test public void testStringEvaluatorConstructorChecksIfAttributeMentionsExistingAttribute() { Attribute amd = when(mock(Attribute.class).getName()).thenReturn("#CHROM").getMock(); when(amd.getDataType()).thenReturn(STRING); when(amd.getExpression()).thenReturn("bogus"); try { new StringExpressionEvaluator(amd, entityType); fail("expected illegal argument exception"); } catch (IllegalArgumentException expected) { assertEquals( expected.getMessage(), "Expression for attribute '#CHROM' references non-existant attribute 'bogus'."); } }
@Test public void testGenerateRules() { EntityType targetRefEntityType = createCategoricalRefEntityType("HOP_HYPERTENSION"); Entity targetEntity1 = new DynamicEntity( targetRefEntityType, of("code", 0, "label", "Never had high blood pressure ")); Entity targetEntity2 = new DynamicEntity( targetRefEntityType, of("code", 1, "label", "Ever had high blood pressure ")); Entity targetEntity3 = new DynamicEntity(targetRefEntityType, of("code", 9, "label", "Missing")); Mockito.when(dataService.findAll(targetRefEntityType.getName())) .thenReturn(Stream.of(targetEntity1, targetEntity2, targetEntity3)); targetAttribute = attrMetaFactory.create().setName("History of Hypertension").setDataType(CATEGORICAL); targetAttribute.setRefEntity(targetRefEntityType); EntityType sourceRefEntityType = createCategoricalRefEntityType("High_blood_pressure_ref"); Entity sourceEntity1 = new DynamicEntity(targetRefEntityType, of("code", 1, "label", "yes")); Entity sourceEntity2 = new DynamicEntity(targetRefEntityType, of("code", 2, "label", "no")); Entity sourceEntity3 = new DynamicEntity(targetRefEntityType, of("code", 3, "label", "I do not know")); Mockito.when(dataService.findAll(sourceRefEntityType.getName())) .thenReturn(Stream.of(sourceEntity1, sourceEntity2, sourceEntity3)); sourceAttribute = attrMetaFactory.create().setName("High_blood_pressure").setDataType(CATEGORICAL); sourceAttribute.setRefEntity(sourceRefEntityType); String generatedAlgorithm = categoryAlgorithmGenerator.generate( targetAttribute, singletonList(sourceAttribute), targetEntityType, sourceEntityType); String expectedAlgorithm = "$('High_blood_pressure').map({\"1\":\"1\",\"2\":\"0\",\"3\":\"9\"}, null, null).value();"; Assert.assertEquals(generatedAlgorithm, expectedAlgorithm); }
private FileMeta convertFile(Attribute attr, Object paramValue) { Entity entity = convertRef(attr, paramValue); if (entity == null) { return null; } if (!(entity instanceof FileMeta)) { throw new MolgenisValidationException( new ConstraintViolation( format( "Attribute [%s] value is of type [%s] instead of [%s]", attr.getName(), entity.getClass().getSimpleName(), FileMeta.class.getSimpleName()))); } return (FileMeta) entity; }
@BeforeMethod public void init() { dataService = Mockito.mock(DataService.class); categoryAlgorithmGenerator = new OneToOneCategoryAlgorithmGenerator(dataService); EntityType targetRefEntityType = createCategoricalRefEntityType("POTATO_REF"); Entity targetEntity1 = new DynamicEntity(targetRefEntityType, of("code", 1, "label", "Almost daily + daily")); Entity targetEntity2 = new DynamicEntity(targetRefEntityType, of("code", 2, "label", "Several times a week")); Entity targetEntity3 = new DynamicEntity(targetRefEntityType, of("code", 3, "label", "About once a week")); Entity targetEntity4 = new DynamicEntity( targetRefEntityType, of("code", 4, "label", "Never + fewer than once a week")); Entity targetEntity5 = new DynamicEntity(targetRefEntityType, of("code", 9, "label", "missing")); targetAttribute = attrMetaFactory .create() .setName("Current Consumption Frequency of Potatoes") .setDataType(CATEGORICAL); targetAttribute.setRefEntity(targetRefEntityType); Mockito.when(dataService.findAll(targetRefEntityType.getName())) .thenReturn( Stream.of(targetEntity1, targetEntity2, targetEntity3, targetEntity4, targetEntity5)); targetEntityType = entityTypeFactory.create("target"); targetEntityType.addAttribute(targetAttribute); EntityType sourceRefEntityType = createCategoricalRefEntityType("LifeLines_POTATO_REF"); Entity sourceEntity1 = new DynamicEntity(targetRefEntityType, of("code", 1, "label", "Not this month")); Entity sourceEntity2 = new DynamicEntity(targetRefEntityType, of("code", 2, "label", "1 day per month")); Entity sourceEntity3 = new DynamicEntity(targetRefEntityType, of("code", 3, "label", "2-3 days per month")); Entity sourceEntity4 = new DynamicEntity(targetRefEntityType, of("code", 4, "label", "1 day per week")); Entity sourceEntity5 = new DynamicEntity(targetRefEntityType, of("code", 5, "label", "2-3 days per week")); Entity sourceEntity6 = new DynamicEntity(targetRefEntityType, of("code", 6, "label", "4-5 days per week")); Entity sourceEntity7 = new DynamicEntity(targetRefEntityType, of("code", 7, "label", "6-7 days per week")); Entity sourceEntity8 = new DynamicEntity(targetRefEntityType, of("code", 8, "label", "9 days per week")); sourceAttribute = attrMetaFactory.create().setName("MESHED_POTATO").setDataType(CATEGORICAL); sourceAttribute.setLabel( "How often did you eat boiled or mashed potatoes (also in stew) in the past month? Baked potatoes are asked later"); sourceAttribute.setRefEntity(sourceRefEntityType); Mockito.when(dataService.findAll(sourceRefEntityType.getName())) .thenReturn( Stream.of( sourceEntity1, sourceEntity2, sourceEntity3, sourceEntity4, sourceEntity5, sourceEntity6, sourceEntity7, sourceEntity8)); sourceEntityType = entityTypeFactory.create("source"); sourceEntityType.addAttributes(Lists.newArrayList(sourceAttribute)); }
@Override public Object getIdValue() { // abstract entities might not have an id attribute Attribute idAttr = entityType.getIdAttribute(); return idAttr != null ? get(idAttr.getName()) : null; }
@Override public Object getLabelValue() { // abstract entities might not have an label attribute Attribute labelAttr = entityType.getLabelAttribute(); return labelAttr != null ? get(labelAttr.getName()) : null; }
/** * Validate is value is of the type defined by the attribute data type. * * @param attrName attribute name * @param value value (must be of the type defined by the attribute data type.) */ protected void validateValueType(String attrName, Object value) { if (value == null) { return; } Attribute attr = entityType.getAttribute(attrName); if (attr == null) { throw new UnknownAttributeException(format("Unknown attribute [%s]", attrName)); } AttributeType dataType = attr.getDataType(); switch (dataType) { case BOOL: if (!(value instanceof Boolean)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Boolean.class.getSimpleName(), attrName)); } break; case CATEGORICAL: // expected type is FileMeta. validation is not possible because molgenis-data does not // depend on molgenis-file case FILE: case XREF: if (!(value instanceof Entity)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Entity.class.getSimpleName(), attrName)); } break; case CATEGORICAL_MREF: case MREF: case ONE_TO_MANY: if (!(value instanceof Iterable)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Iterable.class.getSimpleName(), attrName)); } break; case COMPOUND: throw new IllegalArgumentException( format("Unexpected data type [%s] for attribute: [%s]", dataType.toString(), attrName)); case DATE: case DATE_TIME: if (!(value instanceof java.util.Date)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), java.util.Date.class.getSimpleName(), attrName)); } break; case DECIMAL: if (!(value instanceof Double)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Double.class.getSimpleName(), attrName)); } break; case EMAIL: case ENUM: case HTML: case HYPERLINK: case SCRIPT: case STRING: case TEXT: if (!(value instanceof String)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), String.class.getSimpleName(), attrName)); } break; case INT: if (!(value instanceof Integer)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Integer.class.getSimpleName(), attrName)); } break; case LONG: if (!(value instanceof Long)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Long.class.getSimpleName(), attrName)); } break; default: throw new RuntimeException(format("Unknown data type [%s]", dataType.toString())); } }