Guest User

Untitled

a guest
Oct 25th, 2017
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.34 KB | None | 0 0
  1. public class AccountViewImpl implements AccountView {
  2. private final static Logger log = LoggerFactory.getLogger(AccountViewImpl.class.getName());
  3. private Writer writer;
  4. private Reader reader;
  5. private AccountService accountService;
  6.  
  7. public AccountViewImpl(Writer writer, Reader reader, AccountService accountService) {
  8. this.writer = writer;
  9. this.reader = reader;
  10. this.accountService = accountService;
  11. }
  12.  
  13. public void setWriter(Writer writer) {
  14. this.writer = writer;
  15. }
  16.  
  17. public void setReader(Reader reader) {
  18. this.reader = reader;
  19. }
  20.  
  21. public void setAccountService(AccountService accountService) {
  22. this.accountService = accountService;
  23. }
  24.  
  25. @Override
  26. public void createAccount() throws IOException {
  27. writer.printLine("Input client id: ");
  28. Long clientId = reader.readLong();
  29. writer.printLine("Input login: ");
  30. String login = reader.readString();
  31. writer.printLine("Input password: ");
  32. String password = reader.readString();
  33. AccountDTO account = new AccountDTO(login, password);
  34. writer.printLine(accountService.createAccount(account, clientId) ? "Account was created." :
  35. "Account wasn't created.");
  36. }
  37.  
  38. @Override
  39. public void updateAccount() throws IOException {
  40. writer.printLine("Input account id: ");
  41. Long accountId = reader.readLong();
  42. AccountDTO account = accountService.findAccount(accountId);
  43. if (account == null) {
  44. writer.printLine("Account wasn't found.");
  45. return;
  46. }
  47. writer.printLine("Input new login: ");
  48. account.setLogin(reader.readString());
  49. writer.printLine("Input new password: ");
  50. account.setPassword(reader.readString());
  51. writer.printLine(accountService.changeAccount(account) ? "Account was updated." : "Account wasn't changed.");
  52. log.info("AccountDTO updated.");
  53. }
  54.  
  55. @Override
  56. public void deleteAccount() throws IOException {
  57. writer.printLine("Input account id: ");
  58. Long accountId = reader.readLong();
  59. writer.printLine(accountService.deleteAccount(accountId) ? "Account with id = " + accountId +
  60. " was deleted." : "Account wasn't deleted. Check id.");
  61. }
  62. }
  63.  
  64. public class ClientViewImpl implements ClientView {
  65. private final static Logger log = LoggerFactory.getLogger(ClientViewImpl.class.getName());
  66. private Writer writer;
  67. private Reader reader;
  68. private ClientService clientService;
  69.  
  70. public ClientViewImpl(Writer writer, Reader reader, ClientService clientService) {
  71. this.writer = writer;
  72. this.reader = reader;
  73. this.clientService = clientService;
  74. }
  75.  
  76. public void setWriter(Writer writer) {
  77. this.writer = writer;
  78. }
  79.  
  80. public void setReader(Reader reader) {
  81. this.reader = reader;
  82. }
  83.  
  84. public void setClientService(ClientService clientService) {
  85. this.clientService = clientService;
  86. }
  87.  
  88. @Override
  89. public void createClient() throws IOException {
  90. ClientDTO client = new ClientDTO();
  91. writer.printLine("Input client name: ");
  92. client.setName(reader.readString());
  93. writer.printLine("Input client e-mail: ");
  94. client.setEmail(reader.readString());
  95. writer.printLine(clientService.saveClient(client) ? "Client created." : "Client wasn't created.");
  96. }
  97.  
  98. @Override
  99. public void deleteClient() throws IOException {
  100. writer.printLine("Input client id: ");
  101. Long clientId = reader.readLong();
  102. writer.printLine(clientService.deleteClient(clientId) ? "Client with id = " + clientId +
  103. " was deleted." : "Client wasn't found.");
  104. }
  105.  
  106. @Override
  107. public void displayAllClientsInfo() {
  108. final Collection<ClientDTO> clients = clientService.getAllClients();
  109. StringBuilder output = new StringBuilder();
  110. if (clients != null && clients.size() > 0) {
  111. output.append(StringUtils.center("Clients", 55));
  112. output.append(StringUtils.center("Accounts", 85) + "n");
  113. output.append(StringUtils.repeat("-", 140) + "n");
  114. String columnsNames = String.format("%1$5s%2$25s%3$27s%4$3s%5$30s%6$25s%7$25s", "id", "e-mail", "name |",
  115. "id", "created", "login", "password");
  116. output.append(columnsNames + "n");
  117. output.append(StringUtils.repeat("=", 140) + "n");
  118. for (ClientDTO client : clients) {
  119. output.append(String.format("%1$5d%2$25s%3$25s |", client.getId(), client.getEmail(),
  120. client.getName()) + "n");
  121. List<AccountDTO> accounts = client.getAccounts();
  122. if (accounts != null && accounts.size() > 0) {
  123. for (AccountDTO ac : accounts) {
  124. output.append(String.format("%1$57s%2$3d%3$30s%4$25s%5$25s", "|", ac.getId(),
  125. ac.getCreated(), ac.getLogin(), ac.getPassword()) + "n");
  126. }
  127. }
  128. output.append(StringUtils.repeat("-", 140) + "n");
  129. }
  130. writer.printLine(output);
  131. } else {
  132. writer.printLine("No data to display.");
  133. log.info("No data to display.");
  134. }
  135. }
  136. }
  137.  
  138. /*
  139. Naming convention for tests: MethodName_StateUnderTest_ExpectedBehavior
  140. */
  141. @RunWith(JUnitPlatform.class)
  142. @SelectClasses({
  143. AccountViewImplTest.class,
  144. ClientViewImplTest.class
  145. })
  146. public class UnitTestSuite {
  147. }
  148.  
  149. public class AccountViewImplTest {
  150. private Writer writer;
  151. private Reader reader;
  152. private AccountService accountService;
  153. private AccountView accountView;
  154.  
  155. @BeforeEach
  156. void setUp() {
  157. writer = mock(Writer.class);
  158. reader = mock(Reader.class);
  159. accountService = mock(AccountService.class);
  160. accountView = new AccountViewImpl(writer, reader, accountService);
  161. }
  162.  
  163. @Test
  164. void createAccount_correctInput_callServiceWithThatInput() throws IOException, ParseException {
  165. // Given
  166. AccountDTO accountDTO = new AccountDTO("login1", "passw0rd");
  167. when(reader.readLong()).thenReturn(1L);
  168. when(reader.readString()).thenReturn("login1", "passw0rd");
  169. when(accountService.createAccount(notNull(), eq(1L))).thenReturn(true);
  170. // When
  171. accountView.createAccount();
  172. // Then
  173. verify(accountService).createAccount(accountDTO, 1L);
  174. verify(writer).printLine("Account was created.");
  175. }
  176.  
  177. @Test
  178. void updateAccount_correctInput_callServiceWithThatInput() throws IOException, ParseException {
  179. // Given
  180. DateFormat df = new SimpleDateFormat("HH:mm:ss dd.MM.yyyy");
  181. AccountDTO account = new AccountDTO(1L, "login", "password",
  182. df.parse("10:15:30 24.10.2017"));
  183. when(accountService.findAccount(eq(1L))).thenReturn(account);
  184. when(reader.readLong()).thenReturn(1L);
  185. when(reader.readString()).thenReturn("NewLogin", "NewPassword");
  186. account.setLogin("NewLogin");
  187. account.setPassword("NewPassword");
  188. when(accountService.changeAccount(account)).thenReturn(true);
  189. // When
  190. accountView.updateAccount();
  191. // Then
  192. verify(accountService).findAccount(1L);
  193. verify(accountService).changeAccount(account);
  194. verify(writer).printLine("Account was updated.");
  195. }
  196.  
  197. @Test
  198. void updateAccount_badInputAccountId_PrintError() throws IOException, ParseException {
  199. // Given
  200. when(reader.readLong()).thenReturn(1L);
  201. when(accountService.findAccount(eq(1L))).thenReturn(null);
  202. // When
  203. accountView.updateAccount();
  204. // Then
  205. verify(accountService).findAccount(1L);
  206. verify(writer).printLine("Account wasn't found.");
  207. }
  208.  
  209. @Test
  210. void deleteAccount_correctInput_callServiceWithThatInput() throws IOException {
  211. // Given
  212. when(reader.readLong()).thenReturn(1L);
  213. when(accountService.deleteAccount(eq(1L))).thenReturn(true);
  214. // When
  215. accountView.deleteAccount();
  216. // Then
  217. verify(accountService).deleteAccount(1L);
  218. verify(writer).printLine("Account with id = " + 1 + " was deleted.");
  219. }
  220.  
  221. @Test
  222. void deleteAccount_badInputAccountId_PrintError() throws IOException {
  223. // Given
  224. when(reader.readLong()).thenReturn(1L);
  225. when(accountService.deleteAccount(eq(1L))).thenReturn(false);
  226. // When
  227. accountView.deleteAccount();
  228. // Then
  229. verify(accountService).deleteAccount(1L);
  230. verify(writer).printLine("Account wasn't deleted. Check id.");
  231. }
  232. }
  233.  
  234. public class ClientViewImplTest {
  235. private Writer writer;
  236. private Reader reader;
  237. private ClientService clientService;
  238. private ClientViewImpl clientView;
  239.  
  240. @BeforeEach
  241. void setUp() {
  242. writer = mock(Writer.class);
  243. reader = mock(Reader.class);
  244. clientService = mock(ClientService.class);
  245. clientView = new ClientViewImpl(writer, reader, clientService);
  246. }
  247.  
  248. @Test
  249. void createClient_correctInput_callServiceWithThatInput() throws IOException {
  250. // Given
  251. ClientDTO client = new ClientDTO();
  252. client.setName("William");
  253. client.setEmail("William@company.com");
  254. when(reader.readString()).thenReturn("William", "William@company.com");
  255. when(clientService.saveClient(notNull())).thenReturn(true);
  256. // When
  257. clientView.createClient();
  258. // Then
  259. verify(clientService).saveClient(client);
  260. verify(writer).printLine("Client created.");
  261. }
  262.  
  263. @Test
  264. void deleteClient_correctInput_callServiceWithThatInput() throws IOException {
  265. // Given
  266. when(reader.readLong()).thenReturn(1L);
  267. when(clientService.deleteClient(eq(1L))).thenReturn(true);
  268. // When
  269. clientView.deleteClient();
  270. // Then
  271. verify(clientService).deleteClient(1L);
  272. verify(writer).printLine("Client with id = " + 1L + " was deleted.");
  273. }
  274.  
  275. @SuppressWarnings("unchecked")
  276. @Test
  277. void displayAllClientsInfo_correctInput_callWriter() throws ParseException {
  278. // Given
  279. DateFormat df = new SimpleDateFormat("HH:mm:ss dd.MM.yyyy");
  280. List<ClientDTO> clients = new ArrayList<>();
  281. clients.add(new ClientDTO(1L, "John Smith", "client@example.com", Arrays.asList(
  282. new AccountDTO(10L, "JSmith1", "zzwvp0d9", df.parse("10:15:30 20.10.2017")),
  283. new AccountDTO(20L, "JSmith2", "mhjnbgfv", df.parse("10:15:30 5.5.2017")),
  284. new AccountDTO(30L, "JSmith3", "ytersds1", df.parse("15:00:30 12.10.2017"))
  285. )));
  286. clients.add(new ClientDTO(2L, "Jack Black", "jack@example.com", new ArrayList<>()));
  287. when(clientService.getAllClients()).thenReturn(clients);
  288. ArgumentCaptor<Object> argument = ArgumentCaptor.forClass(Object.class);
  289. // When
  290. clientView.displayAllClientsInfo();
  291. // Then
  292. verify(writer).printLine(argument.capture());
  293. final String output = argument.getValue().toString();
  294. assertAll(
  295. // Client
  296. () -> assertTrue(output.contains(Long.toString(1))),
  297. () -> assertTrue(output.contains("client@example.com")),
  298. () -> assertTrue(output.contains("John Smith")),
  299. // Accounts
  300. () -> assertTrue(output.contains(Long.toString(10))),
  301. () -> assertTrue(output.contains("JSmith1")),
  302. () -> assertTrue(output.contains("zzwvp0d9")),
  303. () -> assertTrue(output.contains(df.parse("10:15:30 20.10.2017").toString())),
  304. () -> assertTrue(output.contains(Long.toString(20))),
  305. () -> assertTrue(output.contains("JSmith2")),
  306. () -> assertTrue(output.contains("mhjnbgfv")),
  307. () -> assertTrue(output.contains(df.parse("10:15:30 5.5.2017").toString())),
  308. () -> assertTrue(output.contains(Long.toString(30))),
  309. () -> assertTrue(output.contains("JSmith3")),
  310. () -> assertTrue(output.contains("ytersds1")),
  311. () -> assertTrue(output.contains(df.parse("15:00:30 12.10.2017").toString())),
  312. // Client
  313. () -> assertTrue(output.contains(Long.toString(2))),
  314. () -> assertTrue(output.contains("jack@example.com")),
  315. () -> assertTrue(output.contains("Jack Black"))
  316. );
  317. }
  318. }
Add Comment
Please, Sign In to add comment