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