From 20336ea57a55c657683320e5d87b03e020562b3d Mon Sep 17 00:00:00 2001 From: Fabio Sinibaldi Date: Tue, 10 Jan 2023 11:42:09 +0100 Subject: [PATCH] Tests --- .../application/cms/tests/TestDocuments.java | 2 +- .../geoportal/clients/GenericUseCases.java | 11 +++-- .../clients/ProfiledConcessioniTest.java | 29 +++++++++++- .../clients/ProfiledDocumentsTest.java | 47 ++++++++++++++++++- .../engine/mongo/ProfiledMongoManager.java | 2 +- .../AbstractProfiledDocumentsTests.java | 8 +++- .../profiledDocuments/DummyProjectTest.java | 11 +++-- .../ProfiledConcessioniTests.java | 3 +- .../usecases/ExportConcessioniAsProjects.java | 3 ++ 9 files changed, 101 insertions(+), 15 deletions(-) diff --git a/cms-test-commons/src/main/java/org/gcube/application/cms/tests/TestDocuments.java b/cms-test-commons/src/main/java/org/gcube/application/cms/tests/TestDocuments.java index 009cb68..5b2dbf2 100644 --- a/cms-test-commons/src/main/java/org/gcube/application/cms/tests/TestDocuments.java +++ b/cms-test-commons/src/main/java/org/gcube/application/cms/tests/TestDocuments.java @@ -10,7 +10,7 @@ import java.nio.charset.Charset; import java.util.HashMap; public class TestDocuments { - public static File BASE_FOLDER =new File("test-data/profiledDocuments"); + public static File BASE_FOLDER =new File("../test-data/profiledDocuments"); public static final HashMap documentMap =new HashMap<>(); static{ diff --git a/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/GenericUseCases.java b/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/GenericUseCases.java index 0a23f67..c24da3f 100644 --- a/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/GenericUseCases.java +++ b/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/GenericUseCases.java @@ -52,16 +52,19 @@ public abstract class GenericUseCases extends BasicVreTests{ } protected Project prepareWithFileSet(Project project) throws FileNotFoundException, InvalidRequestException, RemoteException { + + return getClient().registerFileSet(project.getId(), prepareRequest()); + } + + protected RegisterFileSetRequest prepareRequest() throws FileNotFoundException { String parentPath = "$.section"; String fieldName = "fileset"; String fieldDefinition = "$.section._children[?(@.fileset)]"; // Prepare request - RegisterFileSetRequest fsRequest = FileSets. + return FileSets. prepareRequest(new StorageUtils(), parentPath,fieldName,fieldDefinition, new File(Tests.FOLDER_CONCESSIONI,"pos.shp")); - - - return getClient().registerFileSet(project.getId(),fsRequest); } + } diff --git a/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/ProfiledConcessioniTest.java b/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/ProfiledConcessioniTest.java index 8af8aae..e2a17cf 100644 --- a/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/ProfiledConcessioniTest.java +++ b/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/ProfiledConcessioniTest.java @@ -2,12 +2,17 @@ package org.gcube.application.geoportal.clients; import com.fasterxml.jackson.core.JsonProcessingException; import lombok.Data; +import lombok.extern.slf4j.Slf4j; import org.bson.Document; import org.gcube.application.cms.tests.TestDocuments; import org.gcube.application.cms.tests.Tests; import org.gcube.application.geoportal.client.utils.Serialization; import org.gcube.application.geoportal.common.faults.InvalidRequestException; +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.access.Access; +import org.gcube.application.geoportal.common.model.document.access.AccessPolicy; +import org.gcube.application.geoportal.common.model.document.filesets.RegisteredFileSet; 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.useCaseDescriptor.Field; @@ -25,6 +30,9 @@ import java.io.IOException; import java.nio.charset.Charset; import java.rmi.RemoteException; +import static org.junit.Assert.assertEquals; + +@Slf4j public class ProfiledConcessioniTest extends ProfiledDocumentsTest{ @Data @@ -78,11 +86,28 @@ public class ProfiledConcessioniTest extends ProfiledDocumentsTest{ // Set relazione - client.registerFileSet(project.getId(), FileSets. + + Access toSetAccess = new Access(); + toSetAccess.setPolicy(AccessPolicy.RESTRICTED); + toSetAccess.setLicense("CC0-1.0"); + + RegisterFileSetRequest request=FileSets. prepareRequest(new StorageUtils(), "$.relazioneScavo","fileset", "$.relazioneScavo."+Field.CHILDREN+"[?(@.fileset)]", - new File(Tests.FOLDER_CONCESSIONI,"relazione.pdf"))); + new File(Tests.FOLDER_CONCESSIONI,"relazione.pdf")); + request.setToSetAccess(toSetAccess); + + project=client.registerFileSet(project.getId(), request); + + String jsonDoc=project.getTheDocument().toJson(); + log.info("Registered fileset. Result is {}", jsonDoc); + RegisteredFileSet f= Serialization.convert(new JSONPathWrapper(jsonDoc).getByPath( + request.getParentPath()).get(0),RegisteredFileSet.class); + + assertEquals(Serialization.convert(f.getAccess(),Access.class).getPolicy(),toSetAccess.getPolicy()); + assertEquals(Serialization.convert(f.getAccess(),Access.class).getLicense(),toSetAccess.getLicense()); + // Set Abstract diff --git a/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/ProfiledDocumentsTest.java b/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/ProfiledDocumentsTest.java index 5296118..606d7f4 100644 --- a/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/ProfiledDocumentsTest.java +++ b/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/ProfiledDocumentsTest.java @@ -3,22 +3,35 @@ package org.gcube.application.geoportal.clients; import com.fasterxml.jackson.core.JsonProcessingException; import lombok.extern.slf4j.Slf4j; import org.bson.Document; +import org.gcube.application.cms.tests.Tests; +import org.gcube.application.geoportal.client.utils.Serialization; +import org.gcube.application.geoportal.common.faults.InvalidRequestException; +import org.gcube.application.geoportal.common.model.JSONPathWrapper; import org.gcube.application.geoportal.common.model.configuration.Configuration; import org.gcube.application.geoportal.common.model.document.Project; +import org.gcube.application.geoportal.common.model.document.access.Access; +import org.gcube.application.geoportal.common.model.document.access.AccessPolicy; +import org.gcube.application.geoportal.common.model.document.filesets.RegisteredFileSet; import org.gcube.application.geoportal.common.model.document.relationships.RelationshipNavigationObject; import org.gcube.application.geoportal.common.model.rest.CreateRelationshipRequest; import org.gcube.application.geoportal.common.model.rest.DeleteRelationshipRequest; import org.gcube.application.geoportal.common.model.rest.QueryRequest; +import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest; import org.gcube.application.geoportal.common.rest.Projects; +import org.gcube.application.geoportal.common.utils.FileSets; +import org.gcube.application.geoportal.common.utils.StorageUtils; import org.gcube.application.geoportal.common.utils.tests.GCubeTest; import org.junit.Test; +import java.io.File; +import java.io.FileNotFoundException; import java.rmi.RemoteException; import java.time.LocalDateTime; import java.util.Iterator; import java.util.concurrent.atomic.AtomicLong; import static org.gcube.application.geoportal.client.utils.Serialization.write; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeTrue; @@ -33,7 +46,7 @@ public class ProfiledDocumentsTest> exte @Test - public void registerNew() throws RemoteException, JsonProcessingException { + public void registerNew() throws RemoteException, JsonProcessingException, FileNotFoundException, InvalidRequestException { assumeTrue(GCubeTest.isTestInfrastructureEnabled()); Projects client = (Projects) getClient(); @@ -46,6 +59,38 @@ public class ProfiledDocumentsTest> exte Project p =client.createNew(theDoc); log.debug("Registered project (AS JSON) : {}", write(p)); + + + } + + + @Test + public void testAccess() throws FileNotFoundException, RemoteException, InvalidRequestException, JsonProcessingException { + assumeTrue(GCubeTest.isTestInfrastructureEnabled()); + Projects client = (Projects) getClient(); + + Document theDoc= Document.parse("{\n" + + "\"posizionamentoScavo\" :{\n" + + "\t\"titolo\" : \"mio titolo\"}}"); + + theDoc.put("startTime", LocalDateTime.now()); + + Project p =client.createNew(theDoc); + + RegisterFileSetRequest fsRequest = prepareRequest(); + + Access access = new Access(); + access.setLicense("CC0-1.0"); + access.setPolicy(AccessPolicy.RESTRICTED); + fsRequest.setToSetAccess(access); + + p=client.registerFileSet(p.getId(),fsRequest); + + log.info("Registered fileset. Result is {}", Serialization.write(p)); + RegisteredFileSet f= Serialization.convert(new JSONPathWrapper(p.getTheDocument().toJson()).getByPath(fsRequest.getParentPath()).get(0),RegisteredFileSet.class); + + assertEquals(Serialization.convert(f.getAccess(),Access.class).getPolicy(),AccessPolicy.RESTRICTED); + } @Test 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 3f52e50..0ff4e5f 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 @@ -953,7 +953,7 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI< private static final RegisteredFileSet prepareRegisteredFileSet(Access toSetAccess,String docID, String profileID, Document attributes,List files, StorageUtils storage,WorkspaceManager ws) throws StorageHubException, StorageException { - log.debug("Preparing Registered FileSet.."); + log.debug("Preparing Registered FileSet {}"); RegisteredFileSet toReturn = new RegisteredFileSet(); if(attributes!=null) toReturn.putAll(attributes); String uuid = UUID.randomUUID().toString(); diff --git a/geoportal-service/src/test/java/org/gcube/application/geoportal/service/profiledDocuments/AbstractProfiledDocumentsTests.java b/geoportal-service/src/test/java/org/gcube/application/geoportal/service/profiledDocuments/AbstractProfiledDocumentsTests.java index 1d87e55..7effc88 100644 --- a/geoportal-service/src/test/java/org/gcube/application/geoportal/service/profiledDocuments/AbstractProfiledDocumentsTests.java +++ b/geoportal-service/src/test/java/org/gcube/application/geoportal/service/profiledDocuments/AbstractProfiledDocumentsTests.java @@ -9,6 +9,7 @@ import org.gcube.application.cms.tests.TokenSetter; import org.gcube.application.cms.tests.model.BasicTests; import org.gcube.application.geoportal.common.model.configuration.Configuration; import org.gcube.application.geoportal.common.model.document.Project; +import org.gcube.application.geoportal.common.model.document.access.Access; import org.gcube.application.geoportal.common.model.document.lifecycle.LifecycleInformation; import org.gcube.application.geoportal.common.model.rest.QueryRequest; import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest; @@ -29,6 +30,7 @@ import javax.ws.rs.core.MediaType; import java.io.File; import java.util.List; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeTrue; @@ -158,9 +160,12 @@ public abstract class AbstractProfiledDocumentsTests extends BasicServiceTestUni String fieldDefinitionPath, Document attributes, RegisterFileSetRequest.ClashOptions clashPolicy, - String ...files) throws Exception { + Access access, + String ...files) throws Exception { FileSets.RequestBuilder builder = FileSets.build(parentPath,fieldName,fieldDefinitionPath); + builder.setAccess(access); + builder.setClashPolicy(clashPolicy).setAttributes(attributes); for(String file:files) @@ -169,6 +174,7 @@ public abstract class AbstractProfiledDocumentsTests extends BasicServiceTestUni Project doc = check(baseTarget().path(InterfaceConstants.Methods.REGISTER_FILES_PATH).path(id).request(MediaType.APPLICATION_JSON). post(Entity.entity(Serialization.write(builder.getTheRequest()), MediaType.APPLICATION_JSON)), Project.class); + BasicTests.validate(doc); return doc; } 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 4852a0d..118ec4a 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 @@ -4,6 +4,8 @@ import org.bson.Document; 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.access.Access; +import org.gcube.application.geoportal.common.model.document.access.AccessPolicy; 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; @@ -89,7 +91,7 @@ public class DummyProjectTest extends AbstractProfiledDocumentsTests{ fieldDefinition, new Document("customField","customFieldValue"), RegisterFileSetRequest.ClashOptions.REPLACE_EXISTING, - filename); + new Access(),filename); wrapper = new JSONPathWrapper(doc.getTheDocument().toJson()); assertTrue("Relazione exists",wrapper.getMatchingPaths("section").size()==1); @@ -127,8 +129,9 @@ public class DummyProjectTest extends AbstractProfiledDocumentsTests{ Project doc = createNew(baseDoc); - - + Access access =new Access(); + access.setLicense("test-license"); + access.setPolicy(AccessPolicy.RESTRICTED); // INSERT ONE, MERGE INFO (NB default values already exist) doc = upload( @@ -138,7 +141,7 @@ public class DummyProjectTest extends AbstractProfiledDocumentsTests{ fieldDefinition, new Document("customField","customFieldValue"), RegisterFileSetRequest.ClashOptions.REPLACE_EXISTING, - filename); + access,filename); return doc; } diff --git a/geoportal-service/src/test/java/org/gcube/application/geoportal/service/profiledDocuments/ProfiledConcessioniTests.java b/geoportal-service/src/test/java/org/gcube/application/geoportal/service/profiledDocuments/ProfiledConcessioniTests.java index c42b12c..11bfa66 100644 --- a/geoportal-service/src/test/java/org/gcube/application/geoportal/service/profiledDocuments/ProfiledConcessioniTests.java +++ b/geoportal-service/src/test/java/org/gcube/application/geoportal/service/profiledDocuments/ProfiledConcessioniTests.java @@ -8,6 +8,7 @@ import org.gcube.application.cms.serialization.Serialization; import org.gcube.application.cms.tests.model.BasicTests; 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.access.Access; 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.rest.RegisterFileSetRequest; @@ -70,7 +71,7 @@ public class ProfiledConcessioniTests extends AbstractProfiledDocumentsTests{ "fileset", "$.posizionamentoScavo."+ Field.CHILDREN+"[?(@.fileset)]", null, - RegisterFileSetRequest.ClashOptions.MERGE_EXISTING, + RegisterFileSetRequest.ClashOptions.MERGE_EXISTING,new Access(), "concessioni/pos.SHP"); diff --git a/use-cases/src/test/java/org/gcube/application/cms/usecases/ExportConcessioniAsProjects.java b/use-cases/src/test/java/org/gcube/application/cms/usecases/ExportConcessioniAsProjects.java index c265399..af49403 100644 --- a/use-cases/src/test/java/org/gcube/application/cms/usecases/ExportConcessioniAsProjects.java +++ b/use-cases/src/test/java/org/gcube/application/cms/usecases/ExportConcessioniAsProjects.java @@ -250,6 +250,9 @@ public class ExportConcessioniAsProjects { + "[?(@.fileset)]"); req.addAll(convertFiles(content)); req.setAccess(getAccess(rel)); + + + project = client.registerFileSet(project.getId(),req.getTheRequest()); } // upload fileset