From b500fa9ce69b575b83e7b1514aeb23b5895ba610 Mon Sep 17 00:00:00 2001 From: cramakri <cramakri> Date: Wed, 15 Sep 2010 11:35:15 +0000 Subject: [PATCH] LMS-1746 Checking in again (last time didn't take). SVN: 17841 --- .../api/v1/GeneralInformationService.java | 20 +++ .../v1/GeneralInformationServiceLogger.java | 7 + ...pleToDataSetRelatedEntitiesTranslator.java | 101 ++++++++++++++ .../generic/server/api/v1/Translator.java | 14 ++ .../api/v1/IGeneralInformationService.java | 10 ++ .../generic/shared/api/v1/dto/DataSet.java | 114 +++++++++++++++ .../generic/shared/api/v1/dto/Sample.java | 132 ++++++++++++++++-- .../api/v1/GeneralInformationServiceTest.java | 82 ++++++++++- ...oDataSetRelatedEntitiesTranslatorTest.java | 103 ++++++++++++++ .../generic/shared/api/v1/dto/SampleTest.java | 47 ++++++- 10 files changed, 617 insertions(+), 13 deletions(-) create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/generic/server/api/v1/SampleToDataSetRelatedEntitiesTranslator.java create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/DataSet.java create mode 100644 openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/api/v1/SampleToDataSetRelatedEntitiesTranslatorTest.java diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/api/v1/GeneralInformationService.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/api/v1/GeneralInformationService.java index 25028d513e7..e68a302b864 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/api/v1/GeneralInformationService.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/api/v1/GeneralInformationService.java @@ -36,12 +36,15 @@ import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory; import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDatabaseInstanceDAO; import ch.systemsx.cisd.openbis.generic.shared.ICommonServer; import ch.systemsx.cisd.openbis.generic.shared.api.v1.IGeneralInformationService; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataSet; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Project; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Role; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Sample; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.SearchCriteria; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.SpaceWithProjectsAndRoleAssignments; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataSetRelatedEntities; import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DetailedSearchCriteria; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ExternalData; import ch.systemsx.cisd.openbis.generic.shared.basic.dto.RoleWithHierarchy; import ch.systemsx.cisd.openbis.generic.shared.dto.AuthorizationGroupPE; import ch.systemsx.cisd.openbis.generic.shared.dto.DatabaseInstancePE; @@ -224,4 +227,21 @@ public class GeneralInformationService extends AbstractServer<IGeneralInformatio return samples; } + + public List<DataSet> listDataSets(String sessionToken, List<Sample> samples) + { + checkSession(sessionToken); + List<ch.systemsx.cisd.openbis.generic.shared.basic.dto.SampleType> sampleTypes = + commonServer.listSampleTypes(sessionToken); + SampleToDataSetRelatedEntitiesTranslator translator = + new SampleToDataSetRelatedEntitiesTranslator(sampleTypes, samples); + DataSetRelatedEntities dsre = translator.convertToDataSetRelatedEntities(); + List<ExternalData> externalData = commonServer.listRelatedDataSets(sessionToken, dsre); + ArrayList<DataSet> dataSets = new ArrayList<DataSet>(externalData.size()); + for (ExternalData externalDatum : externalData) + { + dataSets.add(Translator.translate(externalDatum)); + } + return dataSets; + } } diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/api/v1/GeneralInformationServiceLogger.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/api/v1/GeneralInformationServiceLogger.java index 30389894a13..2582501e1ac 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/api/v1/GeneralInformationServiceLogger.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/api/v1/GeneralInformationServiceLogger.java @@ -24,6 +24,7 @@ import ch.systemsx.cisd.authentication.ISessionManager; import ch.systemsx.cisd.common.spring.IInvocationLoggerContext; import ch.systemsx.cisd.openbis.generic.server.AbstractServerLogger; import ch.systemsx.cisd.openbis.generic.shared.api.v1.IGeneralInformationService; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataSet; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Role; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Sample; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.SearchCriteria; @@ -76,4 +77,10 @@ class GeneralInformationServiceLogger extends AbstractServerLogger implements return null; } + public List<DataSet> listDataSets(String sessionToken, List<Sample> samples) + { + logAccess(sessionToken, "list-data-sets", "SAMPLES(%s)", samples); + return null; + } + } diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/api/v1/SampleToDataSetRelatedEntitiesTranslator.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/api/v1/SampleToDataSetRelatedEntitiesTranslator.java new file mode 100644 index 00000000000..bbe5246400e --- /dev/null +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/api/v1/SampleToDataSetRelatedEntitiesTranslator.java @@ -0,0 +1,101 @@ +/* + * Copyright 2010 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.generic.server.api.v1; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Sample; +import ch.systemsx.cisd.openbis.generic.shared.basic.BasicEntityInformationHolder; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataSetRelatedEntities; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.EntityKind; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.EntityType; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.SampleType; + +/** + * A class that converts {@link Sample} objects to {@link DataSetRelatedEntities} objects. + * + * @author Chandrasekhar Ramakrishnan + */ +class SampleToDataSetRelatedEntitiesTranslator +{ + // A map from sample type Id to sample type. + private final HashMap<Long, SampleType> sampleTypesMap; + + private final List<Sample> samples; + + private final ArrayList<BasicEntityInformationHolder> entityInformationHolders; + + /** + * Creates a translator from public {@Sample} objects to the internal + * {@link DataSetRelatedEntities} objects. + * <p> + * A list of sample types known to the DB must be provided because Sample knows only the code of + * the SampleType. + * + * @param sampleTypes A list of SampleTypes known to the DB. + * @param samples The samples to convert. + */ + public SampleToDataSetRelatedEntitiesTranslator(List<SampleType> sampleTypes, + List<Sample> samples) + { + this.sampleTypesMap = convertSampleTypesListToMap(sampleTypes); + this.samples = samples; + entityInformationHolders = new ArrayList<BasicEntityInformationHolder>(samples.size()); + } + + private static HashMap<Long, SampleType> convertSampleTypesListToMap( + List<SampleType> sampleTypes) + { + HashMap<Long, SampleType> map = new HashMap<Long, SampleType>(sampleTypes.size()); + + for (SampleType sampleType : sampleTypes) + { + map.put(sampleType.getId(), sampleType); + } + + return map; + } + + public DataSetRelatedEntities convertToDataSetRelatedEntities() + { + for (Sample sample : samples) + { + BasicEntityInformationHolder holderOrNull = + tryConvertSampleToEntityInformationHolder(sample); + if (null != holderOrNull) + { + entityInformationHolders.add(holderOrNull); + } + } + return new DataSetRelatedEntities(entityInformationHolders); + } + + private BasicEntityInformationHolder tryConvertSampleToEntityInformationHolder(Sample sample) + { + EntityType entityType = sampleTypesMap.get(sample.getSampleTypeId()); + if (null == entityType) + { + return null; + } + BasicEntityInformationHolder holder = + new BasicEntityInformationHolder(EntityKind.SAMPLE, entityType, sample.getCode(), + sample.getId()); + return holder; + } +} diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/api/v1/Translator.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/api/v1/Translator.java index cbf4d4e77a4..101a4070368 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/api/v1/Translator.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/api/v1/Translator.java @@ -16,9 +16,12 @@ package ch.systemsx.cisd.openbis.generic.server.api.v1; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataSet; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Role; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Sample; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataSet.DataSetInitializer; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Sample.SampleInitializer; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ExternalData; import ch.systemsx.cisd.openbis.generic.shared.basic.dto.RoleWithHierarchy.RoleCode; import ch.systemsx.cisd.openbis.generic.shared.basic.dto.RoleWithHierarchy.RoleLevel; @@ -40,11 +43,22 @@ class Translator static Sample translate(ch.systemsx.cisd.openbis.generic.shared.basic.dto.Sample privateSample) { SampleInitializer initializer = new SampleInitializer(); + initializer.setId(privateSample.getId()); + initializer.setCode(privateSample.getCode()); initializer.setIdentifier(privateSample.getIdentifier()); + initializer.setSampleTypeId(privateSample.getSampleType().getId()); + initializer.setSampleTypeCode(privateSample.getSampleType().getCode()); return new Sample(initializer); } private Translator() { } + + public static DataSet translate(ExternalData externalDatum) + { + DataSetInitializer initializer = new DataSetInitializer(); + initializer.setCode(externalDatum.getCode()); + return new DataSet(initializer); + } } diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/IGeneralInformationService.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/IGeneralInformationService.java index 15ce9594e5c..adf5228c035 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/IGeneralInformationService.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/IGeneralInformationService.java @@ -23,6 +23,7 @@ import java.util.Set; import org.springframework.transaction.annotation.Transactional; import ch.systemsx.cisd.common.api.IRpcService; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataSet; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Role; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Sample; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.SearchCriteria; @@ -90,4 +91,13 @@ public interface IGeneralInformationService extends IRpcService @RolesAllowed(RoleWithHierarchy.INSTANCE_OBSERVER) public List<Sample> searchForSamples(String sessionToken, SearchCriteria searchCriteria); + /** + * Return all data sets attached to the given samples. + * + * @param samples The samples for which we return attached data sets. + */ + @Transactional(readOnly = true) + @RolesAllowed(RoleWithHierarchy.INSTANCE_OBSERVER) + public List<DataSet> listDataSets(String sessionToken, List<Sample> samples); + } diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/DataSet.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/DataSet.java new file mode 100644 index 00000000000..06dbb657c1a --- /dev/null +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/DataSet.java @@ -0,0 +1,114 @@ +/* + * Copyright 2010 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.generic.shared.api.v1.dto; + +import java.io.Serializable; + +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; +import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; + +/** + * Immutable value object representing a data set. + * + * @author Chandrasekhar Ramakrishnan + */ +public final class DataSet implements Serializable +{ + private static final long serialVersionUID = 1L; + + private final String code; + + /** + * Class used to initialize a new data set instance. Necessary since all the fields of a DataSet + * are final. + * + * @author Chandrasekhar Ramakrishnan + */ + public static final class DataSetInitializer + { + private String code; + + public String getCode() + { + return code; + } + + public void setCode(String code) + { + this.code = code; + } + } + + /** + * Creates a new instance with the provided initializer + * + * @throws IllegalArgumentException if some of the required information is not provided. + */ + public DataSet(DataSetInitializer initializer) + { + String id = initializer.getCode(); + if (id == null || id.length() == 0) + { + throw new IllegalArgumentException("Unspecified code."); + } + this.code = id; + } + + /** + * Returns the sample code; + */ + public String getCode() + { + return code; + } + + @Override + public boolean equals(Object obj) + { + if (obj == this) + { + return true; + } + if (obj instanceof DataSet == false) + { + return false; + } + + EqualsBuilder builder = new EqualsBuilder(); + DataSet other = (DataSet) obj; + builder.append(getCode(), other.getCode()); + return builder.isEquals(); + } + + @Override + public int hashCode() + { + HashCodeBuilder builder = new HashCodeBuilder(); + builder.append(getCode()); + return builder.toHashCode(); + } + + @Override + public String toString() + { + ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE); + builder.append(getCode()); + return builder.toString(); + } +} diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/Sample.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/Sample.java index 87d6444f0f3..dfc8bd8d3ec 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/Sample.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/Sample.java @@ -32,18 +32,47 @@ public final class Sample implements Serializable { private static final long serialVersionUID = 1L; - private final String identifier; - /** * Class used to initialize a new sample instance. Necessary since all the fields of a sample * are final. + * <p> + * All of the properties must be filled (non-null) before being used to initialize a Sample, + * otherwise the Sample constructor will throw an exception. * * @author Chandrasekhar Ramakrishnan */ public static final class SampleInitializer { + private Long id; + + private String code; + private String identifier; + private Long sampleTypeId; + + private String sampleTypeCode; + + public void setId(Long id) + { + this.id = id; + } + + public Long getId() + { + return id; + } + + public void setCode(String code) + { + this.code = code; + } + + public String getCode() + { + return code; + } + public String getIdentifier() { return identifier; @@ -53,8 +82,38 @@ public final class Sample implements Serializable { this.identifier = identifier; } + + public void setSampleTypeId(Long sampleTypeId) + { + this.sampleTypeId = sampleTypeId; + } + + public Long getSampleTypeId() + { + return sampleTypeId; + } + + public void setSampleTypeCode(String sampleTypeCode) + { + this.sampleTypeCode = sampleTypeCode; + } + + public String getSampleTypeCode() + { + return sampleTypeCode; + } } + private final Long id; + + private final String code; + + private final String identifier; + + private final Long sampleTypeId; + + private final String sampleTypeCode; + /** * Creates a new instance with the provided initializer * @@ -62,12 +121,52 @@ public final class Sample implements Serializable */ public Sample(SampleInitializer initializer) { - String id = initializer.getIdentifier(); - if (id == null || id.length() == 0) + checkValidLong(initializer.getId(), "Unspecified id."); + this.id = initializer.getId(); + + checkValidString(initializer.getCode(), "Unspecified code."); + this.code = initializer.getCode(); + + checkValidString(initializer.getIdentifier(), "Unspecified identifier."); + this.identifier = initializer.getIdentifier(); + + checkValidLong(initializer.getSampleTypeId(), "Unspecified sample type id."); + this.sampleTypeId = initializer.getSampleTypeId(); + + checkValidString(initializer.getSampleTypeCode(), "Unspecified sample type code."); + this.sampleTypeCode = initializer.getSampleTypeCode(); + } + + private void checkValidString(String string, String message) throws IllegalArgumentException + { + if (string == null || string.length() == 0) { - throw new IllegalArgumentException("Unspecified identifier."); + throw new IllegalArgumentException(message); } - this.identifier = id; + } + + private void checkValidLong(Long longValue, String message) throws IllegalArgumentException + { + if (longValue == null || longValue == 0) + { + throw new IllegalArgumentException(message); + } + } + + /** + * Returns the sample id. + */ + public Long getId() + { + return id; + } + + /** + * Returns the sample code. + */ + public String getCode() + { + return code; } /** @@ -78,6 +177,22 @@ public final class Sample implements Serializable return identifier; } + /** + * Returns the sample type id. + */ + public Long getSampleTypeId() + { + return sampleTypeId; + } + + /** + * Returns the sample type code. + */ + public String getSampleTypeCode() + { + return sampleTypeCode; + } + @Override public boolean equals(Object obj) { @@ -92,7 +207,7 @@ public final class Sample implements Serializable EqualsBuilder builder = new EqualsBuilder(); Sample other = (Sample) obj; - builder.append(getIdentifier(), other.getIdentifier()); + builder.append(getId(), other.getId()); return builder.isEquals(); } @@ -100,7 +215,7 @@ public final class Sample implements Serializable public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); - builder.append(getIdentifier()); + builder.append(getId()); return builder.toHashCode(); } @@ -109,6 +224,7 @@ public final class Sample implements Serializable { ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE); builder.append(getIdentifier()); + builder.append(getSampleTypeCode()); return builder.toString(); } } diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/api/v1/GeneralInformationServiceTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/api/v1/GeneralInformationServiceTest.java index f78db9631c3..1fd96312e35 100644 --- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/api/v1/GeneralInformationServiceTest.java +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/api/v1/GeneralInformationServiceTest.java @@ -32,14 +32,18 @@ import org.testng.annotations.Test; import ch.rinn.restrictions.Friend; import ch.systemsx.cisd.openbis.generic.shared.AbstractServerTestCase; import ch.systemsx.cisd.openbis.generic.shared.ICommonServer; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataSet; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Project; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Role; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Sample; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.SearchCriteria; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.SpaceWithProjectsAndRoleAssignments; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Sample.SampleInitializer; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.SearchCriteria.MatchClause; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.SearchCriteria.MatchClauseAttribute; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataSetRelatedEntities; import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DetailedSearchCriteria; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.SampleType; import ch.systemsx.cisd.openbis.generic.shared.basic.dto.RoleWithHierarchy.RoleCode; import ch.systemsx.cisd.openbis.generic.shared.dto.GroupPE; import ch.systemsx.cisd.openbis.generic.shared.dto.PersonPE; @@ -180,6 +184,16 @@ public class GeneralInformationServiceTest extends AbstractServerTestCase public void testSearchForSamples() { prepareGetSession(); + prepareSearchForSamples(); + List<Sample> result = service.searchForSamples(SESSION_TOKEN, createSearchCriteria()); + assertEquals(1, result.size()); + Sample resultSample = result.get(0); + assertEquals("/space/code", resultSample.getIdentifier()); + context.assertIsSatisfied(); + } + + private void prepareSearchForSamples() + { context.checking(new Expectations() { { @@ -187,14 +201,76 @@ public class GeneralInformationServiceTest extends AbstractServerTestCase with(any(DetailedSearchCriteria.class))); ch.systemsx.cisd.openbis.generic.shared.basic.dto.Sample returnSample = new ch.systemsx.cisd.openbis.generic.shared.basic.dto.Sample(); + SampleType returnSampleType = new SampleType(); + returnSample.setId(new Long(1)); + returnSample.setCode("code"); returnSample.setIdentifier("/space/code"); + returnSampleType.setId(new Long(1)); + returnSampleType.setCode("sample-type"); + returnSample.setSampleType(returnSampleType); will(returnValue(Collections.singletonList(returnSample))); } }); - List<Sample> result = service.searchForSamples(SESSION_TOKEN, createSearchCriteria()); + } + + @Test + public void testListDataSets() + { + prepareGetSession(); + context.checking(new Expectations() + { + { + one(commonServer).listSampleTypes(SESSION_TOKEN); + SampleType returnSampleType = new SampleType(); + returnSampleType.setId(new Long(1)); + returnSampleType.setCode("sample-type"); + will(returnValue(Collections.singletonList(returnSampleType))); + + one(commonServer).listRelatedDataSets(with(SESSION_TOKEN), + with(any(DataSetRelatedEntities.class))); + ch.systemsx.cisd.openbis.generic.shared.basic.dto.ExternalData returnData = + new ch.systemsx.cisd.openbis.generic.shared.basic.dto.ExternalData(); + returnData.setCode("ds-code"); + will(returnValue(Collections.singletonList(returnData))); + } + }); + + SampleInitializer initializer = new SampleInitializer(); + initializer.setId(new Long(1)); + initializer.setCode("code"); + initializer.setIdentifier("/space/code"); + initializer.setSampleTypeId(new Long(1)); + initializer.setSampleTypeCode("sample-type"); + Sample owner = new Sample(initializer); + List<DataSet> result = + service.listDataSets(SESSION_TOKEN, Collections.singletonList(owner)); assertEquals(1, result.size()); - Sample resultSample = result.get(0); - assertEquals("/space/code", resultSample.getIdentifier()); + DataSet resultDataSet = result.get(0); + assertEquals("ds-code", resultDataSet.getCode()); + context.assertIsSatisfied(); + } + + @Test + public void testListDataSetsWithEmptySampleList() + { + prepareGetSession(); + context.checking(new Expectations() + { + { + one(commonServer).listSampleTypes(SESSION_TOKEN); + SampleType returnSampleType = new SampleType(); + returnSampleType.setId(new Long(1)); + returnSampleType.setCode("sample-type"); + will(returnValue(Collections.singletonList(returnSampleType))); + + one(commonServer).listRelatedDataSets(with(SESSION_TOKEN), + with(any(DataSetRelatedEntities.class))); + will(returnValue(new ArrayList<ch.systemsx.cisd.openbis.generic.shared.basic.dto.ExternalData>())); + } + }); + ArrayList<Sample> samples = new ArrayList<Sample>(); + List<DataSet> result = service.listDataSets(SESSION_TOKEN, samples); + assertEquals(0, result.size()); context.assertIsSatisfied(); } diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/api/v1/SampleToDataSetRelatedEntitiesTranslatorTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/api/v1/SampleToDataSetRelatedEntitiesTranslatorTest.java new file mode 100644 index 00000000000..8be49521105 --- /dev/null +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/api/v1/SampleToDataSetRelatedEntitiesTranslatorTest.java @@ -0,0 +1,103 @@ +/* +; * Copyright 2010 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.generic.server.api.v1; + +import java.util.ArrayList; +import java.util.List; + +import org.testng.AssertJUnit; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Sample; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Sample.SampleInitializer; +import ch.systemsx.cisd.openbis.generic.shared.basic.IEntityInformationHolder; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataSetRelatedEntities; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.SampleType; + +/** + * @author Chandrasekhar Ramakrishnan + */ +public class SampleToDataSetRelatedEntitiesTranslatorTest extends AssertJUnit +{ + private SampleToDataSetRelatedEntitiesTranslator translator; + + private static final Long SAMPLE_ID = new Long(1); + + private static final String SAMPLE_CODE = "sample-code"; + + private static final String SAMPLE_IDENTIFIER = "/space/sample-code"; + + private static final Long SAMPLE_TYPE_ID = new Long(1); + + private static final String SAMPLE_TYPE_CODE = "sample-type"; + + @BeforeMethod + public void setUp() + { + ArrayList<SampleType> sampleTypes = createSampleTypes(); + ArrayList<Sample> samples = createSamples(); + translator = new SampleToDataSetRelatedEntitiesTranslator(sampleTypes, samples); + } + + private ArrayList<Sample> createSamples() + { + ArrayList<Sample> samples = new ArrayList<Sample>(); + SampleInitializer initializer = new SampleInitializer(); + initializer.setId(SAMPLE_ID); + initializer.setCode(SAMPLE_CODE); + initializer.setIdentifier(SAMPLE_IDENTIFIER); + initializer.setSampleTypeId(SAMPLE_TYPE_ID); + initializer.setSampleTypeCode(SAMPLE_TYPE_CODE); + Sample sample = new Sample(initializer); + samples.add(sample); + return samples; + } + + private ArrayList<SampleType> createSampleTypes() + { + ArrayList<SampleType> sampleTypes = new ArrayList<SampleType>(); + SampleType sampleType = new SampleType(); + sampleType.setId(new Long(1)); + sampleType.setCode("SAMPLE_TYPE"); + sampleTypes.add(sampleType); + return sampleTypes; + } + + @Test + public void testTranslator() + { + DataSetRelatedEntities dsre = translator.convertToDataSetRelatedEntities(); + assertNotNull(dsre); + List<? extends IEntityInformationHolder> entities = dsre.getEntities(); + assertEquals(1, entities.size()); + } + + @Test + public void testTranslatorWithEmptySampleTypes() + { + ArrayList<SampleType> sampleTypes = new ArrayList<SampleType>(); + ArrayList<Sample> samples = createSamples(); + SampleToDataSetRelatedEntitiesTranslator myTranslator = + new SampleToDataSetRelatedEntitiesTranslator(sampleTypes, samples); + + DataSetRelatedEntities dsre = myTranslator.convertToDataSetRelatedEntities(); + assertNotNull(dsre); + List<? extends IEntityInformationHolder> entities = dsre.getEntities(); + assertEquals(0, entities.size()); + } +} diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/SampleTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/SampleTest.java index 978d1450aee..4676bc9eef1 100644 --- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/SampleTest.java +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/SampleTest.java @@ -27,33 +27,76 @@ import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Sample.SampleInitializ */ public class SampleTest extends AssertJUnit { + private static final Long SAMPLE_ID = new Long(1); + + private static final String SAMPLE_CODE = "sample-code"; + private static final String SAMPLE_IDENTIFIER = "/space/sample-code"; + private static final Long SAMPLE_TYPE_ID = new Long(1); + + private static final String SAMPLE_TYPE_CODE = "sample-type"; + private Sample sample; @BeforeMethod public void setUp() { SampleInitializer initializer = new SampleInitializer(); + initializer.setId(SAMPLE_ID); + initializer.setCode(SAMPLE_CODE); initializer.setIdentifier(SAMPLE_IDENTIFIER); + initializer.setSampleTypeId(SAMPLE_TYPE_ID); + initializer.setSampleTypeCode(SAMPLE_TYPE_CODE); sample = new Sample(initializer); } + @Test(expectedExceptions = + { IllegalArgumentException.class }) + public void testInitialization() + { + SampleInitializer initializer = new SampleInitializer(); + initializer.setIdentifier(SAMPLE_IDENTIFIER); + new Sample(initializer); + } + @Test public void testEquals() { SampleInitializer initializer = new SampleInitializer(); + initializer.setId(SAMPLE_ID); + initializer.setCode(SAMPLE_CODE); initializer.setIdentifier(SAMPLE_IDENTIFIER); + initializer.setSampleTypeId(SAMPLE_TYPE_ID); + initializer.setSampleTypeCode(SAMPLE_TYPE_CODE); Sample mySample = new Sample(initializer); + assertTrue("Samples with the same id should be equal.", sample.equals(mySample)); + assertEquals(sample.hashCode(), mySample.hashCode()); - assertTrue(sample.equals(mySample)); + initializer = new SampleInitializer(); + initializer.setId(SAMPLE_ID); + initializer.setCode("different-code"); + initializer.setIdentifier("/a/different-identifier"); + initializer.setSampleTypeId(new Long(2)); + initializer.setSampleTypeCode("new-code"); + mySample = new Sample(initializer); + assertTrue("Samples with the same id should be equal.", sample.equals(mySample)); assertEquals(sample.hashCode(), mySample.hashCode()); + + initializer = new SampleInitializer(); + initializer.setId(new Long(2)); + initializer.setCode(SAMPLE_CODE); + initializer.setIdentifier(SAMPLE_IDENTIFIER); + initializer.setSampleTypeId(SAMPLE_TYPE_ID); + initializer.setSampleTypeCode(SAMPLE_TYPE_CODE); + mySample = new Sample(initializer); + assertFalse("Samples with the different ids should not be equal.", sample.equals(mySample)); } @Test public void testToString() { String stringRepresentation = sample.toString(); - assertEquals("Sample[/space/sample-code]", stringRepresentation); + assertEquals("Sample[/space/sample-code,sample-type]", stringRepresentation); } } -- GitLab