Skip to content

Commit

Permalink
Adding all Layers and Transaction service Testing closes #1 Closes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamed-taman committed Sep 6, 2020
1 parent 051fcc6 commit 9ccf256
Show file tree
Hide file tree
Showing 22 changed files with 560 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package org.siriusxi.blueharvest.bank.common.api.dto;

import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

import java.math.BigDecimal;

@Data
@RequiredArgsConstructor
public class TransactionDTO {

@NonNull
private int accountId;

@NonNull
private BigDecimal amount;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.siriusxi.blueharvest.bank.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public final class JsonUtilities {

private JsonUtilities(){}

private static final ObjectMapper mapper = new ObjectMapper();

public static String toJson(Object object) throws IOException {
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return mapper.writeValueAsString(object);
}

public static byte[] toJsonAsByte(Object object) throws IOException {
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return mapper.writeValueAsBytes(object);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.siriusxi.blueharvest.bank.as.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ServiceConfiguration {

@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
package org.siriusxi.blueharvest.bank.as.integration;

import lombok.extern.log4j.Log4j2;
import org.siriusxi.blueharvest.bank.common.api.composite.trx.Transaction;
import org.siriusxi.blueharvest.bank.common.api.dto.TransactionDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

import static org.springframework.http.HttpMethod.GET;

@Component
@Log4j2
public class TransactionIntegration {

public List<Transaction> getAccountTransactions(int id){
return List.of();
private final RestTemplate restTemplate;

private final String transactionServiceUrl;

private final String BASE_URL = "/bank/api/v1/";

private final String QUERY_PARAM = "?accountId=";

@Autowired
public TransactionIntegration(
RestTemplate restTemplate,
@Value("${app.transaction-service.host}") String transactionServiceHost,
@Value("${app.transaction-service.port}") int transactionServicePort) {

this.restTemplate = restTemplate;

transactionServiceUrl = "http://"
.concat(transactionServiceHost)
.concat(":")
.concat(String.valueOf(transactionServicePort))
.concat(BASE_URL)
.concat("transactions");
}

public void createTransaction(TransactionDTO transaction) {
public List<Transaction> getAccountTransactions(int accountId){

String url = transactionServiceUrl.concat(QUERY_PARAM).concat(String.valueOf(accountId));
log.debug("Will call getCustomerAccounts API on URL: {}", url);

return restTemplate.exchange(url, GET, null,
new ParameterizedTypeReference<List<Transaction>>() {}).getBody();
}

public void createTransaction(TransactionDTO transaction) {
restTemplate.postForObject(transactionServiceUrl,transaction, TransactionDTO.class);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.siriusxi.blueharvest.bank.as.persistence.entity.AccountEntity;
import org.siriusxi.blueharvest.bank.as.persistence.AccountRepository;
import org.siriusxi.blueharvest.bank.common.api.composite.account.Account;
import org.siriusxi.blueharvest.bank.common.api.composite.trx.Transaction;
import org.siriusxi.blueharvest.bank.common.api.dto.TransactionDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -40,6 +39,8 @@ public List<Account> getAccounts(int customerId) {

public void createAccount(AccountEntity entity) {
accountRepository.save(entity);
transactionIntegration.createTransaction(new TransactionDTO(entity.getId(),
entity.getBalance()));
log.debug("Account created with ID -----> {}", entity.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ logging.level:
bank: DEBUG
web: DEBUG
root: INFO

# Application specific params
app:
transaction-service:
host: localhost
port: 8092
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.siriusxi.blueharvest.bank.as;

import org.junit.jupiter.api.Test;
import org.siriusxi.blueharvest.bank.as.persistence.AccountRepository;
import org.siriusxi.blueharvest.bank.as.persistence.entity.AccountEntity;
import org.siriusxi.blueharvest.bank.common.api.composite.account.AccountType;
import org.siriusxi.blueharvest.bank.common.api.composite.trx.TransactionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

import java.math.BigDecimal;

import static org.assertj.core.api.Assertions.assertThat;

@DataJpaTest
class AccountRepositoryIntegrationTests {

@Autowired
private AccountRepository accountRepository;

@Test
void whenFindByCustomerId_thenReturnAccount() {
// given
AccountEntity trx = new AccountEntity(99, new BigDecimal("100.20"));
accountRepository.save(trx);

// when
AccountEntity found =
accountRepository.findByCustomerId(trx.getCustomerId()).iterator().next();

// then
assertThat(found.getId()).isEqualTo(1);
assertThat(found.getCustomerId()).isEqualTo(99);
assertThat(found.getBalance()).isEqualTo(new BigDecimal("100.20"));
assertThat(found.getType()).isEqualTo(AccountType.CURRENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public List<CustomerAggregate> getCustomers() {
public void createAccount(@PathVariable int id,
@RequestBody AccountDTO account) {
account.setCustomerId(id);
log.debug("createAccount: creates a new account {} for CustomerId: {}", account.toString(), id);
customerService.createCustomerAccount(account);
log.debug("createAccount: creates a new account {}", account);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.siriusxi.blueharvest.bank.cs.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ServiceConfiguration {

@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,56 @@
package org.siriusxi.blueharvest.bank.cs.integration;

import lombok.extern.log4j.Log4j2;
import org.siriusxi.blueharvest.bank.common.api.composite.account.Account;
import org.siriusxi.blueharvest.bank.common.api.dto.AccountDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

import static org.springframework.http.HttpMethod.GET;

@Component
@Log4j2
public class AccountIntegration {

private final RestTemplate restTemplate;

private final String accountServiceUrl;

private final String BASE_URL = "/bank/api/v1/";

private final String QUERY_PARAM = "?customerId=";

@Autowired
public AccountIntegration(
RestTemplate restTemplate,
@Value("${app.account-service.host}") String accountServiceHost,
@Value("${app.account-service.port}") int accountServicePort) {

this.restTemplate = restTemplate;

public List<Account> getCustomerAccounts(int id){
return List.of();
accountServiceUrl = "http://"
.concat(accountServiceHost)
.concat(":")
.concat(String.valueOf(accountServicePort))
.concat(BASE_URL)
.concat("accounts");
}

public List<Account> getCustomerAccounts(int customerId){

String url = accountServiceUrl.concat(QUERY_PARAM).concat(String.valueOf(customerId));
log.debug("Will call getCustomerAccounts API on URL: {}", url);

return restTemplate.exchange(url, GET, null,
new ParameterizedTypeReference<List<Account>>() {}).getBody();
}

public void createAccount(AccountDTO account) {
restTemplate.postForObject(accountServiceUrl,account, AccountDTO.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.extern.log4j.Log4j2;
import org.siriusxi.blueharvest.bank.common.api.composite.customer.CustomerAggregate;
import org.siriusxi.blueharvest.bank.common.api.dto.AccountDTO;
import org.siriusxi.blueharvest.bank.cs.integration.AccountIntegration;
import org.siriusxi.blueharvest.bank.cs.persistence.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -38,4 +39,8 @@ public List<CustomerAggregate> getCustomers(){
.getCustomerAccounts(entity.getId())))
.collect(Collectors.toList());
}

public void createCustomerAccount(AccountDTO account) {
accountIntegration.createAccount(account);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ logging.level:
blueharvest:
bank: DEBUG
web: DEBUG
root: INFO
root: INFO

# Application specific params
app:
account-service:
host: localhost
port: 8091
16 changes: 8 additions & 8 deletions bank-services/customer-service/src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ CREATE TABLE customer (
balance DECIMAL(10, 2) DEFAULT 0.0
);

INSERT INTO customer (first_name, last_name, balance) VALUES
('Mohamed', 'Taman', 200.20),
('Issa', 'Ahmed', 100.30),
('Matin', 'Abbasi', 10.0),
('Milica', 'Jovicic', 0.0),
('Angèl', 'Wijnhard', 0.0),
('Fatih', 'Akbas',0.0),
('Ibrahim', 'Moustafa', 0.0);
INSERT INTO customer (first_name, last_name) VALUES
('Mohamed', 'Taman'),
('Issa', 'Ahmed'),
('Matin', 'Abbasi'),
('Milica', 'Jovicic'),
('Angèl', 'Wijnhard'),
('Fatih', 'Akbas'),
('Ibrahim', 'Moustafa');
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.siriusxi.blueharvest.bank.cs;

import org.junit.jupiter.api.Test;
import org.siriusxi.blueharvest.bank.cs.persistence.CustomerRepository;
import org.siriusxi.blueharvest.bank.cs.persistence.entity.CustomerEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

import java.math.BigDecimal;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

@DataJpaTest
class CustomerRepositoryIntegrationTests {

@Autowired
private CustomerRepository customerRepository;

@Test
void whenFindAllCustomers_thenReturn7Customers() {
// given all 7 employees already initialized

// when
Iterable<CustomerEntity> found = customerRepository.findAll();

// then
assertThat(found.spliterator().getExactSizeIfKnown()).isEqualTo(7);
}

@Test
void updateCustomerBalance() {
// given 7th employee already initialized with balance 0.0
Optional<CustomerEntity> foundCustomer = customerRepository.findById(7);

CustomerEntity customer = null;

if (foundCustomer.isPresent())
customer = foundCustomer.get();

assertThat(customer.getBalance()).isEqualTo(new BigDecimal("0.00"));

// when
customer.setBalance(new BigDecimal("9999.90"));
customerRepository.save(customer);

// then
foundCustomer = null;
customer = null;

foundCustomer = customerRepository.findById(7);

if (foundCustomer.isPresent())
customer = foundCustomer.get();

assertThat(customer.getBalance()).isEqualTo(new BigDecimal("9999.90"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public List<Transaction> getTransactions(@RequestParam("accountId") int accountI
public void createTransaction(@RequestBody TransactionDTO transaction) {
transactionService.createTransaction(new TransactionEntity(transaction.getAccountId(),
transaction.getAmount()));
log.debug("createAccount: creates a new account {} for CustomerId: {}", transaction.toString());
log.debug("createTransaction: creates a new Transaction {}", transaction);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import org.siriusxi.blueharvest.bank.ts.persistence.entity.TransactionEntity;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface TransactionRepository extends CrudRepository<TransactionEntity, Integer> {

Iterable<TransactionEntity> findByAccountId(int customerId);
List<TransactionEntity> findByAccountId(int customerId);
}
Loading

0 comments on commit 9ccf256

Please sign in to comment.