Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AccountViewImpl implements AccountView {
- private final static Logger log = LoggerFactory.getLogger(AccountViewImpl.class.getName());
- private Writer writer;
- private Reader reader;
- private AccountService accountService;
- public AccountViewImpl(Writer writer, Reader reader, AccountService accountService) {
- this.writer = writer;
- this.reader = reader;
- this.accountService = accountService;
- }
- public void setWriter(Writer writer) {
- this.writer = writer;
- }
- public void setReader(Reader reader) {
- this.reader = reader;
- }
- public void setAccountService(AccountService accountService) {
- this.accountService = accountService;
- }
- @Override
- public void createAccount() throws IOException {
- writer.printLine("Input client id: ");
- Long clientId = reader.readLong();
- writer.printLine("Input login: ");
- String login = reader.readString();
- writer.printLine("Input password: ");
- String password = reader.readString();
- AccountDTO account = new AccountDTO(login, password);
- writer.printLine(accountService.createAccount(account, clientId) ? "Account was created." :
- "Account wasn't created.");
- }
- @Override
- public void updateAccount() throws IOException {
- writer.printLine("Input account id: ");
- Long accountId = reader.readLong();
- AccountDTO account = accountService.findAccount(accountId);
- if (account == null) {
- writer.printLine("Account wasn't found.");
- return;
- }
- writer.printLine("Input new login: ");
- account.setLogin(reader.readString());
- writer.printLine("Input new password: ");
- account.setPassword(reader.readString());
- writer.printLine(accountService.changeAccount(account) ? "Account was updated." : "Account wasn't changed.");
- log.info("AccountDTO updated.");
- }
- @Override
- public void deleteAccount() throws IOException {
- writer.printLine("Input account id: ");
- Long accountId = reader.readLong();
- writer.printLine(accountService.deleteAccount(accountId) ? "Account with id = " + accountId +
- " was deleted." : "Account wasn't deleted. Check id.");
- }
- }
- public class ClientViewImpl implements ClientView {
- private final static Logger log = LoggerFactory.getLogger(ClientViewImpl.class.getName());
- private Writer writer;
- private Reader reader;
- private ClientService clientService;
- public ClientViewImpl(Writer writer, Reader reader, ClientService clientService) {
- this.writer = writer;
- this.reader = reader;
- this.clientService = clientService;
- }
- public void setWriter(Writer writer) {
- this.writer = writer;
- }
- public void setReader(Reader reader) {
- this.reader = reader;
- }
- public void setClientService(ClientService clientService) {
- this.clientService = clientService;
- }
- @Override
- public void createClient() throws IOException {
- ClientDTO client = new ClientDTO();
- writer.printLine("Input client name: ");
- client.setName(reader.readString());
- writer.printLine("Input client e-mail: ");
- client.setEmail(reader.readString());
- writer.printLine(clientService.saveClient(client) ? "Client created." : "Client wasn't created.");
- }
- @Override
- public void deleteClient() throws IOException {
- writer.printLine("Input client id: ");
- Long clientId = reader.readLong();
- writer.printLine(clientService.deleteClient(clientId) ? "Client with id = " + clientId +
- " was deleted." : "Client wasn't found.");
- }
- @Override
- public void displayAllClientsInfo() {
- final Collection<ClientDTO> clients = clientService.getAllClients();
- StringBuilder output = new StringBuilder();
- if (clients != null && clients.size() > 0) {
- output.append(StringUtils.center("Clients", 55));
- output.append(StringUtils.center("Accounts", 85) + "n");
- output.append(StringUtils.repeat("-", 140) + "n");
- String columnsNames = String.format("%1$5s%2$25s%3$27s%4$3s%5$30s%6$25s%7$25s", "id", "e-mail", "name |",
- "id", "created", "login", "password");
- output.append(columnsNames + "n");
- output.append(StringUtils.repeat("=", 140) + "n");
- for (ClientDTO client : clients) {
- output.append(String.format("%1$5d%2$25s%3$25s |", client.getId(), client.getEmail(),
- client.getName()) + "n");
- List<AccountDTO> accounts = client.getAccounts();
- if (accounts != null && accounts.size() > 0) {
- for (AccountDTO ac : accounts) {
- output.append(String.format("%1$57s%2$3d%3$30s%4$25s%5$25s", "|", ac.getId(),
- ac.getCreated(), ac.getLogin(), ac.getPassword()) + "n");
- }
- }
- output.append(StringUtils.repeat("-", 140) + "n");
- }
- writer.printLine(output);
- } else {
- writer.printLine("No data to display.");
- log.info("No data to display.");
- }
- }
- }
- /*
- Naming convention for tests: MethodName_StateUnderTest_ExpectedBehavior
- */
- @RunWith(JUnitPlatform.class)
- @SelectClasses({
- AccountViewImplTest.class,
- ClientViewImplTest.class
- })
- public class UnitTestSuite {
- }
- public class AccountViewImplTest {
- private Writer writer;
- private Reader reader;
- private AccountService accountService;
- private AccountView accountView;
- @BeforeEach
- void setUp() {
- writer = mock(Writer.class);
- reader = mock(Reader.class);
- accountService = mock(AccountService.class);
- accountView = new AccountViewImpl(writer, reader, accountService);
- }
- @Test
- void createAccount_correctInput_callServiceWithThatInput() throws IOException, ParseException {
- // Given
- AccountDTO accountDTO = new AccountDTO("login1", "passw0rd");
- when(reader.readLong()).thenReturn(1L);
- when(reader.readString()).thenReturn("login1", "passw0rd");
- when(accountService.createAccount(notNull(), eq(1L))).thenReturn(true);
- // When
- accountView.createAccount();
- // Then
- verify(accountService).createAccount(accountDTO, 1L);
- verify(writer).printLine("Account was created.");
- }
- @Test
- void updateAccount_correctInput_callServiceWithThatInput() throws IOException, ParseException {
- // Given
- DateFormat df = new SimpleDateFormat("HH:mm:ss dd.MM.yyyy");
- AccountDTO account = new AccountDTO(1L, "login", "password",
- df.parse("10:15:30 24.10.2017"));
- when(accountService.findAccount(eq(1L))).thenReturn(account);
- when(reader.readLong()).thenReturn(1L);
- when(reader.readString()).thenReturn("NewLogin", "NewPassword");
- account.setLogin("NewLogin");
- account.setPassword("NewPassword");
- when(accountService.changeAccount(account)).thenReturn(true);
- // When
- accountView.updateAccount();
- // Then
- verify(accountService).findAccount(1L);
- verify(accountService).changeAccount(account);
- verify(writer).printLine("Account was updated.");
- }
- @Test
- void updateAccount_badInputAccountId_PrintError() throws IOException, ParseException {
- // Given
- when(reader.readLong()).thenReturn(1L);
- when(accountService.findAccount(eq(1L))).thenReturn(null);
- // When
- accountView.updateAccount();
- // Then
- verify(accountService).findAccount(1L);
- verify(writer).printLine("Account wasn't found.");
- }
- @Test
- void deleteAccount_correctInput_callServiceWithThatInput() throws IOException {
- // Given
- when(reader.readLong()).thenReturn(1L);
- when(accountService.deleteAccount(eq(1L))).thenReturn(true);
- // When
- accountView.deleteAccount();
- // Then
- verify(accountService).deleteAccount(1L);
- verify(writer).printLine("Account with id = " + 1 + " was deleted.");
- }
- @Test
- void deleteAccount_badInputAccountId_PrintError() throws IOException {
- // Given
- when(reader.readLong()).thenReturn(1L);
- when(accountService.deleteAccount(eq(1L))).thenReturn(false);
- // When
- accountView.deleteAccount();
- // Then
- verify(accountService).deleteAccount(1L);
- verify(writer).printLine("Account wasn't deleted. Check id.");
- }
- }
- public class ClientViewImplTest {
- private Writer writer;
- private Reader reader;
- private ClientService clientService;
- private ClientViewImpl clientView;
- @BeforeEach
- void setUp() {
- writer = mock(Writer.class);
- reader = mock(Reader.class);
- clientService = mock(ClientService.class);
- clientView = new ClientViewImpl(writer, reader, clientService);
- }
- @Test
- void createClient_correctInput_callServiceWithThatInput() throws IOException {
- // Given
- ClientDTO client = new ClientDTO();
- client.setName("William");
- client.setEmail("William@company.com");
- when(reader.readString()).thenReturn("William", "William@company.com");
- when(clientService.saveClient(notNull())).thenReturn(true);
- // When
- clientView.createClient();
- // Then
- verify(clientService).saveClient(client);
- verify(writer).printLine("Client created.");
- }
- @Test
- void deleteClient_correctInput_callServiceWithThatInput() throws IOException {
- // Given
- when(reader.readLong()).thenReturn(1L);
- when(clientService.deleteClient(eq(1L))).thenReturn(true);
- // When
- clientView.deleteClient();
- // Then
- verify(clientService).deleteClient(1L);
- verify(writer).printLine("Client with id = " + 1L + " was deleted.");
- }
- @SuppressWarnings("unchecked")
- @Test
- void displayAllClientsInfo_correctInput_callWriter() throws ParseException {
- // Given
- DateFormat df = new SimpleDateFormat("HH:mm:ss dd.MM.yyyy");
- List<ClientDTO> clients = new ArrayList<>();
- clients.add(new ClientDTO(1L, "John Smith", "client@example.com", Arrays.asList(
- new AccountDTO(10L, "JSmith1", "zzwvp0d9", df.parse("10:15:30 20.10.2017")),
- new AccountDTO(20L, "JSmith2", "mhjnbgfv", df.parse("10:15:30 5.5.2017")),
- new AccountDTO(30L, "JSmith3", "ytersds1", df.parse("15:00:30 12.10.2017"))
- )));
- clients.add(new ClientDTO(2L, "Jack Black", "jack@example.com", new ArrayList<>()));
- when(clientService.getAllClients()).thenReturn(clients);
- ArgumentCaptor<Object> argument = ArgumentCaptor.forClass(Object.class);
- // When
- clientView.displayAllClientsInfo();
- // Then
- verify(writer).printLine(argument.capture());
- final String output = argument.getValue().toString();
- assertAll(
- // Client
- () -> assertTrue(output.contains(Long.toString(1))),
- () -> assertTrue(output.contains("client@example.com")),
- () -> assertTrue(output.contains("John Smith")),
- // Accounts
- () -> assertTrue(output.contains(Long.toString(10))),
- () -> assertTrue(output.contains("JSmith1")),
- () -> assertTrue(output.contains("zzwvp0d9")),
- () -> assertTrue(output.contains(df.parse("10:15:30 20.10.2017").toString())),
- () -> assertTrue(output.contains(Long.toString(20))),
- () -> assertTrue(output.contains("JSmith2")),
- () -> assertTrue(output.contains("mhjnbgfv")),
- () -> assertTrue(output.contains(df.parse("10:15:30 5.5.2017").toString())),
- () -> assertTrue(output.contains(Long.toString(30))),
- () -> assertTrue(output.contains("JSmith3")),
- () -> assertTrue(output.contains("ytersds1")),
- () -> assertTrue(output.contains(df.parse("15:00:30 12.10.2017").toString())),
- // Client
- () -> assertTrue(output.contains(Long.toString(2))),
- () -> assertTrue(output.contains("jack@example.com")),
- () -> assertTrue(output.contains("Jack Black"))
- );
- }
- }
Add Comment
Please, Sign In to add comment