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);
}
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());
}
}
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);
}
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();
}
Aggregations