Relationship tests

This commit is contained in:
Fabio Sinibaldi 2022-10-25 16:57:41 +02:00
parent 2b40d4058a
commit 118a3474fa
5 changed files with 91 additions and 23 deletions

View File

@ -93,7 +93,7 @@ public class ProfiledDocumentsTest<M extends Project,C extends Projects<M>> exte
Project source = client.createNew(new Document().append("field","value")); Project source = client.createNew(new Document().append("field","value"));
Project target = client.createNew(new Document().append("field2","value")); Project target = client.createNew(new Document().append("field2","value"));
String relId="associate"; String relId="follows";
Project linkedSource = client.setRelation( Project linkedSource = client.setRelation(

View File

@ -339,7 +339,7 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI<
reverseRel.setTargetID(id); reverseRel.setTargetID(id);
reverseRel.setTargetUCD(getUseCaseDescriptor().getId()); reverseRel.setTargetUCD(getUseCaseDescriptor().getId());
log.info("Setting reverse relation {} ",reverseRel); log.info("Setting reverse relation {} ",reverseRel);
otherManager.lock(other.getId(),"Setting reverse relation "+reverseRel); other = otherManager.lock(other.getId(),"Setting reverse relation "+reverseRel);
other.addRelation(reverseRel); other.addRelation(reverseRel);
otherManager.unlockAndUpdate(other); otherManager.unlockAndUpdate(other);
} }
@ -388,7 +388,7 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI<
log.debug("Removing reverse of {} ",r); log.debug("Removing reverse of {} ",r);
ProfiledMongoManager otherManager = (targetUCD.equals(this.useCaseDescriptor.getId()))?this:new ProfiledMongoManager(targetUCD); ProfiledMongoManager otherManager = (targetUCD.equals(this.useCaseDescriptor.getId()))?this:new ProfiledMongoManager(targetUCD);
Project other = getByID(targetId); Project other = getByID(targetId);
otherManager.lock(other.getId(),"Remove reverse relation "+toDeleteReverseRelation + " toward "+getUseCaseDescriptor().getId()+":"+id); other = otherManager.lock(other.getId(),"Remove reverse relation "+toDeleteReverseRelation + " toward "+getUseCaseDescriptor().getId()+":"+id);
final String finalToDeleteReverseRelation = toDeleteReverseRelation; final String finalToDeleteReverseRelation = toDeleteReverseRelation;
other.getRelationships().removeIf(revRel ->revRel.getRelationshipName().equals(finalToDeleteReverseRelation)&& other.getRelationships().removeIf(revRel ->revRel.getRelationshipName().equals(finalToDeleteReverseRelation)&&
revRel.getTargetID().equals(id)&&revRel.getTargetUCD().equals(getUseCaseDescriptor().getId())); revRel.getTargetID().equals(id)&&revRel.getTargetUCD().equals(getUseCaseDescriptor().getId()));

View File

@ -31,21 +31,4 @@ public class InexistentUseCaseDescriptorTests extends AbstractProfiledDocumentsT
assertEquals(404,resp.getStatus()); assertEquals(404,resp.getStatus());
} }
@Override
@Test(expected = Exception.class)
public void getAll() {
super.getAll();
}
@Override
@Test(expected = Exception.class)
public void getByID() {
super.getByID();
}
@Override
@Test(expected = Exception.class)
public void getConfiguration() {
super.getByID();
}
} }

View File

