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