@Override
 public MutateWherePath useKeysValues(String... keys) {
   if (keys.length == 1) {
     return useKeys(s(keys[0]));
   }
   return useKeys(JsonArray.from((Object[]) keys));
 }
  @Test
  public void testConstructorWithEmptyCollectionOverwrites() {
    JsonArrayDocument preExisting = JsonArrayDocument.create(uuid, JsonArray.from("test", "test2"));
    bucket.upsert(preExisting);

    Map<String, Object> map =
        new CouchbaseMap<Object>(uuid, bucket, Collections.<String, Object>emptyMap());

    assertEquals(0, map.size());
  }
  @Test
  public void testConstructorWithCollectionDataOverwrites() {
    JsonArrayDocument preExisting = JsonArrayDocument.create(uuid, JsonArray.from("test", "test2"));
    bucket.upsert(preExisting);

    Map<String, Object> map =
        new CouchbaseMap<Object>(uuid, bucket, Collections.singletonMap("foo", "bar"));

    assertEquals(1, map.size());
    assertTrue(map.containsKey("foo"));
    assertEquals("bar", map.get("foo"));
  }
  private void key(Iterator<Object> iterator) {
    if (!iterator.hasNext() && treeCount > 1) {
      throw new IllegalArgumentException("Not enough parameters for key");
    } else if (!iterator.hasNext()) {
      // probably pattern like findAllByUsername(), just apply query without parameters
      return;
    }

    Object next = iterator.next();
    if (next instanceof String) {
      query.key((String) next);
    } else if (next instanceof Boolean) {
      query.key((Boolean) next);
    } else if (next instanceof Double) {
      query.key((Double) next);
    } else if (next instanceof Integer) {
      query.key((Integer) next);
    } else if (next instanceof Long) {
      query.key((Long) next);
    } else if (next instanceof Collection) {
      // when creating a JsonArray, the from(List) method is preferred because it will convert
      // internal
      // Lists and Maps to JsonObject and JsonArray respectively
      List<Object> arrayContent = new ArrayList<Object>((Collection) next);
      query.key(JsonArray.from(arrayContent));
    } else if (next.getClass().isArray()) {
      List<Object> arrayContent = Arrays.asList((Object[]) next);
      query.key(JsonArray.from(arrayContent));
    } else if (next
        instanceof
        JsonArray) { // discouraged, since it's leaking store-specifics in the method signature
      query.key((JsonArray) next);
    } else if (next
        instanceof
        JsonObject) { // discouraged, since it's leaking store-specifics in the method signature
      query.key((JsonObject) next);
    } else {
      throw new IllegalArgumentException("Unsupported parameter type for key: " + next.getClass());
    }
  }
  @Test
  public void testConstructorWithPreExistingDocumentOfWrongTypeFails() {
    JsonArrayDocument preExisting = JsonArrayDocument.create(uuid, JsonArray.from("test"));
    bucket.upsert(preExisting);

    Map<String, Object> map = new CouchbaseMap<Object>(uuid, bucket);
    try {
      map.size();
      fail("Expected TranscodingException");
    } catch (TranscodingException e) {
      // expected
    }
  }
 @Test
 public void shouldConcatMoreParams() {
   ViewQuery query =
       ViewQuery.from("design", "view")
           .descending()
           .debug()
           .development()
           .group()
           .reduce(false)
           .startKey(JsonArray.from("foo", true));
   assertEquals(
       "reduce=false&group=true&debug=true&descending=true&startkey=%5B%22foo%22%2Ctrue%5D",
       query.toString());
 }
  private JsonArray in(Iterator<Object> iterator) {
    if (!iterator.hasNext()) {
      throw new IllegalArgumentException("Not enough parameters for in");
    }
    Object next = iterator.next();

    List<Object> values;
    if (next instanceof Collection) {
      values = new ArrayList<Object>((Collection<?>) next);
    } else if (next.getClass().isArray()) {
      values = Arrays.asList((Object[]) next);
    } else {
      values = Collections.singletonList(next);
    }
    // using the from(List) method ensure that contained Lists and Maps will be converted to
    // JsonArrays and JsonObjects
    return JsonArray.from(values);
  }
  @Test
  public void shouldHandleEndKey() {
    ViewQuery query = ViewQuery.from("design", "view").endKey("key");
    assertEquals("endkey=%22key%22", query.toString());

    query = ViewQuery.from("design", "view").endKey(1);
    assertEquals("endkey=1", query.toString());

    query = ViewQuery.from("design", "view").endKey(true);
    assertEquals("endkey=true", query.toString());

    query = ViewQuery.from("design", "view").endKey(3.55);
    assertEquals("endkey=3.55", query.toString());

    query = ViewQuery.from("design", "view").endKey(JsonArray.from("foo", 3));
    assertEquals("endkey=%5B%22foo%22%2C3%5D", query.toString());

    query = ViewQuery.from("design", "view").endKey(JsonObject.empty().put("foo", true));
    assertEquals("endkey=%7B%22foo%22%3Atrue%7D", query.toString());
  }
 @Test
 public void shouldHandleKeys() {
   ViewQuery query = ViewQuery.from("design", "view").keys(JsonArray.from("foo", 3, true));
   assertEquals("keys=%5B%22foo%22%2C3%2Ctrue%5D", query.toString());
 }