From 34d93174fe1cba30b6ecabcdb869311b5e502259 Mon Sep 17 00:00:00 2001 From: Frank Date: Tue, 20 Feb 2024 11:21:15 -0300 Subject: [PATCH] fix: the important thing it's work --- docker-compose.yml | 2 +- .../rinha/controllers/CustomerController.java | 25 +++++++------ .../github/rinha/services/AccountService.java | 8 ++--- src/main/resources/application.properties | 1 + .../com/github/rinha/AccountServiceTest.java | 36 +++---------------- .../github/rinha/CustomerControllerTest.java | 16 ++++----- 6 files changed, 29 insertions(+), 59 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 1bf031f..5bd02c4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -56,7 +56,7 @@ services: - "5432:5432" volumes: - ./script.sql:/docker-entrypoint-initdb.d/script.sql - command: 'postgres -c max_connections=1000 -c work_mem=8MB -c hot_standby=off -c shared_buffers=135MB -c checkpoint_timeout=1d -c wal_level=minimal -c synchronous_commit=off -c fsync=off -c full_page_writes=off -c max_wal_senders=0' + # command: 'postgres -c max_connections=1000 -c work_mem=8MB -c hot_standby=off -c shared_buffers=135MB -c checkpoint_timeout=1d -c wal_level=minimal -c synchronous_commit=off -c fsync=off -c full_page_writes=off -c max_wal_senders=0' deploy: resources: limits: diff --git a/src/main/java/com/github/rinha/controllers/CustomerController.java b/src/main/java/com/github/rinha/controllers/CustomerController.java index fece3dd..5eed014 100644 --- a/src/main/java/com/github/rinha/controllers/CustomerController.java +++ b/src/main/java/com/github/rinha/controllers/CustomerController.java @@ -20,21 +20,24 @@ public CustomerController(AccountService accountService) { @GetMapping("/clientes/{id}/extrato") public Mono> getExtratoByClienteId(@PathVariable int id) { - return Mono.just(id) - .filterWhen(accountService::isValidCustomerId) - .flatMap(accountService::findStatementByCustomerId) + if (!accountService.isValidCustomerId(id)) + return Mono.just(ResponseEntity.status(HttpStatus.NOT_FOUND).build()); + + return accountService.findStatementByCustomerId(id) .map(ResponseEntity::ok) - .defaultIfEmpty(ResponseEntity.status(HttpStatus.NOT_FOUND).build()); + .onErrorReturn(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build()); } @PostMapping("/clientes/{id}/transacoes") public Mono> transacionar(@PathVariable int id, @RequestBody TransactionRequest transaction) { - return Mono.just(id) - .filterWhen(accountService::isValidCustomerId) - .flatMap(unused -> accountService.isTransactionValid(transaction)) - .flatMap(clientId -> accountService.updateBalanceAndInsertTransaction(id, transaction.parseValueToInt(), transaction.tipo(), transaction.descricao())) - .map(ResponseEntity::ok) - .switchIfEmpty(Mono.just(ResponseEntity.status(HttpStatus.NOT_FOUND).build())) - .onErrorReturn(ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).build()); + if (!accountService.isValidCustomerId(id)) { + return Mono.just(ResponseEntity.status(HttpStatus.NOT_FOUND).build()); + } else if (!transaction.isRequestValid()) { + return Mono.just(ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).build()); + } else { + return accountService.updateBalanceAndInsertTransaction(id, transaction.parseValueToInt(), transaction.tipo(), transaction.descricao()) + .map(ResponseEntity::ok) + .onErrorReturn(ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).build()); + } } } diff --git a/src/main/java/com/github/rinha/services/AccountService.java b/src/main/java/com/github/rinha/services/AccountService.java index 77823ba..62c707c 100644 --- a/src/main/java/com/github/rinha/services/AccountService.java +++ b/src/main/java/com/github/rinha/services/AccountService.java @@ -43,11 +43,7 @@ public Mono updateBalanceAndInsertTransaction(int id, int value, St return Mono.just(new CustomerDTO(customer.getMaxLimit(), newBalance)); } - public Mono isValidCustomerId(int id) { - return Mono.just(this.accountPersistence.existsCustomerById(id)); - } - - public Mono isTransactionValid(TransactionRequest transaction) { - return transaction.isRequestValid() ? Mono.just(true): Mono.error(UnprocessableException::new); + public Boolean isValidCustomerId(int id) { + return this.accountPersistence.existsCustomerById(id); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index a5f7dde..498eff3 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -3,3 +3,4 @@ spring.datasource.url=jdbc:postgresql://${DB_HOSTNAME:localhost}:5432/${POSTGRES spring.datasource.username=${POSTGRES_USER:admin} spring.datasource.password=${DATABASE_PASSWORD:123} spring.datasource.hikari.maximum-pool-size=15 +spring.main.banner-mode=off \ No newline at end of file diff --git a/src/test/java/com/github/rinha/AccountServiceTest.java b/src/test/java/com/github/rinha/AccountServiceTest.java index 6dbfde8..982b24f 100644 --- a/src/test/java/com/github/rinha/AccountServiceTest.java +++ b/src/test/java/com/github/rinha/AccountServiceTest.java @@ -3,7 +3,6 @@ import com.github.rinha.domain.Customer; import com.github.rinha.domain.dtos.CustomerDTO; import com.github.rinha.domain.dtos.StatementDTO; -import com.github.rinha.domain.dtos.TransactionRequest; import com.github.rinha.domain.dtos.TransactionResponse; import com.github.rinha.domain.exceptions.UnprocessableException; import com.github.rinha.persistence.AccountPersistence; @@ -20,6 +19,7 @@ import java.util.List; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.doNothing; @@ -88,12 +88,10 @@ void shouldVerifyCustomerId() { when(this.accountPersistence.existsCustomerById(anyInt())).thenReturn(true); // When - Mono isCustomerValid = this.accountService.isValidCustomerId(1); + Boolean isCustomerValid = this.accountService.isValidCustomerId(1); // Then - StepVerifier.create(isCustomerValid) - .expectNextMatches(b -> b.equals(true)) - .verifyComplete(); + assertEquals(true, isCustomerValid); } @Test @@ -102,33 +100,9 @@ void shouldVerifyIfIsInvalidCustomerId() { when(this.accountPersistence.existsCustomerById(anyInt())).thenReturn(false); // When - Mono isCustomerValid = this.accountService.isValidCustomerId(1); + Boolean isCustomerValid = this.accountService.isValidCustomerId(1); // Then - StepVerifier.create(isCustomerValid) - .expectNextMatches(b -> b.equals(false)) - .verifyComplete(); - } - - @Test - void shouldVerifyIfIsTransactionValid() { - // When - Mono isTransactionValid = this.accountService.isTransactionValid(new TransactionRequest("1", "c", "test")); - - // Then - StepVerifier.create(isTransactionValid) - .expectNextMatches(b -> b.equals(true)) - .verifyComplete(); - } - - @Test - void shouldVerifyIfIsTransactionInvalid() { - // When - Mono isTransactionValid = this.accountService.isTransactionValid(new TransactionRequest("c", "c", "test")); - - // Then - StepVerifier.create(isTransactionValid) - .expectErrorMatches(throwable -> throwable instanceof UnprocessableException) - .verify(); + assertEquals(false, isCustomerValid); } } diff --git a/src/test/java/com/github/rinha/CustomerControllerTest.java b/src/test/java/com/github/rinha/CustomerControllerTest.java index 74afc86..493efcf 100644 --- a/src/test/java/com/github/rinha/CustomerControllerTest.java +++ b/src/test/java/com/github/rinha/CustomerControllerTest.java @@ -31,7 +31,7 @@ public class CustomerControllerTest { @Test void shouldBeRetrieveStatement() { // Given - when(accountService.isValidCustomerId(anyInt())).thenReturn(Mono.just(true)); + when(accountService.isValidCustomerId(anyInt())).thenReturn(true); when(accountService.findStatementByCustomerId(anyInt())).thenReturn(Mono.just(new StatementDTO(new BalanceDTO(10, 10), List.of(new TransactionResponse(1, "c", "test", "2024-02-19"))))); // When and Then @@ -47,7 +47,7 @@ void shouldBeRetrieveStatement() { @Test void shouldBeReturnNotFoundWhenCustomerIdIsInvalidForStatement() { // Given - when(accountService.isValidCustomerId(anyInt())).thenReturn(Mono.just(false)); + when(accountService.isValidCustomerId(anyInt())).thenReturn(false); // When and Then controller.getExtratoByClienteId(123) @@ -62,8 +62,7 @@ void shouldBeCreateTransaction() { // Given TransactionRequest transaction = new TransactionRequest("100", "c", "test"); - when(accountService.isValidCustomerId(anyInt())).thenReturn(Mono.just(true)); - when(accountService.isTransactionValid(transaction)).thenReturn(Mono.just(true)); + when(accountService.isValidCustomerId(anyInt())).thenReturn(true); when(accountService.updateBalanceAndInsertTransaction(anyInt(), anyInt(), anyString(), anyString())) .thenReturn(Mono.just(new CustomerDTO(123, 10))); @@ -72,7 +71,6 @@ void shouldBeCreateTransaction() { .doOnNext(response -> { assert response.getStatusCodeValue() == HttpStatus.OK.value(); verify(accountService, times(1)).isValidCustomerId(123); - verify(accountService, times(1)).isTransactionValid(transaction); verify(accountService, times(1)).updateBalanceAndInsertTransaction(123, transaction.parseValueToInt(), transaction.tipo(), transaction.descricao()); }) .block(); @@ -83,7 +81,7 @@ void shouldBeReturnNotFoundWhenCustomerIdIsInvalid() { // Given TransactionRequest transaction = new TransactionRequest("100", "c", "test"); - when(accountService.isValidCustomerId(anyInt())).thenReturn(Mono.just(false)); + when(accountService.isValidCustomerId(anyInt())).thenReturn(false); // When and Then controller.transacionar(123, transaction) @@ -96,10 +94,8 @@ void shouldBeReturnNotFoundWhenCustomerIdIsInvalid() { @Test void shouldBeReturnUnprocessableEntityWhenTransactionIsInvalid() { // Given - TransactionRequest transaction = new TransactionRequest("100", "c", "test"); - - when(accountService.isValidCustomerId(anyInt())).thenReturn(Mono.just(true)); - when(accountService.isTransactionValid(transaction)).thenReturn(Mono.just(false)); + TransactionRequest transaction = new TransactionRequest("100", "e", "test"); + when(accountService.isValidCustomerId(anyInt())).thenReturn(true); // When and Then controller.transacionar(123, transaction)