Search in sources :

Example 6 with AccountEmail

use of org.killbill.billing.account.api.AccountEmail in project killbill by killbill.

the class TestAccountDao method testAddAndRemoveMultipleAccountEmails.

@Test(groups = "slow", description = "Test Account DAO: add and remove multiple emails")
public void testAddAndRemoveMultipleAccountEmails() throws AccountApiException {
    final UUID accountId = UUID.randomUUID();
    final String email1 = UUID.randomUUID().toString();
    final String email2 = UUID.randomUUID().toString();
    // Verify the original state
    Assert.assertEquals(accountDao.getEmailsByAccountId(accountId, internalCallContext).size(), 0);
    // Add a new e-mail
    final AccountEmail accountEmail1 = new DefaultAccountEmail(accountId, email1);
    accountDao.addEmail(new AccountEmailModelDao(accountEmail1), internalCallContext);
    final List<AccountEmailModelDao> firstEmails = accountDao.getEmailsByAccountId(accountId, internalCallContext);
    Assert.assertEquals(firstEmails.size(), 1);
    Assert.assertEquals(firstEmails.get(0).getAccountId(), accountId);
    Assert.assertEquals(firstEmails.get(0).getEmail(), email1);
    // Add a second e-mail
    final AccountEmail accountEmail2 = new DefaultAccountEmail(accountId, email2);
    accountDao.addEmail(new AccountEmailModelDao(accountEmail2), internalCallContext);
    final List<AccountEmailModelDao> secondEmails = accountDao.getEmailsByAccountId(accountId, internalCallContext);
    Assert.assertEquals(secondEmails.size(), 2);
    Assert.assertTrue(secondEmails.get(0).getAccountId().equals(accountId));
    Assert.assertTrue(secondEmails.get(1).getAccountId().equals(accountId));
    Assert.assertTrue(secondEmails.get(0).getEmail().equals(email1) || secondEmails.get(0).getEmail().equals(email2));
    Assert.assertTrue(secondEmails.get(1).getEmail().equals(email1) || secondEmails.get(1).getEmail().equals(email2));
    // Delete the first e-mail
    accountDao.removeEmail(new AccountEmailModelDao(accountEmail1), internalCallContext);
    final List<AccountEmailModelDao> thirdEmails = accountDao.getEmailsByAccountId(accountId, internalCallContext);
    Assert.assertEquals(thirdEmails.size(), 1);
    Assert.assertEquals(thirdEmails.get(0).getAccountId(), accountId);
    Assert.assertEquals(thirdEmails.get(0).getEmail(), email2);
    // Verify audits
    final List<AuditLog> auditLogsForAccountEmail1 = auditDao.getAuditLogsForId(TableName.ACCOUNT_EMAIL, accountEmail1.getId(), AuditLevel.FULL, internalCallContext);
    Assert.assertEquals(auditLogsForAccountEmail1.size(), 2);
    Assert.assertEquals(auditLogsForAccountEmail1.get(0).getChangeType(), ChangeType.INSERT);
    Assert.assertEquals(auditLogsForAccountEmail1.get(1).getChangeType(), ChangeType.DELETE);
    final List<AuditLog> auditLogsForAccountEmail2 = auditDao.getAuditLogsForId(TableName.ACCOUNT_EMAIL, accountEmail2.getId(), AuditLevel.FULL, internalCallContext);
    Assert.assertEquals(auditLogsForAccountEmail2.size(), 1);
    Assert.assertEquals(auditLogsForAccountEmail2.get(0).getChangeType(), ChangeType.INSERT);
}
Also used : DefaultAccountEmail(org.killbill.billing.account.api.DefaultAccountEmail) AccountEmail(org.killbill.billing.account.api.AccountEmail) DefaultAccountEmail(org.killbill.billing.account.api.DefaultAccountEmail) UUID(java.util.UUID) AuditLog(org.killbill.billing.util.audit.AuditLog) Test(org.testng.annotations.Test)

Example 7 with AccountEmail

use of org.killbill.billing.account.api.AccountEmail in project killbill by killbill.

the class TestAccountDao method testHandleDuplicateEmails.

@Test(groups = "slow", description = "Test Account DAO: duplicate emails throws an exception")
public void testHandleDuplicateEmails() throws AccountApiException {
    final UUID accountId = UUID.randomUUID();
    final AccountEmail email = new DefaultAccountEmail(accountId, "test@gmail.com");
    Assert.assertEquals(accountDao.getEmailsByAccountId(accountId, internalCallContext).size(), 0);
    final AccountEmailModelDao accountEmailModelDao = new AccountEmailModelDao(email);
    accountDao.addEmail(accountEmailModelDao, internalCallContext);
    Assert.assertEquals(accountDao.getEmailsByAccountId(accountId, internalCallContext).size(), 1);
    try {
        accountDao.addEmail(accountEmailModelDao, internalCallContext);
        Assert.fail();
    } catch (AccountApiException e) {
        Assert.assertEquals(e.getCode(), ErrorCode.ACCOUNT_EMAIL_ALREADY_EXISTS.getCode());
    }
}
Also used : DefaultAccountEmail(org.killbill.billing.account.api.DefaultAccountEmail) AccountEmail(org.killbill.billing.account.api.AccountEmail) DefaultAccountEmail(org.killbill.billing.account.api.DefaultAccountEmail) AccountApiException(org.killbill.billing.account.api.AccountApiException) UUID(java.util.UUID) Test(org.testng.annotations.Test)

