@BeforeTest
  public void beforeTest() {
    initMocks(this);
    DefaultEntityMetaData emd = new DefaultEntityMetaData("MyEntity");
    emd.addAttributeMetaData(new DefaultAttributeMetaData("ID").setAuto(true).setIdAttribute(true));
    emd.addAllAttributeMetaData(asList(CHROM_META, POS_META));
    emd.addAttributeMetaData(new DefaultAttributeMetaData("Description").setNillable(false));

    entityMetaData = emd;
    tabixRepository = new TabixRepository(tabixReader, entityMetaData, CHROM, POS);
  }
 @Override
 public EntityMetaData getInputMetaData() {
   DefaultEntityMetaData metadata =
       new DefaultEntityMetaData(this.getClass().getName(), MapEntity.class);
   metadata.addAttributeMetaData(new DefaultAttributeMetaData(UNIPROT_ID, FieldTypeEnum.STRING));
   return metadata;
 }
  @Test
  public void testAddAndGetEntity() {
    readPackageTree();

    PackageImpl defaultPackage = (PackageImpl) PackageImpl.defaultPackage;
    DefaultEntityMetaData coderMetaData = new DefaultEntityMetaData("Coder");
    coderMetaData.setDescription("A coder");
    coderMetaData.setExtends(metaDataServiceImpl.getEntityMetaData("org_molgenis_Person"));
    coderMetaData.setPackage(defaultPackage);
    coderMetaData.addAttribute("ID", ROLE_ID);
    coderMetaData.addAttribute("simple");
    DefaultAttributeMetaData compoundAttribute = new DefaultAttributeMetaData("compound", COMPOUND);
    coderMetaData.addAttributeMetaData(compoundAttribute);
    compoundAttribute.addAttributePart(new DefaultAttributeMetaData("c1"));
    compoundAttribute.addAttributePart(new DefaultAttributeMetaData("c2"));

    assertEquals(coderMetaData.getIdAttribute().getName(), "ID");
    metaDataServiceImpl.addEntityMeta(coderMetaData);
    DefaultEntityMetaData retrieved = metaDataServiceImpl.getEntityMetaData("Coder");
    assertEquals(retrieved, coderMetaData);
    assertEquals(retrieved.getIdAttribute().getName(), "ID");

    // reboot

    DataServiceImpl dataService = new DataServiceImpl();
    metaDataServiceImpl = new MetaDataServiceImpl(dataService);
    AppSettings appSettings = Mockito.mock(AppSettings.class);
    metaDataServiceImpl.setLanguageService(new LanguageService(dataService, appSettings));
    metaDataServiceImpl.setDefaultBackend(manageableCrudRepositoryCollection);
    retrieved = metaDataServiceImpl.getEntityMetaData("Coder");
    assertEquals(retrieved.getIdAttribute().getName(), "ID");
    assertTrue(
        Iterables.elementsEqual(
            retrieved.getAtomicAttributes(), coderMetaData.getAtomicAttributes()));
  }
 @Override
 public EntityMetaData getOutputMetaData() {
   DefaultEntityMetaData metadata =
       new DefaultEntityMetaData(this.getClass().getName(), MapEntity.class);
   metadata.addAttributeMetaData(new DefaultAttributeMetaData("targetType", FieldTypeEnum.STRING));
   metadata.addAttributeMetaData(new DefaultAttributeMetaData("chemblId", FieldTypeEnum.STRING));
   metadata.addAttributeMetaData(new DefaultAttributeMetaData("geneNames", FieldTypeEnum.STRING));
   metadata.addAttributeMetaData(
       new DefaultAttributeMetaData("description", FieldTypeEnum.STRING));
   metadata.addAttributeMetaData(
       new DefaultAttributeMetaData("compoundCount", FieldTypeEnum.DECIMAL));
   metadata.addAttributeMetaData(
       new DefaultAttributeMetaData("bioactivityCount", FieldTypeEnum.DECIMAL));
   metadata.addAttributeMetaData(
       new DefaultAttributeMetaData("proteinAccession", FieldTypeEnum.STRING));
   metadata.addAttributeMetaData(new DefaultAttributeMetaData("synonyms", FieldTypeEnum.STRING));
   metadata.addAttributeMetaData(new DefaultAttributeMetaData("organism", FieldTypeEnum.STRING));
   metadata.addAttributeMetaData(
       new DefaultAttributeMetaData("preferredName", FieldTypeEnum.STRING));
   return metadata;
 }
  /** Test that a new entity has at least all attributes as of the existing entity. */
  @Test
  public void canIntegrateEntityMetadataCheckFalse() {
    String entityName = "testEntity";
    DataServiceImpl dataServiceImpl = Mockito.mock(DataServiceImpl.class);
    when(dataServiceImpl.hasRepository(entityName)).thenReturn(Boolean.TRUE);

    DefaultEntityMetaData existingEntityMetaData = new DefaultEntityMetaData(entityName);
    existingEntityMetaData.addAttribute("ID", ROLE_ID);
    existingEntityMetaData.addAttributeMetaData(new DefaultAttributeMetaData("existingAttribute"));

    DefaultEntityMetaData newEntityMetaData = new DefaultEntityMetaData(entityName);
    newEntityMetaData.addAttribute("ID", ROLE_ID);

    when(dataServiceImpl.getEntityMetaData(entityName)).thenReturn(existingEntityMetaData);
    MetaDataServiceImpl metaDataService = new MetaDataServiceImpl(dataServiceImpl);

    assertFalse(metaDataService.canIntegrateEntityMetadataCheck(newEntityMetaData));
  }
  @Test
  public void testFindAttributes() {
    DefaultEntityMetaData sourceEntityMetaData = new DefaultEntityMetaData("sourceEntityMetaData");
    EntityMetaData targetEntityMetaData = new DefaultEntityMetaData("targetEntityMetaData");
    DefaultAttributeMetaData targetAttribute = new DefaultAttributeMetaData("targetAttribute");

    // Mock the id's of the attribute entities that should be searched
    List<String> attributeIdentifiers = Arrays.asList("1", "2");
    when(semanticSearchServiceHelper.getAttributeIdentifiers(sourceEntityMetaData))
        .thenReturn(attributeIdentifiers);

    // Mock the createDisMaxQueryRule method
    List<QueryRule> rules = new ArrayList<QueryRule>();
    QueryRule targetQueryRuleLabel =
        new QueryRule(AttributeMetaDataMetaData.LABEL, Operator.FUZZY_MATCH, "height");
    rules.add(targetQueryRuleLabel);
    QueryRule targetQueryRuleOntologyTermTag =
        new QueryRule(AttributeMetaDataMetaData.LABEL, Operator.FUZZY_MATCH, "standing height");
    rules.add(targetQueryRuleOntologyTermTag);
    QueryRule targetQueryRuleOntologyTermTagSyn =
        new QueryRule(AttributeMetaDataMetaData.LABEL, Operator.FUZZY_MATCH, "length");
    rules.add(targetQueryRuleOntologyTermTagSyn);
    QueryRule disMaxQueryRule = new QueryRule(rules);
    disMaxQueryRule.setOperator(Operator.DIS_MAX);

    when(semanticSearchServiceHelper.createDisMaxQueryRuleForAttribute(
            targetEntityMetaData, targetAttribute))
        .thenReturn(disMaxQueryRule);

    MapEntity entity1 =
        new MapEntity(
            ImmutableMap.of(
                AttributeMetaDataMetaData.NAME,
                "height_0",
                AttributeMetaDataMetaData.LABEL,
                "height",
                AttributeMetaDataMetaData.DESCRIPTION,
                "this is a height measurement in m!"));
    List<Entity> attributeMetaDataEntities = Arrays.<Entity>asList(entity1);

    List<QueryRule> disMaxQueryRules =
        Lists.newArrayList(
            new QueryRule(AttributeMetaDataMetaData.IDENTIFIER, Operator.IN, attributeIdentifiers),
            new QueryRule(Operator.AND),
            disMaxQueryRule);

    AttributeMetaData attributeHeight = new DefaultAttributeMetaData("height_0");
    AttributeMetaData attributeWeight = new DefaultAttributeMetaData("weight_0");
    sourceEntityMetaData.addAttributeMetaData(attributeHeight);
    sourceEntityMetaData.addAttributeMetaData(attributeWeight);

    // Case 1
    when(dataService.findAll(
            AttributeMetaDataMetaData.ENTITY_NAME, new QueryImpl(disMaxQueryRules)))
        .thenReturn(attributeMetaDataEntities);

    Iterable<AttributeMetaData> termsActual1 =
        semanticSearchService.findAttributes(
            sourceEntityMetaData, targetEntityMetaData, targetAttribute);

    Iterable<AttributeMetaData> termsExpected1 = Arrays.<AttributeMetaData>asList(attributeHeight);

    assertEquals(termsActual1, termsExpected1);

    // Case 2
    when(dataService.findAll(
            AttributeMetaDataMetaData.ENTITY_NAME, new QueryImpl(disMaxQueryRules)))
        .thenReturn(Arrays.<Entity>asList());

    Iterable<AttributeMetaData> termsActual2 =
        semanticSearchService.findAttributes(
            sourceEntityMetaData, targetEntityMetaData, targetAttribute);

    Iterable<AttributeMetaData> termsExpected2 = Arrays.<AttributeMetaData>asList();

    assertEquals(termsActual2, termsExpected2);

    Mockito.reset(ontologyService);
    attribute.setDescription("Standing height (Ångstrøm)");

    when(ontologyService.findOntologyTerms(
            ontologies, ImmutableSet.of("standing", "height", "ångstrøm"), 100))
        .thenReturn(ontologyTerms);
    Hit<OntologyTerm> result = semanticSearchService.findTags(attribute, ontologies);
    assertEquals(result, Hit.<OntologyTerm>create(standingHeight, 0.76471f));
  }
  @Override
  public Map<String, DefaultEntityMetaData> getEntityMetaData(RepositoryCollection source) {
    // TODO: this task is actually a 'merge' instead of 'import'
    // so we need to consider both new metadata as existing ...

    Map<String, DefaultEntityMetaData> entities =
        new LinkedHashMap<String, DefaultEntityMetaData>();

    // load attributes first (because entities are optional).
    for (Entity a : source.getRepositoryByEntityName("attributes")) {
      int i = 1;
      String entityName = a.getString("entity");

      // required
      if (entityName == null) throw new IllegalArgumentException("attributes.entity is missing");
      if (a.get("name") == null) throw new IllegalArgumentException("attributes.name is missing");

      // create entity if not yet defined
      if (entities.get(entityName) == null)
        entities.put(entityName, new DefaultEntityMetaData(entityName));
      DefaultEntityMetaData md = entities.get(entityName);

      DefaultAttributeMetaData am = new DefaultAttributeMetaData(a.getString("name"));

      if (a.get("dataType") != null) {
        FieldType t = MolgenisFieldTypes.getType(a.getString("dataType"));
        if (t == null)
          throw new IllegalArgumentException(
              "attributes.type error on line " + i + ": " + a.getString("dataType") + " unknown");
        am.setDataType(t);
      }
      if (a.get("nillable") != null) am.setNillable(a.getBoolean("nillable"));
      if (a.get("auto") != null) am.setAuto(a.getBoolean("auto"));
      if (a.get("idAttribute") != null) am.setIdAttribute(a.getBoolean("idAttribute"));

      md.addAttributeMetaData(am);
    }

    // load all entities (optional)
    if (source.getRepositoryByEntityName("entities") != null) {
      int i = 1;
      for (Entity e : source.getRepositoryByEntityName("entities")) {
        i++;
        String entityName = e.getString("name");

        // required
        if (entityName == null)
          throw new IllegalArgumentException("entity.name is missing on line " + i);

        if (entities.get(entityName) == null)
          entities.put(entityName, new DefaultEntityMetaData(entityName));
        DefaultEntityMetaData md = entities.get(entityName);

        if (e.get("description") != null) md.setDescription(e.getString("description"));
      }
    }

    // re-iterate to map the mrefs/xref refEntity (or give error if not found)
    // TODO: consider also those in existing db
    int i = 1;
    for (Entity a : source.getRepositoryByEntityName("attributes")) {
      i++;
      if (a.get("refEntity") != null) {
        DefaultEntityMetaData em = entities.get(a.getString("entity"));
        DefaultAttributeMetaData am =
            (DefaultAttributeMetaData) em.getAttribute(a.getString("name"));

        if (entities.get(a.getString("refEntity")) == null) {
          throw new IllegalArgumentException(
              "attributes.refEntity error on line "
                  + i
                  + ": "
                  + a.getString("refEntity")
                  + " unknown");
        }

        am.setRefEntity(entities.get(a.getString("refEntity")));
      }
    }

    return entities;
  }