From: Gustavo Martin Morcuende Date: Mon, 3 Oct 2016 19:14:42 +0000 (+0200) Subject: AccountDao improvements X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=e22ea54855707ceffdd6ed0c338171d3c357d4fe;p=WebAppTest%2F.git AccountDao improvements --- diff --git a/src/main/java/com/prueba/persistence/dao/AccountDao.java b/src/main/java/com/prueba/persistence/dao/AccountDao.java index 4eff9ad..eea86be 100644 --- a/src/main/java/com/prueba/persistence/dao/AccountDao.java +++ b/src/main/java/com/prueba/persistence/dao/AccountDao.java @@ -18,7 +18,7 @@ public class AccountDao { private static final String PASSWORD = "PASSWORD"; private static final String APP_ROLE_CODE = "APPLICATION_ROLE_CODE"; - public Account findAccountByCode(String accountCode) { + public Account findByCode(String accountCode) { final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource(); final DataBaseAccessImpl dataBaseAccess = new DataBaseAccessImpl(dataSource); @@ -54,7 +54,7 @@ public class AccountDao { return account; } - public void createAccount(Account account) { + public void create(Account account) { final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource(); final DataBaseAccessImpl dataBaseAccess = new DataBaseAccessImpl(dataSource); @@ -70,7 +70,7 @@ public class AccountDao { } - public void deleteAccountByCode(String code) { + public void deleteByCode(String code) { final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource(); final DataBaseAccessImpl dataBaseAccess = new DataBaseAccessImpl(dataSource); @@ -80,4 +80,41 @@ public class AccountDao { }); } + + public Account findByCodeAndPassword(String username, String password) { + final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource(); + final DataBaseAccessImpl dataBaseAccess = new DataBaseAccessImpl(dataSource); + + final List> results = + dataBaseAccess.executeQuery("SELECT * FROM ACCOUNT WHERE CODE = ? AND PASSWORD = ?", + answer -> + { + final List> result = new ArrayList<>(); + while (answer.next()) { + final Map row = new HashMap<>(); + row.put(CODE, answer.getString(CODE)); + row.put(NAME, answer.getString(NAME)); + row.put(SURNAME, answer.getString(SURNAME)); + row.put(PASSWORD, answer.getString(PASSWORD)); + row.put(APP_ROLE_CODE, answer.getString(APP_ROLE_CODE)); + result.add(row); + } + + return result; + }, + preparedStatement -> { + preparedStatement.setString(1, username); + preparedStatement.setString(2, password); + }); + + Account account = null; + if (!results.isEmpty()) { + final Map row = results.get(0); + + account = new Account(row.get(CODE), row.get(NAME), + row.get(SURNAME), row.get(PASSWORD), row.get(APP_ROLE_CODE)); + } + + return account; + } } diff --git a/src/main/java/com/prueba/persistence/dao/LoginDao.java b/src/main/java/com/prueba/persistence/dao/LoginDao.java deleted file mode 100644 index 738f6db..0000000 --- a/src/main/java/com/prueba/persistence/dao/LoginDao.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.prueba.persistence.dao; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.sql.DataSource; - -import com.prueba.core.context.integration.database.impl.DataBaseAccessImpl; -import com.prueba.core.context.web.application.ApplicationWebContext; - -public class LoginDao { - private static final String CODE = "CODE"; - private static final String NAME = "NAME"; - private static final String SURNAME = "SURNAME"; - private static final String PASSWORD = "PASSWORD"; - private static final String APP_ROLE_CODE = "APPLICATION_ROLE_CODE"; - - public List> findUserByCodeAndPassword(String username, String password) { - final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource(); - final DataBaseAccessImpl dataBaseAccess = new DataBaseAccessImpl(dataSource); - - return dataBaseAccess.executeQuery("SELECT * FROM ACCOUNT WHERE CODE = ? AND PASSWORD = ?", - answer -> - { - final List> result = new ArrayList<>(); - while (answer.next()) { - final Map row = new HashMap<>(); - row.put(CODE, answer.getString(CODE)); - row.put(NAME, answer.getString(NAME)); - row.put(SURNAME, answer.getString(SURNAME)); - row.put(PASSWORD, answer.getString(PASSWORD)); - row.put(APP_ROLE_CODE, answer.getString(APP_ROLE_CODE)); - result.add(row); - } - - return result; - }, - preparedStatement -> { - preparedStatement.setString(1, username); - preparedStatement.setString(2, password); - }); - } -} diff --git a/src/main/java/com/prueba/services/impl/ApiServiceImpl.java b/src/main/java/com/prueba/services/impl/ApiServiceImpl.java index 03bb5fc..1c9663c 100644 --- a/src/main/java/com/prueba/services/impl/ApiServiceImpl.java +++ b/src/main/java/com/prueba/services/impl/ApiServiceImpl.java @@ -8,18 +8,18 @@ public class ApiServiceImpl { public Account findAccountByCode(String accountCode) { AccountDao accountDao = new AccountDao(); - return accountDao.findAccountByCode(accountCode); + return accountDao.findByCode(accountCode); } public void createAccount(Account account) { AccountDao accountDao = new AccountDao(); - accountDao.createAccount(account); + accountDao.create(account); } public void deleteAccountByCode(String accountCode) { AccountDao accountDao = new AccountDao(); - accountDao.deleteAccountByCode(accountCode); + accountDao.deleteByCode(accountCode); } } diff --git a/src/main/java/com/prueba/services/impl/LoginServiceImpl.java b/src/main/java/com/prueba/services/impl/LoginServiceImpl.java index 1416020..1423621 100644 --- a/src/main/java/com/prueba/services/impl/LoginServiceImpl.java +++ b/src/main/java/com/prueba/services/impl/LoginServiceImpl.java @@ -10,7 +10,7 @@ import java.util.UUID; import com.prueba.core.context.security.persistence.SessionInfo; import com.prueba.core.context.security.persistence.Sessions; import com.prueba.core.context.security.persistence.context.SessionContext; -import com.prueba.persistence.dao.LoginDao; +import com.prueba.persistence.dao.AccountDao; import com.prueba.view.login.LoginFormImpl; import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; @@ -19,9 +19,9 @@ public class LoginServiceImpl { private static final String COOKIE_HEADER = "Cookie"; public boolean isValidUser(String username, String password) { - final LoginDao dao = new LoginDao(); + final AccountDao dao = new AccountDao(); - if (!dao.findUserByCodeAndPassword(username, password).isEmpty()) { + if (null != dao.findByCodeAndPassword(username, password)) { return true; } else { return false; diff --git a/src/test/java/com/prueba/persistence/dao/AccountDaoIntegrationTest.java b/src/test/java/com/prueba/persistence/dao/AccountDaoIntegrationTest.java index 5b307e5..ed65fe4 100644 --- a/src/test/java/com/prueba/persistence/dao/AccountDaoIntegrationTest.java +++ b/src/test/java/com/prueba/persistence/dao/AccountDaoIntegrationTest.java @@ -27,12 +27,27 @@ public class AccountDaoIntegrationTest { } @Test + public void whenFindAccountByCodeAndPasswordThenRetrieveAccount() { + Account expectedAccount = doAccount(); + + Account account = accountDao.findByCodeAndPassword( + expectedAccount.getCode(), expectedAccount.getPassword()); + + assertNotNull(account); + assertEquals(expectedAccount.getCode(), account.getCode()); + assertEquals(expectedAccount.getName(), account.getName()); + assertEquals(expectedAccount.getSurname(), account.getSurname()); + assertEquals(expectedAccount.getPassword(), account.getPassword()); + assertEquals(expectedAccount.getRole(), account.getRole()); + } + + @Test public void whenCreateNewAccountThenRetrieveNewAccount() { Account expectedAccount = doSampleAccount(); - accountDao.createAccount(expectedAccount); + accountDao.create(expectedAccount); - Account account = accountDao.findAccountByCode(expectedAccount.getCode()); + Account account = accountDao.findByCode(expectedAccount.getCode()); assertNotNull(account); assertEquals(expectedAccount.getCode(), account.getCode()); @@ -46,8 +61,8 @@ public class AccountDaoIntegrationTest { public void whenDeleteAccountThenDoNotRetrieveAgainAccount() { Account expectedAccount = doOtherSampleAccount(); - accountDao.createAccount(expectedAccount); - Account account = accountDao.findAccountByCode(expectedAccount.getCode()); + accountDao.create(expectedAccount); + Account account = accountDao.findByCode(expectedAccount.getCode()); assertNotNull(account); assertEquals(expectedAccount.getCode(), account.getCode()); @@ -56,11 +71,21 @@ public class AccountDaoIntegrationTest { assertEquals(expectedAccount.getPassword(), account.getPassword()); assertEquals(expectedAccount.getRole(), account.getRole()); - accountDao.deleteAccountByCode(expectedAccount.getCode()); + accountDao.deleteByCode(expectedAccount.getCode()); - account = accountDao.findAccountByCode(expectedAccount.getCode()); + account = accountDao.findByCode(expectedAccount.getCode()); assertNull(account); } + + private Account doAccount() { + final String expectedCode = "GUMARTIN"; + final String expectedName = "Gustavo"; + final String expectedSurname = "Martin Morcuende"; + final String expectedPassword = "lame"; + final String expectedAppRoleCode = "ROLE_APP_ADMIN"; + + return new Account(expectedCode, expectedName, expectedSurname, expectedPassword, expectedAppRoleCode); + } private Account doSampleAccount() { final String expectedCode = "PRUEBA";