Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add userdao tests #42

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@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));
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package io.github.projektmedinf.wifisdcryptolocker;
package io.github.projektmedinf.wifisdcryptolocker.service;

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 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;
Expand All @@ -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;

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,11 @@ public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {

@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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,32 @@

/**
* Created by stiefel40k on 20.04.17.
* <p>
* 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);

/**
* Inserts a new user into the database
*
* @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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down