Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' into add-case
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Robertze committed Jun 22, 2015
2 parents 0c0a0f7 + cb6fa3d commit 438a72c
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ private ResultSet get(String sql) throws SQLException {
}

public List<Map<String, String>> 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()) {
Expand All @@ -80,11 +80,11 @@ public List<Map<String, String>> executeQuery(String sql) throws SQLException{
}
details.add(rowDetails);
}
return details;
}
finally{
close();
}
return details;
}
finally{
close();
}
}

private boolean isEmpty(ResultSet rs) throws SQLException {
Expand All @@ -102,4 +102,17 @@ public void close() {
logger.debug("Connection already closed");
}
}

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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public interface IPersistenceService {
boolean open();
boolean isOpen();
List<Map<String, String>> executeQuery(String sql) throws SQLException;
void executeUpdate(String sql) throws SQLException;
void close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@

public interface IVehicleRepository {
List<Vehicle> getVehicles(Defendant defendant) throws RowToModelParseException;
void insertVehicles(Vehicle vehicle, Defendant defendant) throws RowToModelParseException;
}

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,4 +45,26 @@ public List<Vehicle> 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) throws RowToModelParseException {
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){
logger.error("Error inserting vehicle into the database", e);
throw new RowToModelParseException("Error inserting values to database");
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Map<String, String>> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,24 @@ public void testGetVehicles_Empty() throws SQLException, RowToModelParseExceptio
assertTrue(vehicles == null);
verify(db).executeQuery(sql);
}

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);

String sql = "INSERT INTO vehicles SELECT from defendants '"
+vehicle.getRegistration()+"', "
+"indexID"+", '"
+vehicle.getMake()+"', '"
+vehicle.getColour()+"', '"
+vehicle.isTrailer()+"' "
+"where id="+defendant.getId()+";";

IPersistenceService db = mock(IPersistenceService.class);
IVehicleRepository vehicleRepo = new VehicleRepository(db);
vehicleRepo.insertVehicles(vehicle, defendant); // ?
verify(db).executeUpdate(sql);

}
}

0 comments on commit 438a72c

Please sign in to comment.