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);
return account;
}
- public void createAccount(Account account) {
+ public void create(Account account) {
final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource();
final DataBaseAccessImpl dataBaseAccess = new DataBaseAccessImpl(dataSource);
}
- public void deleteAccountByCode(String code) {
+ public void deleteByCode(String code) {
final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource();
final DataBaseAccessImpl dataBaseAccess = new DataBaseAccessImpl(dataSource);
});
}
+
+ public Account findByCodeAndPassword(String username, String password) {
+ final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource();
+ final DataBaseAccessImpl dataBaseAccess = new DataBaseAccessImpl(dataSource);
+
+ final List<Map<String, String>> results =
+ dataBaseAccess.executeQuery("SELECT * FROM ACCOUNT WHERE CODE = ? AND PASSWORD = ?",
+ answer ->
+ {
+ final List<Map<String, String>> result = new ArrayList<>();
+ while (answer.next()) {
+ final Map<String, String> 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<String, String> 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;
+ }
}
+++ /dev/null
-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<Map<String, String>> 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<Map<String, String>> result = new ArrayList<>();
- while (answer.next()) {
- final Map<String, String> 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);
- });
- }
-}
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);
}
}
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;
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;
}
@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());
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());
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";