diff --git a/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/server/RawDataService.java b/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/server/RawDataService.java new file mode 100644 index 0000000000000000000000000000000000000000..5eb5d23bb1eb17955d596338ba3f3f239d86acf6 --- /dev/null +++ b/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/server/RawDataService.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.plugin.phosphonetx.server; + +import java.util.ArrayList; +import java.util.List; + +import javax.annotation.Resource; + +import ch.rinn.restrictions.Private; +import ch.systemsx.cisd.authentication.ISessionManager; +import ch.systemsx.cisd.common.exceptions.UserFailureException; +import ch.systemsx.cisd.openbis.generic.server.AbstractServer; +import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory; +import ch.systemsx.cisd.openbis.generic.shared.ICommonServer; +import ch.systemsx.cisd.openbis.generic.shared.authorization.validator.GroupValidator; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Group; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ListSampleCriteria; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Sample; +import ch.systemsx.cisd.openbis.generic.shared.dto.PersonPE; +import ch.systemsx.cisd.openbis.generic.shared.dto.SampleTypePE; +import ch.systemsx.cisd.openbis.generic.shared.dto.Session; +import ch.systemsx.cisd.openbis.generic.shared.translator.SampleTypeTranslator; +import ch.systemsx.cisd.openbis.plugin.phosphonetx.shared.IRawDataService; + +/** + * Imlementation of {@link IRawDataService}. + * + * @author Franz-Josef Elmer + */ +public class RawDataService extends AbstractServer<IRawDataService> implements IRawDataService +{ + @Private static final String GROUP_CODE = "MS_DATA"; + + @Private static final String RAW_DATA_SAMPLE_TYPE = "MS_INJECTION"; + + @Resource(name = ch.systemsx.cisd.openbis.generic.shared.ResourceNames.COMMON_SERVER) + private ICommonServer commonServer; + + public RawDataService() + { + } + + public RawDataService(ISessionManager<Session> sessionManager, IDAOFactory daoFactory, + ICommonServer commonServer) + { + super(sessionManager, daoFactory); + this.commonServer = commonServer; + } + + public IRawDataService createLogger(boolean invocationSuccessful, long elapsedTime) + { + return new RawDataServiceLogger(getSessionManager(), invocationSuccessful, elapsedTime); + } + + public List<Sample> listRawDataSamples(String sessionToken, String userID) + { + checkSession(sessionToken); + + PersonPE person = getDAOFactory().getPersonDAO().tryFindPersonByUserId(userID); + if (person == null) + { + throw new UserFailureException("Unknown user ID: " + userID); + } + ListSampleCriteria criteria = new ListSampleCriteria(); + SampleTypePE sampleTypePE = + getDAOFactory().getSampleTypeDAO().tryFindSampleTypeByCode(RAW_DATA_SAMPLE_TYPE); + criteria.setSampleType(SampleTypeTranslator.translate(sampleTypePE, null)); + criteria.setIncludeGroup(true); + criteria.setGroupCode(GROUP_CODE); + List<Sample> samples = commonServer.listSamples(sessionToken, criteria); + List<Sample> filteredList = new ArrayList<Sample>(); + GroupValidator validator = new GroupValidator(); + for (Sample sample : samples) + { + Sample parent = sample.getGeneratedFrom(); + if (parent != null) + { + Group group = parent.getGroup(); + if (group == null || validator.doValidation(person, group)) + { + filteredList.add(sample); + } + } + } + return filteredList; + } + +} diff --git a/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/server/RawDataServiceLogger.java b/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/server/RawDataServiceLogger.java new file mode 100644 index 0000000000000000000000000000000000000000..04a028cec3bfef5bbd71f5a6a2c5e8bfe998e81a --- /dev/null +++ b/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/server/RawDataServiceLogger.java @@ -0,0 +1,47 @@ +/* + * 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.plugin.phosphonetx.server; + +import java.util.List; + +import ch.systemsx.cisd.authentication.ISessionManager; +import ch.systemsx.cisd.openbis.generic.server.AbstractServerLogger; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Sample; +import ch.systemsx.cisd.openbis.generic.shared.dto.Session; +import ch.systemsx.cisd.openbis.plugin.phosphonetx.shared.IRawDataService; + +/** + * + * + * @author Franz-Josef Elmer + */ +public class RawDataServiceLogger extends AbstractServerLogger implements IRawDataService +{ + + RawDataServiceLogger(ISessionManager<Session> sessionManager, + boolean invocationSuccessful, long elapsedTime) + { + super(sessionManager, invocationSuccessful, elapsedTime); + } + + public List<Sample> listRawDataSamples(String sessionToken, String userID) + { + logAccess(sessionToken, "list_raw_data_samples", "USER_ID(%s)", userID); + return null; + } + +} diff --git a/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/server/RawDataServiceServer.java b/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/server/RawDataServiceServer.java new file mode 100644 index 0000000000000000000000000000000000000000..309057e003023f5fe8bbd4fd01c43cbb8fb30b8c --- /dev/null +++ b/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/server/RawDataServiceServer.java @@ -0,0 +1,49 @@ +/* + * 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.plugin.phosphonetx.server; + +import javax.annotation.Resource; + +import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +import ch.systemsx.cisd.openbis.plugin.phosphonetx.shared.IRawDataService; +import ch.systemsx.cisd.openbis.plugin.phosphonetx.shared.ResourceNames; + + +/** + * Server wrapping {@link IRawDataService}. + * + * @author Franz-Josef Elmer + */ +@Controller +@RequestMapping( + { "/rmi-phosphonetx-raw-data", "/openbis/rmi-phosphonetx-raw-data" }) +public class RawDataServiceServer extends HttpInvokerServiceExporter +{ + @Resource(name = ResourceNames.PHOSPHONETX_RAW_DATA_SERVICE) + private IRawDataService service; + + @Override + public void afterPropertiesSet() + { + setServiceInterface(IRawDataService.class); + setService(service); + super.afterPropertiesSet(); + } +} diff --git a/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/shared/IRawDataService.java b/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/shared/IRawDataService.java new file mode 100644 index 0000000000000000000000000000000000000000..df2cc2540db8fa4dadf5427090b556e6e61a3dcb --- /dev/null +++ b/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/shared/IRawDataService.java @@ -0,0 +1,42 @@ +/* + * 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.plugin.phosphonetx.shared; + +import java.util.List; + +import org.springframework.transaction.annotation.Transactional; + +import ch.systemsx.cisd.openbis.generic.shared.IServer; +import ch.systemsx.cisd.openbis.generic.shared.authorization.annotation.RoleSet; +import ch.systemsx.cisd.openbis.generic.shared.authorization.annotation.RolesAllowed; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Sample; + +/** + * Service for querying raw data. + * + * @author Franz-Josef Elmer + */ +public interface IRawDataService extends IServer +{ + /** + * Returns all samples of type MS_INJECTION in group MS_DATA which have a parent sample the + * specified user is allow to read. + */ + @Transactional(readOnly = true) + @RolesAllowed(RoleSet.INSTANCE_ADMIN_OBSERVER) + public List<Sample> listRawDataSamples(String sessionToken, String userID); +} diff --git a/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/shared/ResourceNames.java b/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/shared/ResourceNames.java index a691b156c81abb4cf0f752648b82ed4727146977..22d6db2af853a8e9e01bf4c101ea787d44cedf91 100644 --- a/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/shared/ResourceNames.java +++ b/rtd_phosphonetx/source/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/shared/ResourceNames.java @@ -31,6 +31,8 @@ public class ResourceNames public final static String PHOSPHONETX_PLUGIN_SERVER = "phosphonetx-plugin-server"; + public final static String PHOSPHONETX_RAW_DATA_SERVICE = "phosphonetx-raw-data-service"; + public final static String PHOSPHONETX_DAO_FACTORY = "phosphonetx-dao-factory"; public final static String PHOSPHONETX_BO_FACTORY = "phosphonetx-bo-factory"; diff --git a/rtd_phosphonetx/source/java/phosphonetx-applicationContext.xml b/rtd_phosphonetx/source/java/phosphonetx-applicationContext.xml index 3af8c91f3616e68c737e3f4f7dab30daa5513c3e..75d1796c11a7069e0b1b1b0ce05ae161b8aa5dcb 100644 --- a/rtd_phosphonetx/source/java/phosphonetx-applicationContext.xml +++ b/rtd_phosphonetx/source/java/phosphonetx-applicationContext.xml @@ -39,4 +39,29 @@ <constructor-arg ref="dao-factory"/> <constructor-arg ref="phosphonetx-dao-factory"/> </bean> + + <!-- + // Raw Data Server with interceptors for translating results + --> + + <bean id="phosphonetx-raw-data-service" class="org.springframework.aop.framework.ProxyFactoryBean"> + <property name="proxyInterfaces"> + <list> + <value>ch.systemsx.cisd.openbis.plugin.phosphonetx.shared.IRawDataService</value> + </list> + </property> + <property name="target"> + <bean class="ch.systemsx.cisd.openbis.plugin.phosphonetx.server.RawDataService"> + <constructor-arg ref="session-manager" /> + <constructor-arg ref="dao-factory" /> + <constructor-arg ref="common-server" /> + </bean> + </property> + <property name="interceptorNames"> + <list> + <value>exception-translator</value> + </list> + </property> + </bean> + </beans> \ No newline at end of file diff --git a/rtd_phosphonetx/sourceTest/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/RawDataTestClient.java b/rtd_phosphonetx/sourceTest/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/RawDataTestClient.java new file mode 100644 index 0000000000000000000000000000000000000000..b96531f519a29f7277bca526c3897deec7089d6b --- /dev/null +++ b/rtd_phosphonetx/sourceTest/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/RawDataTestClient.java @@ -0,0 +1,53 @@ +/* + * 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.plugin.phosphonetx; + +import java.util.List; + +import ch.systemsx.cisd.common.spring.HttpInvokerUtils; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Sample; +import ch.systemsx.cisd.openbis.generic.shared.dto.SessionContextDTO; +import ch.systemsx.cisd.openbis.plugin.phosphonetx.shared.IRawDataService; + +/** + * + * + * @author Franz-Josef Elmer + */ +public class RawDataTestClient +{ + private static final String SERVER_URL = "http://localhost:8888/openbis"; + private static final String SERVICE_PATH = SERVER_URL + "/rmi-phosphonetx-raw-data"; + + public static void main(String[] args) + { + IRawDataService service = HttpInvokerUtils.createServiceStub(IRawDataService.class, SERVICE_PATH, 5); + + SessionContextDTO session = service.tryToAuthenticate("test_b", "t"); + for (String user : new String[] {"test", "test_a", "test_b", "test_c"}) + { + List<Sample> samples = service.listRawDataSamples(session.getSessionToken(), user); + System.out.println("User: " + user); + for (Sample sample : samples) + { + System.out.println(" " + sample.getCode()+" -> "+sample.getGeneratedFrom().getIdentifier()); + } + + } + + } +} diff --git a/rtd_phosphonetx/sourceTest/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/server/RawDataServiceTest.java b/rtd_phosphonetx/sourceTest/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/server/RawDataServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..13951e4662c793eded77b0e22d9f86a9d8bef76e --- /dev/null +++ b/rtd_phosphonetx/sourceTest/java/ch/systemsx/cisd/openbis/plugin/phosphonetx/server/RawDataServiceTest.java @@ -0,0 +1,289 @@ +/* + * 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.plugin.phosphonetx.server; + +import static ch.systemsx.cisd.openbis.plugin.phosphonetx.server.RawDataService.GROUP_CODE; +import static ch.systemsx.cisd.openbis.plugin.phosphonetx.server.RawDataService.RAW_DATA_SAMPLE_TYPE; + +import java.util.Arrays; +import java.util.LinkedHashSet; +import java.util.List; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.jmock.Expectations; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import ch.rinn.restrictions.Friend; +import ch.systemsx.cisd.common.exceptions.UserFailureException; +import ch.systemsx.cisd.openbis.generic.shared.AbstractServerTestCase; +import ch.systemsx.cisd.openbis.generic.shared.ICommonServer; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DatabaseInstance; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Group; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ListSampleCriteria; +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.DatabaseInstancePE; +import ch.systemsx.cisd.openbis.generic.shared.dto.GroupPE; +import ch.systemsx.cisd.openbis.generic.shared.dto.PersonPE; +import ch.systemsx.cisd.openbis.generic.shared.dto.RoleAssignmentPE; +import ch.systemsx.cisd.openbis.generic.shared.dto.SampleTypePE; +import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.DatabaseInstanceIdentifier; +import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.GroupIdentifier; +import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.SampleIdentifier; +import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.SampleIdentifierFactory; + +/** + * + * + * @author Franz-Josef Elmer + */ +@Friend(toClasses=RawDataService.class) +public class RawDataServiceTest extends AbstractServerTestCase +{ + private static final String USER_ID = "a-user"; + private static final Sample NO_PARENT = create("no-parent", null); + private static final Sample WITH_INSTANCE_PARENT = create("with-instance-parent", "t:/parent"); + private static final Sample WITH_PARENT_IN_G1 = create("with-parent-in-g1", "t:/g1/parent"); + private static final Sample WITH_PARENT_IN_G2 = create("with-parent-in-g2", "t:/g2/parent"); + + private static Sample create(String sampleCode, String parentSampleIdentifierOrNull) + { + Sample sample = new Sample(); + sample.setCode(sampleCode); + if (parentSampleIdentifierOrNull != null) + { + Sample parent = new Sample(); + parent.setIdentifier(parentSampleIdentifierOrNull); + SampleIdentifier identifier = SampleIdentifierFactory.parse(parentSampleIdentifierOrNull); + parent.setCode(identifier.getSampleCode()); + GroupIdentifier groupLevel = identifier.getGroupLevel(); + if (groupLevel != null) + { + Group group = new Group(); + group.setCode(groupLevel.getGroupCode()); + group.setInstance(createDatabaseInstance(groupLevel.getDatabaseInstanceCode())); + parent.setGroup(group); + } + DatabaseInstanceIdentifier databaseInstanceLevel = identifier.getDatabaseInstanceLevel(); + if (databaseInstanceLevel != null) + { + String code = databaseInstanceLevel.getDatabaseInstanceCode(); + DatabaseInstance databaseInstance = createDatabaseInstance(code); + parent.setDatabaseInstance(databaseInstance); + } + sample.setGeneratedFrom(parent); + } + return sample; + } + + private static DatabaseInstance createDatabaseInstance(String code) + { + DatabaseInstance databaseInstance = new DatabaseInstance(); + databaseInstance.setCode(code); + databaseInstance.setUuid(code); + return databaseInstance; + } + + private ICommonServer commonServer; + private RawDataService rawDataService; + + @Override + @BeforeMethod + public final void setUp() + { + super.setUp(); + commonServer = context.mock(ICommonServer.class); + rawDataService = new RawDataService(sessionManager, daoFactory, commonServer); + } + + @Test + public void testListRawDataSamplesForUnknownUser() + { + prepareGetSession(); + context.checking(new Expectations() + { + { + one(personDAO).tryFindPersonByUserId(USER_ID); + } + }); + + try + { + rawDataService.listRawDataSamples(SESSION_TOKEN, USER_ID); + fail("UserFailureException expected."); + } catch (UserFailureException ex) + { + assertEquals("Unknown user ID: " + USER_ID, ex.getMessage()); + } + + context.assertIsSatisfied(); + } + + @Test + public void testListRawDataSamplesForUserWithNoRoles() + { + prepareGetSession(); + prepareListRawDataSamples(); + + List<Sample> samples = rawDataService.listRawDataSamples(SESSION_TOKEN, USER_ID); + + assertSame(WITH_INSTANCE_PARENT, samples.get(0)); + assertEquals(1, samples.size()); + context.assertIsSatisfied(); + } + + @Test + public void testListRawDataSamplesForUserWithRightsForGroupG1() + { + prepareGetSession(); + prepareListRawDataSamples(createRole("G1", "T")); + + List<Sample> samples = rawDataService.listRawDataSamples(SESSION_TOKEN, USER_ID); + + assertSame(WITH_INSTANCE_PARENT, samples.get(0)); + assertSame(WITH_PARENT_IN_G1, samples.get(1)); + assertEquals(2, samples.size()); + context.assertIsSatisfied(); + } + + @Test + public void testListRawDataSamplesForUserWithRightsForGroupG1AndG2() + { + prepareGetSession(); + prepareListRawDataSamples(createRole("G1", "T"), createRole("G2", "T")); + + List<Sample> samples = rawDataService.listRawDataSamples(SESSION_TOKEN, USER_ID); + + assertSame(WITH_INSTANCE_PARENT, samples.get(0)); + assertSame(WITH_PARENT_IN_G1, samples.get(1)); + assertSame(WITH_PARENT_IN_G2, samples.get(2)); + assertEquals(3, samples.size()); + context.assertIsSatisfied(); + } + + @Test + public void testListRawDataSamplesForUserWithRightsForGroupG1ButWrongInstance() + { + prepareGetSession(); + prepareListRawDataSamples(createRole("G1", "X")); + + List<Sample> samples = rawDataService.listRawDataSamples(SESSION_TOKEN, USER_ID); + + assertSame(WITH_INSTANCE_PARENT, samples.get(0)); + assertEquals(1, samples.size()); + context.assertIsSatisfied(); + } + + @Test + public void testListRawDataSamplesForUserWithRightsForInstanceT() + { + prepareGetSession(); + prepareListRawDataSamples(createRole(null, "T")); + + List<Sample> samples = rawDataService.listRawDataSamples(SESSION_TOKEN, USER_ID); + + assertSame(WITH_INSTANCE_PARENT, samples.get(0)); + assertSame(WITH_PARENT_IN_G1, samples.get(1)); + assertSame(WITH_PARENT_IN_G2, samples.get(2)); + assertEquals(3, samples.size()); + context.assertIsSatisfied(); + } + + @Test + public void testListRawDataSamplesForUserWithRightsForInstanceX() + { + prepareGetSession(); + prepareListRawDataSamples(createRole(null, "X")); + + List<Sample> samples = rawDataService.listRawDataSamples(SESSION_TOKEN, USER_ID); + + assertSame(WITH_INSTANCE_PARENT, samples.get(0)); + assertEquals(1, samples.size()); + context.assertIsSatisfied(); + } + + private void prepareListRawDataSamples(final RoleAssignmentPE... roles) + { + context.checking(new Expectations() + { + { + one(personDAO).tryFindPersonByUserId(USER_ID); + PersonPE person = new PersonPE(); + person.setRoleAssignments(new LinkedHashSet<RoleAssignmentPE>(Arrays.asList(roles))); + will(returnValue(person)); + + one(sampleTypeDAO).tryFindSampleTypeByCode(RAW_DATA_SAMPLE_TYPE); + final SampleTypePE sampleType = new SampleTypePE(); + sampleType.setCode(RAW_DATA_SAMPLE_TYPE); + sampleType.setId(20100104l); + sampleType.setListable(Boolean.TRUE); + sampleType.setAutoGeneratedCode(Boolean.FALSE); + sampleType.setGeneratedFromHierarchyDepth(0); + sampleType.setContainerHierarchyDepth(0); + will(returnValue(sampleType)); + + one(commonServer).listSamples(with(SESSION_TOKEN), + with(new BaseMatcher<ListSampleCriteria>() + { + public boolean matches(Object item) + { + if (item instanceof ListSampleCriteria) + { + ListSampleCriteria criteria = (ListSampleCriteria) item; + assertEquals(GROUP_CODE, criteria.getGroupCode()); + assertEquals(true, criteria.isIncludeGroup()); + SampleType type = criteria.getSampleType(); + assertEquals(RAW_DATA_SAMPLE_TYPE, type.getCode()); + assertEquals(sampleType.getId(), type.getId()); + return true; + } + return false; + } + + public void describeTo(Description description) + { + description.appendValue(sampleType); + } + })); + will(returnValue(Arrays.asList(NO_PARENT, WITH_INSTANCE_PARENT, + WITH_PARENT_IN_G1, WITH_PARENT_IN_G2))); + } + }); + } + + private RoleAssignmentPE createRole(String groupCodeOrNull, String dataBaseInstanceUUID) + { + RoleAssignmentPE role = new RoleAssignmentPE(); + if (groupCodeOrNull == null) + { + DatabaseInstancePE databaseInstance = new DatabaseInstancePE(); + databaseInstance.setUuid(dataBaseInstanceUUID); + role.setDatabaseInstance(databaseInstance); + } else + { + GroupPE group = new GroupPE(); + group.setCode(groupCodeOrNull); + DatabaseInstancePE databaseInstance = new DatabaseInstancePE(); + databaseInstance.setUuid(dataBaseInstanceUUID); + group.setDatabaseInstance(databaseInstance); + role.setGroup(group); + } + return role; + } +}