From c0dba697c89a666db054e15c2c301b99c9d547c6 Mon Sep 17 00:00:00 2001 From: bgeVam Date: Tue, 2 May 2017 21:54:54 +0200 Subject: [PATCH] add userdao tests * closes #37 --- .../data/UserDAOImplTest.java | 66 +++++++++++++++++++ .../{ => service}/UserServiceImplTest.java | 15 ++++- .../activities/LoginActivity.java | 6 +- .../wifisdcryptolocker/data/dao/UserDao.java | 17 ++++- .../data/dao/impl/UserDaoImpl.java | 10 ++- .../service/UserService.java | 3 +- .../service/impl/UserServiceImpl.java | 6 +- 7 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 app/src/androidTest/java/io/github/projektmedinf/wifisdcryptolocker/data/UserDAOImplTest.java rename app/src/androidTest/java/io/github/projektmedinf/wifisdcryptolocker/{ => service}/UserServiceImplTest.java (78%) diff --git a/app/src/androidTest/java/io/github/projektmedinf/wifisdcryptolocker/data/UserDAOImplTest.java b/app/src/androidTest/java/io/github/projektmedinf/wifisdcryptolocker/data/UserDAOImplTest.java new file mode 100644 index 0000000..ad48fb6 --- /dev/null +++ b/app/src/androidTest/java/io/github/projektmedinf/wifisdcryptolocker/data/UserDAOImplTest.java @@ -0,0 +1,66 @@ +package io.github.projektmedinf.wifisdcryptolocker.data; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; +import io.github.projektmedinf.wifisdcryptolocker.data.dao.UserDao; +import io.github.projektmedinf.wifisdcryptolocker.data.dao.impl.UserDaoImpl; +import io.github.projektmedinf.wifisdcryptolocker.model.User; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.core.IsNull.nullValue; +import static org.junit.Assert.assertThat; + +/** + * Instrumentation test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class UserDAOImplTest { + + private static final String VALID_USER_NAME = "validUser"; + private static final String ANOTHER_VALID_USER_NAME = "anotherValidUser"; + private static final String INVALID_USER_NAME = "invalidUser"; + + private UserDao userDao; + private User validUser; + + @Before + public void setUp() throws Exception { + Context appContext = InstrumentationRegistry.getTargetContext(); + userDao = new UserDaoImpl(appContext); + userDao.insertUser(VALID_USER_NAME, "12345"); + validUser = userDao.getUserByUserName(VALID_USER_NAME); + } + + @Test + public void shouldReturnUserMatchingTheGivenUserName() throws Exception { + assertThat(userDao.getUserByUserName(VALID_USER_NAME), is(validUser)); + } + + @Test + public void shouldReturnNullIfNoMatchWasFound() throws Exception { + assertThat(userDao.getUserByUserName(INVALID_USER_NAME), is(nullValue())); + } + + @Test + public void shouldReturnTheIdOfTheUserOnSuccess() throws Exception { + Object userId = userDao.insertUser(ANOTHER_VALID_USER_NAME, "12345"); + assertThat(userId instanceof Long, is(true)); + } + + @Test + public void shouldReturnMinusOneIfAGeneralDatabaseErrorOccurs() throws Exception { + //TODO + } + + @Test + public void shouldReturnMinusTwoIfTheUserAlreadyExists() throws Exception { + long userId = userDao.insertUser(VALID_USER_NAME, "12345"); + assertThat(userId, is(-2l)); + } +} diff --git a/app/src/androidTest/java/io/github/projektmedinf/wifisdcryptolocker/UserServiceImplTest.java b/app/src/androidTest/java/io/github/projektmedinf/wifisdcryptolocker/service/UserServiceImplTest.java similarity index 78% rename from app/src/androidTest/java/io/github/projektmedinf/wifisdcryptolocker/UserServiceImplTest.java rename to app/src/androidTest/java/io/github/projektmedinf/wifisdcryptolocker/service/UserServiceImplTest.java index 614e2f4..743ab37 100644 --- a/app/src/androidTest/java/io/github/projektmedinf/wifisdcryptolocker/UserServiceImplTest.java +++ b/app/src/androidTest/java/io/github/projektmedinf/wifisdcryptolocker/service/UserServiceImplTest.java @@ -1,4 +1,4 @@ -package io.github.projektmedinf.wifisdcryptolocker; +package io.github.projektmedinf.wifisdcryptolocker.service; import android.content.Context; import android.support.test.InstrumentationRegistry; @@ -6,10 +6,11 @@ import io.github.projektmedinf.wifisdcryptolocker.data.dao.UserDao; import io.github.projektmedinf.wifisdcryptolocker.data.dao.impl.UserDaoImpl; import io.github.projektmedinf.wifisdcryptolocker.model.User; -import io.github.projektmedinf.wifisdcryptolocker.service.UserService; import io.github.projektmedinf.wifisdcryptolocker.service.impl.UserServiceImpl; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import static org.hamcrest.CoreMatchers.is; @@ -26,7 +27,8 @@ public class UserServiceImplTest { private static final String VALID_USER_NAME = "validUser"; private static final String INVALID_USER_NAME = "invalidUser"; - + @Rule + public ExpectedException expectedException = ExpectedException.none(); private UserService userService; private User validUser; @@ -48,4 +50,11 @@ public void shouldReturnUserMatchingTheGivenUserName() throws Exception { public void shouldReturnNullIfNoMatchWasFound() throws Exception { assertThat(userService.getUserByUserName(INVALID_USER_NAME), is(nullValue())); } + + @Test + public void shouldThrowIllegalArgumentExceptionGivenNull() throws Exception { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("userName cannot be null"); + userService.getUserByUserName(null); + } } diff --git a/app/src/main/java/io/github/projektmedinf/wifisdcryptolocker/activities/LoginActivity.java b/app/src/main/java/io/github/projektmedinf/wifisdcryptolocker/activities/LoginActivity.java index dc45c47..dd0fe04 100644 --- a/app/src/main/java/io/github/projektmedinf/wifisdcryptolocker/activities/LoginActivity.java +++ b/app/src/main/java/io/github/projektmedinf/wifisdcryptolocker/activities/LoginActivity.java @@ -303,7 +303,11 @@ public class UserLoginTask extends AsyncTask { @Override protected Boolean doInBackground(Void... params) { - found = userService.getUserByUserName(mUsername); + try { + found = userService.getUserByUserName(mUsername); + } catch (IllegalArgumentException e) { + Toast.makeText(getApplicationContext(), e.getLocalizedMessage(), Toast.LENGTH_SHORT).show(); + } return found != null && CryptoUtils.comparePasswords(mPassword, found.getPassword()); } diff --git a/app/src/main/java/io/github/projektmedinf/wifisdcryptolocker/data/dao/UserDao.java b/app/src/main/java/io/github/projektmedinf/wifisdcryptolocker/data/dao/UserDao.java index b7e424d..0a9da6d 100644 --- a/app/src/main/java/io/github/projektmedinf/wifisdcryptolocker/data/dao/UserDao.java +++ b/app/src/main/java/io/github/projektmedinf/wifisdcryptolocker/data/dao/UserDao.java @@ -4,9 +4,21 @@ /** * Created by stiefel40k on 20.04.17. + *

