Search in sources :

Example 6 with Expense

use of com.example.expenses.model.Expense 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 7 with Expense

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

the class GetExpensesHandler method handleRequest.

@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) {
    LOG.info("Retrieving all expenses");
    List<Expense> expenses = EXPENSE_AGGREGATE.getAllExpenses();
    try {
        return new APIGatewayProxyResponseEvent().withStatusCode(200).withBody(OBJECT_MAPPER.writeValueAsString(expenses));
    } catch (JsonProcessingException e) {
        return new APIGatewayProxyResponseEvent().withStatusCode(500).withBody("Error in converting result to JSON");
    }
}
Also used : Expense(com.example.expenses.model.Expense) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 8 with Expense

use of com.example.expenses.model.Expense 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 9 with Expense

use of com.example.expenses.model.Expense 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)

Example 10 with Expense

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

the class CreateExpenseHandlerTest method testGetExpenses.

@Test
@DisplayName("Sending a valid request to create an expense must create and return a valid expense")
void testGetExpenses() {
    APIGatewayProxyResponseEvent result = testHandler.handleRequest(new APIGatewayProxyRequestEvent().withBody("{\n" + "    \"email\": \"me@me.com\",\n" + "    \"amount\": 100.0\n" + " }"), testContext);
    assertThat(result.getStatusCode()).isEqualTo(200);
    assertThat(result.getBody()).isNotNull();
    System.out.println("RESPONSE: " + result.getBody());
    try {
        Expense expense = OBJECT_MAPPER.readValue(result.getBody(), Expense.class);
        assertThat(expense).isNotNull();
    } catch (JsonProcessingException e) {
        fail("did not expect json error");
    }
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) Expense(com.example.expenses.model.Expense) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

Expense (com.example.expenses.model.Expense)12 Person (com.example.expenses.model.Person)7 DisplayName (org.junit.jupiter.api.DisplayName)6 Test (org.junit.jupiter.api.Test)6 APIGatewayProxyResponseEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)5 APIGatewayProxyRequestEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent)2 SQSEventNotifier (com.example.expenses.lambda.aws.SQSEventNotifier)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 BigDecimal (java.math.BigDecimal)1 Expression (software.amazon.awssdk.enhanced.dynamodb.Expression)1 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)1