From 0762919f749cb5f238ed9e8abd1a8a53c9d9e1e5 Mon Sep 17 00:00:00 2001 From: Mary Tran Date: Mon, 8 Oct 2018 11:48:58 -0400 Subject: [PATCH 1/4] Added DBHelper for database access and DBTest. Added constructors, setters, and getters to UserType classes --- .../olympus/olympusservices/DBTest.java | 80 ++++++++++ .../olympus/olympusservices/Admin.java | 4 + .../olympus/olympusservices/DBHelper.java | 146 ++++++++++++++++++ .../olympusservices/ServiceProvider.java | 5 + .../uottawa/olympus/olympusservices/User.java | 5 + .../olympus/olympusservices/UserType.java | 61 +++++++- 6 files changed, 298 insertions(+), 3 deletions(-) create mode 100644 OlympusServices/app/src/androidTest/java/com/uottawa/olympus/olympusservices/DBTest.java create mode 100644 OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java diff --git a/OlympusServices/app/src/androidTest/java/com/uottawa/olympus/olympusservices/DBTest.java b/OlympusServices/app/src/androidTest/java/com/uottawa/olympus/olympusservices/DBTest.java new file mode 100644 index 0000000..c372bd8 --- /dev/null +++ b/OlympusServices/app/src/androidTest/java/com/uottawa/olympus/olympusservices/DBTest.java @@ -0,0 +1,80 @@ +package com.uottawa.olympus.olympusservices; + +import android.content.Context; +import android.database.DatabaseUtils; +import android.database.sqlite.SQLiteDatabase; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +@RunWith(AndroidJUnit4.class) +public class DBTest { + private DBHelper dbHelper = new DBHelper(InstrumentationRegistry.getTargetContext()); + + @Test + public void testAddAndDeleteUser(){ + UserType originalUser, dbUser; + boolean deleted; + + originalUser = new Admin(); + dbHelper.addUser(originalUser); + 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()); + + + originalUser = new User("mgarzon", "soccer", "Miguel", "Garzon"); + dbHelper.addUser(originalUser); + dbUser = dbHelper.findUserByUsername("mgarzon"); + + assertEquals("User", dbUser.getClass().getSimpleName()); + assertEquals("mgarzon", dbUser.getUsername()); + assertEquals("soccer", dbUser.getPassword()); + assertEquals("Miguel", dbUser.getFirstname()); + assertEquals("Garzon", dbUser.getLastname()); + + + originalUser = new ServiceProvider("jguzman", "seg2105", "Juan", "Guzman"); + dbHelper.addUser(originalUser); + dbUser = dbHelper.findUserByUsername("jguzman"); + + assertEquals("ServiceProvider", dbUser.getClass().getSimpleName()); + assertEquals("jguzman", dbUser.getUsername()); + assertEquals("seg2105", dbUser.getPassword()); + assertEquals("Juan", dbUser.getFirstname()); + assertEquals("Guzman", dbUser.getLastname()); + + + deleted = dbHelper.deleteUser("admin"); + assertEquals(true, deleted); + + deleted = dbHelper.deleteUser("mgarzon"); + assertEquals(true, deleted); + + deleted = dbHelper.deleteUser("jguzman"); + assertEquals(true, deleted); + + } + + + @Test + public void testAddDuplicateUsers(){ + boolean added; + + added = dbHelper.addUser(new User("mgarzon", "soccer", "Miguel", "Garzon")); + assertTrue(added); + added = dbHelper.addUser(new ServiceProvider("mgarzon", "soccer", "Miguel", "Garzon")); + assertTrue(!added); + + dbHelper.deleteUser("mgarzon"); + } + +} diff --git a/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/Admin.java b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/Admin.java index ca2e6b0..b10f901 100644 --- a/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/Admin.java +++ b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/Admin.java @@ -1,4 +1,8 @@ package com.uottawa.olympus.olympusservices; public class Admin extends UserType { + + Admin(){ + super("admin", "admin", "admin", "admin"); + } } 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 new file mode 100644 index 0000000..2086573 --- /dev/null +++ b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java @@ -0,0 +1,146 @@ +package com.uottawa.olympus.olympusservices; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.database.Cursor; +import android.content.ContentValues; + + +public class DBHelper extends SQLiteOpenHelper { + + private static final int DB_VERSION = 1; + private static final String DB_NAME = "UsersDB.db"; + private static String DB_PATH = ""; + private static SQLiteDatabase readDB; + private static SQLiteDatabase writeDB; + + + private static final String TABLE_LOGIN = "userInfo"; + private static final String COLUMN_USERNAME = "username"; + private static final String COLUMN_PASSWORD = "password"; + private static final String COLUMN_FIRSTNAME = "firstName"; + private static final String COLUMN_LASTNAME = "lastName"; + private static final String COLUMN_USERTYPE = "userType"; + +// private static final String TABLE_SERVICEPROVIDERS = "serviceProviders"; + + +// private static final String TABLE_SERVICES = "services"; + + + public DBHelper(Context context){ + super(context, DB_NAME, null, DB_VERSION); + readDB = this.getReadableDatabase(); + writeDB = this.getWritableDatabase(); + } + + @Override + public void onCreate(SQLiteDatabase db){ + + String CREATE_LOGIN_TABLE = "CREATE TABLE "+ TABLE_LOGIN + "(" + + COLUMN_USERNAME + " STRING UNIQUE NOT NULL PRIMARY KEY ON CONFLICT ROLLBACK," + + COLUMN_PASSWORD + " STRING NOT NULL," + + COLUMN_FIRSTNAME + " STRING DEFAULT 'FirstName'," + + COLUMN_LASTNAME + " STRING DEFAULT 'LastName'," + + COLUMN_USERTYPE + " STRING NOT NULL" + ")"; + + db.execSQL(CREATE_LOGIN_TABLE); + + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ + db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN); + onCreate(db); + } + + @Override + public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { + onUpgrade(db, oldVersion, newVersion); + } + + + 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"); + //If cursor has 1+ elements in it, username already exists in table + if (cursor != null && cursor.getCount() > 0) + return false; + cursor.close(); + + ContentValues values = new ContentValues(); + values.put(COLUMN_USERNAME, userType.getUsername()); + values.put(COLUMN_PASSWORD, userType.getPassword()); + values.put(COLUMN_FIRSTNAME, userType.getFirstname()); + values.put(COLUMN_LASTNAME, userType.getLastname()); + values.put(COLUMN_USERTYPE, userType.getClass().getSimpleName()); + writeDB.insert(TABLE_LOGIN, null, values); + return true; + } + + + public UserType findUserByUsername(String username){ + UserType usertype; + Cursor cursor = readDB.rawQuery("SELECT * FROM " + TABLE_LOGIN + + " WHERE " + COLUMN_USERNAME + " = ?", + new String[]{username}); + + if (cursor.moveToFirst()){ + String password = cursor.getString(1); + String firstname = cursor.getString(2); + String lastname = cursor.getString(3); + if (cursor.getString(4) + .equals("Admin")){ + usertype = new Admin(); + } else if (cursor.getString(4) + .equals("ServiceProvider")){ + usertype = new ServiceProvider(username, password, firstname, lastname); + } else { + usertype = new User(username, password, firstname, lastname); + } + } else { + usertype = null; + } + cursor.close(); + return usertype; + } + + + public boolean updateUser(String username, String password, String firstname, String lastname){ + ContentValues values = new ContentValues(); + values.put(COLUMN_PASSWORD, password); + values.put(COLUMN_FIRSTNAME, firstname); + values.put(COLUMN_LASTNAME, lastname); + + return writeDB.update(TABLE_LOGIN, values, COLUMN_USERNAME+" = ?", + new String[]{username}) > 0; + + } + + + public boolean deleteUser(String username) { + return writeDB.delete(TABLE_LOGIN, COLUMN_USERNAME+" = ?", + new String[]{username}) > 0; + } + + public void printTable(String tableName){ + Cursor cursor = readDB.rawQuery("SELECT * FROM "+tableName, null); + cursor.moveToFirst(); + for (int i = 0; i Date: Mon, 8 Oct 2018 16:25:15 -0400 Subject: [PATCH 2/4] Running unit tests with Robolectric instead of starting up emulator every time --- OlympusServices/app/build.gradle | 11 +++++++++- .../olympus/olympusservices/DBHelper.java | 7 +++++++ .../olympus/olympusservices/DBUnitTest.java} | 21 +++++++++++-------- 3 files changed, 29 insertions(+), 10 deletions(-) rename OlympusServices/app/src/{androidTest/java/com/uottawa/olympus/olympusservices/DBTest.java => test/java/com/uottawa/olympus/olympusservices/DBUnitTest.java} (82%) diff --git a/OlympusServices/app/build.gradle b/OlympusServices/app/build.gradle index 4a8aefa..481e4c8 100644 --- a/OlympusServices/app/build.gradle +++ b/OlympusServices/app/build.gradle @@ -16,6 +16,11 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + testOptions { + unitTests { + includeAndroidResources = true + } + } } dependencies { @@ -24,10 +29,14 @@ dependencies { implementation 'com.android.support:design:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' + testImplementation 'org.robolectric:robolectric:4.0-alpha-3-SNAPSHOT' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.jaredrummler:material-spinner:1.2.5' implementation 'com.android.support:design:28.0.0-alpha3' implementation 'com.rengwuxian.materialedittext:library:2.1.4' - } + +repositories { + maven { url "https://oss.sonatype.org/content/repositories/snapshots" } +} \ No newline at end of file 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 2086573..9c7115e 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 @@ -6,6 +6,13 @@ import android.database.sqlite.SQLiteOpenHelper; import android.database.Cursor; import android.content.ContentValues; +/** + * The class DBHelper allows the Android application to access and perform + * CRUD operations on the tables of the SQLite database. + * There is currently a table of all users' login information. + * + * To use, create an object of this class with the current activity as context. + */ public class DBHelper extends SQLiteOpenHelper { diff --git a/OlympusServices/app/src/androidTest/java/com/uottawa/olympus/olympusservices/DBTest.java b/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBUnitTest.java similarity index 82% rename from OlympusServices/app/src/androidTest/java/com/uottawa/olympus/olympusservices/DBTest.java rename to OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBUnitTest.java index c372bd8..283b933 100644 --- a/OlympusServices/app/src/androidTest/java/com/uottawa/olympus/olympusservices/DBTest.java +++ b/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBUnitTest.java @@ -3,17 +3,20 @@ package com.uottawa.olympus.olympusservices; import android.content.Context; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; -import android.support.test.InstrumentationRegistry; -import android.support.test.runner.AndroidJUnit4; + import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; import static org.junit.Assert.*; -@RunWith(AndroidJUnit4.class) -public class DBTest { - private DBHelper dbHelper = new DBHelper(InstrumentationRegistry.getTargetContext()); +@RunWith(RobolectricTestRunner.class) +@Config(packageName = "com.uottawa.olympus.olympusservices") +public class DBUnitTest { + private DBHelper dbHelper = new DBHelper(RuntimeEnvironment.application); @Test public void testAddAndDeleteUser(){ @@ -54,13 +57,13 @@ public class DBTest { deleted = dbHelper.deleteUser("admin"); - assertEquals(true, deleted); + assertTrue(deleted); deleted = dbHelper.deleteUser("mgarzon"); - assertEquals(true, deleted); + assertTrue(deleted); deleted = dbHelper.deleteUser("jguzman"); - assertEquals(true, deleted); + assertTrue(deleted); } @@ -71,7 +74,7 @@ public class DBTest { added = dbHelper.addUser(new User("mgarzon", "soccer", "Miguel", "Garzon")); assertTrue(added); - added = dbHelper.addUser(new ServiceProvider("mgarzon", "soccer", "Miguel", "Garzon")); + added = dbHelper.addUser(new ServiceProvider("mgarzon", "seg2105", "Juan", "Guzman")); assertTrue(!added); dbHelper.deleteUser("mgarzon"); From a45341d30d3cf8d215c378b56ec2f22fe6dba9c0 Mon Sep 17 00:00:00 2001 From: Mary Tran Date: Mon, 8 Oct 2018 18:00:21 -0400 Subject: [PATCH 3/4] Added docs and ability to get a list of all users from database --- .../olympus/olympusservices/Admin.java | 2 +- .../olympus/olympusservices/DBHelper.java | 97 +++++++++++++++++-- .../olympus/olympusservices/DBUnitTest.java | 37 ++++--- 3 files changed, 114 insertions(+), 22 deletions(-) diff --git a/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/Admin.java b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/Admin.java index b10f901..55fd6bf 100644 --- a/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/Admin.java +++ b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/Admin.java @@ -3,6 +3,6 @@ package com.uottawa.olympus.olympusservices; public class Admin extends UserType { Admin(){ - super("admin", "admin", "admin", "admin"); + super("admin", "admin", "Admin", "Admin"); } } 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 9c7115e..9653b0e 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 @@ -6,24 +6,35 @@ import android.database.sqlite.SQLiteOpenHelper; import android.database.Cursor; import android.content.ContentValues; +import java.util.LinkedList; +import java.util.List; + /** * The class DBHelper allows the Android application to access and perform - * CRUD operations on the tables of the SQLite database. - * There is currently a table of all users' login information. + * CRUD (Create, Read, Update, Delete) operations on the tables of the SQLite database. + * There is currently one table of all users' login information and names. + * Table of service providers and services to come soon. * * To use, create an object of this class with the current activity as context. + * */ public class DBHelper extends SQLiteOpenHelper { + //version of db used for update method private static final int DB_VERSION = 1; + //name of db in app data private static final String DB_NAME = "UsersDB.db"; - private static String DB_PATH = ""; + + //SQLiteDatabase for reading private static SQLiteDatabase readDB; + + //SQLiteDatabase for writing private static SQLiteDatabase writeDB; - + //name of table containing user login information and names private static final String TABLE_LOGIN = "userInfo"; + //columns of TABLE_LOGIN private static final String COLUMN_USERNAME = "username"; private static final String COLUMN_PASSWORD = "password"; private static final String COLUMN_FIRSTNAME = "firstName"; @@ -36,6 +47,12 @@ public class DBHelper extends SQLiteOpenHelper { // private static final String TABLE_SERVICES = "services"; + /** + * Creates an instance of DBHelper to allow activities to access and + * perform CRUD operations on the database via DBHelper's methods + * + * @param context current activity calling DBHelper + */ public DBHelper(Context context){ super(context, DB_NAME, null, DB_VERSION); readDB = this.getReadableDatabase(); @@ -68,6 +85,14 @@ public class DBHelper extends SQLiteOpenHelper { } + /** + * Adds a user to the database. Returns false if there is a user already + * existing in the database with the same username. Returns true if + * successful in adding user to database. + * + * @param userType user to be added + * @return whether adding user was successful + */ public boolean addUser(UserType userType){ //Check for duplicate username by querying login table Cursor cursor = writeDB.query(TABLE_LOGIN, @@ -91,7 +116,14 @@ public class DBHelper extends SQLiteOpenHelper { return true; } - + /** + * Looks in database for user with requested username, and returns an + * object of UserType corresponding to said user's role. + * Returns null if no such user found. + * + * @param username username to look up + * @return object representing user found + */ public UserType findUserByUsername(String username){ UserType usertype; Cursor cursor = readDB.rawQuery("SELECT * FROM " + TABLE_LOGIN @@ -118,8 +150,20 @@ public class DBHelper extends SQLiteOpenHelper { return usertype; } - - public boolean updateUser(String username, String password, String firstname, String lastname){ + /** + * Updates user login information and name for user with requested username. + * Returns true if a user of said username was found and entry updated. + * Returns false if no user was found of said username. + * + * + * @param username username of entry to update + * @param password new password + * @param firstname new first name + * @param lastname new last name + * + * @return whether updating user information was successful + */ + public boolean updateUserInfo(String username, String password, String firstname, String lastname){ ContentValues values = new ContentValues(); values.put(COLUMN_PASSWORD, password); values.put(COLUMN_FIRSTNAME, firstname); @@ -130,12 +174,24 @@ public class DBHelper extends SQLiteOpenHelper { } - + /** + * Looks in database for user with requested username, and deletes the corresponding + * entry. Returns true if a user was deleted, false otherwise. + * + * @param username username of entry to delete + * @return whether a user was deleted + */ public boolean deleteUser(String username) { return writeDB.delete(TABLE_LOGIN, COLUMN_USERNAME+" = ?", new String[]{username}) > 0; } + /** + * Prints all entries of table. One row is printed per line. Columns are + * separated by spaces. + * + * @param tableName name of table to print + */ public void printTable(String tableName){ Cursor cursor = readDB.rawQuery("SELECT * FROM "+tableName, null); cursor.moveToFirst(); @@ -150,4 +206,29 @@ public class DBHelper extends SQLiteOpenHelper { cursor.close(); } + /** + * Returns a list of String arrays containing the username, first name, + * last name, and user type of every user in TABLE_LOGIN. + * + * @return list of arrays of [username, first name, last name, user type] + */ + 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}); + if (cursor.moveToFirst()) { + for (int i = 0; i < cursor.getCount(); i++) { + user = new String[4]; + for (int j = 0; j < cursor.getColumnNames().length; j++) { + user[j] = cursor.getString(j); + } + listOfUsers.add(user); + } + } + cursor.close(); + 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/DBUnitTest.java index 283b933..77fc80f 100644 --- a/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBUnitTest.java +++ b/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBUnitTest.java @@ -23,16 +23,6 @@ public class DBUnitTest { UserType originalUser, dbUser; boolean deleted; - originalUser = new Admin(); - dbHelper.addUser(originalUser); - 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()); - originalUser = new User("mgarzon", "soccer", "Miguel", "Garzon"); dbHelper.addUser(originalUser); @@ -56,9 +46,6 @@ public class DBUnitTest { assertEquals("Guzman", dbUser.getLastname()); - deleted = dbHelper.deleteUser("admin"); - assertTrue(deleted); - deleted = dbHelper.deleteUser("mgarzon"); assertTrue(deleted); @@ -74,10 +61,34 @@ public class DBUnitTest { added = dbHelper.addUser(new User("mgarzon", "soccer", "Miguel", "Garzon")); assertTrue(added); + added = dbHelper.addUser(new User("mgarzon", "seg2105", "Miguel", "Garzon")); + assertTrue(!added); added = dbHelper.addUser(new ServiceProvider("mgarzon", "seg2105", "Juan", "Guzman")); assertTrue(!added); dbHelper.deleteUser("mgarzon"); } + @Test + public void testUpdateUserLogin(){ + boolean updated; + UserType dbUser; + + dbHelper.addUser(new User("mgarzon", "soccer", "Miguel", "Garzon")); + updated = dbHelper.updateUserInfo("mgarzon", "soccer", "Juan", "Guzman"); + assertTrue(updated); + + dbUser = dbHelper.findUserByUsername("mgarzon"); + + assertEquals("mgarzon", dbUser.getUsername()); + assertEquals("soccer", dbUser.getPassword()); + assertEquals("Juan", dbUser.getFirstname()); + assertEquals("Guzman", dbUser.getLastname()); + + updated = dbHelper.updateUserInfo("jguzman", "seg2105", "Juan", "Guzman"); + assertTrue(!updated); + + dbHelper.deleteUser("mgarzon"); + } + } From 96ca7c849008eb463139735e2f8f271227aa841a Mon Sep 17 00:00:00 2001 From: Mary Tran Date: Mon, 8 Oct 2018 18:16:08 -0400 Subject: [PATCH 4/4] Changed some unit test usernames to randomized string --- .../olympus/olympusservices/DBUnitTest.java | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBUnitTest.java b/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBUnitTest.java index 77fc80f..d863fcc 100644 --- a/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBUnitTest.java +++ b/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBUnitTest.java @@ -1,8 +1,8 @@ package com.uottawa.olympus.olympusservices; -import android.content.Context; -import android.database.DatabaseUtils; -import android.database.sqlite.SQLiteDatabase; +//import android.content.Context; +//import android.database.DatabaseUtils; +//import android.database.sqlite.SQLiteDatabase; import org.junit.Test; @@ -21,11 +21,11 @@ public class DBUnitTest { @Test public void testAddAndDeleteUser(){ UserType originalUser, dbUser; - boolean deleted; + boolean deleted, addedOne, addedTwo; originalUser = new User("mgarzon", "soccer", "Miguel", "Garzon"); - dbHelper.addUser(originalUser); + addedOne = dbHelper.addUser(originalUser); dbUser = dbHelper.findUserByUsername("mgarzon"); assertEquals("User", dbUser.getClass().getSimpleName()); @@ -35,22 +35,25 @@ public class DBUnitTest { assertEquals("Garzon", dbUser.getLastname()); - originalUser = new ServiceProvider("jguzman", "seg2105", "Juan", "Guzman"); - dbHelper.addUser(originalUser); - dbUser = dbHelper.findUserByUsername("jguzman"); + originalUser = new ServiceProvider("jbO4aBF4dC", "seg2105", "Juan", "Guzman"); + addedTwo = dbHelper.addUser(originalUser); + dbUser = dbHelper.findUserByUsername("jbO4aBF4dC"); assertEquals("ServiceProvider", dbUser.getClass().getSimpleName()); - assertEquals("jguzman", dbUser.getUsername()); + assertEquals("jbO4aBF4dC", dbUser.getUsername()); assertEquals("seg2105", dbUser.getPassword()); assertEquals("Juan", dbUser.getFirstname()); assertEquals("Guzman", dbUser.getLastname()); + if (addedOne) { + deleted = dbHelper.deleteUser("mgarzon"); + assertTrue(deleted); + } - deleted = dbHelper.deleteUser("mgarzon"); - assertTrue(deleted); - - deleted = dbHelper.deleteUser("jguzman"); - assertTrue(deleted); + if (addedTwo) { + deleted = dbHelper.deleteUser("jbO4aBF4dC"); + assertTrue(deleted); + } } @@ -59,14 +62,14 @@ public class DBUnitTest { public void testAddDuplicateUsers(){ boolean added; - added = dbHelper.addUser(new User("mgarzon", "soccer", "Miguel", "Garzon")); + added = dbHelper.addUser(new User("jbO4aBF4dC", "soccer", "Miguel", "Garzon")); assertTrue(added); - added = dbHelper.addUser(new User("mgarzon", "seg2105", "Miguel", "Garzon")); + added = dbHelper.addUser(new User("jbO4aBF4dC", "seg2105", "Miguel", "Garzon")); assertTrue(!added); - added = dbHelper.addUser(new ServiceProvider("mgarzon", "seg2105", "Juan", "Guzman")); + added = dbHelper.addUser(new ServiceProvider("jbO4aBF4dC", "seg2105", "Juan", "Guzman")); assertTrue(!added); - dbHelper.deleteUser("mgarzon"); + dbHelper.deleteUser("jbO4aBF4dC"); } @Test @@ -74,21 +77,22 @@ public class DBUnitTest { boolean updated; UserType dbUser; - dbHelper.addUser(new User("mgarzon", "soccer", "Miguel", "Garzon")); - updated = dbHelper.updateUserInfo("mgarzon", "soccer", "Juan", "Guzman"); + dbHelper.addUser(new User("jbO4aBF4dC", "soccer", "Miguel", "Garzon")); + updated = dbHelper.updateUserInfo("jbO4aBF4dC", "soccer", "Juan", "Guzman"); assertTrue(updated); - dbUser = dbHelper.findUserByUsername("mgarzon"); + dbUser = dbHelper.findUserByUsername("jbO4aBF4dC"); - assertEquals("mgarzon", dbUser.getUsername()); + assertEquals("jbO4aBF4dC", dbUser.getUsername()); assertEquals("soccer", dbUser.getPassword()); assertEquals("Juan", dbUser.getFirstname()); assertEquals("Guzman", dbUser.getLastname()); - updated = dbHelper.updateUserInfo("jguzman", "seg2105", "Juan", "Guzman"); + //changed on character of username. Everything is case sensitive! + updated = dbHelper.updateUserInfo("JbO4aBF4dC", "seg2105", "Juan", "Guzman"); assertTrue(!updated); - dbHelper.deleteUser("mgarzon"); + dbHelper.deleteUser("jbO4aBF4dC"); } }