@Override
 public EntityMetaData getInputMetaData() {
   DefaultEntityMetaData metadata =
       new DefaultEntityMetaData(this.getClass().getName(), MapEntity.class);
   metadata.addAttributeMetaData(new DefaultAttributeMetaData(UNIPROT_ID, FieldTypeEnum.STRING));
   return metadata;
 }
  @BeforeMethod
  public void readPackageTree() {
    defaultPackage = PackageImpl.defaultPackage;
    PackageImpl org = new PackageImpl("org", "the org package", null);
    molgenis = new PackageImpl("molgenis", "the molgenis package", org);
    org.addSubPackage(molgenis);
    PackageImpl molgenis2 = new PackageImpl("molgenis2", "the molgenis2 package", org);
    org.addSubPackage(molgenis2);

    DefaultEntityMetaData personMetaData = new DefaultEntityMetaData("Person");
    personMetaData.setDescription("A person");
    personMetaData.setPackage(molgenis);
    molgenis.addEntity(personMetaData);

    MapEntity personEntity = new MapEntity();
    personEntity.set(EntityMetaDataMetaData.PACKAGE, molgenis.toEntity());
    personEntity.set(EntityMetaDataMetaData.DESCRIPTION, "A person");
    personEntity.set(EntityMetaDataMetaData.FULL_NAME, "org_molgenis_Person");
    personEntity.set(EntityMetaDataMetaData.SIMPLE_NAME, "Person");
    personEntity.set(EntityMetaDataMetaData.ABSTRACT, true);

    DataServiceImpl dataService = new DataServiceImpl();
    metaDataServiceImpl = new MetaDataServiceImpl(dataService);

    AppSettings appSettings = Mockito.mock(AppSettings.class);
    metaDataServiceImpl.setLanguageService(new LanguageService(dataService, appSettings));
    metaDataServiceImpl.setDefaultBackend(manageableCrudRepositoryCollection);

    assertEquals(metaDataServiceImpl.getRootPackages(), Arrays.asList(defaultPackage));
  }
  @BeforeMethod
  public void beforeMethod() {
    owner = new MolgenisUser();
    owner.setUsername("flup");
    owner.setPassword("geheim");
    owner.setId("12345");
    owner.setActive(true);
    owner.setEmail("*****@*****.**");
    owner.setFirstName("Flup");
    owner.setLastName("de Flap");

    DefaultEntityMetaData target1 = new DefaultEntityMetaData("target1");
    target1.addAttribute("id", ROLE_ID);
    DefaultEntityMetaData target2 = new DefaultEntityMetaData("target2");
    target2.addAttribute("id", ROLE_ID);

    mappingProject = new MappingProject("My first mapping project", owner);
    mappingTarget1 = mappingProject.addTarget(target1);
    mappingTarget2 = mappingProject.addTarget(target2);

    Entity mappingTargetEntity = new MapEntity(MappingTargetRepositoryImpl.META_DATA);
    mappingTargetEntity.set(MappingTargetMetaData.TARGET, "target1");
    mappingTargetEntity.set(MappingTargetMetaData.IDENTIFIER, "mappingTargetID1");
    Entity mappingTargetEntity2 = new MapEntity(MappingTargetRepositoryImpl.META_DATA);
    mappingTargetEntity2.set(MappingTargetMetaData.TARGET, "target2");
    mappingTargetEntity2.set(MappingTargetMetaData.IDENTIFIER, "mappingTargetID2");
    mappingTargetEntities = asList(mappingTargetEntity, mappingTargetEntity2);

    mappingProjectEntity = new MapEntity(META_DATA);
    mappingProjectEntity.set(IDENTIFIER, "mappingProjectID");
    mappingProjectEntity.set(MAPPINGTARGETS, mappingTargetEntities);
    mappingProjectEntity.set(OWNER, owner);
    mappingProjectEntity.set(NAME, "My first mapping project");
  }
  @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);
  }
  /** Test that the same attribute name but different attribute settings are resulting in false */
  @Test
  public void canIntegrateEntityMetadataCheckFalse2() {
    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);

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

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

    assertFalse(metaDataService.canIntegrateEntityMetadataCheck(newEntityMetaData));
  }
 @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
  public void integrationTestMetaData2() {
    DataServiceImpl dataServiceImpl = Mockito.mock(DataServiceImpl.class);
    MetaDataServiceImpl metaDataService = new MetaDataServiceImpl(dataServiceImpl);

    DefaultEntityMetaData newEntityMetaData = new DefaultEntityMetaData("attributes");
    newEntityMetaData.addAttribute("ID");

    List<String> skipEntities = Lists.<String>newArrayList("attributes");
    String defaultPackage = "base";
    ImmutableMap<String, EntityMetaData> newEntitiesMetaDataMap = Mockito.mock(ImmutableMap.class);
    when(newEntitiesMetaDataMap.get("attributes")).thenReturn(newEntityMetaData);
    when(newEntitiesMetaDataMap.keySet()).thenReturn(ImmutableSet.of("attributes"));

    LinkedHashMap<String, Boolean> entitiesImportable = new LinkedHashMap<String, Boolean>();
    entitiesImportable.put("attributes", true);
    assertEquals(
        metaDataService.integrationTestMetaData(
            newEntitiesMetaDataMap, skipEntities, defaultPackage),
        entitiesImportable);
  }
  @Test
  public void resolve() {
    DefaultEntityMetaData e1 = new DefaultEntityMetaData("e1");
    DefaultEntityMetaData e2 = new DefaultEntityMetaData("e2");
    DefaultEntityMetaData e3 = new DefaultEntityMetaData("e3");
    DefaultEntityMetaData e4 = new DefaultEntityMetaData("e4");
    DefaultEntityMetaData e5 = new DefaultEntityMetaData("e5");

    e1.addAttribute("ref").setDataType(MolgenisFieldTypes.XREF).setRefEntity(e5);
    e5.setExtends(e3);
    e3.addAttribute("ref").setDataType(MolgenisFieldTypes.XREF).setRefEntity(e4);
    e3.addAttribute("refSelf").setDataType(MolgenisFieldTypes.XREF).setRefEntity(e3);
    e4.addAttribute("ref").setDataType(MolgenisFieldTypes.XREF).setRefEntity(e2);

    List<EntityMetaData> resolved =
        DependencyResolver.resolve(Sets.<EntityMetaData>newHashSet(e1, e2, e3, e4, e5));
    assertEquals(resolved, Arrays.asList(e2, e4, e3, e5, e1));
  }
 @Override
 public EntityMetaData getEntityMetaData() {
   DefaultEntityMetaData entityMetaData = new DefaultEntityMetaData(OmimAnnotator.NAME);
   entityMetaData.addAttribute(OMIM_GENE_SYMBOLS_COL_NAME, ROLE_ID);
   entityMetaData.addAttribute(OMIM_PHENOTYPE_COL_NAME);
   entityMetaData.addAttribute(OMIM_MIM_NUMBER_COL_NAME);
   entityMetaData.addAttribute(OMIM_CYTO_LOCATION_COL_NAME);
   entityMetaData.addAttribute(OMIM_ENTRY_COL_NAME);
   entityMetaData.addAttribute(OMIM_TYPE_COL_NAME);
   return entityMetaData;
 }
  @Test
  public void integrationTestMetaData() {
    DataServiceImpl dataServiceImpl = Mockito.mock(DataServiceImpl.class);
    when(dataServiceImpl.hasRepository("attributes")).thenReturn(Boolean.FALSE); // To skip the
    // canIntegrateEntityMetadataCheck
    // test
    MetaDataServiceImpl metaDataService = new MetaDataServiceImpl(dataServiceImpl);
    RepositoryCollection repositoryCollection = Mockito.mock(RepositoryCollection.class);

    when(repositoryCollection.getEntityNames()).thenReturn(Lists.newArrayList("attributes"));

    DefaultEntityMetaData newEntityMetaData = new DefaultEntityMetaData("attributes");
    newEntityMetaData.addAttribute("ID");
    Repository repo1 = Mockito.mock(Repository.class);
    when(repositoryCollection.getRepository("attributes")).thenReturn(repo1);
    when(repo1.getEntityMetaData()).thenReturn(newEntityMetaData);

    LinkedHashMap<String, Boolean> entitiesImportable = new LinkedHashMap<String, Boolean>();
    entitiesImportable.put("attributes", true);

    assertEquals(entitiesImportable, metaDataService.integrationTestMetaData(repositoryCollection));
  }
  @Test
  public void resolveSelfReferences() {
    DefaultEntityMetaData emd = new DefaultEntityMetaData("Person");
    emd.addAttribute("name").setIdAttribute(true).setNillable(false);
    emd.addAttribute("father")
        .setDataType(MolgenisFieldTypes.XREF)
        .setNillable(true)
        .setRefEntity(emd);
    emd.addAttribute("mother")
        .setDataType(MolgenisFieldTypes.XREF)
        .setNillable(true)
        .setRefEntity(emd);

    Entity piet = new MapEntity("name");
    piet.set("name", "Piet");

    Entity klaas = new MapEntity("name");
    klaas.set("name", "Klaas");
    klaas.set("father", "Piet");
    klaas.set("mother", "Katrijn");

    Entity jan = new MapEntity("name");
    jan.set("name", "Jan");
    jan.set("father", "Piet");
    jan.set("mother", "Marie");

    Entity katrijn = new MapEntity("name");
    katrijn.set("name", "Katrijn");
    katrijn.set("father", "Jan");
    katrijn.set("mother", "Marie");

    Entity marie = new MapEntity("name");
    marie.set("name", "Marie");

    Repository repo = mock(Repository.class);
    when(repo.getName()).thenReturn("Person");
    when(repo.findOne("Piet")).thenReturn(piet);
    when(repo.findOne("Jan")).thenReturn(jan);
    when(repo.findOne("Marie")).thenReturn(marie);
    when(repo.findOne("Katrijn")).thenReturn(katrijn);

    DataServiceImpl ds = new DataServiceImpl();
    ds.addRepository(repo);

    Iterable<Entity> entities =
        Arrays.<Entity>asList(
            new DefaultEntity(emd, ds, piet),
            new DefaultEntity(emd, ds, klaas),
            new DefaultEntity(emd, ds, jan),
            new DefaultEntity(emd, ds, katrijn),
            new DefaultEntity(emd, ds, marie));

    Iterable<Entity> sorted = new DependencyResolver().resolveSelfReferences(entities, emd);
    List<Entity> sortedList = Lists.newArrayList(sorted);
    assertEquals(sortedList.size(), 5);
    assertEquals(sortedList.get(0).getIdValue(), "Marie");
    assertEquals(sortedList.get(1).getIdValue(), "Piet");
    assertEquals(sortedList.get(2).getIdValue(), "Jan");
    assertEquals(sortedList.get(3).getIdValue(), "Katrijn");
    assertEquals(sortedList.get(4).getIdValue(), "Klaas");
  }
  @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));
  }
  @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 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;
  }