@ -21,6 +21,7 @@ import org.gcube.application.geoportal.service.BasicServiceTestUnit;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.opengis.service.Interface;
import javax.ws.rs.client.Entity; import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget; import javax.ws.rs.client.WebTarget;
@ -58,13 +59,12 @@ public abstract class AbstractProfiledDocumentsTests extends BasicServiceTestUni
} }
@Test @Test
public void getByID(){ public void scanAllById(){
assumeTrue(GCubeTest.isTestInfrastructureEnabled()); assumeTrue(GCubeTest.isTestInfrastructureEnabled());
baseTarget().request(MediaType.APPLICATION_JSON).get(List.class).forEach(d ->{ baseTarget().request(MediaType.APPLICATION_JSON).get(List.class).forEach(d ->{
try { try {
check(baseTarget().path((Serialization.convert(d, Project.class)).getId()) getById((Serialization.convert(d, Project.class)).getId());
.request(MediaType.APPLICATION_JSON).get(), Project.class);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(System.err); e.printStackTrace(System.err);
Assert.fail(e.getMessage()); Assert.fail(e.getMessage());
@ -104,6 +104,36 @@ public abstract class AbstractProfiledDocumentsTests extends BasicServiceTestUni
return doc; return doc;
} }
protected Project getById(String id ) throws Exception {
Project doc =check(baseTarget().path(id).request(MediaType.APPLICATION_JSON).get(), Project.class);
BasicTests.validate(doc);
assertTrue(doc.getLifecycleInformation().getPhase().equals(LifecycleInformation.CommonPhases.DRAFT_PHASE));
return doc;
}
protected Project setRelation(Project current, String targetId, String targetUCD, String relationId) throws Exception {
Project doc =check(baseTarget().path(InterfaceConstants.Methods.RELATIONSHIP).
path(current.getId()).path(relationId).
queryParam(InterfaceConstants.Parameters.TARGET_ID,targetId).
queryParam(InterfaceConstants.Parameters.TARGET_UCD,targetUCD).
request(MediaType.APPLICATION_JSON).
put(Entity.json("")), Project.class);
BasicTests.validate(doc);
return doc;
}
protected Project deleteRelation(Project current, String targetId, String targetUCD, String relationId) throws Exception {
Project doc =check(baseTarget().path(InterfaceConstants.Methods.RELATIONSHIP).
path(current.getId()).path(relationId).
queryParam(InterfaceConstants.Parameters.TARGET_ID,targetId).
queryParam(InterfaceConstants.Parameters.TARGET_UCD,targetUCD).
request(MediaType.APPLICATION_JSON).
delete(), Project.class);
BasicTests.validate(doc);
return doc;
}
protected void delete(String id) throws Exception { protected void delete(String id) throws Exception {
delete(id,false); delete(id,false);
} }

View File

@ -3,20 +3,28 @@ package org.gcube.application.geoportal.service.profiledDocuments;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.bson.Document; import org.bson.Document;
import org.gcube.application.cms.serialization.Serialization; import org.gcube.application.cms.serialization.Serialization;
import org.gcube.application.cms.tests.model.BasicTests;
import org.gcube.application.geoportal.common.model.document.Project; import org.gcube.application.geoportal.common.model.document.Project;
import org.gcube.application.geoportal.common.model.document.identification.SpatialReference; import org.gcube.application.geoportal.common.model.document.identification.SpatialReference;
import org.gcube.application.geoportal.common.model.document.lifecycle.LifecycleInformation; import org.gcube.application.geoportal.common.model.document.lifecycle.LifecycleInformation;
import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest; import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest;
import org.gcube.application.geoportal.common.model.rest.StepExecutionRequest; import org.gcube.application.geoportal.common.model.rest.StepExecutionRequest;
import org.gcube.application.geoportal.common.model.useCaseDescriptor.Field; import org.gcube.application.geoportal.common.model.useCaseDescriptor.Field;
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.common.utils.StorageUtils; import org.gcube.application.geoportal.common.utils.StorageUtils;
import org.gcube.application.geoportal.common.utils.tests.GCubeTest;
import org.gcube.application.geoportal.service.utils.UserUtils; import org.gcube.application.geoportal.service.utils.UserUtils;
import org.junit.Test; import org.junit.Test;
import javax.management.relation.Relation;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget; import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
@Slf4j @Slf4j
public class ProfiledConcessioniTests extends AbstractProfiledDocumentsTests{ public class ProfiledConcessioniTests extends AbstractProfiledDocumentsTests{
@ -25,12 +33,14 @@ public class ProfiledConcessioniTests extends AbstractProfiledDocumentsTests{
@Override @Override
protected WebTarget baseTarget() { protected WebTarget baseTarget() {
assumeTrue(GCubeTest.isTestInfrastructureEnabled());
String testProfileId="profiledConcessioni"; String testProfileId="profiledConcessioni";
return super.baseTarget().path(testProfileId); return super.baseTarget().path(testProfileId);
} }
@Test @Test
public void testSDI() throws Exception { public void testSDI() throws Exception {
assumeTrue(GCubeTest.isTestInfrastructureEnabled());
UserUtils.DEFAULT_ROLES.add("Data-Manager"); UserUtils.DEFAULT_ROLES.add("Data-Manager");
// Create new // Create new
@ -82,6 +92,51 @@ public class ProfiledConcessioniTests extends AbstractProfiledDocumentsTests{
System.out.println("Project "+doc.getId()+" published with spatial reference "+doc.getIdentificationReferenceByType(SpatialReference.SPATIAL_REFERENCE_TYPE).get(0)); System.out.println("Project "+doc.getId()+" published with spatial reference "+doc.getIdentificationReferenceByType(SpatialReference.SPATIAL_REFERENCE_TYPE).get(0));
} }
@Test
public void testRelations() throws Exception {
assumeTrue(GCubeTest.isTestInfrastructureEnabled());
Document theDoc=Document.parse("{\n" +
"\"posizionamentoScavo\" :{\n" +
"\t\"titolo\" : \"mio titolo\"}}");
Project doc1 = createNew(theDoc);
Project doc2 = createNew(theDoc);
String rel = "follows";
String reverse = "precedes";
log.info("Creating relation ");
doc1 = setRelation(doc1,doc2.getId(),null,rel);
log.debug("JSON Doc 1 : {}",Serialization.write(doc1));
// check relation
assertTrue(doc1.getRelationships().size()==1);
assertEquals(doc1.getRelationships().get(0).getTargetID(),doc2.getId());
assertEquals(doc1.getRelationships().get(0).getTargetUCD(),doc2.getProfileID());
assertEquals(doc1.getRelationships().get(0).getRelationshipName(),rel);
// check reverse
doc2=getById(doc2.getId());
log.debug("JSON Doc 2 : {}",Serialization.write(doc2));
assertTrue(doc2.getRelationships().size()==1);
assertEquals(doc2.getRelationships().get(0).getTargetID(),doc1.getId());
assertEquals(doc2.getRelationships().get(0).getTargetUCD(),doc1.getProfileID());
assertEquals(doc2.getRelationships().get(0).getRelationshipName(),reverse);
// clean
doc2 = deleteRelation(doc2, doc1.getId(), doc1.getProfileID(),reverse);
log.debug("JSON Doc 2 : {}",Serialization.write(doc2));
assertTrue(doc2.getRelationships()==null || doc2.getRelationships().isEmpty());
doc1 = getById(doc1.getId());
log.debug("JSON Doc 1 : {}",Serialization.write(doc1));
assertTrue(doc1.getRelationships()==null || doc1.getRelationships().isEmpty());
}
@Test @Test
public void delete() throws Exception { public void delete() throws Exception {
Document theDoc=Document.parse("{\n" + Document theDoc=Document.parse("{\n" +