@Test
  public void test_report_returns_provider_id_error() {

    when(sender.sendPostToServer(SERVER_URL, RESPONSE_HAPPY_PATH_DATA))
        .thenReturn(new ApiHttpResponse(403, PROVIDER_KEY_INVALID_ERROR_RESPONSE));

    ApiTransaction[] transactions = new ApiTransaction[2];
    HashMap<String, String> metrics0 = new HashMap<String, String>();
    metrics0.put("hits", "1");
    metrics0.put("transfer", "4500");

    HashMap<String, String> metrics1 = new HashMap<String, String>();
    metrics1.put("hits", "1");
    metrics1.put("transfer", "2840");

    transactions[0] = new ApiTransaction("bce4c8f4", "2009-01-01 14:23:08", metrics0);
    transactions[1] = new ApiTransaction("bad7e480", "2009-01-01 18:11:59", metrics1);

    try {
      server.report(transactions);
      fail("Should have thrown ApiException");
    } catch (ApiException e) {
      assertEquals(e.getErrorCode(), "provider_key_invalid");
      assertEquals(e.getErrorMessage(), "Provider key \"abcd1234\" is invalid");
    }
  }
  @Test
  public void test_provider_key_invalid_on_authorize() throws ApiException {

    when(sender.sendGetToServer(
            SERVER_URL
                + "/transactions/authorize.xml"
                + "?app_id="
                + APP_ID
                + "&provider_key="
                + PROVIDER_KEY))
        .thenReturn(new ApiHttpResponse(403, PROVIDER_KEY_INVALID_ERROR_RESPONSE));

    AuthorizeResponse response = null;
    try {
      response = server.authorize(null, null);
      fail("Should have thrown ApiException");
    } catch (ApiException e) {
      assertEquals("provider_key_invalid", e.getErrorCode());
      assertEquals("Provider key \"abcd1234\" is invalid", e.getErrorMessage());
    }
  }
  @Test
  public void test_application_not_found_on_authorize() throws ApiException {

    when(sender.sendGetToServer(
            SERVER_URL
                + "/transactions/authorize.xml"
                + "?app_id="
                + APP_ID
                + "&provider_key="
                + PROVIDER_KEY))
        .thenReturn(new ApiHttpResponse(404, APPLICATION_ID_ERROR_RESPONSE));

    AuthorizeResponse response = null;
    try {
      response = server.authorize(null, null);
      fail("Should have thrown ApiException");
    } catch (ApiException e) {
      assertEquals("application_not_found", e.getErrorCode());
      assertEquals("Application with id=\"12345678\" was not found", e.getErrorMessage());
    }
  }
  @Test
  public void test_referrer_is_sent_for_authorize() throws ApiException {

    when(sender.sendGetToServer(
            SERVER_URL
                + "/transactions/authorize.xml"
                + "?app_id="
                + APP_ID
                + "&provider_key="
                + PROVIDER_KEY
                + "&app_key="
                + APP_KEY
                + "&referrer="
                + REFERRER_IP))
        .thenReturn(new ApiHttpResponse(200, HAPPY_PATH_RESPONSE));

    AuthorizeResponse response = server.authorize(APP_KEY, REFERRER_IP);
    assertEquals(true, response.getAuthorized());
    assertEquals("Basic", response.getPlan());
    assertEquals("", response.getReason());
  }
  @Test
  public void test_authorize_exceeded_path() throws ApiException {

    when(sender.sendGetToServer(
            SERVER_URL
                + "/transactions/authorize.xml"
                + "?app_id="
                + APP_ID
                + "&provider_key="
                + PROVIDER_KEY
                + "&app_key="
                + APP_KEY))
        .thenReturn(new ApiHttpResponse(200, EXCEEDED_PATH_RESPONSE));

    AuthorizeResponse response = server.authorize(APP_KEY, null);
    assertEquals(false, response.getAuthorized());
    assertEquals("Pro", response.getPlan());
    assertEquals("Usage limits are exceeded", response.getReason());
    assertEquals(2, response.getUsageReports().size());

    assertUsageRecord(
        response.getUsageReports().get(0),
        "hits",
        "month",
        "2010-08-01 00:00:00 +00:00",
        "2010-09-01 00:00:00 +00:00",
        "17344",
        "20000",
        false);

    assertUsageRecord(
        response.getUsageReports().get(1),
        "hits",
        "day",
        "2010-08-04 00:00:00 +00:00",
        "2010-08-05 00:00:00 +00:00",
        "732",
        "1000",
        true);
  }
  @Test
  public void test_report_happy_path() throws ApiException {

    when(sender.sendPostToServer(SERVER_URL, RESPONSE_HAPPY_PATH_DATA))
        .thenReturn(new ApiHttpResponse(202, null));

    ApiTransaction[] transactions = new ApiTransaction[2];
    HashMap<String, String> metrics0 = new HashMap<String, String>();
    metrics0.put("hits", "1");
    metrics0.put("transfer", "4500");

    HashMap<String, String> metrics1 = new HashMap<String, String>();
    metrics1.put("hits", "1");
    metrics1.put("transfer", "2840");

    transactions[0] = new ApiTransaction("bce4c8f4", "2009-01-01 14:23:08", metrics0);
    transactions[1] = new ApiTransaction("bad7e480", "2009-01-01 18:11:59", metrics1);

    assertEquals(
        RESPONSE_HAPPY_PATH_DATA, ((Api2Impl) server).formatPostData(transactions).toString());

    server.report(transactions);
  }