Skip to content
Snippets Groups Projects
Commit ee63d387 authored by pkupczyk's avatar pkupczyk
Browse files

SSDM-1019 : V3 AS API - Fully cover all existing methods with automated tests

SVN: 32714
parent 4e40d1ee
No related branches found
No related tags found
No related merge requests found
...@@ -21,6 +21,7 @@ import java.util.List; ...@@ -21,6 +21,7 @@ import java.util.List;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import ch.ethz.sis.openbis.generic.server.api.v3.executor.IOperationContext; import ch.ethz.sis.openbis.generic.server.api.v3.executor.IOperationContext;
...@@ -36,6 +37,7 @@ import ch.ethz.sis.openbis.generic.shared.api.v3.dto.id.experiment.ExperimentPer ...@@ -36,6 +37,7 @@ import ch.ethz.sis.openbis.generic.shared.api.v3.dto.id.experiment.ExperimentPer
import ch.ethz.sis.openbis.generic.shared.api.v3.exceptions.UnauthorizedObjectAccessException; import ch.ethz.sis.openbis.generic.shared.api.v3.exceptions.UnauthorizedObjectAccessException;
import ch.systemsx.cisd.common.exceptions.UserFailureException; import ch.systemsx.cisd.common.exceptions.UserFailureException;
import ch.systemsx.cisd.openbis.generic.server.authorization.validator.ProjectByIdentiferValidator; import ch.systemsx.cisd.openbis.generic.server.authorization.validator.ProjectByIdentiferValidator;
import ch.systemsx.cisd.openbis.generic.server.business.bo.DataAccessExceptionTranslator;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory; import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory;
import ch.systemsx.cisd.openbis.generic.shared.dto.EntityTypePE; import ch.systemsx.cisd.openbis.generic.shared.dto.EntityTypePE;
import ch.systemsx.cisd.openbis.generic.shared.dto.ExperimentPE; import ch.systemsx.cisd.openbis.generic.shared.dto.ExperimentPE;
...@@ -90,22 +92,29 @@ public class CreateExperimentExecutor implements ICreateExperimentExecutor ...@@ -90,22 +92,29 @@ public class CreateExperimentExecutor implements ICreateExperimentExecutor
@Override @Override
public List<ExperimentPermId> create(IOperationContext context, List<ExperimentCreation> creations) public List<ExperimentPermId> create(IOperationContext context, List<ExperimentCreation> creations)
{ {
List<ExperimentPermId> result = new LinkedList<ExperimentPermId>(); try
for (ExperimentCreation creation : creations)
{ {
context.pushContextDescription(ExperimentContextDescription.creating(creation)); List<ExperimentPermId> result = new LinkedList<ExperimentPermId>();
ExperimentPE experiment = createExperimentPE(context, creation); for (ExperimentCreation creation : creations)
daoFactory.getExperimentDAO().createOrUpdateExperiment(experiment, context.getSession().tryGetPerson()); {
createAttachmentExecutor.create(context, experiment, creation.getAttachments()); context.pushContextDescription(ExperimentContextDescription.creating(creation));
addTagToEntityExecutor.add(context, experiment, creation.getTagIds());
result.add(new ExperimentPermId(experiment.getPermId()));
context.popContextDescription(); ExperimentPE experiment = createExperimentPE(context, creation);
} daoFactory.getExperimentDAO().createOrUpdateExperiment(experiment, context.getSession().tryGetPerson());
createAttachmentExecutor.create(context, experiment, creation.getAttachments());
addTagToEntityExecutor.add(context, experiment, creation.getTagIds());
result.add(new ExperimentPermId(experiment.getPermId()));
context.popContextDescription();
}
return result; return result;
} catch (DataAccessException e)
{
DataAccessExceptionTranslator.throwException(e, "Experiment", EntityKind.EXPERIMENT);
return null;
}
} }
private ExperimentPE createExperimentPE(IOperationContext context, ExperimentCreation experimentCreation) private ExperimentPE createExperimentPE(IOperationContext context, ExperimentCreation experimentCreation)
......
...@@ -25,7 +25,9 @@ import java.util.LinkedList; ...@@ -25,7 +25,9 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import ch.ethz.sis.openbis.generic.server.api.v3.executor.IOperationContext; import ch.ethz.sis.openbis.generic.server.api.v3.executor.IOperationContext;
...@@ -33,11 +35,17 @@ import ch.ethz.sis.openbis.generic.server.api.v3.executor.attachment.ICreateAtta ...@@ -33,11 +35,17 @@ import ch.ethz.sis.openbis.generic.server.api.v3.executor.attachment.ICreateAtta
import ch.ethz.sis.openbis.generic.server.api.v3.executor.property.IUpdateEntityPropertyExecutor; import ch.ethz.sis.openbis.generic.server.api.v3.executor.property.IUpdateEntityPropertyExecutor;
import ch.ethz.sis.openbis.generic.server.api.v3.executor.tag.IAddTagToEntityExecutor; import ch.ethz.sis.openbis.generic.server.api.v3.executor.tag.IAddTagToEntityExecutor;
import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.sample.SampleCreation; import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.sample.SampleCreation;
import ch.ethz.sis.openbis.generic.shared.api.v3.dto.id.sample.SampleIdentifier;
import ch.ethz.sis.openbis.generic.shared.api.v3.dto.id.sample.SamplePermId; import ch.ethz.sis.openbis.generic.shared.api.v3.dto.id.sample.SamplePermId;
import ch.ethz.sis.openbis.generic.shared.api.v3.exceptions.UnauthorizedObjectAccessException;
import ch.systemsx.cisd.common.exceptions.UserFailureException; import ch.systemsx.cisd.common.exceptions.UserFailureException;
import ch.systemsx.cisd.openbis.generic.server.authorization.validator.SampleByIdentiferValidator;
import ch.systemsx.cisd.openbis.generic.server.business.bo.DataAccessExceptionTranslator;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory; import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory;
import ch.systemsx.cisd.openbis.generic.shared.dto.PersonPE; import ch.systemsx.cisd.openbis.generic.shared.dto.PersonPE;
import ch.systemsx.cisd.openbis.generic.shared.dto.SamplePE; import ch.systemsx.cisd.openbis.generic.shared.dto.SamplePE;
import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.SampleIdentifierFactory;
import ch.systemsx.cisd.openbis.generic.shared.dto.properties.EntityKind;
import ch.systemsx.cisd.openbis.generic.shared.util.RelationshipUtils; import ch.systemsx.cisd.openbis.generic.shared.util.RelationshipUtils;
/** /**
...@@ -99,38 +107,44 @@ public class CreateSampleExecutor implements ICreateSampleExecutor ...@@ -99,38 +107,44 @@ public class CreateSampleExecutor implements ICreateSampleExecutor
@Override @Override
public List<SamplePermId> create(IOperationContext context, List<SampleCreation> creations) public List<SamplePermId> create(IOperationContext context, List<SampleCreation> creations)
{ {
List<SamplePermId> permIdsAll = new LinkedList<SamplePermId>(); try
Map<SampleCreation, SamplePE> samplesAll = new LinkedHashMap<SampleCreation, SamplePE>();
int batchSize = 1000;
for (int batchStart = 0; batchStart < creations.size(); batchStart += batchSize)
{ {
List<SampleCreation> creationsBatch = creations.subList(batchStart, Math.min(batchStart + batchSize, creations.size())); List<SamplePermId> permIdsAll = new LinkedList<SamplePermId>();
createSamples(context, creationsBatch, permIdsAll, samplesAll); Map<SampleCreation, SamplePE> samplesAll = new LinkedHashMap<SampleCreation, SamplePE>();
for (SampleCreation creation : creationsBatch) int batchSize = 1000;
for (int batchStart = 0; batchStart < creations.size(); batchStart += batchSize)
{ {
SamplePE sample = samplesAll.get(creation); List<SampleCreation> creationsBatch = creations.subList(batchStart, Math.min(batchStart + batchSize, creations.size()));
createAttachmentExecutor.create(context, sample, creation.getAttachments()); createSamples(context, creationsBatch, permIdsAll, samplesAll);
addTagToEntityExecutor.add(context, sample, creation.getTagIds());
for (SampleCreation creation : creationsBatch)
{
SamplePE sample = samplesAll.get(creation);
createAttachmentExecutor.create(context, sample, creation.getAttachments());
addTagToEntityExecutor.add(context, sample, creation.getTagIds());
}
daoFactory.getSessionFactory().getCurrentSession().flush();
daoFactory.getSessionFactory().getCurrentSession().clear();
} }
reloadSamples(samplesAll);
setSampleRelatedSamplesExecutor.set(context, samplesAll);
verifySampleExecutor.verify(context, samplesAll.values());
daoFactory.getSessionFactory().getCurrentSession().flush(); daoFactory.getSessionFactory().getCurrentSession().flush();
daoFactory.getSessionFactory().getCurrentSession().clear(); daoFactory.getSessionFactory().getCurrentSession().clear();
return permIdsAll;
} catch (DataAccessException e)
{
DataAccessExceptionTranslator.throwException(e, "Sample", EntityKind.SAMPLE);
return null;
} }
reloadSamples(samplesAll);
setSampleRelatedSamplesExecutor.set(context, samplesAll);
verifySampleExecutor.verify(context, samplesAll.values());
daoFactory.getSessionFactory().getCurrentSession().flush();
daoFactory.getSessionFactory().getCurrentSession().clear();
return permIdsAll;
} }
private void createSamples(IOperationContext context, List<SampleCreation> creationsBatch, private void createSamples(IOperationContext context, List<SampleCreation> creationsBatch,
List<SamplePermId> permIdsAll, Map<SampleCreation, SamplePE> samplesAll) List<SamplePermId> permIdsAll, Map<SampleCreation, SamplePE> samplesAll)
{ {
Map<SampleCreation, SamplePE> batchMap = new LinkedHashMap<SampleCreation, SamplePE>(); Map<SampleCreation, SamplePE> batchMap = new LinkedHashMap<SampleCreation, SamplePE>();
...@@ -157,6 +171,12 @@ public class CreateSampleExecutor implements ICreateSampleExecutor ...@@ -157,6 +171,12 @@ public class CreateSampleExecutor implements ICreateSampleExecutor
for (Map.Entry<SampleCreation, SamplePE> batchEntry : batchMap.entrySet()) for (Map.Entry<SampleCreation, SamplePE> batchEntry : batchMap.entrySet())
{ {
SamplePE sample = batchEntry.getValue(); SamplePE sample = batchEntry.getValue();
if (false == new SampleByIdentiferValidator().doValidation(context.getSession().tryGetPerson(), sample))
{
throw new UnauthorizedObjectAccessException(new SampleIdentifier(sample.getIdentifier()));
}
Map<String, String> properties = batchEntry.getKey().getProperties(); Map<String, String> properties = batchEntry.getKey().getProperties();
updateEntityPropertyExecutor.update(context, sample, sample.getEntityType(), properties); updateEntityPropertyExecutor.update(context, sample, sample.getEntityType(), properties);
} }
...@@ -168,11 +188,13 @@ public class CreateSampleExecutor implements ICreateSampleExecutor ...@@ -168,11 +188,13 @@ public class CreateSampleExecutor implements ICreateSampleExecutor
private SamplePE createSamplePE(IOperationContext context, SampleCreation sampleCreation) private SamplePE createSamplePE(IOperationContext context, SampleCreation sampleCreation)
{ {
if (sampleCreation.getCode() == null) if (StringUtils.isEmpty(sampleCreation.getCode()))
{ {
throw new UserFailureException("No code for sample provided"); throw new UserFailureException("Code cannot be empty.");
} }
SampleIdentifierFactory.assertValidCode(sampleCreation.getCode());
SamplePE sample = new SamplePE(); SamplePE sample = new SamplePE();
sample.setCode(sampleCreation.getCode()); sample.setCode(sampleCreation.getCode());
String createdPermId = daoFactory.getPermIdDAO().createPermId(); String createdPermId = daoFactory.getPermIdDAO().createPermId();
......
...@@ -68,7 +68,7 @@ public class SetSampleTypeExecutor implements ISetSampleTypeExecutor ...@@ -68,7 +68,7 @@ public class SetSampleTypeExecutor implements ISetSampleTypeExecutor
if (creation.getTypeId() == null) if (creation.getTypeId() == null)
{ {
throw new UserFailureException("No type for sample provided"); throw new UserFailureException("Type id cannot be null.");
} else } else
{ {
EntityTypePE type = typeMap.get(creation.getTypeId()); EntityTypePE type = typeMap.get(creation.getTypeId());
......
...@@ -74,6 +74,28 @@ public class CreateExperimentTest extends AbstractExperimentTest ...@@ -74,6 +74,28 @@ public class CreateExperimentTest extends AbstractExperimentTest
}, "Code cannot be empty", ExperimentContextDescription.creating(experiment)); }, "Code cannot be empty", ExperimentContextDescription.creating(experiment));
} }
@Test
public void testCreateExperimentWithExistingCode()
{
final String sessionToken = v3api.login(TEST_USER, PASSWORD);
final ExperimentCreation experiment = new ExperimentCreation();
experiment.setCode("EXPERIMENT_WITH_EXISTING_CODE");
experiment.setTypeId(new EntityTypePermId("SIRNA_HCS"));
experiment.setProjectId(new ProjectIdentifier("/CISD/NEMO"));
v3api.createExperiments(sessionToken, Arrays.asList(experiment));
assertUserFailureException(new IDelegatedAction()
{
@Override
public void execute()
{
v3api.createExperiments(sessionToken, Arrays.asList(experiment));
}
}, "Experiment already exists in the database and needs to be unique", ExperimentContextDescription.creating(experiment));
}
@Test @Test
public void testCreateExperimentWithIncorrectCode() public void testCreateExperimentWithIncorrectCode()
{ {
...@@ -307,7 +329,7 @@ public class CreateExperimentTest extends AbstractExperimentTest ...@@ -307,7 +329,7 @@ public class CreateExperimentTest extends AbstractExperimentTest
} }
@Test @Test
public void testCreateExperimentWithIncorrectPropertyValue() public void testCreateExperimentWithIncorrectDatePropertyValue()
{ {
final String sessionToken = v3api.login(TEST_USER, PASSWORD); final String sessionToken = v3api.login(TEST_USER, PASSWORD);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment