Tested locking

This commit is contained in:
Fabio Sinibaldi 2022-03-22 17:29:25 +01:00
parent 9db702aedf
commit 7f48262034
5 changed files with 120 additions and 4 deletions

View File

@ -15,7 +15,7 @@ import java.util.Map;
@Getter
@Setter
@ToString(callSuper = true)
public class Field extends Document {
public class Field extends Document {
public static final String TYPE="_type";
public static final String CHILDREN="_children";

View File

@ -11,8 +11,8 @@ public class GCubeTest {
}
//testContext = "/pred4s/preprod/preVRE";
testContext = "/gcube/devsec/devVRE";
// testContext = "/pred4s/preprod/preVRE";
// testContext = "/gcube/devsec/devVRE";
System.out.println("TEST CONTEXT = "+testContext);

View File

@ -441,6 +441,10 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI<
doc.setTheDocument(Document.parse(docWrapper.getValueCTX().jsonString()));
doc = onUpdate(doc);
}catch (Throwable t){
log.warn("Unexpected Exception while trying to registering fileset on {}.",id,t);
log.debug("Request was {}",request);
log.debug("Complete doc was {} ",doc);
}finally {
return unlockAndUpdate(doc);
}

View File

@ -0,0 +1,111 @@
package org.gcube.application.geoportal.service.engine.mongo;
import org.bson.Document;
import org.gcube.application.cms.plugins.faults.EventException;
import org.gcube.application.cms.plugins.faults.StepException;
import org.gcube.application.cms.tests.TokenSetter;
import org.gcube.application.cms.tests.model.concessioni.TestConcessioniModel;
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.rest.RegisterFileSetRequest;
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.application.geoportal.service.model.internal.faults.*;
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
import org.geotoolkit.referencing.operation.provider.PolarStereographic;
import org.junit.Before;
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 {
String profileID = "profiledConcessioni";
ProfiledMongoManager manager= null;
@Before
public void inits() throws RegistrationException, ConfigurationException {
assumeTrue(GCubeTest.isTestInfrastructureEnabled());
TokenSetter.set(GCubeTest.getContext());
manager = new ProfiledMongoManager(profileID);
}
@Test
public void testLocking() throws StepException, EventException, IOException, ProjectLockedException, ProjectNotFoundException, InvalidLockException {
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
public void testUnlock() throws StepException, EventException, IOException, ProjectNotFoundException, ProjectLockedException, InvalidLockException, DeletionException, ConfigurationException, StorageHubException, StorageException {
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(
"$.relazioneScavo","fileset","relazioneScavo."+ Field.CHILDREN+"[?(@.fileset)]");
StorageUtils s= new StorageUtils();
builder.add(
s.putOntoStorage(new File(TestConcessioniModel.getBaseFolder(),"pos.shp"),"pos.shp"));
p= managerInterface.registerFileSet(p.getId(),builder.getTheRequest());
checkIsLockCleaned(p.getId());
// delete fileset
p=managerInterface.deleteFileSet(p.getId(),"$.relazioneScavo.fileset",false);
checkIsLockCleaned(p.getId());
// perform step
p=managerInterface.performStep(p.getId(),"SUBMIT DRAFT",new Document());
checkIsLockCleaned(p.getId());
}
private void checkIsLockCleaned(String id) throws ProjectNotFoundException {
assertNull(manager.getByID(id).getLock());
}
}

View File

@ -40,7 +40,8 @@ public class ProfiledConcessioniTests extends AbstractProfiledDocumentsTests{
doc = upload(
new StorageUtils(),
doc.getId(),
"posizionamentoScavo","fileset",
"posizionamentoScavo",
"fileset",
"posizionamentoScavo."+ Field.CHILDREN+"[?(@.fileset)]",
null,
RegisterFileSetRequest.ClashOptions.MERGE_EXISTING,