Skip to content

Commit

Permalink
Ensure a new auto generated FoI is created when the old one is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed Apr 8, 2019
1 parent aaf2931 commit 83d55be
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
Expand Up @@ -37,6 +37,7 @@
import de.fraunhofer.iosb.ilt.frostserver.persistence.postgres.PostgresPersistenceManager;
import de.fraunhofer.iosb.ilt.frostserver.persistence.postgres.Utils;
import de.fraunhofer.iosb.ilt.frostserver.persistence.postgres.relationalpaths.AbstractQFeatures;
import de.fraunhofer.iosb.ilt.frostserver.persistence.postgres.relationalpaths.AbstractQLocations;
import de.fraunhofer.iosb.ilt.frostserver.persistence.postgres.relationalpaths.AbstractQObservations;
import de.fraunhofer.iosb.ilt.frostserver.persistence.postgres.relationalpaths.QCollection;
import de.fraunhofer.iosb.ilt.frostserver.query.Query;
Expand Down Expand Up @@ -226,6 +227,12 @@ public void delete(PostgresPersistenceManager<I, J> pm, J entityId) throws NoSuc
if (count == 0) {
throw new NoSuchEntityException("FeatureOfInterest " + entityId + " not found.");
}
AbstractQLocations<? extends AbstractQLocations, I, J> qLoc = qCollection.qLocations;
pm.createQueryFactory()
.update(qLoc)
.setNull(qLoc.getGenFoiId())
.where(qLoc.getGenFoiId().eq(entityId))
.execute();
}

@Override
Expand Down
Expand Up @@ -31,6 +31,7 @@
import static de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.factories.EntityFactories.CHANGED_MULTIPLE_ROWS;
import static de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.factories.EntityFactories.NO_ID_OR_NOT_FOUND;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.tables.AbstractTableFeatures;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.tables.AbstractTableLocations;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.tables.AbstractTableObservations;
import de.fraunhofer.iosb.ilt.frostserver.query.Query;
import de.fraunhofer.iosb.ilt.frostserver.util.IncompleteEntityException;
Expand Down Expand Up @@ -227,6 +228,13 @@ public void delete(PostgresPersistenceManager<J> pm, J entityId) throws NoSuchEn
if (count == 0) {
throw new NoSuchEntityException("FeatureOfInterest " + entityId + " not found.");
}
// Delete references to the FoI in the Locations table.
AbstractTableLocations<J> tLoc = entityFactories.tableCollection.tableLocations;
pm.getDslContext()
.update(tLoc)
.set(tLoc.getGenFoiId(), (J) null)
.where(tLoc.getGenFoiId().eq(entityId))
.execute();
}

