diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/plugin/generic/EntityVerificationTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/plugin/generic/EntityVerificationTest.java new file mode 100644 index 0000000000000000000000000000000000000000..fae68344c726a3470b51cb70cb78efea4461cffc --- /dev/null +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/plugin/generic/EntityVerificationTest.java @@ -0,0 +1,146 @@ +/* + * Copyright 2012 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.openbis.systemtest.plugin.generic; + +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertTrue; +import static org.testng.AssertJUnit.fail; + +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.List; + +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import ch.systemsx.cisd.openbis.generic.shared.basic.TechId; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DeletionType; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.IEntityProperty; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ListSampleCriteria; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.NewAttachment; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.NewSample; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Sample; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.SampleType; +import ch.systemsx.cisd.openbis.generic.shared.dto.SampleUpdatesDTO; +import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.ExperimentIdentifier; +import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.SampleIdentifier; + +/** + * Tests that the entity verification scripts are called when creating or updating the entities + * + * @author Jakub Straszewski + */ +// NOTE: we depend on transaction beeing committed as part of this test. +@Transactional(propagation = Propagation.NOT_SUPPORTED) +public class EntityVerificationTest extends GenericSystemTestCase +{ + // create sample of a type, that has attached script that forbids creation + + private static final String IMPOSSIBLE_TYPE = "IMPOSSIBLE"; + + private static final String IMPOSSIBLE_TO_UPDATE_TYPE = "IMPOSSIBLE_TO_UPDATE"; + + private void registerNewSample(String identifier, String type) + { + final NewSample newSample = new NewSample(); + newSample.setIdentifier(identifier); + final SampleType sampleType = new SampleType(); + sampleType.setCode(type); + newSample.setSampleType(sampleType); + genericClientService.registerSample(systemSessionToken, newSample); + } + + @BeforeMethod + public void setUp() + { + logIntoCommonClientService(); + } + + private SampleType getSampleType(String sampleTypeCode) + { + List<SampleType> sampleTypes = commonClientService.listSampleTypes(); + for (SampleType sampleType : sampleTypes) + { + if (sampleType.getCode().equals(sampleTypeCode)) + { + return sampleType; + } + } + fail("No sample type found with code " + sampleTypeCode); + return null; // satisfy compiler + } + + @Test + public void testRegisterImpossible() + { + try + { + registerNewSample("/CISD/EVT1", IMPOSSIBLE_TYPE); + fail("Registering of sample with impossible type should fail"); + } catch (Exception ufe) + { + assertTrue(ufe.getMessage().contains("Validation of sample")); + } + } + + @Test + public void testRegisterImpossibleToUpdate() + { + registerNewSample("/CISD/EVT1", IMPOSSIBLE_TO_UPDATE_TYPE); + + ListSampleCriteria listCriteria = new ListSampleCriteria(); + listCriteria.setIncludeSpace(true); + listCriteria.setSpaceCode("CISD"); + listCriteria.setSampleType(getSampleType(IMPOSSIBLE_TO_UPDATE_TYPE)); + + List<Sample> samples = etlService.listSamples(systemSessionToken, listCriteria); + + assertEquals("one sample should be registered", 1, samples.size()); + + Sample sample = samples.get(0); + + String[] modifiedParentCodesOrNull = new String[] + { "DYNA-TEST-1" }; + String containerIdentifierOrNull = null; + SampleIdentifier sampleIdentifier = SampleIdentifier.create("CISD", "EVT1"); + Date version = sample.getModificationDate(); + ExperimentIdentifier experimentIdentifierOrNull = null; + TechId sampleId = new TechId(sample.getId()); + List<IEntityProperty> properties = Collections.emptyList(); + Collection<NewAttachment> attachments = Collections.emptyList(); + SampleUpdatesDTO update = + new SampleUpdatesDTO(sampleId, properties, experimentIdentifierOrNull, attachments, + version, sampleIdentifier, containerIdentifierOrNull, + modifiedParentCodesOrNull); + + try + { + etlService.updateSample(systemSessionToken, update); + fail("update of sample with impossible to update type should fail"); + } catch (Exception ufe) + { + assertTrue(ufe.getMessage().contains("Validation of sample")); + } + + // cleanup + commonServer.deleteSamples(systemSessionToken, Collections.singletonList(sampleId), "Yup", + DeletionType.PERMANENT); + } +} diff --git a/openbis/sourceTest/sql/postgresql/119/055=sample_types.tsv b/openbis/sourceTest/sql/postgresql/119/055=sample_types.tsv index 3ab96788278dab948809ae8b9b74ce1f686c4f96..35478d15bc111d7d7f2913ab17363bcd24c137bd 100644 --- a/openbis/sourceTest/sql/postgresql/119/055=sample_types.tsv +++ b/openbis/sourceTest/sql/postgresql/119/055=sample_types.tsv @@ -1,7 +1,9 @@ 1 MASTER_PLATE Master Plate 1 t 0 0 2009-03-23 15:34:44.462776+01 f S f f \N f 2 DILUTION_PLATE Dilution Plate 1 t 1 0 2009-03-23 15:34:44.462776+01 f S f f \N f 5 CONTROL_LAYOUT Control layout 1 t 0 0 2009-03-23 15:34:44.462776+01 f S f f \N f -6 WELL Plate Well 1 f 0 1 2009-03-23 15:34:44.462776+01 f S f f \N f -3 CELL_PLATE Cell Plate 1 t 1 0 2009-03-23 15:34:44.462776+01 f S f f \N f -4 REINFECT_PLATE Re-infection Plate 1 t 1 0 2009-03-23 15:34:44.462776+01 f S f f \N f -7 DYNAMIC_PLATE Dynamic Plate 1 t 1 0 2009-03-23 15:34:44.462776+01 f S f f \N f +6 WELL Plate Well 1 f 0 1 2009-03-23 15:34:44.462776+01 f S f f 5 f +3 CELL_PLATE Cell Plate 1 t 1 0 2009-03-23 15:34:44.462776+01 f S f f 5 f +4 REINFECT_PLATE Re-infection Plate 1 t 1 0 2009-03-23 15:34:44.462776+01 f S f f 5 f +7 DYNAMIC_PLATE Dynamic Plate 1 t 1 0 2009-03-23 15:34:44.462776+01 f S f f 5 f +8 IMPOSSIBLE Impossible to create 1 t 1 0 2009-03-23 15:34:44.462776+01 f S f f 6 f +9 IMPOSSIBLE_TO_UPDATE Impossible to update 1 t 1 0 2009-03-23 15:34:44.462776+01 f S f f 7 f \ No newline at end of file diff --git a/openbis/sourceTest/sql/postgresql/119/057=scripts.tsv b/openbis/sourceTest/sql/postgresql/119/057=scripts.tsv index 6e104ddda42532d44839399b246748d2d33928f2..8eb289b702bcb5642bc68013e2e97f81beb38d1a 100644 --- a/openbis/sourceTest/sql/postgresql/119/057=scripts.tsv +++ b/openbis/sourceTest/sql/postgresql/119/057=scripts.tsv @@ -2,3 +2,6 @@ 2 1 code \N entity.code() 2010-10-27 15:16:48.994831+02 2 \N DYNAMIC_PROPERTY 3 1 date \N str(currentDate().getTime()) 2010-10-27 15:16:48.994831+02 2 \N DYNAMIC_PROPERTY 4 1 managed list \N fake script 2010-10-27 15:16:48.994831+02 2 \N MANAGED_PROPERTY +5 1 validateOK \N def validate(entity, isNew):\n pass\n 2010-10-27 15:16:48.994831+02 2 \N ENTITY_VALIDATION +6 1 validateFAIL \N def validate(entity, isNew):\n return "This check always fail"\n 2010-10-27 15:16:48.994831+02 2 \N ENTITY_VALIDATION +7 1 validateUpdateFAIL \N def validate(entity, isNew):\n if (not isNew):\n return "Cannot update this entity"\n 2010-10-27 15:16:48.994831+02 2 \N ENTITY_VALIDATION \ No newline at end of file