Skip to content

Commit

Permalink
revert: 6fc40bc
Browse files Browse the repository at this point in the history
  • Loading branch information
franklaercio committed Feb 21, 2024
1 parent a52205c commit d81eea8
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 30 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +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'
deploy:
resources:
limits:
Expand Down
2 changes: 1 addition & 1 deletion nginx.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
events {
worker_connections 400;
worker_connections 1000;
}

http {
Expand Down
25 changes: 11 additions & 14 deletions src/main/java/com/github/rinha/controllers/CustomerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,21 @@ public CustomerController(AccountService accountService) {

@GetMapping("/clientes/{id}/extrato")
public Mono<ResponseEntity<StatementDTO>> getExtratoByClienteId(@PathVariable int id) {
if (!accountService.isValidCustomerId(id))
return Mono.just(ResponseEntity.status(HttpStatus.NOT_FOUND).build());

return accountService.findStatementByCustomerId(id)
return Mono.just(id)
.filterWhen(accountService::isValidCustomerId)
.flatMap(accountService::findStatementByCustomerId)
.map(ResponseEntity::ok)
.onErrorReturn(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build());
.defaultIfEmpty(ResponseEntity.status(HttpStatus.NOT_FOUND).build());
}

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

public Boolean isValidCustomerId(int id) {
return this.accountPersistence.existsCustomerById(id);
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);
}
}
3 changes: 1 addition & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://${DB_HOSTNAME:localhost}:5432/${POSTGRES_DB:rinha}
spring.datasource.username=${POSTGRES_USER:admin}
spring.datasource.password=${DATABASE_PASSWORD:123}
spring.datasource.hikari.maximum-pool-size=10
spring.main.banner-mode=off
spring.datasource.hikari.maximum-pool-size=15
36 changes: 31 additions & 5 deletions src/test/java/com/github/rinha/AccountServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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 @@ -19,7 +20,6 @@

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,10 +88,12 @@ void shouldVerifyCustomerId() {
when(this.accountPersistence.existsCustomerById(anyInt())).thenReturn(true);

// When
Boolean isCustomerValid = this.accountService.isValidCustomerId(1);
Mono<Boolean> isCustomerValid = this.accountService.isValidCustomerId(1);

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

@Test
Expand All @@ -100,9 +102,33 @@ void shouldVerifyIfIsInvalidCustomerId() {
when(this.accountPersistence.existsCustomerById(anyInt())).thenReturn(false);

// When
Boolean isCustomerValid = this.accountService.isValidCustomerId(1);
Mono<Boolean> isCustomerValid = this.accountService.isValidCustomerId(1);

// Then
assertEquals(false, isCustomerValid);
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();
}
}
16 changes: 10 additions & 6 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(true);
when(accountService.isValidCustomerId(anyInt())).thenReturn(Mono.just(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(false);
when(accountService.isValidCustomerId(anyInt())).thenReturn(Mono.just(false));

// When and Then
controller.getExtratoByClienteId(123)
Expand All @@ -62,7 +62,8 @@ void shouldBeCreateTransaction() {
// Given
TransactionRequest transaction = new TransactionRequest("100", "c", "test");

when(accountService.isValidCustomerId(anyInt())).thenReturn(true);
when(accountService.isValidCustomerId(anyInt())).thenReturn(Mono.just(true));
when(accountService.isTransactionValid(transaction)).thenReturn(Mono.just(true));
when(accountService.updateBalanceAndInsertTransaction(anyInt(), anyInt(), anyString(), anyString()))
.thenReturn(Mono.just(new CustomerDTO(123, 10)));

Expand All @@ -71,6 +72,7 @@ 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 @@ -81,7 +83,7 @@ void shouldBeReturnNotFoundWhenCustomerIdIsInvalid() {
// Given
TransactionRequest transaction = new TransactionRequest("100", "c", "test");

when(accountService.isValidCustomerId(anyInt())).thenReturn(false);
when(accountService.isValidCustomerId(anyInt())).thenReturn(Mono.just(false));

// When and Then
controller.transacionar(123, transaction)
Expand All @@ -94,8 +96,10 @@ void shouldBeReturnNotFoundWhenCustomerIdIsInvalid() {
@Test
void shouldBeReturnUnprocessableEntityWhenTransactionIsInvalid() {
// Given
TransactionRequest transaction = new TransactionRequest("100", "e", "test");
when(accountService.isValidCustomerId(anyInt())).thenReturn(true);
TransactionRequest transaction = new TransactionRequest("100", "c", "test");

when(accountService.isValidCustomerId(anyInt())).thenReturn(Mono.just(true));
when(accountService.isTransactionValid(transaction)).thenReturn(Mono.just(false));

// When and Then
controller.transacionar(123, transaction)
Expand Down

0 comments on commit d81eea8

Please sign in to comment.