@Override
Expand Down
Expand Up @@ -85,6 +85,7 @@ public class TestSuite {
// de.fraunhofer.iosb.ilt.sta.persistence.pgjooq.imp.PostgresPersistenceManagerUuid
// de.fraunhofer.iosb.ilt.sta.persistence.pgjooq.imp.PostgresPersistenceManagerString
// de.fraunhofer.iosb.ilt.frostserver.persistence.postgres.longid.PostgresPersistenceManagerLong
// de.fraunhofer.iosb.ilt.frostserver.persistence.postgres.stringid.PostgresPersistenceManagerString
public static final String VAL_PERSISTENCE_MANAGER = "de.fraunhofer.iosb.ilt.sta.persistence.pgjooq.imp.PostgresPersistenceManagerLong";
public static final String VAL_PG_DB = "sensorthings";
public static final String VAL_PG_USER = "sensorthings";
Expand Down
Expand Up @@ -18,7 +18,6 @@
import de.fraunhofer.iosb.ilt.statests.util.EntityUtils;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -80,7 +79,7 @@ public static void tearDown() {
* @throws URISyntaxException If the service url is incorrect.
*/
@Test
public void testMultipleLocations() throws ServiceFailureException, URISyntaxException {
public void testMultipleLocations() throws ServiceFailureException {
EntityUtils.deleteAll(service);

Thing thing = new Thing("Thing 1", "The first thing.");
Expand All @@ -96,7 +95,7 @@ public void testMultipleLocations() throws ServiceFailureException, URISyntaxExc
THINGS.add(thing);

Sensor sensor = new Sensor("Sensor 1", "The first sensor.", "text", "Some metadata.");
ObservedProperty obsProp = new ObservedProperty("Temperature", new URI("http://ucom.org/temperature"), "The temperature of the thing.");
ObservedProperty obsProp = new ObservedProperty("Temperature", "http://ucom.org/temperature", "The temperature of the thing.");
Datastream datastream = new Datastream("Datastream 1", "The temperature of thing 1, sensor 1.", "someType", new UnitOfMeasurement("degree celcius", "°C", "ucum:T"));
datastream.setSensor(sensor);
datastream.setObservedProperty(obsProp);
Expand Down Expand Up @@ -193,10 +192,9 @@ public void testHistoricalLocationThing() throws ServiceFailureException {
* Datastream(y) exists, but is not part of the, also existing, Things(x).
*
* @throws ServiceFailureException If the service doesn't respond.
* @throws URISyntaxException
*/
@Test
public void testPostInvalidPath() throws ServiceFailureException, URISyntaxException {
public void testPostInvalidPath() throws ServiceFailureException {
EntityUtils.deleteAll(service);
// Create two things

Expand All @@ -214,7 +212,7 @@ public void testPostInvalidPath() throws ServiceFailureException, URISyntaxExcep
Sensor sensor1 = new Sensor("Test Thermometre", "Test Sensor", "None", "-");
service.create(sensor1);

ObservedProperty obsProp1 = new ObservedProperty("Temperature", new URI("http://example.org"), "-");
ObservedProperty obsProp1 = new ObservedProperty("Temperature", "http://example.org", "-");
service.create(obsProp1);

Datastream datastream1 = new Datastream("Ds 1, Thing 1", "The datastream of Thing 1", "http://www.opengis.net/def/observationType/OGC-OM/2.0/OM_Measurement", new UnitOfMeasurement("Degrees Celcius", "°C", "http://qudt.org/vocab/unit#DegreeCelsius"));
Expand Down Expand Up @@ -299,4 +297,45 @@ public void testPostInvalidPath() throws ServiceFailureException, URISyntaxExcep
response = HTTPMethods.doGet(urlObsGood);
Assert.assertEquals("Get should return 404 Not Found for url " + urlObsGood, 404, response.code);
}

@Test
public void testRecreateAutomaticFoi() throws ServiceFailureException {
EntityUtils.deleteAll(service);
// Create two things

Location location1 = new Location("LocationThing1", "Location of Thing 1", "application/geo+json", new Point(8, 50));
service.create(location1);

Thing thing1 = new Thing("Thing 1", "The first thing.");
thing1.getLocations().add(location1.withOnlyId());
service.create(thing1);

Sensor sensor1 = new Sensor("Test Thermometre", "Test Sensor", "None", "-");
service.create(sensor1);

ObservedProperty obsProp1 = new ObservedProperty("Temperature", "http://example.org", "-");
service.create(obsProp1);

Datastream datastream1 = new Datastream("Ds 1, Thing 1", "The datastream of Thing 1", "http://www.opengis.net/def/observationType/OGC-OM/2.0/OM_Measurement", new UnitOfMeasurement("Degrees Celcius", "°C", "http://qudt.org/vocab/unit#DegreeCelsius"));
datastream1.setThing(thing1);
datastream1.setSensor(sensor1);
datastream1.setObservedProperty(obsProp1);
service.create(datastream1);

Observation obs1 = new Observation(1.0, datastream1);
service.create(obs1);

FeatureOfInterest foiGenerated1 = service.observations().find(obs1.getId()).getFeatureOfInterest();
Assert.assertNotNull(foiGenerated1);

service.delete(foiGenerated1);

Observation obs2 = new Observation(1.0, datastream1);
service.create(obs2);

FeatureOfInterest foiGenerated2 = service.observations().find(obs2.getId()).getFeatureOfInterest();
Assert.assertNotNull(foiGenerated2);

Assert.assertNotEquals(foiGenerated1, foiGenerated2);
}
}

0 comments on commit 83d55be

Please sign in to comment.