Ejemplo n.º 1
0
  public static void removeReplicatorTestDoc(CloudantClient account, String replicatorDocId)
      throws Exception {

    // Grab replicator doc revision using HTTP HEAD command
    String replicatorDb = "_replicator";
    URI uri = URI.create(account.getBaseUri() + replicatorDb + "/" + replicatorDocId);
    HttpConnection head = Http.HEAD(uri);

    // add a response interceptor to allow us to retrieve the ETag revision header
    final AtomicReference<String> revisionRef = new AtomicReference<String>();
    head.responseInterceptors.add(
        new HttpConnectionResponseInterceptor() {

          @Override
          public HttpConnectionInterceptorContext interceptResponse(
              HttpConnectionInterceptorContext context) {
            revisionRef.set(context.connection.getConnection().getHeaderField("ETag"));
            return context;
          }
        });

    account.executeRequest(head);
    String revision = revisionRef.get();
    assertNotNull("The revision should not be null", revision);
    Database replicator = account.database(replicatorDb, false);
    Response removeResponse = replicator.remove(replicatorDocId, revision.replaceAll("\"", ""));

    assertThat(removeResponse.getError(), is(nullValue()));
  }
Ejemplo n.º 2
0
 @Before
 public void setUp() {
   account = CloudantClientHelper.getClient();
   // replciate the animals db for search tests
   com.cloudant.client.api.Replication r = account.replication();
   r.source("https://examples.cloudant.com/animaldb");
   r.createTarget(true);
   r.target(CloudantClientHelper.SERVER_URI.toString() + "/animaldb");
   r.trigger();
   db = account.database("animaldb", false);
 }
Ejemplo n.º 3
0
  public static ReplicatorDocument waitForReplicatorToReachStatus(
      CloudantClient account, String replicatorDocId, String status) throws Exception {
    ReplicatorDocument replicatorDoc = null;

    boolean finished = false;

    long startTime = System.currentTimeMillis();
    long timeout = startTime + TIMEOUT_MILLISECONDS;
    // initial wait of 100 ms
    long delay = 100;

    while (!finished && System.currentTimeMillis() < timeout) {
      // Sleep before finding replication document
      Thread.sleep(delay);

      replicatorDoc = account.replicator().replicatorDocId(replicatorDocId).find();

      // Check if replicator doc is in specified state
      String state;
      if (replicatorDoc != null && (state = replicatorDoc.getReplicationState()) != null) {
        // if we've reached the status or we reached an error then we are finished
        if (state.equalsIgnoreCase(status) || state.equalsIgnoreCase("error")) {
          finished = true;
        }
      }
      // double the delay for the next iteration
      delay *= 2;
    }
    if (!finished) {
      throw new TimeoutException("Timed out waiting for replication to complete");
    }
    return replicatorDoc;
  }
Ejemplo n.º 4
0
  public static void main(String[] args) {
    CloudantClient client =
        new CloudantClient("http://username.cloudant.com", "username", "password");
    Database db = client.database("java-searchindex", false);

    Map<String, Object> animals = new HashMap<>();
    animals.put("index", "function(doc){ index(\"default\", doc._id); }");

    Map<String, Object> indexes = new HashMap<>();
    indexes.put("animals", animals);

    Map<String, Object> ddoc = new HashMap<>();
    ddoc.put("_id", "_design/searchindex");
    ddoc.put("indexes", indexes);

    db.save(ddoc);
  }
Ejemplo n.º 5
0
  // Test case for issue #31
  @Test
  public void customGsonDeserializerTest() {
    Map<String, Object> h = new HashMap<String, Object>();
    h.put("_id", "serializertest");
    h.put("date", "2015-01-23T18:25:43.511Z");
    db.save(h);

    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    account.setGsonBuilder(builder);

    db.find(Foo.class, "serializertest"); // should not throw a JsonSyntaxException
  }
 public Database getDatabase() {
   if (cloudantClient == null) {
     try {
       cloudantClient = new CloudantClient(dbHost, dbUser, dbPassword);
     } catch (Exception e) {
       // TODO: handle exception
     }
   }
   if (db == null) {
     try {
       db = cloudantClient.database(dbName, true);
     } catch (Exception e) {
       // TODO: handle exception
     }
   }
   return db;
 }
Ejemplo n.º 7
0
  @Test
  @Category(RequiresCloudantService.class)
  public void permissions() {
    Map<String, EnumSet<Permissions>> userPerms = db.getPermissions();
    assertNotNull(userPerms);
    ApiKey key = account.generateApiKey();
    EnumSet<Permissions> p = EnumSet.<Permissions>of(Permissions._reader, Permissions._writer);
    db.setPermissions(key.getKey(), p);
    userPerms = db.getPermissions();
    assertNotNull(userPerms);
    assertEquals(userPerms.size(), 1);
    assertEquals(userPerms.get(key.getKey()), p);

    p = EnumSet.noneOf(Permissions.class);
    db.setPermissions(key.getKey(), p);
    userPerms = db.getPermissions();
    assertNotNull(userPerms);
    assertEquals(userPerms.size(), 1);
    assertEquals(userPerms.get(key.getKey()), p);
  }
Ejemplo n.º 8
0
 @After
 public void tearDown() {
   account.deleteDB("animaldb");
   account.shutdown();
 }