@BeforeClass
  public static void setUpClass() {

    // Connect
    bucket = BucketFactory.getBucket();

    // Clean
    try {
      bucket.remove("count::dmaier");
      bucket.remove("count::dostrovsky");
    } catch (DocumentDoesNotExistException e) {
      LOG.warning("Could not remove a nonexistent document");
    }
  }
 @After
 public void deleteDoc() {
   try {
     bucket.remove(uuid);
   } catch (DocumentDoesNotExistException e) {
     // ignore
   }
 }
  @Test
  public final void givenId_whenRead_thenReturnsPerson() {
    // create and insert person document
    String id = insertRandomPersonDocument().id();

    // read person and check results
    assertNotNull(personService.read(id));

    // cleanup
    bucket.remove(id);
  }
  public static boolean flushCleanup(Bucket bucket, long start) {
    for (long i = 0; i <= 999999999; i++) {
      String id = new String("__flush_marker_" + i);
      JsonObject content = JsonObject.empty().put("data", "temp");
      JsonDocument doc = JsonDocument.create(id, content);
      bucket.upsert(doc);

      bucket.remove(id);
    }

    return true;
  }
  @Test
  public final void givenRandomPerson_whenCreate_thenPersonPersisted() {
    // create person
    Person person = randomPerson();
    personService.create(person);

    // check results
    assertNotNull(person.getId());
    assertNotNull(bucket.get(person.getId()));

    // cleanup
    bucket.remove(person.getId());
  }
  @Test
  public final void givenNewHometown_whenUpdate_thenNewHometownPersisted() {
    // create and insert person document
    JsonDocument doc = insertRandomPersonDocument();

    // update person
    Person expected = converter.fromDocument(doc);
    String updatedHomeTown = RandomStringUtils.randomAlphabetic(12);
    expected.setHomeTown(updatedHomeTown);
    personService.update(expected);

    // check results
    JsonDocument actual = bucket.get(expected.getId());
    assertNotNull(actual);
    assertNotNull(actual.content());
    assertEquals(expected.getHomeTown(), actual.content().getString("homeTown"));

    // cleanup
    bucket.remove(expected.getId());
  }
  @Test
  public final void givenIds_whenReadBulk_thenReturnsOnlyPersonsWithMatchingIds() {
    List<String> ids = new ArrayList<>();

    // add some person documents
    for (int i = 0; i < 5; i++) {
      ids.add(insertRandomPersonDocument().id());
    }

    // perform bulk read
    List<Person> persons = personService.readBulk(ids);

    // check results
    for (Person person : persons) {
      assertTrue(ids.contains(person.getId()));
    }

    // cleanup
    for (String id : ids) {
      bucket.remove(id);
    }
  }
  @Test
  public final void givenPersons_whenInsertBulk_thenPersonsAreInserted() {

    // create some persons
    List<Person> persons = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
      persons.add(randomPerson());
    }

    // perform bulk insert
    personService.createBulk(persons);

    // check results
    for (Person person : persons) {
      assertNotNull(bucket.get(person.getId()));
    }

    // cleanup
    for (Person person : persons) {
      bucket.remove(person.getId());
    }
  }
  @Test
  public final void givenPersons_whenUpdateBulk_thenPersonsAreUpdated() {

    List<String> ids = new ArrayList<>();

    // add some person documents
    for (int i = 0; i < 5; i++) {
      ids.add(insertRandomPersonDocument().id());
    }

    // load persons from Couchbase
    List<Person> persons = new ArrayList<>();
    for (String id : ids) {
      persons.add(converter.fromDocument(bucket.get(id)));
    }

    // modify persons
    for (Person person : persons) {
      person.setHomeTown(RandomStringUtils.randomAlphabetic(10));
    }

    // perform bulk update
    personService.updateBulk(persons);

    // check results
    for (Person person : persons) {
      JsonDocument doc = bucket.get(person.getId());
      assertEquals(person.getName(), doc.content().getString("name"));
      assertEquals(person.getHomeTown(), doc.content().getString("homeTown"));
    }

    // cleanup
    for (String id : ids) {
      bucket.remove(id);
    }
  }
 public static boolean singleDocCleanup(Bucket bucket, String id) {
   bucket.remove(id);
   return true;
 }
 public static boolean docCleanup(Bucket bucket, long start) {
   for (long i = start; i <= 999999999; i++) {
     if (bucket.get(Long.toString(i)) != null) bucket.remove(Long.toString(i));
   }
   return true;
 }