Search in sources :

Example 11 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 12 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 = 204, message = "Successful operation"), @ApiResponse(code = 400, message = "Invalid account id supplied") })
public Response removeEmail(@PathParam(ID_PARAM_NAME) final UUID accountId, @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 List<AccountEmail> emails = accountUserApi.getEmails(accountId, context.createTenantContextWithAccountId(accountId, request));
    for (final AccountEmail cur : emails) {
        if (cur.getEmail().equals(email)) {
            final AccountEmailJson accountEmailJson = new AccountEmailJson(accountId, email);
            final AccountEmail accountEmail = accountEmailJson.toAccountEmail(cur.getId());
            accountUserApi.removeEmail(accountId, accountEmail, context.createCallContextWithAccountId(accountId, createdBy, reason, comment, request));
        }
    }
    return Response.status(Status.NO_CONTENT).build();
}
Also used : AccountEmailJson(org.killbill.billing.jaxrs.json.AccountEmailJson) AccountEmail(org.killbill.billing.account.api.AccountEmail) 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)

Example 13 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", response = AccountEmailJson.class, responseContainer = "List")
@ApiResponses(value = { @ApiResponse(code = 201, message = "Email created successfully"), @ApiResponse(code = 400, message = "Invalid account id supplied"), @ApiResponse(code = 404, message = "Account not found") })
public Response addEmail(@PathParam(ID_PARAM_NAME) final UUID accountId, final AccountEmailJson json, @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.createCallContextWithAccountId(accountId, createdBy, reason, comment, request);
    // 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) 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 14 with AccountEmail

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

the class AccountResource method getEmails.

/*
     * *************************     EMAILS     *****************************
     */
@TimedResource
@GET
@Path("/{accountId:" + UUID_PATTERN + "}/" + EMAILS)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve an account emails", response = AccountEmailJson.class, responseContainer = "List")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id supplied") })
public Response getEmails(@PathParam(ID_PARAM_NAME) final UUID accountId, @javax.ws.rs.core.Context final HttpServletRequest request) {
    final List<AccountEmail> emails = accountUserApi.getEmails(accountId, context.createTenantContextWithAccountId(accountId, request));
    final List<AccountEmailJson> emailsJson = new ArrayList<AccountEmailJson>();
    for (final AccountEmail email : emails) {
        emailsJson.add(new AccountEmailJson(email.getAccountId(), email.getEmail()));
    }
    return Response.status(Status.OK).entity(emailsJson).build();
}
Also used : AccountEmailJson(org.killbill.billing.jaxrs.json.AccountEmailJson) AccountEmail(org.killbill.billing.account.api.AccountEmail) ArrayList(java.util.ArrayList) Path(javax.ws.rs.Path) TimedResource(org.killbill.commons.metrics.TimedResource) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

AccountEmail (org.killbill.billing.account.api.AccountEmail)14 UUID (java.util.UUID)8 ApiOperation (io.swagger.annotations.ApiOperation)6 ApiResponses (io.swagger.annotations.ApiResponses)6 Path (javax.ws.rs.Path)6 Produces (javax.ws.rs.Produces)6 DefaultAccountEmail (org.killbill.billing.account.api.DefaultAccountEmail)6 TimedResource (org.killbill.commons.metrics.TimedResource)6 Test (org.testng.annotations.Test)5 AccountEmailJson (org.killbill.billing.jaxrs.json.AccountEmailJson)4 ArrayList (java.util.ArrayList)3 Predicate (com.google.common.base.Predicate)2 LinkedList (java.util.LinkedList)2 Consumes (javax.ws.rs.Consumes)2 DELETE (javax.ws.rs.DELETE)2 GET (javax.ws.rs.GET)2 POST (javax.ws.rs.POST)2 AccountEmailModelDao (org.killbill.billing.account.dao.AccountEmailModelDao)2 AuditLog (org.killbill.billing.util.audit.AuditLog)2 CallContext (org.killbill.billing.util.callcontext.CallContext)2