From 33817f6bc990018454e86b7cfe504b5446dc7df9 Mon Sep 17 00:00:00 2001 From: felmer <felmer> Date: Wed, 25 Mar 2009 10:04:39 +0000 Subject: [PATCH] LMS-779 DataStoreServiceTests added. IDataSetCommandExcecutor interface changed. SVN: 10374 --- .../server/DataSetCommandExecuter.java | 15 +- .../dss/generic/server/DataStoreService.java | 18 +- .../dss/generic/server/IDataSetCommand.java | 5 +- .../server/IDataSetCommandExecutor.java | 26 +- .../IDataSetCommandExecutorFactory.java | 5 +- .../generic/server/DataStoreServiceTest.java | 280 ++++++++++++++++++ 6 files changed, 338 insertions(+), 11 deletions(-) create mode 100644 datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreServiceTest.java diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataSetCommandExecuter.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataSetCommandExecuter.java index e0f5880d92d..0cfd3d3078b 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataSetCommandExecuter.java +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataSetCommandExecuter.java @@ -17,6 +17,7 @@ package ch.systemsx.cisd.openbis.dss.generic.server; import java.io.File; +import java.util.List; import org.apache.commons.lang.time.StopWatch; import org.apache.log4j.Logger; @@ -26,6 +27,7 @@ import ch.systemsx.cisd.common.collections.ExtendedBlockingQueueFactory; import ch.systemsx.cisd.common.collections.IExtendedBlockingQueue; import ch.systemsx.cisd.common.logging.LogCategory; import ch.systemsx.cisd.common.logging.LogFactory; +import ch.systemsx.cisd.openbis.generic.shared.dto.DataSetUploadContext; /** * @@ -83,9 +85,20 @@ class DataSetCommandExecuter implements IDataSetCommandExecutor thread.start(); } + public void scheduleDeletionOfDataSets(List<String> locations) + { + scheduleCommand(new DeletionCommand(locations)); + } + public void scheduleUploadingDataSetsToCIFEX(ICIFEXRPCServiceFactory cifexServiceFactory, + MailClientParameters mailClientParameters, List<String> locations, + DataSetUploadContext uploadContext) + { + scheduleCommand(new UploadingCommand(cifexServiceFactory, mailClientParameters, locations, + uploadContext)); + } - public void scheduleCommand(IDataSetCommand command) + private void scheduleCommand(IDataSetCommand command) { if (operationLog.isDebugEnabled()) { diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreService.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreService.java index 4291856f13b..83ad81a0e81 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreService.java +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreService.java @@ -74,7 +74,7 @@ public class DataStoreService extends AbstractServiceWithLogger<IDataStoreServic this.storeRoot = storeRoot; } - public void afterPropertiesSet() throws Exception + public void afterPropertiesSet() { String prefix = "Property 'storeRoot' "; if (storeRoot == null) @@ -141,8 +141,8 @@ public class DataStoreService extends AbstractServiceWithLogger<IDataStoreServic throws InvalidAuthenticationException { sessionTokenManager.assertValidSessionToken(sessionToken); - - commandExecuter.scheduleCommand(new DeletionCommand(dataSetLocations)); + + commandExecuter.scheduleDeletionOfDataSets(dataSetLocations); } public void uploadDataSetsToCIFEX(String sessionToken, List<String> dataSetLocations, @@ -150,7 +150,8 @@ public class DataStoreService extends AbstractServiceWithLogger<IDataStoreServic { sessionTokenManager.assertValidSessionToken(sessionToken); - CIFEXRPCServiceFactory serviceFactory = new CIFEXRPCServiceFactory(context.getCifexURL()); + ICIFEXRPCServiceFactory serviceFactory = + createCIFEXRPCServiceFactory(context.getCifexURL()); ICIFEXRPCService service = serviceFactory.createService(); String userID = context.getUserID(); String password = context.getPassword(); @@ -158,8 +159,13 @@ public class DataStoreService extends AbstractServiceWithLogger<IDataStoreServic { throw new InvalidSessionException("User couldn't be authenticated at CIFEX."); } - commandExecuter.scheduleCommand(new UploadingCommand(serviceFactory, mailClientParameters, - dataSetLocations, context)); + commandExecuter.scheduleUploadingDataSetsToCIFEX(serviceFactory, mailClientParameters, + dataSetLocations, context); + } + + protected ICIFEXRPCServiceFactory createCIFEXRPCServiceFactory(String cifexURL) + { + return new CIFEXRPCServiceFactory(cifexURL); } } diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/IDataSetCommand.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/IDataSetCommand.java index 91710146f62..d1dcbb20912 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/IDataSetCommand.java +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/IDataSetCommand.java @@ -20,11 +20,14 @@ import java.io.File; import java.io.Serializable; /** - * + * Interface of all commands operating on data sets in a data store. * * @author Franz-Josef Elmer */ interface IDataSetCommand extends Serializable { + /** + * Executes the command in the provided data store. + */ void execute(File store); } diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/IDataSetCommandExecutor.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/IDataSetCommandExecutor.java index bc8af69aa6c..b84aecc2837 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/IDataSetCommandExecutor.java +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/IDataSetCommandExecutor.java @@ -16,15 +16,37 @@ package ch.systemsx.cisd.openbis.dss.generic.server; +import java.util.List; + +import ch.systemsx.cisd.openbis.generic.shared.dto.DataSetUploadContext; + /** + * Executor of commands operating on data sets in a data store. Commands are expected to be executed + * asynchronously in the order they had been scheduled. * - * * @author Franz-Josef Elmer */ interface IDataSetCommandExecutor { + /** + * Starts up executor. + */ void start(); - void scheduleCommand(IDataSetCommand command); + /** + * Schedules deletion of all data sets at specified locations. + */ + void scheduleDeletionOfDataSets(List<String> locations); + /** + * Schedules uploading of all data sets at specified locations to CIFEX using the specified + * upload context. + * + * @param cifexServiceFactory Factory for creating CIFEX upload service. + * @param mailClientParameters Parameters needed for sending an e-mail to the user if + * uploading failed. + */ + void scheduleUploadingDataSetsToCIFEX(ICIFEXRPCServiceFactory cifexServiceFactory, + MailClientParameters mailClientParameters, List<String> locations, + DataSetUploadContext uploadContext); } \ No newline at end of file diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/IDataSetCommandExecutorFactory.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/IDataSetCommandExecutorFactory.java index 96e51f3b122..6081d0040f5 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/IDataSetCommandExecutorFactory.java +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/IDataSetCommandExecutorFactory.java @@ -19,11 +19,14 @@ package ch.systemsx.cisd.openbis.dss.generic.server; import java.io.File; /** - * + * Factory of {@link IDataSetCommandExecutor} operating on a specified data store. * * @author Franz-Josef Elmer */ interface IDataSetCommandExecutorFactory { + /** + * Creates command executor for the specified data store. + */ IDataSetCommandExecutor create(File store); } diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreServiceTest.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreServiceTest.java new file mode 100644 index 00000000000..ef7e1c64b9f --- /dev/null +++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreServiceTest.java @@ -0,0 +1,280 @@ +/* + * Copyright 2009 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.dss.generic.server; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +import org.jmock.Expectations; +import org.jmock.Mockery; +import org.testng.AssertJUnit; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import ch.systemsx.cisd.cifex.rpc.ICIFEXRPCService; +import ch.systemsx.cisd.common.exceptions.InvalidAuthenticationException; +import ch.systemsx.cisd.common.exceptions.InvalidSessionException; +import ch.systemsx.cisd.common.filesystem.FileUtilities; +import ch.systemsx.cisd.openbis.generic.shared.IDataStoreService; +import ch.systemsx.cisd.openbis.generic.shared.dto.DataSetUploadContext; + +/** + * + * + * @author Franz-Josef Elmer + */ +public class DataStoreServiceTest extends AssertJUnit +{ + private static final String INVALID_SESSION_TOKEN_MSG = "Invalid session token."; + private static final String CIFEX_URL = "cifexURL"; + private static final File TEST_FOLDER = new File("targets/data-store-service-test"); + private static final File TEST_STORE = new File(TEST_FOLDER, "store"); + + private static final class MockDataStoreService extends DataStoreService + { + private final ICIFEXRPCServiceFactory cifexServiceFactory; + private final String expectedCIFEXURL; + + MockDataStoreService(SessionTokenManager sessionTokenManager, + IDataSetCommandExecutorFactory commandExecutorFactory, + MailClientParameters mailClientParameters, + ICIFEXRPCServiceFactory cifexServiceFactory, String expectedCIFEXURL) + { + super(sessionTokenManager, commandExecutorFactory, mailClientParameters); + this.cifexServiceFactory = cifexServiceFactory; + this.expectedCIFEXURL = expectedCIFEXURL; + } + + @Override + protected ICIFEXRPCServiceFactory createCIFEXRPCServiceFactory(String cifexURL) + { + AssertJUnit.assertEquals(expectedCIFEXURL, cifexURL); + return cifexServiceFactory; + } + } + + private SessionTokenManager sessionTokenManager; + private String sessionToken; + private Mockery context; + private IDataSetCommandExecutorFactory commandExecutorFactory; + private IDataSetCommandExecutor commandExecutor; + private ICIFEXRPCServiceFactory cifexServiceFactory; + private MailClientParameters mailClientParameters; + private ICIFEXRPCService cifexService; + + @BeforeMethod + public void setup() + { + context = new Mockery(); + sessionTokenManager = new SessionTokenManager(); + sessionToken = sessionTokenManager.drawSessionToken(); + commandExecutorFactory = context.mock(IDataSetCommandExecutorFactory.class); + commandExecutor = context.mock(IDataSetCommandExecutor.class); + cifexServiceFactory = context.mock(ICIFEXRPCServiceFactory.class); + cifexService = context.mock(ICIFEXRPCService.class); + mailClientParameters = new MailClientParameters(); + mailClientParameters.setFrom("a@bc.de"); + mailClientParameters.setSmtpHost("file://targets/email"); + FileUtilities.deleteRecursively(TEST_FOLDER); + TEST_STORE.mkdirs(); + } + + @AfterMethod + public void tearDown() + { + // To following line of code should also be called at the end of each test method. + // Otherwise one do not known which test failed. + context.assertIsSatisfied(); + } + + @Test + public void testGetVersionForInvalidSessionToken() + { + try + { + createService().getVersion("invalid"); + fail("InvalidAuthenticationException expected"); + } catch (InvalidAuthenticationException e) + { + assertEquals(INVALID_SESSION_TOKEN_MSG, e.getMessage()); + } + + context.assertIsSatisfied(); + } + + @Test + public void testGetVersion() + { + assertEquals(IDataStoreService.VERSION, createService().getVersion(sessionToken)); + + context.assertIsSatisfied(); + } + + @Test + public void testGetKnownDataSetsForInvalidSessionToken() + { + try + { + createService().getKnownDataSets("invalid", null); + fail("InvalidAuthenticationException expected"); + } catch (InvalidAuthenticationException e) + { + assertEquals(INVALID_SESSION_TOKEN_MSG, e.getMessage()); + } + + context.assertIsSatisfied(); + } + + @Test + public void testGetKnownDataSets() throws IOException + { + String location = "ds1"; + new File(TEST_STORE, location).createNewFile(); + + IDataStoreService service = createService(); + List<String> knownDataSets = service.getKnownDataSets(sessionToken, Arrays.asList(location, "ds2")); + + assertEquals(1, knownDataSets.size()); + assertSame(location, knownDataSets.get(0)); + context.assertIsSatisfied(); + } + + @Test + public void testDeleteDataSetsForInvalidSessionToken() + { + try + { + createService().deleteDataSets("invalid", null); + fail("InvalidAuthenticationException expected"); + } catch (InvalidAuthenticationException e) + { + assertEquals(INVALID_SESSION_TOKEN_MSG, e.getMessage()); + } + + context.assertIsSatisfied(); + } + + @Test + public void testDeleteDataSets() + { + final List<String> locations = Arrays.asList("d1", "d2"); + context.checking(new Expectations() + { + { + one(commandExecutor).scheduleDeletionOfDataSets(locations); + } + }); + createService().deleteDataSets(sessionToken, locations); + + context.assertIsSatisfied(); + } + + @Test + public void testUploadDataSetsForInvalidSessionToken() + { + try + { + createService().uploadDataSetsToCIFEX("invalid", null, null); + fail("InvalidAuthenticationException expected"); + } catch (InvalidAuthenticationException e) + { + assertEquals(INVALID_SESSION_TOKEN_MSG, e.getMessage()); + } + + context.assertIsSatisfied(); + } + + @Test + public void testUploadDataSetsForInvalidPassword() + { + final List<String> locations = Arrays.asList("d1", "d2"); + final DataSetUploadContext uploadContext = new DataSetUploadContext(); + uploadContext.setCifexURL(CIFEX_URL); + uploadContext.setUserID("user"); + uploadContext.setPassword("pwd"); + context.checking(new Expectations() + { + { + one(cifexServiceFactory).createService(); + will(returnValue(cifexService)); + + one(cifexService).login(uploadContext.getUserID(), uploadContext.getPassword()); + will(returnValue(null)); + } + }); + + try + { + createService().uploadDataSetsToCIFEX(sessionToken, locations, uploadContext); + fail("InvalidSessionException expected"); + } catch (InvalidSessionException e) + { + assertEquals("User couldn't be authenticated at CIFEX.", e.getMessage()); + } + + context.assertIsSatisfied(); + } + + @Test + public void testUploadDataSets() + { + final List<String> locations = Arrays.asList("d1", "d2"); + final DataSetUploadContext uploadContext = new DataSetUploadContext(); + uploadContext.setCifexURL(CIFEX_URL); + uploadContext.setUserID("user"); + uploadContext.setPassword("pwd"); + context.checking(new Expectations() + { + { + one(cifexServiceFactory).createService(); + will(returnValue(cifexService)); + + one(cifexService).login(uploadContext.getUserID(), uploadContext.getPassword()); + will(returnValue("token")); + + one(commandExecutor).scheduleUploadingDataSetsToCIFEX(cifexServiceFactory, + mailClientParameters, locations, uploadContext); + } + }); + + createService().uploadDataSetsToCIFEX(sessionToken, locations, uploadContext); + + context.assertIsSatisfied(); + } + + private IDataStoreService createService() + { + context.checking(new Expectations() + { + { + one(commandExecutorFactory).create(TEST_STORE); + will(returnValue(commandExecutor)); + + one(commandExecutor).start(); + } + }); + MockDataStoreService service = + new MockDataStoreService(sessionTokenManager, commandExecutorFactory, mailClientParameters, + cifexServiceFactory, CIFEX_URL); + service.setStoreRoot(TEST_STORE); + service.afterPropertiesSet(); + return service; + } +} -- GitLab