Example 8 with AccountEmail

use of org.killbill.billing.account.api.AccountEmail in project killbill by killbill.

the class AccountResource method addEmail.

@TimedResource
@POST
@Path("/{accountId:" + UUID_PATTERN + "}/" + EMAILS)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Add account email")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id supplied"), @ApiResponse(code = 404, message = "Account not found") })
public Response addEmail(final AccountEmailJson json, @PathParam(ID_PARAM_NAME) final String id, @HeaderParam(HDR_CREATED_BY) final String createdBy, @HeaderParam(HDR_REASON) final String reason, @HeaderParam(HDR_COMMENT) final String comment, @javax.ws.rs.core.Context final HttpServletRequest request, @javax.ws.rs.core.Context final UriInfo uriInfo) throws AccountApiException {
    verifyNonNullOrEmpty(json, "AccountEmailJson body should be specified");
    verifyNonNullOrEmpty(json.getEmail(), "AccountEmailJson email needs to be set");
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);
    final UUID accountId = UUID.fromString(id);
    // Make sure the account exist or we will confuse the history and auditing code
    accountUserApi.getAccountById(accountId, callContext);
    // Make sure the email doesn't exist
    final AccountEmail existingEmail = Iterables.<AccountEmail>tryFind(accountUserApi.getEmails(accountId, callContext), new Predicate<AccountEmail>() {

        @Override
        public boolean apply(final AccountEmail input) {
            return input.getEmail().equals(json.getEmail());
        }
    }).orNull();
    if (existingEmail == null) {
        accountUserApi.addEmail(accountId, json.toAccountEmail(UUIDs.randomUUID()), callContext);
    }
    return uriBuilder.buildResponse(uriInfo, AccountResource.class, "getEmails", json.getAccountId(), request);
}
Also used : AccountEmail(org.killbill.billing.account.api.AccountEmail) UUID(java.util.UUID) CallContext(org.killbill.billing.util.callcontext.CallContext) Predicate(com.google.common.base.Predicate) Path(javax.ws.rs.Path) TimedResource(org.killbill.commons.metrics.TimedResource) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 9 with AccountEmail

use of org.killbill.billing.account.api.AccountEmail in project killbill by killbill.

the class AccountResource method removeEmail.

@TimedResource
@DELETE
@Path("/{accountId:" + UUID_PATTERN + "}/" + EMAILS + "/{email}")
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Delete email from account")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id supplied") })
public Response removeEmail(@PathParam(ID_PARAM_NAME) final String id, @PathParam("email") final String email, @HeaderParam(HDR_CREATED_BY) final String createdBy, @HeaderParam(HDR_REASON) final String reason, @HeaderParam(HDR_COMMENT) final String comment, @javax.ws.rs.core.Context final HttpServletRequest request) {
    final UUID accountId = UUID.fromString(id);
    final List<AccountEmail> emails = accountUserApi.getEmails(accountId, context.createContext(request));
    for (final AccountEmail cur : emails) {
        if (cur.getEmail().equals(email)) {
            final AccountEmailJson accountEmailJson = new AccountEmailJson(accountId.toString(), email);
            final AccountEmail accountEmail = accountEmailJson.toAccountEmail(cur.getId());
            accountUserApi.removeEmail(accountId, accountEmail, context.createContext(createdBy, reason, comment, request));
        }
    }
    return Response.status(Status.OK).build();
}
Also used : AccountEmailJson(org.killbill.billing.jaxrs.json.AccountEmailJson) AccountEmail(org.killbill.billing.account.api.AccountEmail) UUID(java.util.UUID) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) TimedResource(org.killbill.commons.metrics.TimedResource) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

AccountEmail (org.killbill.billing.account.api.AccountEmail)9 UUID (java.util.UUID)7 Test (org.testng.annotations.Test)5 DefaultAccountEmail (org.killbill.billing.account.api.DefaultAccountEmail)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 TimedResource (org.killbill.commons.metrics.TimedResource)3 ArrayList (java.util.ArrayList)2 AccountEmailJson (org.killbill.billing.jaxrs.json.AccountEmailJson)2 AuditLog (org.killbill.billing.util.audit.AuditLog)2 Predicate (com.google.common.base.Predicate)1 IOException (java.io.IOException)1 Consumes (javax.ws.rs.Consumes)1 DELETE (javax.ws.rs.DELETE)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 AccountApiException (org.killbill.billing.account.api.AccountApiException)1 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)1