Bugfix relation management

This commit is contained in:
Fabio Sinibaldi 2022-09-30 14:57:28 +02:00
parent ef30cd46d4
commit 3b2ae25034
2 changed files with 50 additions and 5 deletions

View File

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

View File

@ -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 {