Skip to content

Commit

Permalink
Merge pull request #39 from jeffreybakker/feature/test
Browse files Browse the repository at this point in the history
Change return type of findBankAccountsByPrimaryHolder
  • Loading branch information
jeffreybakker authored Jul 10, 2017
2 parents d9b20f0 + e397415 commit 5ea1bb3
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 12 deletions.
5 changes: 5 additions & 0 deletions banq.iml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.1" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents.client5:httpclient5:5.0-alpha1" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents.core5:httpcore5:5.0-alpha1" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.10" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:1.5.2.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:1.5.2.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:1.5.2.RELEASE" level="project" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public interface BankAccountRepository extends JpaRepository<BankAccount, Intege
@Query("SELECT account FROM BankAccount account JOIN account.holders holder WHERE holder.id = ?1")
List<BankAccount> findBankAccountsByHolders(int customer);

List<BankAccount> findBankAccountsByPrimaryHolder(Customer customer);
BankAccount findBankAccountsByPrimaryHolder(Customer customer);

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public NewAccountBean openAccount(String name, String surname, String initials,
address, String telephoneNumber, String email, String username, String password) throws
InvalidParamValueError {
Customer customer = new Customer(name, surname, initials, dob, ssn, address, telephoneNumber, email,
username, password);
username, password);
try {
customerRepository.save(customer);
} catch (Exception e) {
Expand Down Expand Up @@ -84,6 +84,10 @@ public void closeAccount(String authToken, String iBAN) throws NotAuthorizedErro
long accountNumber = IBANUtil.getAccountNumber(iBAN);
BankAccount account = repository.findOne((int) accountNumber);

if (account == null) {
throw new InvalidParamValueError("Bank account does not exist");
}

if (!account.getPrimaryHolder().equals(customer)) {
throw new NotAuthorizedError();
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/honours/ing/banq/info/InfoServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,14 @@ public List<UserAccessBean> getUserAccess(String authToken) throws NotAuthorized
}

List<BankAccount> accounts = bankAccountRepository.findBankAccountsByHolders(customer.getId());
List<BankAccount> primaryAccounts = bankAccountRepository.findBankAccountsByPrimaryHolder(customer);
BankAccount primaryAccount = bankAccountRepository.findBankAccountsByPrimaryHolder(customer);

List<UserAccessBean> userAccessBeanList = new ArrayList<>();
for (BankAccount account : accounts) {
userAccessBeanList.add(new UserAccessBean(account, account.getPrimaryHolder()));
}

for (BankAccount account : primaryAccounts) {
userAccessBeanList.add(new UserAccessBean(account, account.getPrimaryHolder()));
}
userAccessBeanList.add(new UserAccessBean(primaryAccount, primaryAccount.getPrimaryHolder()));

return userAccessBeanList;
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/honours/ing/banq/BoilerplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import honours.ing.banq.info.InfoService;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -22,6 +23,7 @@
/**
* @author Kevin Witlox
*/
@Ignore
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@Import(TestConfiguration.class)
Expand Down
58 changes: 54 additions & 4 deletions src/test/java/honours/ing/banq/account/BankAccountServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package honours.ing.banq.account;

import honours.ing.banq.InvalidParamValueError;
import honours.ing.banq.account.bean.NewAccountBean;
import honours.ing.banq.auth.AuthService;
import honours.ing.banq.bean.AccountInfo;
import honours.ing.banq.util.IBANUtil;
import honours.ing.banq.util.StringUtil;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -39,26 +42,30 @@ public class BankAccountServiceTest {
private String username;
private String password;
private String token;
private String ssn;
private String email;

private Queue<String> accounts;

@Before
public void setUp() throws Exception {
username = StringUtil.generate(10);
password = StringUtil.generate(10);
ssn = StringUtil.generate(10);
email = StringUtil.generate(10);

accounts = new ArrayBlockingQueue<>(10, true);

// First account
NewAccountBean account = service.openAccount(
"University", "of Twente", "UT",
Calendar.getInstance().getTime().toString(), StringUtil.generate(10),
"1996-1-1", ssn,
"Universiteitsstraat 1, Enschede", "06-12345678",
StringUtil.generate(10), username, password);
accounts.add(account.getiBAN());

// Get the auth token
authService.getAuthToken(username, password);
token = authService.getAuthToken(username, password).getAuthToken();

// And a few more
for (int i = 0; i < 4; i++) {
Expand All @@ -80,20 +87,63 @@ public void openAccount() throws Exception {

username = StringUtil.generate(10);
password = StringUtil.generate(10);
ssn = StringUtil.generate(10);
email = StringUtil.generate(10);

NewAccountBean account = service.openAccount(
"University", "of Twente", "UT",
Calendar.getInstance().getTime().toString(), StringUtil.generate(10),
"1996-1-1", StringUtil.generate(10),
"Universiteitsstraat 1, Enschede", "06-12345678",
StringUtil.generate(10), username, password);
email, username, password);
accounts.add(account.getiBAN());

// Get the auth token
token = authService.getAuthToken(username, password).getAuthToken();
}

@Test(expected = InvalidParamValueError.class)
public void openAccountNonUniqueSSN() throws Exception {
service.openAccount(
"University", "of Twente", "UT",
"1996-1-1", ssn,
"Universiteitsstraat 1, Enschede", "06-12345678",
StringUtil.generate(10), StringUtil.generate(10), StringUtil.generate(10));
}

@Test
public void openAccountNonUniqueEmail() throws Exception {
service.openAccount(
"University", "of Twente", "UT",
"1996-1-1", StringUtil.generate(10),
"Universiteitsstraat 1, Enschede", "06-12345678",
email, StringUtil.generate(10), password);
}

@Test
public void openAccountNonUniqueUsername() throws Exception {
service.openAccount(
"University", "of Twente", "UT",
"1996-1-1", StringUtil.generate(10),
"Universiteitsstraat 1, Enschede", "06-12345678",
StringUtil.generate(10), StringUtil.generate(10), StringUtil.generate(10));
}

@Test
public void openAdditionalAccount() throws Exception {
NewAccountBean account = service.openAdditionalAccount(token);
accounts.add(account.getiBAN());
}

@Test
public void closeAccount() throws Exception {
if (!accounts.isEmpty()) {
service.closeAccount(token, accounts.poll());
}
}

@Test(expected = InvalidParamValueError.class)
public void closeNonExistingAccount() throws Exception {
service.closeAccount(token, IBANUtil.generateIBAN(123456789));
}

}
2 changes: 1 addition & 1 deletion src/test/java/honours/ing/banq/auth/AuthServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void setUp() throws Exception {

NewAccountBean account = accountService.openAccount(
"University", "of Twente", "UT",
Calendar.getInstance().getTime().toString(), StringUtil.generate(10),
"1996-1-1", StringUtil.generate(10),
"Universiteitsstraat 1, Enschede", "06-12345678",
StringUtil.generate(10), username, password);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package honours.ing.banq;
package honours.ing.banq.util;

import org.junit.Test;

Expand Down

0 comments on commit 5ea1bb3

Please sign in to comment.