@Test
  public void alias_TagsByAlias() throws Exception {
    cleanUpGraph();
    SystemUser su = registerSystemUser("alias_Simple");
    Fortress fortress =
        fortressService.registerFortress(
            su.getCompany(), new FortressInputBean("alias_Simple", true));

    TagInputBean tagInput = new TagInputBean("TagA", "AliasTest", "rlxA");

    EntityInputBean inputBean =
        new EntityInputBean(fortress, "*****@*****.**", "CompanyNode", DateTime.now(), "AAA");
    inputBean.addTag(tagInput);
    // Creating the tag for an entity
    mediationFacade.trackEntity(su.getCompany(), inputBean).getEntity();

    Tag tag = tagService.findTag(su.getCompany(), null, tagInput.getCode());
    assertNotNull(tag);

    // The above is the setup.

    // Alias nodes are not stored in the _Tag bucket; then are connected to a _Tag. Since we're not
    // searching by a Tag type(Label)
    // we can't find JUST by the Alias.key

    // Now create an alias for TagA such that when we track a new entity with zzz as the tag value
    // the entity will be mapped to TagA
    tagService.createAlias(su.getCompany(), tag, "AliasTest", "zzz");

    // Make sure creating it twice doesn't cause an error
    tagService.createAlias(su.getCompany(), tag, "AliasTest", "zzz");

    // An alias exists for this tag that points to TagA.
    tagInput = new TagInputBean("zzz", "AliasTest", "rlxA");
    inputBean =
        new EntityInputBean(fortress, "*****@*****.**", "CompanyNode", DateTime.now(), "BBB");
    inputBean.addTag(tagInput);
    mediationFacade.trackEntity(su.getCompany(), inputBean).getEntity();
    Tag aliasTag = tagService.findTag(su.getCompany(), "AliasTest", null, "zzz");
    assertNotNull(aliasTag);
    assertEquals(
        "The call to find tag with an alias should find the aliased tag",
        tag.getId(),
        aliasTag.getId());

    assertEquals(
        "Couldn't find via case-insensitive check",
        2,
        entityTagService.findEntityTags(su.getCompany(), tag.getCode().toLowerCase()).size());
    assertEquals(
        "Couldn't find via case-insensitive check",
        2,
        entityTagService.findEntityTags(su.getCompany(), tag.getCode().toUpperCase()).size());
  }
  @Test
  public void merge_Simple() throws Exception {
    cleanUpGraph();
    SystemUser su = registerSystemUser("merge_Simple");
    Fortress fortress =
        fortressService.registerFortress(
            su.getCompany(), new FortressInputBean("merge_Simple", true));

    TagInputBean tagInputA = new TagInputBean("TagA", "MoveTag", "rlxA");
    TagInputBean tagInputB = new TagInputBean("TagB", "MoveTag", "rlxB");

    EntityInputBean inputBean =
        new EntityInputBean(fortress, "*****@*****.**", "CompanyNode", DateTime.now(), "AAA");
    inputBean.addTag(tagInputA);
    Entity entityA = mediationFacade.trackEntity(su.getCompany(), inputBean).getEntity();
    inputBean =
        new EntityInputBean(fortress, "*****@*****.**", "CompanyNode", DateTime.now(), "BBB");
    inputBean.addTag(tagInputB);
    Entity entityB = mediationFacade.trackEntity(su.getCompany(), inputBean).getEntity();

    assertEquals(1, entityTagService.getEntityTags(entityA).size());
    assertEquals(1, entityTagService.getEntityTags(entityB).size());

    Tag tagA = tagService.findTag(su.getCompany(), null, tagInputA.getCode());
    assertNotNull(tagA);
    Tag tagB = tagService.findTag(su.getCompany(), null, tagInputB.getCode());
    assertNotNull(tagB);

    // The above is the setup. We will look to merge tagA into tagB. The end result will be that
    // entity

    Collection<Long> results = entityTagService.mergeTags(tagA.getId(), tagB.getId());
    assertEquals("One Entity should have been affected by this operation", 1, results.size());
    Long entityResult = results.iterator().next();
    assertEquals("The wrong Entity was affected by this operation", entityA.getId(), entityResult);

    entityA = entityService.getEntity(su.getCompany(), entityA.getMetaKey());
    Collection<EntityTag> tags = entityTagService.getEntityTags(entityA);
    assertEquals(1, tags.size());
    assertEquals(tagInputB.getName(), tags.iterator().next().getTag().getName());

    assertNull(
        "TagA should have been deleted",
        tagService.findTag(su.getCompany(), null, tagInputA.getCode()));
    tags = entityTagService.getEntityTags(entityB);
    assertEquals(1, tags.size());
    assertEquals(tagInputB.getName(), tags.iterator().next().getTag().getName());

    assertEquals(2, entityTagService.findEntityTags(su.getCompany(), tagInputB.getCode()).size());

    // assertEquals("rlxA", tags.iterator().next().);

  }
  //    @Test
  // @Repeat(value = 1)
  public void entitiesUnderLoad() throws Exception {
    // This test suffered under DAT-348 and was quarantined.
    String companyName = "entitiesUnderLoad";
    setSecurity();
    SystemUser su = registerSystemUser(companyName, "entitiesUnderLoad");

    Fortress fortress =
        fortressService.registerFortress(
            su.getCompany(), new FortressInputBean("entitiesUnderLoad", true));
    String docType = "entitiesUnderLoad";

    int tagCount = 1; // unique tags per entity - tags are shared across the entities
    int docCount = 1; // how many entities to create per thread
    // Tried reducing threadMax
    int threadMax = 3; // Each thread will create a unique document type
    ArrayList<TagInputBean> tags = getTags(tagCount, false);

    Collection<Tag> createdTags =
        tagService.findTags(fortress.getCompany(), tags.get(0).getLabel());
    assertEquals("Database is not in a cleared down state", 0, createdTags.size());

    Map<Integer, EntityRunner> runners = new HashMap<>();

    CountDownLatch latch = new CountDownLatch(threadMax);
    CountDownLatch startSignal = new CountDownLatch(1);
    for (int thread = 0; thread < threadMax; thread++) {
      EntityRunner runner =
          addEntityRunner(
              thread + 1,
              su,
              fortress,
              docType,
              "ABC" + thread,
              docCount,
              tags,
              latch,
              startSignal);
      runners.put(thread, runner);
    }
    startSignal.countDown();
    latch.await();
    Tag found = null;

    for (int thread = 0; thread < threadMax; thread++) {
      assertEquals("Thread " + (thread + 1), true, runners.get(thread).isWorked());
      for (int count = 0; count < docCount; count++) {
        // Thread.sleep(2000);
        Entity entity =
            entityService.findByCode(
                su.getCompany(), fortress.getName(), docType, "ABC" + thread + "" + count);
        assertNotNull(entity);
        Collection<EntityTag> entityTags = entityTagService.findEntityTags(entity);
        if (entityTags.size() == 0) {
          logger.debug("Why is this 0?");
        }
        assertEquals(tagCount, entityTags.size());
        // Make sure every thread's tags point to the same tag
        if (found == null) found = entityTags.iterator().next().getTag();
        else
          assertEquals(
              found.toString() + " / " + entityTags.iterator().next().getTag().toString(),
              found.getId(),
              entityTags.iterator().next().getTag().getId());
      }
    }
    assertNotNull(
        tagService.findTag(fortress.getCompany(), "Deadlock", null, tags.get(0).getCode()));

    createdTags = tagService.findTags(fortress.getCompany(), "Deadlock");
    assertEquals(false, createdTags.isEmpty());
    if (createdTags.size() != tagCount) {

      for (Tag createdTag : createdTags) {
        // logger.info(createdTag.toString());
        logger.info("Finding... {}", createdTag.toString());
        Tag xtra =
            tagService.findTag(su.getCompany(), createdTag.getLabel(), null, createdTag.getCode());

        logger.info(xtra.toString());
      }
    }
    assertEquals(tagCount, createdTags.size());
  }