Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/runner/controllers/CustomerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ else if(response == 1 )

@JsonView(Views.Email.class)
@PutMapping(value = "/update/email")
public ResponseEntity<?> updateEmail(@RequestBody String email) throws Exception {
public ResponseEntity<?> updateEmail(@RequestBody String email) {
String currentPrincipalName = SecurityContextHolder.getContext().getAuthentication().getName();
Customer customerReturned =customerServices.readCustomerByLogin(currentPrincipalName);
Long id = customerReturned.getId();
Expand All @@ -96,7 +96,7 @@ else if(response == 1 )

@JsonView(Views.Address.class)
@PutMapping(value = "/update/address")
public ResponseEntity<?> updateEmail(@RequestBody Address address) throws Exception {
public ResponseEntity<?> updateEmail(@RequestBody Address address) {
String currentPrincipalName = SecurityContextHolder.getContext().getAuthentication().getName();
Customer customerReturned =customerServices.readCustomerByLogin(currentPrincipalName);
Long id = customerReturned.getId();
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/runner/entities/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ public class Address {
@JsonView(Views.Address.class)
private String zipcode;

public Address() {
}

//for testing
public Address(Long id, String firstLine, String secondLIne, String city, String state, String zipcode) {
this.id = id;
this.firstLine = firstLine;
this.secondLIne = secondLIne;
this.city = city;
this.state = state;
this.zipcode = zipcode;
}

public Long getId() {
return id;
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/runner/entities/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,20 @@ public class Customer {
public Customer() {
}

public Customer(Long id, String firstName, String middleName, String lastName, String socialSecurity) {
//for testing
public Customer(Long id, String firstName, String lastName, Login login, Set<Account> accounts) {
this.id = id;
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.socialSecurity = socialSecurity;
this.login = login;
this.accounts = accounts;
}

//for testing
public Customer(Long id, String firstName, String lastName, Login login, Set<Account> accounts) {
public Customer(Long id, String firstName, String lastName, Address address, Login login, Set<Account> accounts) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.login = login;
this.accounts = accounts;
}
Expand Down
50 changes: 32 additions & 18 deletions src/main/java/runner/services/CustomerServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public Customer createCustomer(Customer customer) {

//Check if the username already exist
public Boolean checkLogin(Login login) {

List<String> logins= customerRepo.findAllLoginsNative();
long count = logins.stream().filter(name -> name.equalsIgnoreCase(login.getUsername())).count();
return count!=0 ? true:false;
Expand Down Expand Up @@ -79,27 +78,42 @@ public Customer readCustomerByLogin(String name) {
}
}
//Delete the customer from DB after checking that all account balance are < 0
//Returns 0 = Deleted , 1 : Customer not found , 2 : Accounts with > 0 balance exist
public int deleteCustomer(Long id) {
Customer customer = customerRepo.findCustomerById(id);
Set<Account> accounts; // To collect all accounts belonging to this customer
List<Account> result ; //To collect accounts with balance greater than 0
if (customer != null) {
accounts=getAllAccounts(id);
result = accounts.stream().filter((account) -> account.getBalance() > 0).collect(Collectors.toList());
loggerService.log(Level.INFO, "Account list size " + result.size());
if(result.size()==0 && accounts.size()!=0) {
if (customer.getAccounts() != null) {
return checkAccountBalanceAndDelete(id, customer);
}
else
{
customerRepo.delete(customer);
loggerService.log(Level.INFO, "User has been deleted as account balance for all accounts =0");
loggerService.log(Level.INFO, "User has been deleted as no accounts found for this customer");
return 0;
}
else if(result.size()>0) {
loggerService.log(Level.WARNING, "The customer had a balance greater than 0 and could not remove the account # " + id);
return 2;
}
}
loggerService.log(Level.WARNING, "The customer not found" + id);
return 1;
}

// Used in above method to decide if the customer can be deleted
int checkAccountBalanceAndDelete(Long id, Customer customer) {
Set<Account> accounts; // To collect all accounts belonging to this customer
List<Account> result ; //To collect accounts with balance greater than 0
accounts = getAllAccounts(id);
result = accounts.stream().filter((account) -> account.getBalance() > 0).collect(Collectors.toList());
loggerService.log(Level.INFO, "Account list size " + result.size());
if (result.size() == 0 && accounts.size() != 0) {
customerRepo.delete(customer);
loggerService.log(Level.INFO, "User has been deleted as account balance for all accounts 0 , All accounts are deleted as well.");
return 0;
} else if (result.size() > 0) {
loggerService.log(Level.WARNING, "The customer had a balance greater than 0 and could not remove the account # " + id);
return 2;
}
return 4;
}

//Update the Customer (all fields) in the DB ,based on body of request
public Customer updateCustomer(Long id, Customer customer) throws Exception {
loggerService.log(Level.INFO, "Finding the customer to be updated");
Expand Down Expand Up @@ -140,12 +154,12 @@ public int updateCustomerPhoneNumber(Long id, String phone) throws ParseExceptio
customer.setPhoneNumber(phone);
customerRepo.save(customer);
loggerService.log(Level.INFO, "User with Id " + customer.getId() + " phone number has been updated");
return 0;
return 0; // Phone number has been updated
} else {
return 1;
return 1; // Customer not found
}
}
return 2;
return 2; // Phone number format is incorrect
}

//Update Email number ,check syntax based on the REGEX
Expand All @@ -164,12 +178,12 @@ public int updateCustomerEmail(Long id, String email) {
customer.setEmail(email);
customerRepo.save(customer);
loggerService.log(Level.INFO, "customer with Id " + customer.getId() + " email id has been updated");
return 0;
return 0; // Updated
} else {
return 1;
return 1; // Customer not found
}
}
return 2;
return 2; // Email format incorrect
}
//Update Customer address
public Customer updateCustomerAddress(Long id, Address address) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ spring.datasource.initialization-mode=never
#Need this for testing to work properly
spring.main.allow-bean-definition-overriding=true


#jdbc:mysql://localhost:3306/moneymanagement
#jdbc:mysql://mysql-moneymanagement.cvq8xhrssade.us-east-2.rds.amazonaws.com/moneymanagement

48 changes: 40 additions & 8 deletions src/test/java/runner/controllers/CustomerControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import runner.entities.Customer;
import runner.entities.*;
import runner.enums.AccountType;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
Expand All @@ -32,17 +38,43 @@ public class CustomerControllerTest {
@MockBean
private CustomerController userController;

private Customer customer;
// private Customer customer;

ObjectMapper objectMapper = new ObjectMapper();

// @Before
// public void setUp() {
// userController = Mockito.mock(CustomerController.class);
// mockMvc = MockMvcBuilders.standaloneSetup(userController).build();
// this.customer = new Customer( 1L, "Radha" , "Ramnik","Patel","234324");
// }

Account account1;
Account account2;
Account account3;
Set<Account> testAccounts;
Login login;
Customer customer;
Transaction transaction;
Set<Account> transactionAccount;
Transaction withdrawalTransaction;
Transaction depositTransaction;
ArrayList<Transaction> myTransactionList;
Address address;
@Before
public void setUp() {
userController = Mockito.mock(CustomerController.class);
mockMvc = MockMvcBuilders.standaloneSetup(userController).build();
this.customer = new Customer( 1L, "Radha" , "Ramnik","Patel","234324");
public void setup(){
account1 = new Account(1L,"12345", AccountType.CHECKING,100.00,"abcdefg", new HashSet<Transaction>());
account2 = new Account(2L,"54321", AccountType.SAVINGS,0.00,"gfedcba", new HashSet<Transaction>());
account3 = new Account(2L,"56789", AccountType.SAVINGS,100.00,"qwerty", new HashSet<Transaction>());
testAccounts = new HashSet<Account>();
testAccounts.add(account1);
testAccounts.add(account2);
login = new Login(1L,"user","password",customer); //customer would be null here due to order of code;
customer = new Customer(1L,"John","Doe",login,testAccounts);
address = new Address(1L,"Address Line 1", "Address Line 2", "Bear","DE","19701");
}


// @Test
// public void findUserTest() throws Exception {
// Mockito.when(userController.readById(1L)).thenReturn(new ResponseEntity<>(customer, HttpStatus.OK));
Expand All @@ -54,12 +86,12 @@ public void setUp() {

@Test
public void createUserTest() throws Exception {
Customer customer1 = new Customer( 2L, "Rekha" , "Jagdish","Patel","234234324");
// Customer customer1 = new Customer( 2L, "Rekha" , "Jagdish","Patel","234234324");
// BDDMockito
// .given(userController.create(user1))
// .willReturn(user1);

String jsonRequest = objectMapper.writeValueAsString(customer1);
String jsonRequest = objectMapper.writeValueAsString(customer);
// Mockito.when(userController.create(customer1)).thenReturn(new ResponseEntity<>(customer1, HttpStatus.CREATED));

MvcResult result= mockMvc.perform(post("/profile/create")
Expand Down
67 changes: 45 additions & 22 deletions src/test/java/runner/entities/CustomerEntityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -11,8 +12,11 @@
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import runner.AppRunner;
import runner.enums.AccountType;

import javax.persistence.Entity;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

@ActiveProfiles("test")
Expand All @@ -22,12 +26,31 @@
"DB_PASS=password",
"DB_URL=jdbc:mysql://localhost:3306/moneymanagement"})
public class CustomerEntityTest {
@MockBean
private Customer customer;
// private Login login;
// private Set<Account> expectedAccount;
// private Account account1;
// private Address expectedAddress;

Account account1;
Account account2;
Account account3;
Set<Account> testAccounts;
Login login;
Customer customer;
Transaction transaction;
Set<Account> transactionAccount;
Transaction withdrawalTransaction;
Transaction depositTransaction;
ArrayList<Transaction> myTransactionList;
Address address;
@Before
public void setup(){
account1 = new Account(1L,"12345", AccountType.CHECKING,100.00,"abcdefg", new HashSet<Transaction>());
account2 = new Account(2L,"54321", AccountType.SAVINGS,0.00,"gfedcba", new HashSet<Transaction>());
account3 = new Account(2L,"56789", AccountType.SAVINGS,100.00,"qwerty", new HashSet<Transaction>());
testAccounts = new HashSet<Account>();
testAccounts.add(account1);
testAccounts.add(account2);
login = new Login(1L,"user","password",customer); //customer would be null here due to order of code;
address = new Address(1L,"Address Line 1", "Address Line 2", "Bear","DE","19701");
customer = new Customer(1L,"John","Doe",address,login,testAccounts);
}

@Test
public void testClassSignatureAnnotations() {
Expand All @@ -36,10 +59,10 @@ public void testClassSignatureAnnotations() {
@Test
public void testCreateJson() throws JsonProcessingException {
ObjectMapper objectMapper= new ObjectMapper();
Customer expectedCustomer = new Customer( 1L, "Radha" , "Ramnik","Patel","234324");
String json = objectMapper.writeValueAsString(expectedCustomer);
// Customer expectedCustomer = new Customer( 1L, "Radha" , "Ramnik","Patel","234324");
String json = objectMapper.writeValueAsString(customer);
Customer actualCustomer =objectMapper.readValue(json, Customer.class);
Assert.assertEquals(expectedCustomer.getId(), actualCustomer.getId());
Assert.assertEquals(customer.getId(), actualCustomer.getId());

}

Expand Down Expand Up @@ -174,18 +197,18 @@ public void testCreateJson() throws JsonProcessingException {
// Assert.assertEquals(expectedAccount.size(),actualAccount.size());
// }
//
// @Test
// public void setAndGetAddressTest()
// {
// //Given
// expectedAddress = new Address();
// expectedAddress.setFirstLine("First Line of Address");
// expectedAddress.setSecondLIne("Second Line of Address");
//@Test
// public void setAndGetAddressTest()
// {
// //Given
// expectedAddress = new Address();
// expectedAddress.setFirstLine("First Line of Address");
// expectedAddress.setSecondLIne("Second Line of Address");
//
// //When
// user.setAddress(expectedAddress);
// //Then
// Address actualAddress = user.getAddress();
// Assert.assertEquals(expectedAddress,actualAddress);
// }
// //When
// customer.setAddress(expectedAddress);
// //Then
// Address actualAddress = customer.getAddress();
// Assert.assertEquals(expectedAddress,actualAddress);
// }
}
Loading