From 7c003cfafa1e9b284b7936ed5d4acdf898af0271 Mon Sep 17 00:00:00 2001 From: pawc Date: Mon, 22 Jun 2015 17:49:54 +0200 Subject: [PATCH 1/8] WIP --- .../server/datalayer/DatabasePersistence.java | 6 ++++++ .../server/datalayer/EvidenceRepository.java | 2 +- .../server/datalayer/IPersistenceService.java | 1 + .../server/datalayer/IVehicleRepository.java | 1 + .../server/datalayer/VehicleRepository.java | 20 +++++++++++++++++++ 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/com/kritsit/casetracker/server/datalayer/DatabasePersistence.java b/server/src/main/java/com/kritsit/casetracker/server/datalayer/DatabasePersistence.java index 908af74..ad80a75 100644 --- a/server/src/main/java/com/kritsit/casetracker/server/datalayer/DatabasePersistence.java +++ b/server/src/main/java/com/kritsit/casetracker/server/datalayer/DatabasePersistence.java @@ -102,4 +102,10 @@ public void close() { logger.debug("Connection already closed"); } } + + @Override + public void executeUpdate(String sql) throws SQLException { + // TODO Auto-generated method stub + + } } diff --git a/server/src/main/java/com/kritsit/casetracker/server/datalayer/EvidenceRepository.java b/server/src/main/java/com/kritsit/casetracker/server/datalayer/EvidenceRepository.java index 3ae1229..64520b5 100644 --- a/server/src/main/java/com/kritsit/casetracker/server/datalayer/EvidenceRepository.java +++ b/server/src/main/java/com/kritsit/casetracker/server/datalayer/EvidenceRepository.java @@ -1,6 +1,6 @@ package com.kritsit.casetracker.server.datalayer; -import java.io.File;; +import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Map; diff --git a/server/src/main/java/com/kritsit/casetracker/server/datalayer/IPersistenceService.java b/server/src/main/java/com/kritsit/casetracker/server/datalayer/IPersistenceService.java index a4c5bc7..947a716 100644 --- a/server/src/main/java/com/kritsit/casetracker/server/datalayer/IPersistenceService.java +++ b/server/src/main/java/com/kritsit/casetracker/server/datalayer/IPersistenceService.java @@ -8,5 +8,6 @@ public interface IPersistenceService { boolean open(); boolean isOpen(); List> executeQuery(String sql) throws SQLException; + void executeUpdate(String sql) throws SQLException; void close(); } diff --git a/server/src/main/java/com/kritsit/casetracker/server/datalayer/IVehicleRepository.java b/server/src/main/java/com/kritsit/casetracker/server/datalayer/IVehicleRepository.java index 3826ec2..5361b26 100644 --- a/server/src/main/java/com/kritsit/casetracker/server/datalayer/IVehicleRepository.java +++ b/server/src/main/java/com/kritsit/casetracker/server/datalayer/IVehicleRepository.java @@ -7,4 +7,5 @@ public interface IVehicleRepository { List getVehicles(Defendant defendant) throws RowToModelParseException; + void insertVehicles(Vehicle vehicle, Defendant defendant) throws Exception; } diff --git a/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java b/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java index 72a4081..1ecdaf4 100644 --- a/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java +++ b/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java @@ -44,4 +44,24 @@ public List getVehicles(Defendant defendant) throws RowToModelParseExce throw new RowToModelParseException("Error retrieving vehicles from database for defendant name: " + defendant.getName()); } } + + public void insertVehicles(Vehicle vehicle, Defendant defendant) { + try{ + logger.info("Inserting a vehicle for defendant {}", defendant.getName()); + + String sql = "INSERT INTO vehicles SELECT from defendants '" + +vehicle.getRegistration()+"', " + +"indexID"+", '" + +vehicle.getMake()+"', '" + +vehicle.getColour()+"', '" + +vehicle.isTrailer()+"' " + +"where id="+defendant.getId()+";"; + + db.executeUpdate(sql); + + } + catch(Exception e){ + + } + } } From b9069a3eb7d02b583f9f8b769482826fd36ca7dc Mon Sep 17 00:00:00 2001 From: pawc Date: Mon, 22 Jun 2015 17:55:02 +0200 Subject: [PATCH 2/8] WIP --- .../server/datalayer/DatabasePersistence.java | 2 +- .../server/datalayer/VehicleRepository.java | 26 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/server/src/main/java/com/kritsit/casetracker/server/datalayer/DatabasePersistence.java b/server/src/main/java/com/kritsit/casetracker/server/datalayer/DatabasePersistence.java index ad80a75..6c65635 100644 --- a/server/src/main/java/com/kritsit/casetracker/server/datalayer/DatabasePersistence.java +++ b/server/src/main/java/com/kritsit/casetracker/server/datalayer/DatabasePersistence.java @@ -105,7 +105,7 @@ public void close() { @Override public void executeUpdate(String sql) throws SQLException { - // TODO Auto-generated method stub + // yet to be implemented } } diff --git a/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java b/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java index 1ecdaf4..16f514d 100644 --- a/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java +++ b/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java @@ -46,19 +46,19 @@ public List getVehicles(Defendant defendant) throws RowToModelParseExce } public void insertVehicles(Vehicle vehicle, Defendant defendant) { - try{ - logger.info("Inserting a vehicle for defendant {}", defendant.getName()); - - String sql = "INSERT INTO vehicles SELECT from defendants '" - +vehicle.getRegistration()+"', " - +"indexID"+", '" - +vehicle.getMake()+"', '" - +vehicle.getColour()+"', '" - +vehicle.isTrailer()+"' " - +"where id="+defendant.getId()+";"; - - db.executeUpdate(sql); - + try{ + logger.info("Inserting a vehicle for defendant {}", defendant.getName()); + + String sql = "INSERT INTO vehicles SELECT from defendants '" + +vehicle.getRegistration()+"', " + +"indexID"+", '" + +vehicle.getMake()+"', '" + +vehicle.getColour()+"', '" + +vehicle.isTrailer()+"' " + +"where id="+defendant.getId()+";"; + + db.executeUpdate(sql); + } catch(Exception e){ From 7cafed9d83ce625400f859d597d09232ee8c6432 Mon Sep 17 00:00:00 2001 From: pawc Date: Mon, 22 Jun 2015 17:59:27 +0200 Subject: [PATCH 3/8] WIP --- .../server/datalayer/VehicleRepository.java | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java b/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java index 16f514d..2ce35b0 100644 --- a/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java +++ b/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java @@ -46,22 +46,22 @@ public List getVehicles(Defendant defendant) throws RowToModelParseExce } public void insertVehicles(Vehicle vehicle, Defendant defendant) { - try{ - logger.info("Inserting a vehicle for defendant {}", defendant.getName()); - - String sql = "INSERT INTO vehicles SELECT from defendants '" - +vehicle.getRegistration()+"', " - +"indexID"+", '" - +vehicle.getMake()+"', '" - +vehicle.getColour()+"', '" - +vehicle.isTrailer()+"' " - +"where id="+defendant.getId()+";"; - - db.executeUpdate(sql); - - } - catch(Exception e){ - - } + try{ + logger.info("Inserting a vehicle for defendant {}", defendant.getName()); + + String sql = "INSERT INTO vehicles SELECT from defendants '" + +vehicle.getRegistration()+"', " + +"indexID"+", '" + +vehicle.getMake()+"', '" + +vehicle.getColour()+"', '" + +vehicle.isTrailer()+"' " + +"where id="+defendant.getId()+";"; + + db.executeUpdate(sql); + + } + catch(Exception e){ + + } } } From 917d35ad53aae2205a805ac02886296870448465 Mon Sep 17 00:00:00 2001 From: pawc Date: Mon, 22 Jun 2015 19:09:57 +0200 Subject: [PATCH 4/8] WIP --- .../server/datalayer/VehicleRepository.java | 4 ++-- .../datalayer/VehicleRepositoryTest.java | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java b/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java index 2ce35b0..727c138 100644 --- a/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java +++ b/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java @@ -46,11 +46,11 @@ public List getVehicles(Defendant defendant) throws RowToModelParseExce } public void insertVehicles(Vehicle vehicle, Defendant defendant) { - try{ + try{ logger.info("Inserting a vehicle for defendant {}", defendant.getName()); String sql = "INSERT INTO vehicles SELECT from defendants '" - +vehicle.getRegistration()+"', " + +vehicle.getRegistration()+"', " +"indexID"+", '" +vehicle.getMake()+"', '" +vehicle.getColour()+"', '" diff --git a/server/src/test/java/com/kritsit/casetracker/server/datalayer/VehicleRepositoryTest.java b/server/src/test/java/com/kritsit/casetracker/server/datalayer/VehicleRepositoryTest.java index 5fe2ace..60caa86 100644 --- a/server/src/test/java/com/kritsit/casetracker/server/datalayer/VehicleRepositoryTest.java +++ b/server/src/test/java/com/kritsit/casetracker/server/datalayer/VehicleRepositoryTest.java @@ -77,4 +77,25 @@ public void testGetVehicles_Empty() throws SQLException, RowToModelParseExceptio assertTrue(vehicles == null); verify(db).executeQuery(sql); } + + public void testInsertVehicles() throws Exception{ + String id = "9802245849032"; + Defendant defendant = new Defendant(id, "Bob", "Dylan", "1 address road", "0212221233", "test@testing.co.za", false); + Vehicle vehicle = new Vehicle("ZSZ1234", "Citroen", "silver", false); + + String sql = "INSERT INTO vehicles SELECT from defendants '" + +vehicle.getRegistration()+"', " + +"indexID"+", '" + +vehicle.getMake()+"', '" + +vehicle.getColour()+"', '" + +vehicle.isTrailer()+"' " + +"where id="+defendant.getId()+";"; + + assertTrue(sql instanceof String); + IPersistenceService db = mock(IPersistenceService.class); + IVehicleRepository vehicleRepo = new VehicleRepository(db); + vehicleRepo.insertVehicles(vehicle, defendant); // ? + verify(db).executeUpdate(sql); + + } } From 20b8a1abf1f8bbeec42deccaa35491521de0c95d Mon Sep 17 00:00:00 2001 From: pawc Date: Mon, 22 Jun 2015 20:30:27 +0200 Subject: [PATCH 5/8] new catch block for insertVehicle --- .../server/datalayer/IVehicleRepository.java | 2 +- .../server/datalayer/VehicleRepository.java | 11 +++++++---- .../server/datalayer/VehicleRepositoryTest.java | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/server/src/main/java/com/kritsit/casetracker/server/datalayer/IVehicleRepository.java b/server/src/main/java/com/kritsit/casetracker/server/datalayer/IVehicleRepository.java index 5361b26..616bfe4 100644 --- a/server/src/main/java/com/kritsit/casetracker/server/datalayer/IVehicleRepository.java +++ b/server/src/main/java/com/kritsit/casetracker/server/datalayer/IVehicleRepository.java @@ -7,5 +7,5 @@ public interface IVehicleRepository { List getVehicles(Defendant defendant) throws RowToModelParseException; - void insertVehicles(Vehicle vehicle, Defendant defendant) throws Exception; + void insertVehicles(Vehicle vehicle, Defendant defendant) throws RowToModelParseException; } diff --git a/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java b/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java index 727c138..3a8ff99 100644 --- a/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java +++ b/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java @@ -3,6 +3,7 @@ import com.kritsit.casetracker.shared.domain.model.Defendant; import com.kritsit.casetracker.shared.domain.model.Vehicle; +import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -45,7 +46,7 @@ public List getVehicles(Defendant defendant) throws RowToModelParseExce } } - public void insertVehicles(Vehicle vehicle, Defendant defendant) { + public void insertVehicles(Vehicle vehicle, Defendant defendant) throws RowToModelParseException { try{ logger.info("Inserting a vehicle for defendant {}", defendant.getName()); @@ -60,8 +61,10 @@ public void insertVehicles(Vehicle vehicle, Defendant defendant) { db.executeUpdate(sql); } - catch(Exception e){ - - } + + catch(Exception e){ + logger.error("Error inserting values to database"); + throw new RowToModelParseException("Error inserting values to database"); + } } } diff --git a/server/src/test/java/com/kritsit/casetracker/server/datalayer/VehicleRepositoryTest.java b/server/src/test/java/com/kritsit/casetracker/server/datalayer/VehicleRepositoryTest.java index 60caa86..d2f9dbc 100644 --- a/server/src/test/java/com/kritsit/casetracker/server/datalayer/VehicleRepositoryTest.java +++ b/server/src/test/java/com/kritsit/casetracker/server/datalayer/VehicleRepositoryTest.java @@ -78,7 +78,7 @@ public void testGetVehicles_Empty() throws SQLException, RowToModelParseExceptio verify(db).executeQuery(sql); } - public void testInsertVehicles() throws Exception{ + public void testInsertVehicles() throws SQLException, RowToModelParseException{ String id = "9802245849032"; Defendant defendant = new Defendant(id, "Bob", "Dylan", "1 address road", "0212221233", "test@testing.co.za", false); Vehicle vehicle = new Vehicle("ZSZ1234", "Citroen", "silver", false); From 57a7d9269b4d335dc101a782968599c05d3ee26e Mon Sep 17 00:00:00 2001 From: pawc Date: Mon, 22 Jun 2015 21:11:20 +0200 Subject: [PATCH 6/8] VehicleRepository: changes to catch block and test --- .../casetracker/server/datalayer/IVehicleRepository.java | 1 + .../casetracker/server/datalayer/VehicleRepository.java | 2 +- .../casetracker/server/datalayer/VehicleRepositoryTest.java | 3 +-- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/com/kritsit/casetracker/server/datalayer/IVehicleRepository.java b/server/src/main/java/com/kritsit/casetracker/server/datalayer/IVehicleRepository.java index 616bfe4..bfe219f 100644 --- a/server/src/main/java/com/kritsit/casetracker/server/datalayer/IVehicleRepository.java +++ b/server/src/main/java/com/kritsit/casetracker/server/datalayer/IVehicleRepository.java @@ -9,3 +9,4 @@ public interface IVehicleRepository { List getVehicles(Defendant defendant) throws RowToModelParseException; void insertVehicles(Vehicle vehicle, Defendant defendant) throws RowToModelParseException; } + \ No newline at end of file diff --git a/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java b/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java index 3a8ff99..e92bcb7 100644 --- a/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java +++ b/server/src/main/java/com/kritsit/casetracker/server/datalayer/VehicleRepository.java @@ -63,7 +63,7 @@ public void insertVehicles(Vehicle vehicle, Defendant defendant) throws RowToMod } catch(Exception e){ - logger.error("Error inserting values to database"); + logger.error("Error inserting vehicle into the database", e); throw new RowToModelParseException("Error inserting values to database"); } } diff --git a/server/src/test/java/com/kritsit/casetracker/server/datalayer/VehicleRepositoryTest.java b/server/src/test/java/com/kritsit/casetracker/server/datalayer/VehicleRepositoryTest.java index d2f9dbc..4890f11 100644 --- a/server/src/test/java/com/kritsit/casetracker/server/datalayer/VehicleRepositoryTest.java +++ b/server/src/test/java/com/kritsit/casetracker/server/datalayer/VehicleRepositoryTest.java @@ -90,8 +90,7 @@ public void testInsertVehicles() throws SQLException, RowToModelParseException{ +vehicle.getColour()+"', '" +vehicle.isTrailer()+"' " +"where id="+defendant.getId()+";"; - - assertTrue(sql instanceof String); + IPersistenceService db = mock(IPersistenceService.class); IVehicleRepository vehicleRepo = new VehicleRepository(db); vehicleRepo.insertVehicles(vehicle, defendant); // ? From b4820c83febf72e568f12ffa875eac3b2241563d Mon Sep 17 00:00:00 2001 From: pawc Date: Mon, 22 Jun 2015 21:29:22 +0200 Subject: [PATCH 7/8] implementing executeUpdate in DataPersistence --- .../server/datalayer/DatabasePersistence.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/com/kritsit/casetracker/server/datalayer/DatabasePersistence.java b/server/src/main/java/com/kritsit/casetracker/server/datalayer/DatabasePersistence.java index 6c65635..6ab4411 100644 --- a/server/src/main/java/com/kritsit/casetracker/server/datalayer/DatabasePersistence.java +++ b/server/src/main/java/com/kritsit/casetracker/server/datalayer/DatabasePersistence.java @@ -103,9 +103,16 @@ public void close() { } } - @Override public void executeUpdate(String sql) throws SQLException { - // yet to be implemented - + logger.info("Inserting changes to database"); + try{ + open(); + Statement statement = connection.createStatement(); + statement.executeUpdate(sql); + } + finally{ + close(); + logger.info("Database updated"); + } } } From cb6fa3d089bf8358d7c35112b0ae53f431ddf503 Mon Sep 17 00:00:00 2001 From: Kyle Robertze Date: Mon, 22 Jun 2015 22:43:55 +0200 Subject: [PATCH 8/8] added intergration test for database select and update --- .../server/datalayer/DatabasePersistence.java | 46 +++++++++---------- .../datalayer/DatabasePersistenceIT.java | 22 +++++++-- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/server/src/main/java/com/kritsit/casetracker/server/datalayer/DatabasePersistence.java b/server/src/main/java/com/kritsit/casetracker/server/datalayer/DatabasePersistence.java index 6ab4411..e62b4b3 100644 --- a/server/src/main/java/com/kritsit/casetracker/server/datalayer/DatabasePersistence.java +++ b/server/src/main/java/com/kritsit/casetracker/server/datalayer/DatabasePersistence.java @@ -61,13 +61,13 @@ private ResultSet get(String sql) throws SQLException { } public List> executeQuery(String sql) throws SQLException{ - try { - open(); - ResultSet rs = get(sql); - if (isEmpty(rs)) { + try { + open(); + ResultSet rs = get(sql); + if (isEmpty(rs)) { logger.debug("ResultSet empty"); - return null; - } + return null; + } List details = new ArrayList<>(); rs.beforeFirst(); while (rs.next()) { @@ -80,11 +80,11 @@ public List> executeQuery(String sql) throws SQLException{ } details.add(rowDetails); } - return details; - } - finally{ - close(); - } + return details; + } + finally{ + close(); + } } private boolean isEmpty(ResultSet rs) throws SQLException { @@ -103,16 +103,16 @@ public void close() { } } - public void executeUpdate(String sql) throws SQLException { - logger.info("Inserting changes to database"); - try{ - open(); - Statement statement = connection.createStatement(); - statement.executeUpdate(sql); - } - finally{ - close(); - logger.info("Database updated"); - } - } + public void executeUpdate(String sql) throws SQLException { + logger.info("Inserting changes to database"); + try{ + open(); + Statement statement = connection.createStatement(); + statement.executeUpdate(sql); + } + finally{ + close(); + logger.info("Database updated"); + } + } } diff --git a/server/src/test/java/com/kritsit/casetracker/server/datalayer/DatabasePersistenceIT.java b/server/src/test/java/com/kritsit/casetracker/server/datalayer/DatabasePersistenceIT.java index 844606b..fc06eb2 100644 --- a/server/src/test/java/com/kritsit/casetracker/server/datalayer/DatabasePersistenceIT.java +++ b/server/src/test/java/com/kritsit/casetracker/server/datalayer/DatabasePersistenceIT.java @@ -1,11 +1,15 @@ package com.kritsit.casetracker.server.datalayer; +import com.kritsit.casetracker.server.datalayer.DatabasePersistence; +import com.kritsit.casetracker.server.datalayer.IPersistenceService; + import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; -import com.kritsit.casetracker.server.datalayer.DatabasePersistence; -import com.kritsit.casetracker.server.datalayer.IPersistenceService; +import java.sql.SQLException; +import java.util.List; +import java.util.Map; public class DatabasePersistenceIT extends TestCase { private IPersistenceService db; @@ -30,8 +34,18 @@ public void testCreation() { public void testOpen() { assertTrue(db.isOpen()); } - - public void tearDown() { + + public void testInsertQuery() throws SQLException { + String insert = "INSERT INTO staff VALUES (NULL, 'test', 'test', 'test', 'test', 'test', -1, -1, -1);"; + String select = "SELECT * FROM staff WHERE firstName='test' AND lastName='test' AND username='test' AND passwordHash=-1;"; + db.executeUpdate(insert); + List> result = db.executeQuery(select); + assertFalse(result.isEmpty()); + } + + public void tearDown() throws SQLException { + String delete = "DELETE FROM staff WHERE firstName='test' AND lastName='test' AND username='test' AND passwordHash=-1;"; + db.executeUpdate(delete); db.close(); } }