Search in sources :

Example 1 with Person

use of com.example.expenses.model.Person in project aws-java-serverless by hermanlintvelt.

the class ExpenseAggregate method createMockedData.

public void createMockedData() {
    Person me = new Person("me@me.com");
    createExpense(new Expense(BigDecimal.valueOf(150.0), me));
    createExpense(new Expense(BigDecimal.valueOf(234.0), me));
    createExpense(new Expense(BigDecimal.valueOf(234.0), new Person("you@me.com")));
}
Also used : Expense(com.example.expenses.model.Expense) Person(com.example.expenses.model.Person)

Example 2 with Person

use of com.example.expenses.model.Person in project aws-java-serverless by hermanlintvelt.

the class CreateExpenseHandlerJsonNode method handleRequest.

@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) {
    LOG.info("Creating an expense from request: " + request.toString());
    try {
        JsonNode jsonNode = OBJECT_MAPPER.readTree(request.getBody());
        String email = jsonNode.get("email").asText();
        BigDecimal amount = BigDecimal.valueOf(jsonNode.get("amount").asDouble());
        Expense newExpense = EXPENSE_AGGREGATE.createExpense(new Expense(amount, new Person(email)));
        LOG.info("Created expense: " + newExpense);
        return new APIGatewayProxyResponseEvent().withStatusCode(200).withBody(OBJECT_MAPPER.writeValueAsString(newExpense));
    } catch (JsonProcessingException e) {
        LOG.error("Error parsing request body to create new expense", e);
        return new APIGatewayProxyResponseEvent().withStatusCode(400).withBody("Invalid Expense object in request body.");
    }
}
Also used : Expense(com.example.expenses.model.Expense) JsonNode(com.fasterxml.jackson.databind.JsonNode) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Person(com.example.expenses.model.Person) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) BigDecimal(java.math.BigDecimal)

Example 3 with Person

use of com.example.expenses.model.Person in project aws-java-serverless by hermanlintvelt.

the class CreateExpenseHandlerRequestObject method handleRequest.

@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) {
    LOG.info("Creating an expense from request: " + request.toString());
    try {
        CreateExpenseRequest createExpenseRequest = OBJECT_MAPPER.readValue(request.getBody(), CreateExpenseRequest.class);
        Expense newExpense = EXPENSE_AGGREGATE.createExpense(new Expense(BigDecimal.valueOf(createExpenseRequest.getAmount()), new Person(createExpenseRequest.getEmail())));
        LOG.info("Created expense: " + newExpense);
        return new APIGatewayProxyResponseEvent().withStatusCode(200).withBody(OBJECT_MAPPER.writeValueAsString(newExpense));
    } catch (JsonProcessingException e) {
        LOG.error("Error parsing request body to create new expense", e);
        return new APIGatewayProxyResponseEvent().withStatusCode(400).withBody("Invalid Expense object in request body.");
    }
}
Also used : Expense(com.example.expenses.model.Expense) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Person(com.example.expenses.model.Person) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 4 with Person

use of com.example.expenses.model.Person in project aws-java-serverless by hermanlintvelt.

the class ExpenseAggregateTest method createExpense.

@Test
@DisplayName("Creating test should result in expense existing in service")
void createExpense() {
    Person me = new Person("me@me.com");
    Expense expense = new Expense(BigDecimal.valueOf(150.0), me);
    expenseAggregate.createExpense(expense);
    assertThat(expenseAggregate.getAllExpenses()).asList().contains(expense);
}
Also used : Expense(com.example.expenses.model.Expense) Person(com.example.expenses.model.Person) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 5 with Person

use of com.example.expenses.model.Person in project aws-java-serverless by hermanlintvelt.

the class ExpenseAggregateTest method findExpensesPaidBy.

@Test
@DisplayName("If two expenses are created via service, the service must found them for person")
void findExpensesPaidBy() {
    Person me = new Person("me@me.com");
    Expense expense1 = new Expense(BigDecimal.valueOf(150.0), me);
    expenseAggregate.createExpense(expense1);
    Expense expense2 = new Expense(BigDecimal.valueOf(80.0), me);
    expenseAggregate.createExpense(expense2);
    List<Expense> foundExpenses = expenseAggregate.findExpensesPaidBy(me.getEmail());
    assertThat(foundExpenses).isNotNull();
    assertThat(foundExpenses).asList().isNotEmpty();
    assertThat(foundExpenses).asList().contains(expense1, expense2);
}
Also used : Expense(com.example.expenses.model.Expense) Person(com.example.expenses.model.Person) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

Person (com.example.expenses.model.Person)9 Expense (com.example.expenses.model.Expense)7 DisplayName (org.junit.jupiter.api.DisplayName)6 Test (org.junit.jupiter.api.Test)6 APIGatewayProxyResponseEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 BigDecimal (java.math.BigDecimal)1