public void testRawAddition() throws Exception {
    final FundsMutationSubjectRepository subjectRepository = bundle.fundsMutationSubjects();

    FundsMutationSubject parts =
        FundsMutationSubject.builder(subjectRepository)
            .setName("Parts")
            .setType(FundsMutationSubject.Type.PRODUCT)
            .build();
    subjectRepository.rawAddition(parts);
    final Long partsId = subjectRepository.currentSeqValue();
    assertEquals("Parts not found after insert", parts, subjectRepository.getById(partsId).get());
    try {
      parts =
          FundsMutationSubject.builder(subjectRepository)
              .setName("Parts")
              .setType(FundsMutationSubject.Type.OCCASION)
              .build();
      subjectRepository.rawAddition(parts);
      fail("Name unique index failed on \"Parts\"");
    } catch (Exception ignored) {
    }
    try {
      parts =
          FundsMutationSubject.builder(subjectRepository)
              .setName("Parts2")
              .setType(FundsMutationSubject.Type.OCCASION)
              .setId(partsId)
              .build();
      subjectRepository.rawAddition(parts);
      fail("Insert on used id passed");
    } catch (Exception ignored) {
    }
  }
  public void testAddSubject() throws Exception {
    final FundsMutationSubjectRepository subjectRepository = bundle.fundsMutationSubjects();

    FundsMutationSubject cars =
        FundsMutationSubject.builder(subjectRepository)
            .setName("Cars")
            .setType(FundsMutationSubject.Type.PRODUCT)
            .build();
    subjectRepository.addSubject(cars);
    final Optional<FundsMutationSubject> cars1 = subjectRepository.findByName("Cars");
    final int carsId = (int) cars1.get().id.getAsLong();
    assertTrue(
        "Parent and root did not initialize", cars1.get().parentId == 0 && cars1.get().rootId == 0);
    FundsMutationSubject mitsubishi =
        FundsMutationSubject.builder(subjectRepository)
            .setName("Mitsubishi")
            .setType(FundsMutationSubject.Type.PRODUCT)
            .setParentId(carsId)
            .build();
    subjectRepository.addSubject(mitsubishi);
    final Optional<FundsMutationSubject> mitsubishi1 = subjectRepository.findByName("Mitsubishi");
    assertTrue("Root id of child did not initialize", mitsubishi1.get().rootId == carsId);
    final Optional<FundsMutationSubject> cars2 = subjectRepository.findByName("Cars");
    assertTrue("Child flag did not update", cars2.get().childFlag);
  }
  public void testSearchByString() throws Exception {
    final FundsMutationSubjectRepository subjectRepository = bundle.fundsMutationSubjects();

    FundsMutationSubject clothes =
        FundsMutationSubject.builder(subjectRepository)
            .setName("Clothes")
            .setType(FundsMutationSubject.Type.PRODUCT)
            .build();
    subjectRepository.addSubject(clothes);
    FundsMutationSubject cleaners =
        FundsMutationSubject.builder(subjectRepository)
            .setName("Cleaners")
            .setType(FundsMutationSubject.Type.PRODUCT)
            .build();
    subjectRepository.addSubject(cleaners);
    ImmutableList<FundsMutationSubject> found = subjectRepository.nameLikeSearch("Cl%");
    assertTrue(
        "Search strange result " + found.get(0),
        found.get(0).equals(cleaners) || found.get(0).equals(clothes));
    assertTrue(
        "Search strange result " + found.get(1),
        found.get(1).equals(cleaners) || found.get(1).equals(clothes));
    found = subjectRepository.nameLikeSearch("%s");
    assertTrue(
        "Search strange result " + found.get(0),
        found.get(0).equals(cleaners) || found.get(0).equals(clothes));
    assertTrue(
        "Search strange result " + found.get(1),
        found.get(1).equals(cleaners) || found.get(1).equals(clothes));
    found = subjectRepository.nameLikeSearch("%ers%");
    assertTrue("Search strange result " + found.get(0), found.get(0).equals(cleaners));
  }
 @Override
 public FundsMutationSubject rawAddition(final FundsMutationSubject subject) {
   if (subject.id.isPresent()) {
     final int idConcrete = (int) subject.id.getAsLong();
     checkState(
         PseudoTable.nonUniqueIndexedInsert(
                 table,
                 parentIndex,
                 idConcrete,
                 key -> subject,
                 Optional.ofNullable(subject.parentId > 0 ? (int) subject.parentId : null),
                 Optional.empty(),
                 o ->
                     checkState(
                         nameUniqueIndex.putIfAbsent(subject.name, (Integer) o) == null,
                         "Name %s already occupied",
                         subject.name))
             .equals(subject),
         "Id %s occupied",
         idConcrete);
     return subject;
   } else {
     return rawAddition(
         FundsMutationSubject.builder(this)
             .setFundsMutationSubject(subject)
             .setId(idSequence.incrementAndGet())
             .build());
   }
 }
 @Override
 public void clear() {
   table.clear();
   nameUniqueIndex.clear();
   parentIndex.clear();
   addSubject(FundsMutationSubject.getCurrencyConversionDifferenceSubject(INSTANCE));
 }
  public void testFindByName() throws Exception {
    final FundsMutationSubjectRepository subjectRepository = bundle.fundsMutationSubjects();

    FundsMutationSubject med =
        FundsMutationSubject.builder(subjectRepository)
            .setName("Medical")
            .setType(FundsMutationSubject.Type.SERVICE)
            .build();
    subjectRepository.addSubject(med);
    assertEquals("Grooming not found", med, subjectRepository.findByName("Medical").get());
    assertFalse("Non existent found", subjectRepository.findByName("TEstDTdgS").isPresent());
  }
  public void testFindById() throws Exception {
    final FundsMutationSubjectRepository subjectRepository = bundle.fundsMutationSubjects();

    FundsMutationSubject con =
        FundsMutationSubject.builder(subjectRepository)
            .setName("Grooming")
            .setType(FundsMutationSubject.Type.SERVICE)
            .setId(100)
            .build();
    subjectRepository.addSubject(con);
    assertEquals("Grooming not found", con, subjectRepository.getById(100L).get());
  }
  public void testUpdateChildFlag() throws Exception {
    final FundsMutationSubjectRepository subjectRepository = bundle.fundsMutationSubjects();

    FundsMutationSubject con =
        FundsMutationSubject.builder(subjectRepository)
            .setName("Consulting")
            .setType(FundsMutationSubject.Type.SERVICE)
            .build();
    subjectRepository.addSubject(con);
    final Long id = subjectRepository.currentSeqValue();
    subjectRepository.updateChildFlag(id);
    assertTrue("Child flag not updated", subjectRepository.getById(id).get().childFlag);
  }
  public void testFindByParent() throws Exception {
    final FundsMutationSubjectRepository subjectRepository = bundle.fundsMutationSubjects();

    try {
      FundsMutationSubject food =
          FundsMutationSubject.builder(subjectRepository)
              .setName("Food")
              .setType(FundsMutationSubject.Type.PRODUCT)
              .build();
      subjectRepository.addSubject(food);
    } catch (Exception ignore) {
    }
    final FundsMutationSubject food1 = subjectRepository.findByName("Food").get();
    final FundsMutationSubject meat =
        FundsMutationSubject.builder(subjectRepository)
            .setName("Meat")
            .setType(FundsMutationSubject.Type.PRODUCT)
            .setParentId(food1.id.getAsLong())
            .build();
    final FundsMutationSubject vegs =
        FundsMutationSubject.builder(subjectRepository)
            .setName("Vegs")
            .setType(FundsMutationSubject.Type.PRODUCT)
            .setParentId(food1.id.getAsLong())
            .build();
    subjectRepository.addSubject(meat);
    subjectRepository.addSubject(vegs);
    subjectRepository
        .findByParent(food1.id.getAsLong())
        .forEach(
            new Consumer<FundsMutationSubject>() {
              @Override
              public void accept(FundsMutationSubject subject) {
                assertTrue(
                    "Wrong byParent stream: " + subject.name,
                    subject.equals(meat) || subject.equals(vegs));
              }
            });
  }
 public void testDescription() throws Exception {
   final FundsMutationSubjectRepository subjectRepository = bundle.fundsMutationSubjects();
   final String descCheck = "Don't you dare!";
   FundsMutationSubject withDesc =
       FundsMutationSubject.builder(subjectRepository)
           .setType(FundsMutationSubject.Type.PRODUCT)
           .setName("Cucumber")
           .setDescription(descCheck)
           .build();
   subjectRepository.rawAddition(withDesc);
   assertEquals(
       "Description don't match",
       descCheck,
       subjectRepository.findByName("Cucumber").get().description);
 }
 @Override
 public void updateChildFlag(long id) {
   final long start = System.currentTimeMillis();
   FundsMutationSubject inter;
   while (!table.replace(
       (int) id,
       inter = table.get((int) id),
       FundsMutationSubject.builder(this)
           .setFundsMutationSubject(inter)
           .setChildFlag(true)
           .build())) {
     checkState(
         System.currentTimeMillis() - start < 10000,
         "Record unbelievably busy: unable to set child flag to record with id %s",
         id);
   }
 }
 static {
   INSTANCE = new FundsMutationSubjectPseudoTable();
   INSTANCE.addSubject(FundsMutationSubject.getCurrencyConversionDifferenceSubject(INSTANCE));
 }