+ * Data layer for {@code User}. + * + * @see io.github.projektmedinf.wifisdcryptolocker.model.User */ public interface UserDao { + /** + * Get the {@code User} by its {@code userName} + * + * @param userName name of the wanted user + * @return the user matching the given user name + * @should return user matching the given user name + * @should return null if no match was found + */ User getUserByUserName(String userName); /** @@ -14,7 +26,10 @@ public interface UserDao { * * @param userName the chosen username * @param password the hashed password - * @return the id of the user on success. -1 if a general database error occurred and -2 i the user already exists. + * @return the id of the user on success + * @should return the id of the user on success + * @should return -1 if a general database error occurs + * @should return -2 if the user already exists */ long insertUser(String userName, String password); } diff --git a/app/src/main/java/io/github/projektmedinf/wifisdcryptolocker/data/dao/impl/UserDaoImpl.java b/app/src/main/java/io/github/projektmedinf/wifisdcryptolocker/data/dao/impl/UserDaoImpl.java index 7cb6ab1..0b468c9 100644 --- a/app/src/main/java/io/github/projektmedinf/wifisdcryptolocker/data/dao/impl/UserDaoImpl.java +++ b/app/src/main/java/io/github/projektmedinf/wifisdcryptolocker/data/dao/impl/UserDaoImpl.java @@ -21,14 +21,15 @@ */ public class UserDaoImpl implements UserDao { - - private SQLiteDatabase sqLiteDatabase; - public UserDaoImpl(Context context){ + public UserDaoImpl(Context context) { sqLiteDatabase = new DatabaseHelper(context).getReadableDatabase(); } + /** + * @see UserDao#getUserByUserName(String) + */ @Override public User getUserByUserName(String userName) { User toReturn = null; @@ -57,6 +58,9 @@ public User getUserByUserName(String userName) { return toReturn; } + /** + * @see UserDao#insertUser(String, String) + */ @Override public long insertUser(String userName, String password) { if (getUserByUserName(userName) != null) { diff --git a/app/src/main/java/io/github/projektmedinf/wifisdcryptolocker/service/UserService.java b/app/src/main/java/io/github/projektmedinf/wifisdcryptolocker/service/UserService.java index 8463bef..5dbe440 100644 --- a/app/src/main/java/io/github/projektmedinf/wifisdcryptolocker/service/UserService.java +++ b/app/src/main/java/io/github/projektmedinf/wifisdcryptolocker/service/UserService.java @@ -16,11 +16,12 @@ public interface UserService { * * @param userName name of the wanted user * @return the user matching the given user name + * @throws illegal argument exception given null * @should return user matching the given user name * @should return null if no match was found * @should throw illegal argument exception given null */ - User getUserByUserName(String userName); + User getUserByUserName(String userName) throws IllegalArgumentException; /** * Insert a new {@code User} given an {@code userName} and a {@code password} diff --git a/app/src/main/java/io/github/projektmedinf/wifisdcryptolocker/service/impl/UserServiceImpl.java b/app/src/main/java/io/github/projektmedinf/wifisdcryptolocker/service/impl/UserServiceImpl.java index e24dfd5..ca2c849 100644 --- a/app/src/main/java/io/github/projektmedinf/wifisdcryptolocker/service/impl/UserServiceImpl.java +++ b/app/src/main/java/io/github/projektmedinf/wifisdcryptolocker/service/impl/UserServiceImpl.java @@ -17,11 +17,15 @@ public UserServiceImpl(Context context) { userDao = new UserDaoImpl(context); } + /** * @see UserService#getUserByUserName(String) */ @Override - public User getUserByUserName(String userName) { + public User getUserByUserName(String userName) throws IllegalArgumentException { + if (userName == null) { + throw new IllegalArgumentException("userName cannot be null"); + } return userDao.getUserByUserName(userName); }