Skip to content
Snippets Groups Projects
Commit 55debe73 authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Fixed problem with uninitialized db

parent 2adb200f
No related branches found
No related tags found
No related merge requests found
...@@ -5,11 +5,13 @@ import java.sql.Date; ...@@ -5,11 +5,13 @@ import java.sql.Date;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time; import java.sql.Time;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalTime; import java.time.LocalTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.Collection; import java.util.Collection;
import java.util.Scanner;
import tdt4140.gr1800.app.core.GeoLocation; import tdt4140.gr1800.app.core.GeoLocation;
import tdt4140.gr1800.app.core.GeoLocations; import tdt4140.gr1800.app.core.GeoLocations;
...@@ -19,34 +21,61 @@ public class DbAccessImpl extends AbstractDbAccessImpl { ...@@ -19,34 +21,61 @@ public class DbAccessImpl extends AbstractDbAccessImpl {
private final DbAccessHelper helper; private final DbAccessHelper helper;
private DbAccessImpl(DbAccessHelper helper) { private DbAccessImpl(final DbAccessHelper helper) {
this.helper = helper; this.helper = helper;
} }
public DbAccessImpl(Connection dbConnection) { public DbAccessImpl(final Connection dbConnection) {
this(new DbAccessHelper(dbConnection)); this(new DbAccessHelper(dbConnection));
} }
public DbAccessImpl(String connectionUri, String user, String pass) throws SQLException { public DbAccessImpl(final String connectionUri, final String user, final String pass) throws SQLException {
this(DriverManager.getConnection(connectionUri, user, pass)); this(DriverManager.getConnection(connectionUri, user, pass));
} }
public DbAccessImpl(String connectionUri) throws SQLException { public DbAccessImpl(final String connectionUri) throws SQLException {
this(connectionUri, "SA", ""); this(connectionUri, "SA", "");
} }
// //
public void executeStatements(final String path, final boolean cleanUp) throws SQLException {
final Statement dbStatement = helper.getDbConnection().createStatement();
final StringBuilder buffer = new StringBuilder();
try (Scanner scanner = new Scanner(getClass().getResourceAsStream(path))) {
while (scanner.hasNextLine()) {
final String line = scanner.nextLine();
final int pos = line.indexOf(";");
if (pos >= 0) {
buffer.append(line.substring(0, pos + 1));
final String sql = buffer.toString();
buffer.setLength(0);
if (pos < line.length()) {
buffer.append(line.substring(pos + 1));
}
if (cleanUp || (! sql.startsWith("DROP"))) {
dbStatement.execute(sql);
}
} else {
buffer.append(line);
buffer.append("\n");
}
}
}
}
//
@Override @Override
public synchronized Person createPerson(String name, String email) { public synchronized Person createPerson(final String name, final String email) {
Person person = super.createPerson(name, email); final Person person = super.createPerson(name, email);
int id = helper.executeDbInsertGettingIdentity(String.format("INSERT INTO person (name, email) VALUES ('%s', '%s')", name, email)); final int id = helper.executeDbInsertGettingIdentity(String.format("INSERT INTO person (name, email) VALUES ('%s', '%s')", name, email));
personIds.set(person, id); personIds.set(person, id);
return person; return person;
} }
protected Person createPerson(int id, String name, String email) { protected Person createPerson(final int id, final String name, final String email) {
Person person = new Person(); final Person person = new Person();
person.setName(name); person.setName(name);
person.setEmail(email); person.setEmail(email);
personIds.remove(id); personIds.remove(id);
...@@ -55,17 +84,17 @@ public class DbAccessImpl extends AbstractDbAccessImpl { ...@@ -55,17 +84,17 @@ public class DbAccessImpl extends AbstractDbAccessImpl {
} }
@Override @Override
public Collection<Person> getAllPersons(boolean refresh) { public Collection<Person> getAllPersons(final boolean refresh) {
if (refresh) { if (refresh) {
personIds.clear(); personIds.clear();
ResultSet result = helper.executeQuery("SELECT id, name, email FROM person"); final ResultSet result = helper.executeQuery("SELECT id, name, email FROM person");
try { try {
while (result.next()) { while (result.next()) {
int id = result.getInt(1); final int id = result.getInt(1);
String name = result.getString(2), email = result.getString(3); final String name = result.getString(2), email = result.getString(3);
createPerson(id, name, email); createPerson(id, name, email);
} }
} catch (SQLException e) { } catch (final SQLException e) {
helper.throwException(e); helper.throwException(e);
} }
} }
...@@ -73,19 +102,19 @@ public class DbAccessImpl extends AbstractDbAccessImpl { ...@@ -73,19 +102,19 @@ public class DbAccessImpl extends AbstractDbAccessImpl {
} }
@Override @Override
public Person getPerson(int id, boolean refresh) { public Person getPerson(final int id, final boolean refresh) {
Person person = null; Person person = null;
if (! refresh) { if (! refresh) {
person = super.getPerson(id, false); person = super.getPerson(id, false);
} }
if (person == null || refresh) { if (person == null || refresh) {
ResultSet result = helper.executeQuery("SELECT id, name, email FROM person WHERE id = ?", id); final ResultSet result = helper.executeQuery("SELECT id, name, email FROM person WHERE id = ?", id);
try { try {
if (result.next()) { if (result.next()) {
String name = result.getString(2), email = result.getString(3); final String name = result.getString(2), email = result.getString(3);
person = createPerson(id, name, email); person = createPerson(id, name, email);
} }
} catch (SQLException e) { } catch (final SQLException e) {
helper.throwException(e); helper.throwException(e);
} }
} }
...@@ -93,20 +122,20 @@ public class DbAccessImpl extends AbstractDbAccessImpl { ...@@ -93,20 +122,20 @@ public class DbAccessImpl extends AbstractDbAccessImpl {
} }
@Override @Override
public Person getPersonByName(String name, boolean refresh) { public Person getPersonByName(final String name, final boolean refresh) {
Person person = null; Person person = null;
if (! refresh) { if (! refresh) {
person = super.getPersonByName(name, false); person = super.getPersonByName(name, false);
} }
if (person == null || refresh) { if (person == null || refresh) {
ResultSet result = helper.executeQuery("SELECT id, name, email FROM person WHERE name = ?", name); final ResultSet result = helper.executeQuery("SELECT id, name, email FROM person WHERE name = ?", name);
try { try {
if (result.next()) { if (result.next()) {
int id = result.getInt(1); final int id = result.getInt(1);
String dbName = result.getString(2), email = result.getString(3); final String dbName = result.getString(2), email = result.getString(3);
person = createPerson(id, dbName, email); person = createPerson(id, dbName, email);
} }
} catch (SQLException e) { } catch (final SQLException e) {
helper.throwException(e); helper.throwException(e);
} }
} }
...@@ -114,20 +143,20 @@ public class DbAccessImpl extends AbstractDbAccessImpl { ...@@ -114,20 +143,20 @@ public class DbAccessImpl extends AbstractDbAccessImpl {
} }
@Override @Override
public Person getPersonByEmail(String email, boolean refresh) { public Person getPersonByEmail(final String email, final boolean refresh) {
Person person = null; Person person = null;
if (! refresh) { if (! refresh) {
person = super.getPersonByEmail(email, false); person = super.getPersonByEmail(email, false);
} }
if (person == null || refresh) { if (person == null || refresh) {
ResultSet result = helper.executeQuery("SELECT id, name, email FROM person WHERE email = ?", email); final ResultSet result = helper.executeQuery("SELECT id, name, email FROM person WHERE email = ?", email);
try { try {
if (result.next()) { if (result.next()) {
int id = result.getInt(1); final int id = result.getInt(1);
String name = result.getString(2), dbEmail = result.getString(3); final String name = result.getString(2), dbEmail = result.getString(3);
person = createPerson(id, name, dbEmail); person = createPerson(id, name, dbEmail);
} }
} catch (SQLException e) { } catch (final SQLException e) {
helper.throwException(e); helper.throwException(e);
} }
} }
...@@ -135,13 +164,13 @@ public class DbAccessImpl extends AbstractDbAccessImpl { ...@@ -135,13 +164,13 @@ public class DbAccessImpl extends AbstractDbAccessImpl {
} }
@Override @Override
public synchronized void updatePersonData(Person person) { public synchronized void updatePersonData(final Person person) {
helper.executeDbStatement("UPDATE person SET name = ?, email = ? WHERE id = ?", person.getName(), person.getEmail(), getId(person)); helper.executeDbStatement("UPDATE person SET name = ?, email = ? WHERE id = ?", person.getName(), person.getEmail(), getId(person));
} }
@Override @Override
public synchronized void deletePerson(Person person) { public synchronized void deletePerson(final Person person) {
int id = getId(person); final int id = getId(person);
super.deletePerson(person); super.deletePerson(person);
helper.executeDbStatement("DELETE FROM person WHERE id = ?", id); helper.executeDbStatement("DELETE FROM person WHERE id = ?", id);
// not needed with ON DELETE CASCADE set on foreign keys // not needed with ON DELETE CASCADE set on foreign keys
...@@ -152,9 +181,9 @@ public class DbAccessImpl extends AbstractDbAccessImpl { ...@@ -152,9 +181,9 @@ public class DbAccessImpl extends AbstractDbAccessImpl {
// //
@Override @Override
public GeoLocations createGeoLocations(Person owner) { public GeoLocations createGeoLocations(final Person owner) {
GeoLocations geoLocations = super.createGeoLocations(owner); final GeoLocations geoLocations = super.createGeoLocations(owner);
int id = helper.executeDbInsertGettingIdentity(String.format("INSERT INTO geoLocations (ownerId) VALUES ('%s')", getId(owner))); final int id = helper.executeDbInsertGettingIdentity(String.format("INSERT INTO geoLocations (ownerId) VALUES ('%s')", getId(owner)));
geoLocationsIds.set(geoLocations, id); geoLocationsIds.set(geoLocations, id);
return geoLocations; return geoLocations;
} }
...@@ -165,51 +194,51 @@ public class DbAccessImpl extends AbstractDbAccessImpl { ...@@ -165,51 +194,51 @@ public class DbAccessImpl extends AbstractDbAccessImpl {
} }
@Override @Override
public Collection<GeoLocations> getGeoLocations(Person owner, boolean refresh) { public Collection<GeoLocations> getGeoLocations(final Person owner, final boolean refresh) {
Collection<GeoLocations> existingGeoLocations = super.getGeoLocations(owner, false); final Collection<GeoLocations> existingGeoLocations = super.getGeoLocations(owner, false);
if (refresh || existingGeoLocations.isEmpty()) { if (refresh || existingGeoLocations.isEmpty()) {
owner.removeGeolocations((String[]) null); owner.removeGeolocations((String[]) null);
geoLocationsIds.removeAll(existingGeoLocations); geoLocationsIds.removeAll(existingGeoLocations);
existingGeoLocations.clear(); existingGeoLocations.clear();
int ownerId = getId(owner); final int ownerId = getId(owner);
ResultSet result = helper.executeQuery("SELECT id, path, name, description, date, time, zone FROM geoLocations WHERE ownerId = ?", ownerId); final ResultSet result = helper.executeQuery("SELECT id, path, name, description, date, time, zone FROM geoLocations WHERE ownerId = ?", ownerId);
try { try {
while (result.next()) { while (result.next()) {
int id = result.getInt(1); final int id = result.getInt(1);
boolean path = result.getBoolean(2); final boolean path = result.getBoolean(2);
String name = result.getString(3), description = result.getString(4); final String name = result.getString(3), description = result.getString(4);
GeoLocations geoLocations = new GeoLocations(owner); final GeoLocations geoLocations = new GeoLocations(owner);
owner.addGeolocations(geoLocations); owner.addGeolocations(geoLocations);
geoLocations.setPath(path); geoLocations.setPath(path);
geoLocations.setName(name); geoLocations.setName(name);
geoLocations.setDescription(description); geoLocations.setDescription(description);
Date date = result.getDate(5); final Date date = result.getDate(5);
Time time = result.getTime(6); final Time time = result.getTime(6);
String zone = result.getString(7); final String zone = result.getString(7);
geoLocations.setDate(date != null ? date.toLocalDate() : null); geoLocations.setDate(date != null ? date.toLocalDate() : null);
geoLocations.setTime(time != null ? time.toLocalTime() : null); geoLocations.setTime(time != null ? time.toLocalTime() : null);
geoLocations.setZone(zone != null ? ZoneId.of(zone) : null); geoLocations.setZone(zone != null ? ZoneId.of(zone) : null);
existingGeoLocations.add(geoLocations); existingGeoLocations.add(geoLocations);
geoLocationsIds.set(geoLocations, id); geoLocationsIds.set(geoLocations, id);
ResultSet tagResults = helper.executeQuery(String.format("SELECT tag FROM tag WHERE ownerId = ? AND ownerType = '%s'", TagOwnerType.GLS), id); final ResultSet tagResults = helper.executeQuery(String.format("SELECT tag FROM tag WHERE ownerId = ? AND ownerType = '%s'", TagOwnerType.GLS), id);
while (tagResults.next()) { while (tagResults.next()) {
geoLocations.addTags(tagResults.getString(1)); geoLocations.addTags(tagResults.getString(1));
} }
} }
} catch (SQLException e) { } catch (final SQLException e) {
helper.throwException(e); helper.throwException(e);
} }
ResultSet tagResult = helper.executeQuery(String.format("SELECT geoLocations.id, tag.tag FROM geoLocations, tag WHERE geoLocations.ownerId = ? AND tag.ownerId = geoLocations.id AND ownerType = '%s'", TagOwnerType.GLS), ownerId); final ResultSet tagResult = helper.executeQuery(String.format("SELECT geoLocations.id, tag.tag FROM geoLocations, tag WHERE geoLocations.ownerId = ? AND tag.ownerId = geoLocations.id AND ownerType = '%s'", TagOwnerType.GLS), ownerId);
try { try {
while (tagResult.next()) { while (tagResult.next()) {
int geoLocationsId = tagResult.getInt(1); final int geoLocationsId = tagResult.getInt(1);
String tag = tagResult.getString(2); final String tag = tagResult.getString(2);
GeoLocations geoLocations = geoLocationsIds.get(geoLocationsId); final GeoLocations geoLocations = geoLocationsIds.get(geoLocationsId);
if (geoLocations != null) { if (geoLocations != null) {
geoLocations.addTags(tag); geoLocations.addTags(tag);
} }
} }
} catch (SQLException e) { } catch (final SQLException e) {
helper.throwException(e); helper.throwException(e);
} }
} }
...@@ -217,17 +246,17 @@ public class DbAccessImpl extends AbstractDbAccessImpl { ...@@ -217,17 +246,17 @@ public class DbAccessImpl extends AbstractDbAccessImpl {
} }
@Override @Override
public void updateGeoLocationsData(GeoLocations geoLocations) { public void updateGeoLocationsData(final GeoLocations geoLocations) {
boolean path = geoLocations.isPath(); final boolean path = geoLocations.isPath();
String name = geoLocations.getName(), desc = geoLocations.getDescription(); final String name = geoLocations.getName(), desc = geoLocations.getDescription();
LocalDate date = geoLocations.getDate(); final LocalDate date = geoLocations.getDate();
LocalTime time = geoLocations.getTime(); final LocalTime time = geoLocations.getTime();
String zone = (geoLocations.getZone() != null ? geoLocations.getZone().getId() : null); final String zone = (geoLocations.getZone() != null ? geoLocations.getZone().getId() : null);
int ownerId = getId(geoLocations); final int ownerId = getId(geoLocations);
helper.executeDbStatement("UPDATE geoLocations SET path = ?, name = ?, description = ?, date = ?, time = ?, zone = ? WHERE id = ?", path, name, desc, Date.valueOf(date), Time.valueOf(time), zone, ownerId); helper.executeDbStatement("UPDATE geoLocations SET path = ?, name = ?, description = ?, date = ?, time = ?, zone = ? WHERE id = ?", path, name, desc, Date.valueOf(date), Time.valueOf(time), zone, ownerId);
deleteTags(ownerId, TagOwnerType.GLS); deleteTags(ownerId, TagOwnerType.GLS);
String insertStatement = "INSERT INTO tag (ownerId, ownerType, tag) VALUES "; String insertStatement = "INSERT INTO tag (ownerId, ownerType, tag) VALUES ";
String[] tags = geoLocations.getTags(); final String[] tags = geoLocations.getTags();
for (int i = 0; i < tags.length; i++) { for (int i = 0; i < tags.length; i++) {
if (i > 0) { if (i > 0) {
insertStatement += ", "; insertStatement += ", ";
...@@ -237,13 +266,13 @@ public class DbAccessImpl extends AbstractDbAccessImpl { ...@@ -237,13 +266,13 @@ public class DbAccessImpl extends AbstractDbAccessImpl {
helper.executeDbStatement(insertStatement); helper.executeDbStatement(insertStatement);
} }
protected void deleteTags(int ownerId, TagOwnerType ownerType) { protected void deleteTags(final int ownerId, final TagOwnerType ownerType) {
helper.executeDbStatement(String.format("DELETE FROM tag WHERE ownerId = ? AND ownerType = '%s'", ownerType), ownerId); helper.executeDbStatement(String.format("DELETE FROM tag WHERE ownerId = ? AND ownerType = '%s'", ownerType), ownerId);
} }
@Override @Override
public void deleteGeoLocations(GeoLocations geoLocations) { public void deleteGeoLocations(final GeoLocations geoLocations) {
int ownerId = getId(geoLocations); final int ownerId = getId(geoLocations);
super.deleteGeoLocations(geoLocations); super.deleteGeoLocations(geoLocations);
helper.executeDbStatement("DELETE FROM geoLocations WHERE id = ?", ownerId); helper.executeDbStatement("DELETE FROM geoLocations WHERE id = ?", ownerId);
deleteTags(ownerId, TagOwnerType.GLS); deleteTags(ownerId, TagOwnerType.GLS);
...@@ -252,7 +281,7 @@ public class DbAccessImpl extends AbstractDbAccessImpl { ...@@ -252,7 +281,7 @@ public class DbAccessImpl extends AbstractDbAccessImpl {
// //
@Override @Override
public void updateGeoLocationData(GeoLocations geoLocations, GeoLocation geoLocation) { public void updateGeoLocationData(final GeoLocations geoLocations, final GeoLocation geoLocation) {
throw new UnsupportedOperationException("NYI"); throw new UnsupportedOperationException("NYI");
} }
} }
package tdt4140.gr1800.app.json;
import java.util.function.Consumer;
import java.util.function.Function;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.ObjectNode;
public abstract class IdDeserializer<T> extends StdDeserializer<T> {
public IdDeserializer(final Class<T> clazz) {
super(clazz);
}
void deserialize(final ObjectNode objectNode, final String fieldName, final Consumer<String> setter) throws JsonProcessingException {
if (objectNode.has(fieldName)) {
final String s = objectNode.get(fieldName).asText();
setter.accept(s);
}
}
<U> void deserialize(final ObjectNode objectNode, final String fieldName, final Function<String, U> parser, final Consumer<U> setter) throws JsonProcessingException {
if (objectNode.has(fieldName)) {
final String s = objectNode.get(fieldName).asText();
final U value = parser.apply(s);
setter.accept(value);
}
}
}
...@@ -6,14 +6,13 @@ import com.fasterxml.jackson.core.JsonParser; ...@@ -6,14 +6,13 @@ import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import tdt4140.gr1800.app.core.GeoLocations; import tdt4140.gr1800.app.core.GeoLocations;
import tdt4140.gr1800.app.core.Person; import tdt4140.gr1800.app.core.Person;
public class PersonDeserializer extends StdDeserializer<Person> { public class PersonDeserializer extends IdDeserializer<Person> {
public PersonDeserializer() { public PersonDeserializer() {
super(Person.class); super(Person.class);
...@@ -31,10 +30,8 @@ public class PersonDeserializer extends StdDeserializer<Person> { ...@@ -31,10 +30,8 @@ public class PersonDeserializer extends StdDeserializer<Person> {
if (jsonNode instanceof ObjectNode) { if (jsonNode instanceof ObjectNode) {
final ObjectNode objectNode = (ObjectNode) jsonNode; final ObjectNode objectNode = (ObjectNode) jsonNode;
final Person person = new Person(); final Person person = new Person();
if (objectNode.has(GeoLocationsSerializer.NAME_FIELD_NAME)) { deserialize(objectNode, PersonSerializer.NAME_FIELD_NAME, person::setName);
final String name = objectNode.get(GeoLocationsSerializer.NAME_FIELD_NAME).asText(); deserialize(objectNode, PersonSerializer.EMAIL_FIELD_NAME, person::setEmail);
person.setName(name);
}
if (objectNode.has(GeoLocationsSerializer.LOCATIONS_FIELD_NAME)) { if (objectNode.has(GeoLocationsSerializer.LOCATIONS_FIELD_NAME)) {
final JsonNode locationsNode = objectNode.get(GeoLocationsSerializer.LOCATIONS_FIELD_NAME); final JsonNode locationsNode = objectNode.get(GeoLocationsSerializer.LOCATIONS_FIELD_NAME);
if (locationsNode instanceof ArrayNode) { if (locationsNode instanceof ArrayNode) {
......
...@@ -3,11 +3,9 @@ package tdt4140.gr1800.app.db; ...@@ -3,11 +3,9 @@ package tdt4140.gr1800.app.db;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalTime; import java.time.LocalTime;
import java.util.Collection; import java.util.Collection;
import java.util.Scanner;
import org.junit.After; import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
...@@ -27,8 +25,9 @@ public class HsqldbAccessTest { ...@@ -27,8 +25,9 @@ public class HsqldbAccessTest {
public void setUp() throws Exception { public void setUp() throws Exception {
Class.forName("org.hsqldb.jdbc.JDBCDriver"); Class.forName("org.hsqldb.jdbc.JDBCDriver");
dbCon = DriverManager.getConnection("jdbc:hsqldb:mem:" + HsqldbAccessTest.class.getName() + testNum, "SA", ""); dbCon = DriverManager.getConnection("jdbc:hsqldb:mem:" + HsqldbAccessTest.class.getName() + testNum, "SA", "");
executeStatements("schema.sql"); final DbAccessImpl dbAccess = new DbAccessImpl(dbCon);
dbAccess = new DbAccessImpl(dbCon); dbAccess.executeStatements("schema.sql", false);
this.dbAccess = dbAccess;
testNum++; testNum++;
} }
...@@ -37,37 +36,12 @@ public class HsqldbAccessTest { ...@@ -37,37 +36,12 @@ public class HsqldbAccessTest {
if (dbCon != null) { if (dbCon != null) {
try { try {
dbCon.close(); dbCon.close();
} catch (SQLException e) { } catch (final SQLException e) {
} }
} }
} }
protected void executeStatements(String path) throws SQLException { protected void checkPersonData(final Person hal, final Person dbHal) {
Statement dbStatement = dbCon.createStatement();
StringBuilder buffer = new StringBuilder();
try (Scanner scanner = new Scanner(HsqldbAccessTest.class.getResourceAsStream(path))) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
int pos = line.indexOf(";");
if (pos >= 0) {
buffer.append(line.substring(0, pos + 1));
String sql = buffer.toString();
buffer.setLength(0);
if (pos < line.length()) {
buffer.append(line.substring(pos + 1));
}
if (! sql.startsWith("DROP")) {
dbStatement.execute(sql);
}
} else {
buffer.append(line);
buffer.append("\n");
}
}
}
}
protected void checkPersonData(Person hal, Person dbHal) {
Assert.assertNotNull(dbHal); Assert.assertNotNull(dbHal);
Assert.assertEquals(hal.getName(), dbHal.getName()); Assert.assertEquals(hal.getName(), dbHal.getName());
Assert.assertEquals(hal.getEmail(), dbHal.getEmail()); Assert.assertEquals(hal.getEmail(), dbHal.getEmail());
...@@ -75,95 +49,95 @@ public class HsqldbAccessTest { ...@@ -75,95 +49,95 @@ public class HsqldbAccessTest {
@Test @Test
public void testCreatePersonGetAllPersons() { public void testCreatePersonGetAllPersons() {
Person hal = dbAccess.createPerson("hal", "hal@ntnu.no"); final Person hal = dbAccess.createPerson("hal", "hal@ntnu.no");
Collection<Person> persons = dbAccess.getAllPersons(false); final Collection<Person> persons = dbAccess.getAllPersons(false);
Assert.assertEquals(1, persons.size()); Assert.assertEquals(1, persons.size());
checkPersonData(hal, persons.iterator().next()); checkPersonData(hal, persons.iterator().next());
dbAccess.personIds.clear(); dbAccess.personIds.clear();
Collection<Person> dbPersons = dbAccess.getAllPersons(true); final Collection<Person> dbPersons = dbAccess.getAllPersons(true);
Assert.assertEquals(1, dbPersons.size()); Assert.assertEquals(1, dbPersons.size());
checkPersonData(hal, dbPersons.iterator().next()); checkPersonData(hal, dbPersons.iterator().next());
} }
@Test @Test
public void testCreatePersonGetPerson() { public void testCreatePersonGetPerson() {
Person hal = dbAccess.createPerson("hal", "hal@ntnu.no"); final Person hal = dbAccess.createPerson("hal", "hal@ntnu.no");
int id = dbAccess.getId(hal); final int id = dbAccess.getId(hal);
Person dbHal = dbAccess.getPerson(id, true); final Person dbHal = dbAccess.getPerson(id, true);
checkPersonData(hal, dbHal); checkPersonData(hal, dbHal);
Assert.assertSame(dbHal, dbAccess.getPerson(id, false)); Assert.assertSame(dbHal, dbAccess.getPerson(id, false));
} }
@Test @Test
public void testCreatePersonGetPersonByName() { public void testCreatePersonGetPersonByName() {
Person hal = dbAccess.createPerson("hal", "hal@ntnu.no"); final Person hal = dbAccess.createPerson("hal", "hal@ntnu.no");
Person dbHal = dbAccess.getPersonByName(hal.getName(), true); final Person dbHal = dbAccess.getPersonByName(hal.getName(), true);
checkPersonData(hal, dbHal); checkPersonData(hal, dbHal);
} }
@Test @Test
public void testCreatePersonGetPersonByEmail() { public void testCreatePersonGetPersonByEmail() {
Person hal = dbAccess.createPerson("hal", "hal@ntnu.no"); final Person hal = dbAccess.createPerson("hal", "hal@ntnu.no");
Person dbHal = dbAccess.getPersonByEmail(hal.getEmail(), true); final Person dbHal = dbAccess.getPersonByEmail(hal.getEmail(), true);
checkPersonData(hal, dbHal); checkPersonData(hal, dbHal);
} }
@Test @Test
public void testCreatePersonUpdatePerson() { public void testCreatePersonUpdatePerson() {
Person hal = dbAccess.createPerson("hal", "hal@ntnu.no"); final Person hal = dbAccess.createPerson("hal", "hal@ntnu.no");
int id = dbAccess.getId(hal); final int id = dbAccess.getId(hal);
hal.setName("Hallvard"); hal.setName("Hallvard");
hal.setEmail("hallvard.traetteberg@gmail.com"); hal.setEmail("hallvard.traetteberg@gmail.com");
dbAccess.updatePersonData(hal); dbAccess.updatePersonData(hal);
Person dbHal = dbAccess.getPerson(id, true); final Person dbHal = dbAccess.getPerson(id, true);
checkPersonData(hal, dbHal); checkPersonData(hal, dbHal);
} }
@Test @Test
public void testCreatePersonDeletePerson() { public void testCreatePersonDeletePerson() {
Person hal = dbAccess.createPerson("hal", "hal@ntnu.no"); final Person hal = dbAccess.createPerson("hal", "hal@ntnu.no");
dbAccess.deletePerson(hal); dbAccess.deletePerson(hal);
Collection<Person> persons = dbAccess.getAllPersons(true); final Collection<Person> persons = dbAccess.getAllPersons(true);
Assert.assertEquals(0, persons.size()); Assert.assertEquals(0, persons.size());
} }
@Test @Test
public void testCreatePersonCreateGeoLocationsGetGeoLocations() { public void testCreatePersonCreateGeoLocationsGetGeoLocations() {
Person hal = dbAccess.createPerson("hal", "hal@ntnu.no"); final Person hal = dbAccess.createPerson("hal", "hal@ntnu.no");
// no GeoLocations so far // no GeoLocations so far
Assert.assertEquals(0, dbAccess.getGeoLocations(hal, false).size()); Assert.assertEquals(0, dbAccess.getGeoLocations(hal, false).size());
GeoLocations geoLocations1 = dbAccess.createGeoLocations(hal); final GeoLocations geoLocations1 = dbAccess.createGeoLocations(hal);
// check we have one locally // check we have one locally
Assert.assertEquals(1, hal.getGeoLocations((String[]) null).size()); Assert.assertEquals(1, hal.getGeoLocations((String[]) null).size());
Collection<GeoLocations> geoLocations = dbAccess.getGeoLocations(hal, false); final Collection<GeoLocations> geoLocations = dbAccess.getGeoLocations(hal, false);
Assert.assertEquals(1, hal.getGeoLocations((String[]) null).size()); Assert.assertEquals(1, hal.getGeoLocations((String[]) null).size());
Assert.assertEquals(1, geoLocations.size()); Assert.assertEquals(1, geoLocations.size());
Assert.assertSame(geoLocations1, geoLocations.iterator().next()); Assert.assertSame(geoLocations1, geoLocations.iterator().next());
dbAccess.geoLocationsIds.clear(); dbAccess.geoLocationsIds.clear();
// check we have one in the db // check we have one in the db
Collection<GeoLocations> dbGeoLocations = dbAccess.getGeoLocations(hal, true); final Collection<GeoLocations> dbGeoLocations = dbAccess.getGeoLocations(hal, true);
Assert.assertEquals(1, hal.getGeoLocations((String[]) null).size()); Assert.assertEquals(1, hal.getGeoLocations((String[]) null).size());
Assert.assertEquals(1, dbGeoLocations.size()); Assert.assertEquals(1, dbGeoLocations.size());
} }
@Test @Test
public void testCreatePersonCreateUpdateGeoLocationsGetGeoLocations() { public void testCreatePersonCreateUpdateGeoLocationsGetGeoLocations() {
Person hal = dbAccess.createPerson("hal", "hal@ntnu.no"); final Person hal = dbAccess.createPerson("hal", "hal@ntnu.no");
GeoLocations geoLocations1 = dbAccess.createGeoLocations(hal); final GeoLocations geoLocations1 = dbAccess.createGeoLocations(hal);
geoLocations1.setName("geoLocs1"); geoLocations1.setName("geoLocs1");
geoLocations1.setDescription("my first geo-location"); geoLocations1.setDescription("my first geo-location");
geoLocations1.setDate(LocalDate.of(2018, 3, 14)); geoLocations1.setDate(LocalDate.of(2018, 3, 14));
geoLocations1.setTime(LocalTime.of(11, 14)); geoLocations1.setTime(LocalTime.of(11, 14));
geoLocations1.setZone("Europe/Oslo"); geoLocations1.setZone("Europe/Oslo");
String[] tags = {"tag1", "tag2"}; final String[] tags = {"tag1", "tag2"};
geoLocations1.addTags(tags); geoLocations1.addTags(tags);
dbAccess.updateGeoLocationsData(geoLocations1); dbAccess.updateGeoLocationsData(geoLocations1);
dbAccess.geoLocationsIds.clear(); dbAccess.geoLocationsIds.clear();
// check the db // check the db
Collection<GeoLocations> dbGeoLocations = dbAccess.getGeoLocations(hal, true); final Collection<GeoLocations> dbGeoLocations = dbAccess.getGeoLocations(hal, true);
Assert.assertEquals(1, hal.getGeoLocations((String[]) null).size()); Assert.assertEquals(1, hal.getGeoLocations((String[]) null).size());
Assert.assertEquals(1, dbGeoLocations.size()); Assert.assertEquals(1, dbGeoLocations.size());
GeoLocations dbGeoLocations1 = dbGeoLocations.iterator().next(); final GeoLocations dbGeoLocations1 = dbGeoLocations.iterator().next();
Assert.assertEquals(geoLocations1.getName(), dbGeoLocations1.getName()); Assert.assertEquals(geoLocations1.getName(), dbGeoLocations1.getName());
Assert.assertEquals(geoLocations1.getDescription(), dbGeoLocations1.getDescription()); Assert.assertEquals(geoLocations1.getDescription(), dbGeoLocations1.getDescription());
Assert.assertEquals(geoLocations1.getDate(), dbGeoLocations1.getDate()); Assert.assertEquals(geoLocations1.getDate(), dbGeoLocations1.getDate());
...@@ -175,9 +149,9 @@ public class HsqldbAccessTest { ...@@ -175,9 +149,9 @@ public class HsqldbAccessTest {
@Test @Test
public void testCreatePersonCreateGeoLocationsDeleteGeoLocations() { public void testCreatePersonCreateGeoLocationsDeleteGeoLocations() {
Person hal = dbAccess.createPerson("hal", "hal@ntnu.no"); final Person hal = dbAccess.createPerson("hal", "hal@ntnu.no");
GeoLocations geoLocations = dbAccess.createGeoLocations(hal); final GeoLocations geoLocations = dbAccess.createGeoLocations(hal);
Collection<GeoLocations> dbGeoLocations = dbAccess.getGeoLocations(hal, true); final Collection<GeoLocations> dbGeoLocations = dbAccess.getGeoLocations(hal, true);
Assert.assertEquals(1, dbGeoLocations.size()); Assert.assertEquals(1, dbGeoLocations.size());
dbAccess.deleteGeoLocations(geoLocations); dbAccess.deleteGeoLocations(geoLocations);
} }
......
...@@ -6,11 +6,6 @@ ...@@ -6,11 +6,6 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/webapp">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"> <classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes> <attributes>
<attribute name="optional" value="true"/> <attribute name="optional" value="true"/>
......
...@@ -44,7 +44,9 @@ public class GeoServlet extends HttpServlet { ...@@ -44,7 +44,9 @@ public class GeoServlet extends HttpServlet {
dbConnectionUrl = dbConnectionParam; dbConnectionUrl = dbConnectionParam;
} }
try { try {
dbAccess = new DbAccessImpl(dbConnectionUrl); final DbAccessImpl dbAccess = new DbAccessImpl(dbConnectionUrl);
dbAccess.executeStatements("schema.sql", false);
this.dbAccess = dbAccess;
} catch (final SQLException e) { } catch (final SQLException e) {
throw new ServletException("Error when initializing database connection (" + dbConnectionUrl + "): " + e, e); throw new ServletException("Error when initializing database connection (" + dbConnectionUrl + "): " + e, e);
} }
...@@ -55,6 +57,7 @@ public class GeoServlet extends HttpServlet { ...@@ -55,6 +57,7 @@ public class GeoServlet extends HttpServlet {
final RestEntity<GeoLocation> geoLocationEntity = new RestEntity<>(); final RestEntity<GeoLocation> geoLocationEntity = new RestEntity<>();
final RestRelation<Void, Person> rootPersonRelation = new RootPersonsRelation("persons", personEntity); final RestRelation<Void, Person> rootPersonRelation = new RootPersonsRelation("persons", personEntity);
rootPersonRelation.setDbAccess(dbAccess);
rootEntity.addRelation(rootPersonRelation); rootEntity.addRelation(rootPersonRelation);
final RestRelation<Person, ?> personGeoLocationsRelation = new PersonGeoLocationsRelation("geoLocations", geoLocationsEntity); final RestRelation<Person, ?> personGeoLocationsRelation = new PersonGeoLocationsRelation("geoLocations", geoLocationsEntity);
personGeoLocationsRelation.setDbAccess(dbAccess); personGeoLocationsRelation.setDbAccess(dbAccess);
...@@ -105,7 +108,7 @@ public class GeoServlet extends HttpServlet { ...@@ -105,7 +108,7 @@ public class GeoServlet extends HttpServlet {
response.setContentType("application/json"); response.setContentType("application/json");
response.setStatus(HttpServletResponse.SC_OK); response.setStatus(HttpServletResponse.SC_OK);
try (OutputStream output = response.getOutputStream()) { try (OutputStream output = response.getOutputStream()) {
objectMapper.writeValue(output, result); objectMapper.writerWithDefaultPrettyPrinter().writeValue(output, result);
} catch (final Exception e) { } catch (final Exception e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} }
......
...@@ -67,8 +67,9 @@ public class GeoLocationsServerIT { ...@@ -67,8 +67,9 @@ public class GeoLocationsServerIT {
try (OutputStream conOut = con.getOutputStream()) { try (OutputStream conOut = con.getOutputStream()) {
conOut.write(bytes); conOut.write(bytes);
} }
try (InputStream conIn = url.openStream()) { try (InputStream conIn = con.getInputStream()) {
final JsonNode jsonTree = objectMapper.readTree(conIn); final JsonNode jsonTree = objectMapper.readTree(conIn);
// System.out.println(jsonTree);
Assert.assertTrue(jsonTree instanceof ObjectNode); Assert.assertTrue(jsonTree instanceof ObjectNode);
Assert.assertTrue(((ObjectNode) jsonTree).has("id")); Assert.assertTrue(((ObjectNode) jsonTree).has("id"));
final Person conPerson = objectMapper.treeToValue(jsonTree, Person.class); final Person conPerson = objectMapper.treeToValue(jsonTree, Person.class);
......
...@@ -222,12 +222,6 @@ public class HttpMethodTest { ...@@ -222,12 +222,6 @@ public class HttpMethodTest {
Assert.assertEquals(false, dbAccessRecord.get("getGeoLocations")); Assert.assertEquals(false, dbAccessRecord.get("getGeoLocations"));
Assert.assertTrue(result instanceof Collection<?>); Assert.assertTrue(result instanceof Collection<?>);
Assert.assertEquals(2, ((Collection<?>) result).size()); Assert.assertEquals(2, ((Collection<?>) result).size());
try {
objectMapper.writerWithDefaultPrettyPrinter().writeValue(System.out, person);
} catch (final Exception e) {
e.printStackTrace();
}
} }
@Test @Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment