@Test
  public void testSelectRequestMethod() {
    HttpString _method = new HttpString("UNKNOWN");
    assertEquals(RequestContext.METHOD.OTHER, RequestContext.selectRequestMethod(_method));

    _method = new HttpString("GET");
    assertEquals(RequestContext.METHOD.GET, RequestContext.selectRequestMethod(_method));

    _method = new HttpString("PATCH");
    assertEquals(RequestContext.METHOD.PATCH, RequestContext.selectRequestMethod(_method));
  }
  @Test
  public void test_FILE_selectRequestType() {
    String[] pathTokens = "/db/mybucket.files/123".split("/");
    assertEquals(RequestContext.TYPE.FILE, RequestContext.selectRequestType(pathTokens));

    pathTokens = "/db/mybucket.files/123/binary".split("/");
    assertEquals(RequestContext.TYPE.FILE_BINARY, RequestContext.selectRequestType(pathTokens));

    pathTokens = "/db/mybucket.files/123/456".split("/");
    assertEquals(RequestContext.TYPE.FILE, RequestContext.selectRequestType(pathTokens));
  }
  /**
   * @param exchange
   * @param context
   * @throws Exception
   */
  @Override
  public void handleRequest(HttpServerExchange exchange, RequestContext context) throws Exception {
    DBObject content = context.getContent();

    // cannot PATCH with no data
    if (content == null) {
      ResponseHelper.endExchangeWithMessage(
          exchange, HttpStatus.SC_NOT_ACCEPTABLE, "data is empty");
      return;
    }

    // cannot PATCH an array
    if (content instanceof BasicDBList) {
      ResponseHelper.endExchangeWithMessage(
          exchange, HttpStatus.SC_NOT_ACCEPTABLE, "data cannot be an array");
      return;
    }

    Object id = context.getDocumentId();

    if (content.get("_id") == null) {
      content.put("_id", id);
    } else if (!content.get("_id").equals(id)) {
      ResponseHelper.endExchangeWithMessage(
          exchange,
          HttpStatus.SC_NOT_ACCEPTABLE,
          "_id in json data cannot be different than id in URL");
      return;
    }

    ObjectId requestEtag = RequestHelper.getWriteEtag(exchange);

    OperationResult result =
        documentDAO.upsertDocument(
            context.getDBName(),
            context.getCollectionName(),
            context.getDocumentId(),
            content,
            requestEtag,
            true);

    if (result.getEtag() != null) {
      exchange.getResponseHeaders().put(Headers.ETAG, result.getEtag().toString());
    } else {
      ResponseHelper.endExchangeWithMessage(
          exchange,
          HttpStatus.SC_CONFLICT,
          "The document's ETag must be provided using the '" + Headers.IF_MATCH + "' header");
      return;
    }

    // send the warnings if any (and in case no_content change the return code to ok
    if (context.getWarnings() != null && !context.getWarnings().isEmpty()) {
      sendWarnings(result.getHttpCode(), exchange, context);
    } else {
      exchange.setResponseCode(result.getHttpCode());
    }

    exchange.endExchange();
  }
  @Test
  public void testGetMappedRequestUri() {
    HttpServerExchange ex = mock(HttpServerExchange.class);
    when(ex.getRequestPath()).thenReturn("/");
    when(ex.getRequestMethod()).thenReturn(HttpString.EMPTY);

    String whatUri = "/mydb/mycollection";
    String whereUri = "/";

    RequestContext context = new RequestContext(ex, whereUri, whatUri);
    assertEquals("/mydb/mycollection", context.getMappedRequestUri());

    whatUri = "*";
    whereUri = "/data";

    context = new RequestContext(ex, whereUri, whatUri);
    assertEquals("/", context.getMappedRequestUri());
  }
  @Test
  public void testSelectRequestType() {
    String[] pathTokens = "/".split("/");
    assertEquals(RequestContext.TYPE.ROOT, RequestContext.selectRequestType(pathTokens));

    pathTokens = "/db".split("/");
    assertEquals(RequestContext.TYPE.DB, RequestContext.selectRequestType(pathTokens));

    pathTokens = "/db/collection".split("/");
    assertEquals(RequestContext.TYPE.COLLECTION, RequestContext.selectRequestType(pathTokens));

    pathTokens = "/db/collection/document".split("/");
    assertEquals(RequestContext.TYPE.DOCUMENT, RequestContext.selectRequestType(pathTokens));

    pathTokens = "/db/collection/_indexes".split("/");
    assertEquals(
        RequestContext.TYPE.COLLECTION_INDEXES, RequestContext.selectRequestType(pathTokens));

    pathTokens = "/db/collection/_indexes/123".split("/");
    assertEquals(RequestContext.TYPE.INDEX, RequestContext.selectRequestType(pathTokens));

    pathTokens = "/db/collection/_aggrs/test".split("/");
    assertEquals(RequestContext.TYPE.AGGREGATION, RequestContext.selectRequestType(pathTokens));
  }
 @Test
 public void test_COLLECTION_FILES_selectRequestType() {
   String[] pathTokens = "/db/mybucket.files".split("/");
   assertEquals(RequestContext.TYPE.FILES_BUCKET, RequestContext.selectRequestType(pathTokens));
 }