@Test
  public void testGetAccountReferenceFromSeveral() throws Exception {
    doReturn(
            "[{"
                + "roles:[\""
                + SaasAccountServiceProxy.ACCOUNT_OWNER_ROLE
                + "\"],"
                + "accountReference:{id:\""
                + ACCOUNT_ID
                + "\",name:\""
                + ACCOUNT_NAME
                + "\"}"
                + "},{roles:[\""
                + SaasAccountServiceProxy.ACCOUNT_OWNER_ROLE
                + "\"],"
                + "accountReference:{id:\"another-account-id\"}"
                + "}]")
        .when(transport)
        .doGet(endsWith("account"), eq(TOKEN));
    AccountReference accountRef = saasAccountServiceProxy.getAccountWhereUserIsOwner(null, TOKEN);

    assertNotNull(accountRef);
    assertEquals(accountRef.getId(), ACCOUNT_ID);
    assertEquals(accountRef.getName(), ACCOUNT_NAME);
  }
 @Test(expectedExceptions = HttpException.class, expectedExceptionsMessageRegExp = "error")
 public void testGetSubscriptionWhenException() throws IOException {
   doThrow(new HttpException(500, "error"))
       .when(transport)
       .doGet(endsWith("subscription/find/account?id=" + ACCOUNT_ID), eq(TOKEN));
   saasAccountServiceProxy.getSubscription(SUBSCRIPTION, TOKEN, ACCOUNT_ID);
 }
  @Test(expectedExceptions = AuthenticationException.class)
  public void testCheckSubscriptionErrorIfAuthenticationFailed() throws Exception {
    doThrow(new AuthenticationException())
        .when(transport)
        .doGet(endsWith("subscription/find/account?id=accountId"), eq(TOKEN));

    saasAccountServiceProxy.hasValidSubscription(SUBSCRIPTION, TOKEN, ACCOUNT_ID);
  }
  @Test
  public void testGetSubscriptionWhenDescriptorNull() throws IOException {
    doReturn("[]")
        .when(transport)
        .doGet(endsWith("subscription/find/account?id=" + ACCOUNT_ID), eq(TOKEN));

    SubscriptionDescriptor descriptor =
        saasAccountServiceProxy.getSubscription(SUBSCRIPTION, TOKEN, ACCOUNT_ID);
    assertNull(descriptor);
  }
  @Test(
      expectedExceptions = IllegalArgumentException.class,
      expectedExceptionsMessageRegExp = "Can't validate subscription. End date attribute is absent")
  public void testCheckSubscriptionErrorIfEndDateIsAbsent() throws Exception {
    when(transport.doGet(endsWith("account"), eq(TOKEN)))
        .thenReturn("[{roles:[\"account/owner\"],accountReference:{id:accountId}}]");
    when(transport.doGet(endsWith("subscription/find/account?id=accountId"), eq(TOKEN)))
        .thenReturn("[{serviceId:OnPremises,id:subscriptionId,startDate:\"10/12/2012\"}]");

    saasAccountServiceProxy.hasValidSubscription(SUBSCRIPTION, TOKEN, ACCOUNT_ID);
  }
  @Test
  public void testGetAccountReferenceReturnNullIfAccountWasNotFound() throws Exception {
    doReturn(
            "[{"
                + "roles:[\"account/member\"],"
                + "accountReference:{id:\""
                + ACCOUNT_ID
                + "\"}"
                + "}]")
        .when(transport)
        .doGet(endsWith("account"), eq(TOKEN));
    AccountReference accountRef = saasAccountServiceProxy.getAccountWhereUserIsOwner(null, TOKEN);

    assertNull(accountRef);
  }
  @Test
  public void testGetAccountReferenceWithSpecificNameReturnNullIfAccountWasNotFound()
      throws Exception {
    doReturn(
            "[{"
                + "roles:[\""
                + SaasAccountServiceProxy.ACCOUNT_OWNER_ROLE
                + "\"],"
                + "accountReference:{id:\""
                + ACCOUNT_ID
                + "\",name:\""
                + ACCOUNT_NAME
                + "\"}"
                + "}]")
        .when(transport)
        .doGet(endsWith("account"), eq(TOKEN));

    AccountReference accountRef =
        saasAccountServiceProxy.getAccountWhereUserIsOwner("another name", TOKEN);

    assertNull(accountRef);
  }
  @Test
  public void testGetSubscription() throws IOException, JsonParseException {
    final String SUBSCRIPTION_ID = "subscription_id1";
    SimpleDateFormat subscriptionDateFormat =
        new SimpleDateFormat(SaasAccountServiceProxy.SUBSCRIPTION_DATE_FORMAT);

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, -1);
    String startDate = subscriptionDateFormat.format(cal.getTime());

    cal = Calendar.getInstance();
    cal.add(Calendar.DATE, 1);
    String endDate = subscriptionDateFormat.format(cal.getTime());

    String testDescriptorJson =
        "{serviceId:"
            + SaasAccountServiceProxy.ON_PREMISES
            + ",id:"
            + SUBSCRIPTION_ID
            + ",startDate: \""
            + startDate
            + "\",endDate:\""
            + endDate
            + "\"}";
    doReturn("[" + testDescriptorJson + "]")
        .when(transport)
        .doGet(endsWith("subscription/find/account?id=" + ACCOUNT_ID), eq(TOKEN));

    SubscriptionDescriptor descriptor =
        saasAccountServiceProxy.getSubscription(SUBSCRIPTION, TOKEN, ACCOUNT_ID);
    assertNotNull(descriptor);
    assertEquals(descriptor.getServiceId(), "OnPremises");
    assertEquals(descriptor.getId(), "subscription_id1");
    assertEquals(descriptor.getStartDate(), startDate);
    assertEquals(descriptor.getEndDate(), endDate);
    assertTrue(descriptor.getProperties().isEmpty());
    assertTrue(descriptor.getLinks().isEmpty());
  }
  @Test(expectedExceptions = IOException.class)
  public void testGetAccountReferenceErrorIfAuthenticationFailed() throws Exception {
    doThrow(AuthenticationException.class).when(transport).doGet(endsWith("account"), eq(TOKEN));

    saasAccountServiceProxy.getAccountWhereUserIsOwner(null, TOKEN);
  }