@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!");
  }
 @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);
 }
 @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 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) {
   }
 }
 @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.");
   }
 }
Пример #6
0
 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 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'.");
   }
 }
Пример #8
0
  /**
   * 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()));
    }
  }