diff --git a/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java index 9653b0e..d448bb0 100644 --- a/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java +++ b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java @@ -57,6 +57,7 @@ public class DBHelper extends SQLiteOpenHelper { super(context, DB_NAME, null, DB_VERSION); readDB = this.getReadableDatabase(); writeDB = this.getWritableDatabase(); + addUser(new Admin()); } @Override @@ -70,7 +71,6 @@ public class DBHelper extends SQLiteOpenHelper { + COLUMN_USERTYPE + " STRING NOT NULL" + ")"; db.execSQL(CREATE_LOGIN_TABLE); - } @Override @@ -96,14 +96,16 @@ public class DBHelper extends SQLiteOpenHelper { public boolean addUser(UserType userType){ //Check for duplicate username by querying login table Cursor cursor = writeDB.query(TABLE_LOGIN, - new String[] {COLUMN_USERNAME}, - COLUMN_USERNAME + " = ?", - new String[]{userType.getUsername()}, - null, null, null, - "1"); + new String[] {COLUMN_USERNAME}, + COLUMN_USERNAME + " = ?", + new String[]{userType.getUsername()}, + null, null, null, + "1"); //If cursor has 1+ elements in it, username already exists in table - if (cursor != null && cursor.getCount() > 0) + if (cursor != null && cursor.getCount() > 0){ + cursor.close(); return false; + } cursor.close(); ContentValues values = new ContentValues(); @@ -127,8 +129,8 @@ public class DBHelper extends SQLiteOpenHelper { public UserType findUserByUsername(String username){ UserType usertype; Cursor cursor = readDB.rawQuery("SELECT * FROM " + TABLE_LOGIN - + " WHERE " + COLUMN_USERNAME + " = ?", - new String[]{username}); + + " WHERE " + COLUMN_USERNAME + " = ?", + new String[]{username}); if (cursor.moveToFirst()){ String password = cursor.getString(1); @@ -215,12 +217,15 @@ public class DBHelper extends SQLiteOpenHelper { public List getAllUsers(){ List listOfUsers = new LinkedList<>(); String[] user; - Cursor cursor = readDB.rawQuery("SELECT ? , ?, ?, ? FROM "+TABLE_LOGIN, - new String[]{COLUMN_USERNAME, COLUMN_FIRSTNAME, - COLUMN_LASTNAME, COLUMN_USERTYPE}); + Cursor cursor = readDB.rawQuery("SELECT " + COLUMN_USERNAME + ", " + + COLUMN_FIRSTNAME + ", " + + COLUMN_LASTNAME + ", " + + COLUMN_USERTYPE + + " FROM "+TABLE_LOGIN, null); + if (cursor.moveToFirst()) { for (int i = 0; i < cursor.getCount(); i++) { - user = new String[4]; + user = new String[cursor.getColumnNames().length]; for (int j = 0; j < cursor.getColumnNames().length; j++) { user[j] = cursor.getString(j); } @@ -231,4 +236,4 @@ public class DBHelper extends SQLiteOpenHelper { return listOfUsers; } -} \ No newline at end of file +} diff --git a/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBUnitTest.java b/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBIntegrationTest.java similarity index 76% rename from OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBUnitTest.java rename to OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBIntegrationTest.java index d863fcc..41fa25b 100644 --- a/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBUnitTest.java +++ b/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBIntegrationTest.java @@ -11,13 +11,26 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; +import java.util.List; + import static org.junit.Assert.*; @RunWith(RobolectricTestRunner.class) @Config(packageName = "com.uottawa.olympus.olympusservices") -public class DBUnitTest { +public class DBIntegrationTest { private DBHelper dbHelper = new DBHelper(RuntimeEnvironment.application); + + @Test + public void testAdminExists(){ + UserType dbUser = dbHelper.findUserByUsername("admin"); + assertEquals("Admin", dbUser.getClass().getSimpleName()); + assertEquals("admin", dbUser.getUsername()); + assertEquals("admin", dbUser.getPassword()); + assertEquals("Admin", dbUser.getFirstname()); + assertEquals("Admin", dbUser.getLastname()); + } + @Test public void testAddAndDeleteUser(){ UserType originalUser, dbUser; @@ -95,4 +108,20 @@ public class DBUnitTest { dbHelper.deleteUser("jbO4aBF4dC"); } + @Test + public void testGetAllUsers(){ + dbHelper.addUser(new User("jbO4aBF4dC", "soccer", "Miguel", "Garzon")); + + List allUsers = dbHelper.getAllUsers(); + + for (String[] user : allUsers){ + UserType usertype = dbHelper.findUserByUsername(user[0]); + assertEquals(usertype.getFirstname(), user[1]); + assertEquals(usertype.getLastname(), user[2]); + assertEquals(usertype.getClass().getSimpleName(), user[3]); + } + + dbHelper.deleteUser("jbO4aBF4dC"); + } } +