-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
306 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/com/twu/biblioteca/accounts/application/AccountInteractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.twu.biblioteca.accounts.application; | ||
|
||
import com.twu.biblioteca.accounts.core.Account; | ||
|
||
import java.util.Optional; | ||
|
||
public class AccountInteractor { | ||
private final AccountRepository accountRepository; | ||
|
||
public AccountInteractor(AccountRepository accountRepository) { | ||
this.accountRepository = accountRepository; | ||
} | ||
|
||
public Account find(String libraryNumber) { | ||
Optional<Account> account = this.accountRepository.find(libraryNumber); | ||
if (account.isPresent()) return account.get(); | ||
else throw new AccountNotFound(); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/twu/biblioteca/accounts/application/AccountNotFound.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.twu.biblioteca.accounts.application; | ||
|
||
class AccountNotFound extends RuntimeException { | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/twu/biblioteca/accounts/application/AccountRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.twu.biblioteca.accounts.application; | ||
|
||
import com.twu.biblioteca.accounts.core.Account; | ||
|
||
import java.util.Optional; | ||
|
||
public interface AccountRepository { | ||
Optional<Account> find(String aLibraryNumber); | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/com/twu/biblioteca/accounts/core/Account.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.twu.biblioteca.accounts.core; | ||
|
||
public class Account { | ||
private String phoneNumber; | ||
private String address; | ||
private String email; | ||
private String name; | ||
|
||
void setPhoneNumber(String phoneNumber) { | ||
this.phoneNumber = phoneNumber; | ||
} | ||
|
||
public String getPhoneNumber() { | ||
return phoneNumber; | ||
} | ||
|
||
void setAddress(String address) { | ||
this.address = address; | ||
} | ||
|
||
public String getAddress() { | ||
return address; | ||
} | ||
|
||
void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/twu/biblioteca/accounts/core/AccountBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.twu.biblioteca.accounts.core; | ||
|
||
public class AccountBuilder { | ||
private String phoneNumber; | ||
private String address; | ||
private String email; | ||
private String name; | ||
|
||
public Account build() { | ||
Account account = new Account(); | ||
account.setName(name); | ||
account.setEmail(email); | ||
account.setAddress(address); | ||
account.setPhoneNumber(phoneNumber); | ||
return account; | ||
} | ||
|
||
public AccountBuilder withPhoneNumber(String aPhoneNumber) { | ||
this.phoneNumber = aPhoneNumber; | ||
return this; | ||
} | ||
|
||
public AccountBuilder withAddress(String anAddress) { | ||
this.address = anAddress; | ||
return this; | ||
} | ||
|
||
public AccountBuilder withEmail(String anEmail) { | ||
this.email = anEmail; | ||
return this; | ||
} | ||
|
||
public AccountBuilder withName(String aName) { | ||
this.name = aName; | ||
return this; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/twu/biblioteca/accounts/infrastructure/AccountPresenter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.twu.biblioteca.accounts.infrastructure; | ||
|
||
import com.twu.biblioteca.accounts.application.AccountInteractor; | ||
import com.twu.biblioteca.accounts.core.Account; | ||
|
||
import java.io.PrintStream; | ||
|
||
public class AccountPresenter { | ||
private final AccountInteractor accountInteractor; | ||
private final PrintStream out; | ||
|
||
public AccountPresenter(AccountInteractor accountInteractor, PrintStream out) { | ||
this.accountInteractor = accountInteractor; | ||
this.out = out; | ||
} | ||
|
||
public void showInfoOf(String accountNumber) { | ||
Account account = accountInteractor.find(accountNumber); | ||
out.println("Name: " + account.getName()); | ||
out.println("Email: " + account.getEmail()); | ||
out.println("Address: " + account.getAddress()); | ||
out.println("Phone: " + account.getPhoneNumber()); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...ava/com/twu/biblioteca/accounts/infrastructure/persistence/InMemoryAccountRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.twu.biblioteca.accounts.infrastructure.persistence; | ||
|
||
import com.twu.biblioteca.accounts.application.AccountRepository; | ||
import com.twu.biblioteca.accounts.core.Account; | ||
import com.twu.biblioteca.accounts.core.AccountBuilder; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
public class InMemoryAccountRepository implements AccountRepository { | ||
private Map<String, Account> accounts; | ||
|
||
public InMemoryAccountRepository(Collection<String[]> accounts) { | ||
this.accounts = accounts.stream().collect(Collectors.toMap(mapKey(), mapAccount())); | ||
} | ||
|
||
private Function<String[], Account> mapAccount() { | ||
return values -> new AccountBuilder().withName(values[0]).withEmail(values[1]).withAddress(values[2]).withPhoneNumber(values[3]).build(); | ||
} | ||
|
||
private Function<String[], String> mapKey() { | ||
return values -> values[4]; | ||
} | ||
|
||
@Override | ||
public Optional<Account> find(String aLibraryNumber) { | ||
return this.accounts.containsKey(aLibraryNumber) ? Optional.of(this.accounts.get(aLibraryNumber)) : Optional.empty(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...persistance/InMemoryAccessRepository.java → ...persistence/InMemoryAccessRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...e/persistance/InMemoryBookRepository.java → ...e/persistence/InMemoryBookRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/test/java/com/twu/biblioteca/accounts/application/AccountInteractorShould.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.twu.biblioteca.accounts.application; | ||
|
||
import com.twu.biblioteca.accounts.core.Account; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class AccountInteractorShould { | ||
|
||
private AccountRepository accountRepoMock; | ||
private AccountInteractor accountInteractor; | ||
|
||
@Before | ||
public void setUp() { | ||
accountRepoMock = mock(AccountRepository.class); | ||
accountInteractor = new AccountInteractor(accountRepoMock); | ||
} | ||
|
||
@Test (expected = AccountNotFound.class) | ||
public void throwAccountNotFoundWhenAccountIsNotInRepo() { | ||
when(accountRepoMock.find("aLibraryNumber")).thenReturn(Optional.empty()); | ||
accountInteractor.find("aLibraryNumber"); | ||
} | ||
|
||
@Test | ||
public void returnAnAccountWhenItIsInTheRepo() { | ||
Account value = new Account(); | ||
when(accountRepoMock.find("aLibraryNumber")).thenReturn(Optional.of(value)); | ||
Account expectedAccount = accountInteractor.find("aLibraryNumber"); | ||
assertThat(expectedAccount, is(value)); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/test/java/com/twu/biblioteca/accounts/infrastructure/AccountPresenterShould.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.twu.biblioteca.accounts.infrastructure; | ||
|
||
import com.twu.biblioteca.accounts.application.AccountInteractor; | ||
import com.twu.biblioteca.accounts.core.AccountBuilder; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.PrintStream; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
public class AccountPresenterShould { | ||
private AccountInteractor accountInteractorMock; | ||
private PrintStream outMock; | ||
private AccountPresenter accountPresenter; | ||
|
||
@Before | ||
public void setUp() { | ||
accountInteractorMock = mock(AccountInteractor.class); | ||
outMock = mock(PrintStream.class); | ||
accountPresenter = new AccountPresenter(accountInteractorMock, outMock); | ||
} | ||
|
||
@Test | ||
public void printAccountNameWhenAskingForAccountDetails() { | ||
when(accountInteractorMock.find("Some account")).thenReturn(new AccountBuilder().withName("A name").build()); | ||
accountPresenter.showInfoOf("Some account"); | ||
verify(outMock).println("Name: A name"); | ||
} | ||
|
||
@Test | ||
public void printAccountEmailWhenAskingForAccountDetails() { | ||
when(accountInteractorMock.find("Some account")).thenReturn(new AccountBuilder().withEmail("An email").build()); | ||
accountPresenter.showInfoOf("Some account"); | ||
verify(outMock).println("Email: An email"); | ||
} | ||
|
||
@Test | ||
public void printAccountAddressWhenAskingForAccountDetails() { | ||
when(accountInteractorMock.find("Some account")).thenReturn(new AccountBuilder().withAddress("An address").build()); | ||
accountPresenter.showInfoOf("Some account"); | ||
verify(outMock).println("Address: An address"); | ||
} | ||
|
||
@Test | ||
public void printAccountPhoneNumberWhenAskingForAccountDetails() { | ||
when(accountInteractorMock.find("Some account")).thenReturn(new AccountBuilder().withPhoneNumber("A phone number").build()); | ||
accountPresenter.showInfoOf("Some account"); | ||
verify(outMock).println("Phone: A phone number"); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...m/twu/biblioteca/accounts/infrastructure/persistence/InMemoryAccountRepositoryShould.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.twu.biblioteca.accounts.infrastructure.persistence; | ||
|
||
import com.twu.biblioteca.accounts.application.AccountRepository; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Optional; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
|
||
public class InMemoryAccountRepositoryShould { | ||
|
||
@Test | ||
public void returnAnEmptyAccountWhenNotFindingOne() { | ||
AccountRepository accountRepository = new InMemoryAccountRepository(Collections.emptyList()); | ||
assertThat(accountRepository.find("some account number"), is(Optional.empty())); | ||
} | ||
|
||
@Test | ||
public void returnAnAccountWhenFindingIt() { | ||
AccountRepository accountRepository = new InMemoryAccountRepository(Arrays.asList(new String[][] { | ||
new String[] {"Some name", "Some email", "Some address", "Some phone number", "some account number"} | ||
})); | ||
assertThat(accountRepository.find("some account number").isPresent(), is(true)); | ||
assertThat(accountRepository.find("some account number").get().getName(), is("Some name")); | ||
assertThat(accountRepository.find("some account number").get().getEmail(), is("Some email")); | ||
assertThat(accountRepository.find("some account number").get().getAddress(), is("Some address")); | ||
assertThat(accountRepository.find("some account number").get().getPhoneNumber(), is("Some phone number")); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...tance/InMemoryAccessRepositoryShould.java → ...tence/InMemoryAccessRepositoryShould.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...istance/InMemoryBookRepositoryShould.java → ...istence/InMemoryBookRepositoryShould.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters