diff --git a/OlympusServices/.idea/codeStyles/Project.xml b/OlympusServices/.idea/codeStyles/Project.xml index 30aa626..d5ec3da 100644 --- a/OlympusServices/.idea/codeStyles/Project.xml +++ b/OlympusServices/.idea/codeStyles/Project.xml @@ -1,5 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + \ 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 d4d1f92..eadb140 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 @@ -256,8 +256,8 @@ public class DBHelper extends SQLiteOpenHelper { values.put(COLUMN_COMPANY, company); } - boolean licensed = serviceProvider.isLicensed(); - values.put(COLUMN_LICENSED, Boolean.toString(licensed)); + String licensed = String.valueOf(serviceProvider.isLicensed()); + values.put(COLUMN_LICENSED, licensed); } @@ -338,7 +338,7 @@ public class DBHelper extends SQLiteOpenHelper { if (address != null && !address.equals(""))values.put(COLUMN_ADDRESS, address); if (phonenumber != null && !phonenumber.equals(""))values.put(COLUMN_PHONE, phonenumber); if (companyname != null && !companyname.equals(""))values.put(COLUMN_COMPANY, companyname); - if (licensed != null)values.put(COLUMN_LICENSED, Boolean.toString(licensed)); + if (licensed != null)values.put(COLUMN_LICENSED, String.valueOf(licensed)); return writeDB.update(TABLE_LOGIN, values, COLUMN_USERNAME+" = ?", @@ -353,8 +353,32 @@ public class DBHelper extends SQLiteOpenHelper { * @return whether a user was deleted */ public boolean deleteUser(String username) { - return writeDB.delete(TABLE_LOGIN, COLUMN_USERNAME+" = ?", - new String[]{username}) > 0; + if (username == null) return false; + + Cursor cursor = readDB.rawQuery("SELECT * FROM " + TABLE_LOGIN + + " WHERE " + COLUMN_USERNAME + " = ?", + new String[]{username}); + + if (!cursor.moveToFirst()) return false; + + boolean deleted; + if (cursor.getString(4).equals("ServiceProvider")) { + deleted = writeDB.delete(TABLE_LOGIN, COLUMN_USERNAME+" = ?", + new String[]{username}) > 0; + + if (deleted) { + writeDB.delete(TABLE_SERVICEPROVIDERS, COLUMN_SERVICEPROVIDERNAME + " = ?", + new String[]{username}); + writeDB.delete(TABLE_AVAILABILITY, COLUMN_AVAILABILITYNAME + " = ?", + new String[]{username}); + } + cursor.close(); + return deleted; + } else { + cursor.close(); + return writeDB.delete(TABLE_LOGIN, COLUMN_USERNAME+" = ?", + new String[]{username}) > 0; + } } /** @@ -487,22 +511,13 @@ public class DBHelper extends SQLiteOpenHelper { boolean deleted; String nullify = null; service = service.toLowerCase().trim(); - ContentValues contentValues = new ContentValues(); - contentValues.put(COLUMN_SERVICEPROVIDERSERVICE, nullify); - writeDB.update(TABLE_SERVICEPROVIDERS, contentValues, COLUMN_SERVICEPROVIDERSERVICE+" = ?", - new String[]{service}); deleted = writeDB.delete(TABLE_SERVICES, COLUMN_SERVICE+" = ?", new String[]{service}) > 0; if (deleted) { - writeDB.delete(TABLE_SERVICEPROVIDERS, COLUMN_SERVICEPROVIDERSERVICE+" = ?", + writeDB.delete(TABLE_SERVICEPROVIDERS, COLUMN_SERVICEPROVIDERSERVICE + " = ?", new String[]{service}); - } else { - ContentValues restoreContentValues = new ContentValues(); - restoreContentValues.put(COLUMN_SERVICEPROVIDERSERVICE, service); - writeDB.update(TABLE_SERVICEPROVIDERS, restoreContentValues, COLUMN_SERVICEPROVIDERSERVICE+" = ?", - null); } return deleted; } @@ -543,7 +558,7 @@ public class DBHelper extends SQLiteOpenHelper { new String[]{serviceProviderUsername, serviceName}, null, null, null, "1"); - //If cursor has 1+ elements in it, username already exists in table + //If cursor has 1+ elements in it, username/service combination already exists in table if (cursor != null && cursor.getCount() > 0){ cursor.close(); return false; @@ -629,6 +644,7 @@ public class DBHelper extends SQLiteOpenHelper { addAvailabilityToContentValues(contentValues, COLUMN_FRISTART, COLUMN_FRIEND, availabilities[4]); addAvailabilityToContentValues(contentValues, COLUMN_SATSTART, COLUMN_SATEND, availabilities[5]); addAvailabilityToContentValues(contentValues, COLUMN_SUNSTART, COLUMN_SUNEND, availabilities[6]); + if (!cursor.moveToFirst()){ contentValues.put(COLUMN_AVAILABILITYNAME, serviceProvider.getUsername()); writeDB.insert(TABLE_AVAILABILITY, null, contentValues); @@ -648,7 +664,7 @@ public class DBHelper extends SQLiteOpenHelper { } else { int startTime = startAndEndTimes[0]*60+startAndEndTimes[1]; int endTime = startAndEndTimes[2]*60+startAndEndTimes[3]; - if (endTime - startTime <=0 || startTime > 1439 || startTime <= 0 + if (endTime - startTime <=0 || startTime > 1439 || startTime < 0 || endTime > 1439 || endTime <= 0) { contentValues.put(startColumn, 0); contentValues.put(endColumn, 0); @@ -674,6 +690,8 @@ public class DBHelper extends SQLiteOpenHelper { serviceProvider.setAvailabilities(i, start/60, start%60, end/60, end%60); } + } else { + return new int[7][4]; } return serviceProvider.getAvailabilities(); } diff --git a/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBIntegrationTest.java b/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBIntegrationTest.java index 7a38f8a..0d6fbf5 100644 --- a/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBIntegrationTest.java +++ b/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBIntegrationTest.java @@ -23,6 +23,8 @@ public class DBIntegrationTest { //testing user login table @Test public void testAdminExists(){ + //Admin is automatically created when DB starts up, if admin does not already exist. + //so findUserByUsername("admin") should always return an Admin object UserType dbUser = dbHelper.findUserByUsername("admin"); assertEquals("Admin", dbUser.getClass().getSimpleName()); assertEquals("admin", dbUser.getUsername()); @@ -36,29 +38,32 @@ public class DBIntegrationTest { UserType originalUser, dbUser; boolean deleted, addedOne, addedTwo; - + //add a HomeOwner to database originalUser = new HomeOwner("mgarzon", "soccer", "Miguel", "Garzon"); addedOne = dbHelper.addUser(originalUser); - dbUser = dbHelper.findUserByUsername("mgarzon"); + //test retrieving HomeOwner, and confirm that user info is the same as that in object passed + dbUser = dbHelper.findUserByUsername("mgarzon"); assertEquals("HomeOwner", dbUser.getClass().getSimpleName()); assertEquals("mgarzon", dbUser.getUsername()); assertEquals("soccer", dbUser.getPassword()); assertEquals("Miguel", dbUser.getFirstname()); assertEquals("Garzon", dbUser.getLastname()); - + //add a ServiceProvider to database originalUser = new ServiceProvider("jbO4aBF4dC", "seg2105", "Juan", "Guzman", "testaddress", "8888888888", "companydotcom", true); addedTwo = dbHelper.addUser(originalUser); - dbUser = dbHelper.findUserByUsername("jbO4aBF4dC"); + //test retrieving ServiceProvider, and confirm that user info is the same as that in object passed + dbUser = dbHelper.findUserByUsername("jbO4aBF4dC"); assertEquals("ServiceProvider", dbUser.getClass().getSimpleName()); assertEquals("jbO4aBF4dC", dbUser.getUsername()); assertEquals("seg2105", dbUser.getPassword()); assertEquals("Juan", dbUser.getFirstname()); assertEquals("Guzman", dbUser.getLastname()); + //if user exsists in database, delete and test that user has been deleted if (addedOne) { deleted = dbHelper.deleteUser("mgarzon"); assertTrue(deleted); @@ -68,16 +73,72 @@ public class DBIntegrationTest { deleted = dbHelper.deleteUser("jbO4aBF4dC"); assertTrue(deleted); } - } + @Test + public void testAddAndGetServiceProvider(){ + //ServiceProviders have extra fields that can be added to the DB + ServiceProvider serviceProvider = new ServiceProvider("7MuF1c59XP", null, null, null, + "testaddress", "8888888888", "companydotcom", true); + dbHelper.addUser(serviceProvider); + + //retrieve ServiceProvider and test the newly added fields + UserType userType = dbHelper.findUserByUsername("7MuF1c59XP"); + //UserType returned should be an instance of ServiceProvider. + //Also serves as check before casting + assertTrue(userType instanceof ServiceProvider); + + ServiceProvider dbServiceProvider = (ServiceProvider) userType; + assertEquals(serviceProvider.getAddress(), dbServiceProvider.getAddress()); + assertEquals(serviceProvider.getPhonenumber(), dbServiceProvider.getPhonenumber()); + assertEquals(serviceProvider.getCompanyname(), dbServiceProvider.getCompanyname()); + assertEquals(serviceProvider.isLicensed(), dbServiceProvider.isLicensed()); + + dbHelper.deleteUser("7MuF1c59XP"); + } + + @Test + public void testDeleteServiceProvider(){ + //make sure all the rows related to ServiceProvider in all tables are deleted + ServiceProvider serviceProvider = new ServiceProvider("jbO4aBF4dC", null, null, null, + "testaddress", "8888888888", "companydotcom", true); + dbHelper.addUser(serviceProvider); + + Service service1 = new Service("Hitman", 12358); + Service service2 = new Service("Exterminating flatworms", 392.457); + dbHelper.addService(service1); + dbHelper.addService(service2); + + dbHelper.addServiceProvidedByUser(serviceProvider, service1); + dbHelper.addServiceProvidedByUser(serviceProvider, service2); + + serviceProvider.setAvailabilities(0, 4, 18, 19, 30); + serviceProvider.setAvailabilities(1, 5, 20, 21, 11); + serviceProvider.setAvailabilities(3, 7, 12, 15, 14); + serviceProvider.setAvailabilities(4, 0, 0, 23, 29); + dbHelper.updateAvailability(serviceProvider); + + dbHelper.deleteUser("jbO4aBF4dC"); + + List providersList = dbHelper.getAllProvidersByService("hitman"); + assertEquals(0, providersList.size()); + providersList = dbHelper.getAllProvidersByService("hitman"); + assertEquals(0, providersList.size()); + + int[][] availabilities = dbHelper.getAvailabilities(serviceProvider); + for (int i = 0; i<7; i++) { + for (int j = 0; j < 4; j++) { + assertEquals(0, availabilities[i][j]); + } + } + } @Test public void testAddDuplicateUsers(){ boolean added; - added = dbHelper.addUser(new HomeOwner("jbO4aBF4dC", "soccer", "Miguel", "Garzon")); assertTrue(added); + //should not be able to add user of same username, regardless of user type added = dbHelper.addUser(new HomeOwner("jbO4aBF4dC", "seg2105", "Miguel", "Garzon")); assertTrue(!added); added = dbHelper.addUser(new ServiceProvider("jbO4aBF4dC", "seg2105", "Juan", "Guzman", @@ -173,6 +234,8 @@ public class DBIntegrationTest { assertTrue(added); added = dbHelper.addService(new Service("Exterminating flatworms", 25.00)); assertTrue(!added); + added = dbHelper.addService(new Service("extermiNating fLatworms", 25.00)); + assertTrue(!added); dbHelper.deleteService("Exterminating flatworms"); } @@ -227,16 +290,19 @@ public class DBIntegrationTest { @Test public void testGetAllServicesProvidedByUserAndDeleteService(){ - dbHelper.addUser(new ServiceProvider("jbO4aBF4dC", null, null, null, - "testaddress", "8888888888", "companydotcom", true)); + ServiceProvider serviceProvider = new ServiceProvider("jbO4aBF4dC", null, null, null, + "testaddress", "8888888888", "companydotcom", true); + dbHelper.addUser(serviceProvider); - dbHelper.addService(new Service("Hitman", 12358)); - dbHelper.addService(new Service("Exterminating flatworms", 392.457)); + Service service1 = new Service("Hitman", 12358); + Service service2 = new Service("Exterminating flatworms", 392.457); + dbHelper.addService(service1); + dbHelper.addService(service2); - dbHelper.addServiceProvidedByUser("jbO4aBF4dC", "hitman"); - dbHelper.addServiceProvidedByUser("jbO4aBF4dC", "exterminating flatworms"); + dbHelper.addServiceProvidedByUser(serviceProvider, service1); + dbHelper.addServiceProvidedByUser(serviceProvider, service2); - List servicesProvidedByUser = dbHelper.getAllServicesProvidedByUser("jbO4aBF4dC"); + List servicesProvidedByUser = dbHelper.getAllServicesProvidedByUser(serviceProvider); assertEquals(2, servicesProvidedByUser.size()); assertEquals("hitman", servicesProvidedByUser.get(0)[0]); assertEquals(12358, Double.parseDouble(servicesProvidedByUser.get(0)[1]), 0.00001); @@ -276,18 +342,53 @@ public class DBIntegrationTest { dbHelper.deleteUser("7MuF1c59XP"); } + + @Test + public void testDeleteServiceProvidedByUser(){ + ServiceProvider serviceProvider = new ServiceProvider("jbO4aBF4dC", null, null, null, + "testaddress", "8888888888", "companydotcom", true); + dbHelper.addUser(serviceProvider); + + Service service1 = new Service("Hitman", 12358); + Service service2 = new Service("Exterminating flatworms", 392.457); + dbHelper.addService(service1); + dbHelper.addService(service2); + + dbHelper.addServiceProvidedByUser(serviceProvider, service1); + dbHelper.addServiceProvidedByUser(serviceProvider, service2); + + List servicesProvidedByUser = dbHelper.getAllServicesProvidedByUser(serviceProvider); + assertEquals(2, servicesProvidedByUser.size()); + + dbHelper.deleteServiceProvidedByUser("jbO4aBF4dC","hitman"); + servicesProvidedByUser = dbHelper.getAllServicesProvidedByUser("jbO4aBF4dC"); + assertEquals(1, servicesProvidedByUser.size()); + + dbHelper.deleteServiceProvidedByUser("jbO4aBF4dC", "exterminating flatworms"); + servicesProvidedByUser = dbHelper.getAllServicesProvidedByUser("jbO4aBF4dC"); + assertEquals(0, servicesProvidedByUser.size()); + + dbHelper.deleteUser("jbO4aBF4dC"); + } + + + @Test public void testUpdateAndGetAvailability(){ ServiceProvider serviceProvider = new ServiceProvider("jbO4aBF4dC", null, null, null, "testaddress", "8888888888", "companydotcom", true); serviceProvider.setAvailabilities(0, 4, 18, 19, 30); - serviceProvider.setAvailabilities(3, 8, 12, 15, 14); + serviceProvider.setAvailabilities(1, 5, 20, 21, 11); + serviceProvider.setAvailabilities(3, 7, 12, 15, 14); + serviceProvider.setAvailabilities(4, 0, 0, 23, 29); //TODO:Perhaps implement a deep clone function for UserType? ServiceProvider serviceProvider2 = new ServiceProvider("jbO4aBF4dC", null, null, null, "testaddress", "8888888888", "companydotcom", true); serviceProvider2.setAvailabilities(0, 4, 18, 19, 30); - serviceProvider2.setAvailabilities(3, 8, 12, 15, 14); + serviceProvider2.setAvailabilities(1, 5, 20, 21, 11); + serviceProvider2.setAvailabilities(3, 7, 12, 15, 14); + serviceProvider2.setAvailabilities(4, 0, 0, 23, 29); dbHelper.addUser(serviceProvider2); @@ -305,7 +406,32 @@ public class DBIntegrationTest { assertEquals(availabilities[i][j], dbAvailabilities[i][j]); } } + dbHelper.deleteUser("jbO4aBF4dC"); } + + @Test + public void testInvalidAvailability(){ + ServiceProvider serviceProvider = new ServiceProvider("jbO4aBF4dC", null, null, null, + "testaddress", "8888888888", "companydotcom", true); + serviceProvider.setAvailabilities(2, 8, 14, 8, 14); + serviceProvider.setAvailabilities(3, 15, 12, 8, 14); + + + dbHelper.addUser(serviceProvider); + + boolean updated = dbHelper.updateAvailability(serviceProvider); + assertTrue(updated); + int[][] dbAvailabilities = dbHelper.getAvailabilities(serviceProvider); + int[][] availabilities = serviceProvider.getAvailabilities(); + + for (int i = 0; i<7; i++) { + for (int j = 0; j < 4; j++) { + assertEquals(0, availabilities[i][j]); + assertEquals(0, dbAvailabilities[i][j]); + } + } + dbHelper.deleteUser("jbO4aBF4dC"); + } }