From 3b2ae2503480939dd1e9538ba0f3c3d28c9d8a62 Mon Sep 17 00:00:00 2001 From: Fabio Sinibaldi Date: Fri, 30 Sep 2022 14:57:28 +0200 Subject: [PATCH] Bugfix relation management --- .../engine/mongo/ProfiledMongoManager.java | 6 +-- .../profiledDocuments/DummyProjectTest.java | 49 ++++++++++++++++++- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/ProfiledMongoManager.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/ProfiledMongoManager.java index f72b5c7..f1db782 100644 --- a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/ProfiledMongoManager.java +++ b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/ProfiledMongoManager.java @@ -335,7 +335,7 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI< @Override public Project deleteRelation(String id, String relation, String targetUCD, String targetId) throws IOException, EventException, ProjectLockedException, ProjectNotFoundException, InvalidLockException, InvalidUserRoleException, UnauthorizedAccess, RegistrationException, ConfigurationException { - Project toUpdate=lock(id,"Set Relation"); + Project toUpdate=lock(id,"Delete Relation"); try{ // check if relation existing List relations = toUpdate.getRelationships(); @@ -346,9 +346,9 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI< r.getTargetUCD().equals(targetUCD)&& r.getTargetID().equals(targetId)); // update only if something changed - if(toUpdate.getRelationships().size()!=beforeSize) return onUpdate(toUpdate); + if(toUpdate.getRelationships().size()!=beforeSize) return unlockAndUpdate(toUpdate); } - return unlockAndUpdate(toUpdate); + return unlock(toUpdate); }catch(Throwable t){ log.error("Unexpected exception ",t); unlock(toUpdate); diff --git a/geoportal-service/src/test/java/org/gcube/application/geoportal/service/profiledDocuments/DummyProjectTest.java b/geoportal-service/src/test/java/org/gcube/application/geoportal/service/profiledDocuments/DummyProjectTest.java index cc3a14a..4852a0d 100644 --- a/geoportal-service/src/test/java/org/gcube/application/geoportal/service/profiledDocuments/DummyProjectTest.java +++ b/geoportal-service/src/test/java/org/gcube/application/geoportal/service/profiledDocuments/DummyProjectTest.java @@ -5,6 +5,7 @@ import org.gcube.application.cms.serialization.Serialization; import org.gcube.application.geoportal.common.model.JSONPathWrapper; import org.gcube.application.geoportal.common.model.document.Project; import org.gcube.application.geoportal.common.model.document.filesets.RegisteredFileSet; +import org.gcube.application.geoportal.common.model.document.relationships.Relationship; import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest; import org.gcube.application.geoportal.common.model.useCaseDescriptor.Field; import org.gcube.application.geoportal.common.rest.InterfaceConstants; @@ -19,8 +20,7 @@ import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import java.time.LocalDateTime; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; import static org.junit.Assume.assumeTrue; public class DummyProjectTest extends AbstractProfiledDocumentsTests{ @@ -155,6 +155,51 @@ public class DummyProjectTest extends AbstractProfiledDocumentsTests{ assertNull(p.getLock()); } + @Test + public void testRelationships() throws Exception { + assumeTrue(GCubeTest.isTestInfrastructureEnabled()); + Project a = createNew(new Document("key","value")); + Project b = createNew(new Document("key","value")); + + // set relation a -- precedes --> b + System.out.println("Setting relation.."); + String relId="precedes"; + a = check(baseTarget(). + path(InterfaceConstants.Methods.RELATIONSHIP). + path(a.getId()). + path(relId). + queryParam(InterfaceConstants.Parameters.TARGET_UCD,b.getProfileID()). + queryParam(InterfaceConstants.Parameters.TARGET_ID,b.getId()). + request(MediaType.APPLICATION_JSON). + put(Entity.json("")), Project.class); + // check set relation in a + System.out.println("Checking relation a->b"); + assertTrue(a.getRelationships()!=null && a.getRelationships().size()==1); + Relationship rel = a.getRelationships().get(0); + assertEquals(rel.getRelationshipName(),relId); + assertEquals(rel.getTargetUCD(),b.getProfileID()); + assertEquals(rel.getTargetID(),b.getId()); + + // TODO TBD check reciprocity : expected relation b -- follows -> a + + // delete relation + System.out.println("Deleting relation a->b"); + a = check(baseTarget(). + path(InterfaceConstants.Methods.RELATIONSHIP). + path(a.getId()). + path(relId). + queryParam(InterfaceConstants.Parameters.TARGET_UCD,b.getProfileID()). + queryParam(InterfaceConstants.Parameters.TARGET_ID,b.getId()). + request(MediaType.APPLICATION_JSON). + delete(), Project.class); + // check deleted + + System.out.println("Checking deleted relation"); + assertTrue(a.getRelationships()==null || a.getRelationships().isEmpty()); + + + } + // @Test // public void testSDI() throws Exception {