gcube-cms-suite/geoportal-service/src/test/java/org/gcube/application/geoportal/service/engine/mongo/LockTests.java

124 lines
5.1 KiB
Java
Raw Normal View History

2022-03-22 17:29:25 +01:00
package org.gcube.application.geoportal.service.engine.mongo;
import org.bson.Document;
2022-10-27 16:03:38 +02:00
import org.gcube.application.cms.implementations.faults.*;
2023-01-10 15:57:40 +01:00
import org.gcube.application.cms.implementations.utils.UserUtils;
2022-03-22 17:29:25 +01:00
import org.gcube.application.cms.plugins.faults.EventException;
2022-04-08 13:17:52 +02:00
import org.gcube.application.cms.plugins.faults.InsufficientPrivileges;
2022-03-22 17:29:25 +01:00
import org.gcube.application.cms.plugins.faults.StepException;
2022-09-26 16:11:20 +02:00
import org.gcube.application.cms.tests.Tests;
2022-03-22 17:29:25 +01:00
import org.gcube.application.cms.tests.TokenSetter;
import org.gcube.application.geoportal.common.faults.StorageException;
import org.gcube.application.geoportal.common.model.document.Lock;
import org.gcube.application.geoportal.common.model.document.Project;
import org.gcube.application.geoportal.common.model.rest.ConfigurationException;
import org.gcube.application.geoportal.common.model.useCaseDescriptor.Field;
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.gcube.application.geoportal.service.BasicServiceTestUnit;
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
import org.junit.Before;
2022-04-07 18:12:42 +02:00
import org.junit.BeforeClass;
2022-03-22 17:29:25 +01:00
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import static junit.framework.TestCase.*;
import static org.junit.Assume.assumeTrue;
public class LockTests extends BasicServiceTestUnit {
2023-01-10 15:57:40 +01:00
String profileID = "basic";
2022-03-22 17:29:25 +01:00
ProfiledMongoManager manager= null;
2022-04-07 18:12:42 +02:00
@BeforeClass
public static void setLocalFolder(){
USE_LOCAL_FOLDER=true;
}
2022-03-22 17:29:25 +01:00
@Before
public void inits() throws RegistrationException, ConfigurationException {
assumeTrue(GCubeTest.isTestInfrastructureEnabled());
TokenSetter.set(GCubeTest.getContext());
manager = new ProfiledMongoManager(profileID);
2022-04-01 19:11:11 +02:00
2022-04-07 18:12:42 +02:00
// NB Role testing not expected in VRE
2022-04-01 19:11:11 +02:00
UserUtils.DEFAULT_ROLES.add("FakeAdmin");
2022-03-22 17:29:25 +01:00
}
@Test
2022-03-30 18:39:10 +02:00
public void testLocking() throws StepException, EventException, IOException, ProjectLockedException, ProjectNotFoundException, InvalidLockException, InvalidUserRoleException, UnauthorizedAccess {
2022-03-22 18:07:34 +01:00
assumeTrue(GCubeTest.isTestInfrastructureEnabled());
2022-03-22 17:29:25 +01:00
Project p = manager.registerNew(new Document("dummyField","dummyValue"));
// lock
p= manager.lock(p.getId(),"Testing");
assertNotNull(p.getLock());
assertNotNull(p.getLock().getId());
assertNotNull(p.getLock().getInfo());
assertTrue(p.getLock().getOperation().equals("Testing"));
try{
Lock l =p.getLock();
System.out.println("Lock is "+l);
p = manager.lock(p.getId(),"Should Fail");
fail("Expected Lock exception. New Lock is "+p.getLock());
}catch(ProjectLockedException e){
// expected lock exception
System.out.println("Successfully blocked invalid lock operation");
}
try{
Lock l =p.getLock();
System.out.println("Lock is "+l);
manager.update(p.getId(),new Document("should","fail"));
fail("Expected Lock exception. New Lock is "+p.getLock());
}catch (ProjectLockedException e){
// expected lock exception
System.out.println("Successfully blocked invalid edit operation");
}
manager.unlockAndUpdate(p);
manager.update(p.getId(),new Document("should","fail"));
}
@Test
2022-04-08 13:17:52 +02:00
public void testUnlock() throws StepException, EventException, IOException, ProjectNotFoundException, ProjectLockedException, InvalidLockException, DeletionException, ConfigurationException, StorageHubException, StorageException, InvalidUserRoleException, UnauthorizedAccess, InsufficientPrivileges {
2022-03-22 18:07:34 +01:00
assumeTrue(GCubeTest.isTestInfrastructureEnabled());
2022-03-22 17:29:25 +01:00
MongoManagerI<Project> managerInterface = manager;
// create
Project p = managerInterface.registerNew(new Document("dummyField","dummyValue"));
checkIsLockCleaned(p.getId());
// edit
p=managerInterface.update(p.getId(),new Document("some","field"));
checkIsLockCleaned(p.getId());
// register fileset
FileSets.RequestBuilder builder = FileSets.build(
2023-01-10 15:57:40 +01:00
"$.section","fileset","section."+ Field.CHILDREN+"[?(@.fileset)]");
2022-03-22 17:29:25 +01:00
StorageUtils s= new StorageUtils();
builder.add(
2022-09-26 16:11:20 +02:00
s.putOntoStorage(new File(Tests.FOLDER_CONCESSIONI,"pos.shp"),"pos.shp"));
2022-03-22 17:29:25 +01:00
p= managerInterface.registerFileSet(p.getId(),builder.getTheRequest());
checkIsLockCleaned(p.getId());
// delete fileset
2023-01-10 15:57:40 +01:00
p=managerInterface.deleteFileSet(p.getId(),"$.section.fileset",false);
2022-03-22 17:29:25 +01:00
checkIsLockCleaned(p.getId());
// perform step
p=managerInterface.performStep(p.getId(),"SUBMIT DRAFT",new Document());
checkIsLockCleaned(p.getId());
}
2022-03-29 18:06:09 +02:00
private void checkIsLockCleaned(String id) throws ProjectNotFoundException, InvalidUserRoleException, UnauthorizedAccess {
2022-03-22 17:29:25 +01:00
assertNull(manager.getByID(id).getLock());
}
}