Skip to content

Commit

Permalink
Merge pull request #3 from franklaercio/fix-ko-at-start-app
Browse files Browse the repository at this point in the history
fix: the important thing it's work
  • Loading branch information
franklaercio authored Feb 20, 2024
2 parents 6fc40bc + 34d9317 commit 824d80b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 59 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
25 changes: 14 additions & 11 deletions src/main/java/com/github/rinha/controllers/CustomerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,24 @@ public CustomerController(AccountService accountService) {

@GetMapping("/clientes/{id}/extrato")
public Mono<ResponseEntity<StatementDTO>> 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<ResponseEntity<CustomerDTO>> 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());
}
}
}
8 changes: 2 additions & 6 deletions src/main/java/com/github/rinha/services/AccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ public Mono<CustomerDTO> updateBalanceAndInsertTransaction(int id, int value, St
return Mono.just(new CustomerDTO(customer.getMaxLimit(), newBalance));
}

public Mono<Boolean> isValidCustomerId(int id) {
return Mono.just(this.accountPersistence.existsCustomerById(id));
}

public Mono<Boolean> isTransactionValid(TransactionRequest transaction) {
return transaction.isRequestValid() ? Mono.just(true): Mono.error(UnprocessableException::new);
public Boolean isValidCustomerId(int id) {
return this.accountPersistence.existsCustomerById(id);
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 5 additions & 31 deletions src/test/java/com/github/rinha/AccountServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -88,12 +88,10 @@ void shouldVerifyCustomerId() {
when(this.accountPersistence.existsCustomerById(anyInt())).thenReturn(true);

// When
Mono<Boolean> 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
Expand All @@ -102,33 +100,9 @@ void shouldVerifyIfIsInvalidCustomerId() {
when(this.accountPersistence.existsCustomerById(anyInt())).thenReturn(false);

// When
Mono<Boolean> 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<Boolean> isTransactionValid = this.accountService.isTransactionValid(new TransactionRequest("1", "c", "test"));

// Then
StepVerifier.create(isTransactionValid)
.expectNextMatches(b -> b.equals(true))
.verifyComplete();
}

@Test
void shouldVerifyIfIsTransactionInvalid() {
// When
Mono<Boolean> isTransactionValid = this.accountService.isTransactionValid(new TransactionRequest("c", "c", "test"));

// Then
StepVerifier.create(isTransactionValid)
.expectErrorMatches(throwable -> throwable instanceof UnprocessableException)
.verify();
assertEquals(false, isCustomerValid);
}
}
16 changes: 6 additions & 10 deletions src/test/java/com/github/rinha/CustomerControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)));

Expand All @@ -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();
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 824d80b

Please sign in to comment.