Skip to content
Snippets Groups Projects
Commit 294ef0bb authored by felmer's avatar felmer
Browse files

SP-262, BIS-155: DataStoreServiceRegistratorTest (unit tests) and...

SP-262, BIS-155: DataStoreServiceRegistratorTest (unit tests) and DataStoreServiceRegistrationTest (system test) introduced.

SVN: 26420
parent 1265740c
No related branches found
No related tags found
No related merge requests found
......@@ -64,6 +64,11 @@ public class CommonServiceProvider
return new MailClient(mailClientParameters);
}
public static Object tryToGetBean(String beanName)
{
return applicationContext.getBean(beanName);
}
private CommonServiceProvider()
{
}
......
......@@ -41,6 +41,8 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.DataStoreServicePE;
import ch.systemsx.cisd.openbis.generic.shared.dto.DatastoreServiceDescriptions;
/**
* Implementation of {@link IDataStoreServiceRegistrator}.
*
* @author Franz-Josef Elmer
*/
public class DataStoreServiceRegistrator implements IDataStoreServiceRegistrator
......@@ -90,22 +92,25 @@ public class DataStoreServiceRegistrator implements IDataStoreServiceRegistrator
DatastoreServiceDescriptions serviceDescriptions)
{
Set<DataStoreServicePE> services = new HashSet<DataStoreServicePE>();
IDataSetTypeDAO dataSetTypeDAO = daoFactory.getDataSetTypeDAO();
List<DataSetTypePE> allDataSetTypes = dataSetTypeDAO.listAllEntities();
Set<DataStoreServicePE> processing =
createDataStoreServices(serviceDescriptions.getProcessingServiceDescriptions(),
DataStoreServiceKind.PROCESSING);
DataStoreServiceKind.PROCESSING, allDataSetTypes);
services.addAll(processing);
Set<DataStoreServicePE> queries =
createDataStoreServices(serviceDescriptions.getReportingServiceDescriptions(),
DataStoreServiceKind.QUERIES);
DataStoreServiceKind.QUERIES, allDataSetTypes);
services.addAll(queries);
return services;
}
private Set<DataStoreServicePE> createDataStoreServices(
List<DatastoreServiceDescription> serviceDescriptions, DataStoreServiceKind serviceKind)
List<DatastoreServiceDescription> serviceDescriptions,
DataStoreServiceKind serviceKind, List<DataSetTypePE> allDataSetTypes)
{
Set<DataStoreServicePE> services = new HashSet<DataStoreServicePE>();
for (DatastoreServiceDescription desc : serviceDescriptions)
......@@ -114,7 +119,8 @@ public class DataStoreServiceRegistrator implements IDataStoreServiceRegistrator
service.setKey(desc.getKey());
service.setLabel(desc.getLabel());
service.setKind(serviceKind);
Set<DataSetTypePE> datasetTypes = extractDatasetTypes(desc.getDatasetTypeCodes(), desc);
Set<DataSetTypePE> datasetTypes =
extractDataSetTypes(desc.getDatasetTypeCodes(), desc, allDataSetTypes);
service.setDatasetTypes(datasetTypes);
service.setReportingPluginTypeOrNull(desc.tryReportingPluginType());
services.add(service);
......@@ -122,26 +128,19 @@ public class DataStoreServiceRegistrator implements IDataStoreServiceRegistrator
return services;
}
/**
* Find the data set type objects specified by the dataSetTypeCodes.
*
* @return A set of DataSetTypePE objects.
*/
private Set<DataSetTypePE> extractDatasetTypes(String[] dataSetTypeCodes,
DatastoreServiceDescription serviceDescription)
private Set<DataSetTypePE> extractDataSetTypes(String[] dataSetTypeCodePatterns,
DatastoreServiceDescription serviceDescription, List<DataSetTypePE> allDataSetTypes)
{
Set<DataSetTypePE> dataSetTypes = new HashSet<DataSetTypePE>();
Set<String> missingCodes = new HashSet<String>();
IDataSetTypeDAO dataSetTypeDAO = daoFactory.getDataSetTypeDAO();
List<DataSetTypePE> allDataSetTypes = dataSetTypeDAO.listAllEntities();
for (String dataSetTypeCode : dataSetTypeCodes)
for (String pattern : dataSetTypeCodePatterns)
{
boolean found = false;
// Try to find the specified data set type
for (DataSetTypePE dataSetType : allDataSetTypes)
{
if (dataSetType.getCode().matches(dataSetTypeCode))
if (dataSetType.getCode().matches(pattern))
{
dataSetTypes.add(dataSetType);
found = true;
......@@ -149,7 +148,7 @@ public class DataStoreServiceRegistrator implements IDataStoreServiceRegistrator
}
if (false == found)
{
missingCodes.add(dataSetTypeCode);
missingCodes.add(pattern);
}
}
if (missingCodes.size() > 0)
......
......@@ -21,10 +21,16 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.DataStorePE;
import ch.systemsx.cisd.openbis.generic.shared.dto.DatastoreServiceDescriptions;
/**
* Interface of the bean which handles registration of data store services.
*
* @author Franz-Josef Elmer
*/
public interface IDataStoreServiceRegistrator
{
/**
* Registers for the specified data store all reporting/processing plugins specified in the
* service descriptions.
*/
public void setServiceDescriptions(DataStorePE dataStore,
DatastoreServiceDescriptions serviceDescriptions);
......
/*
* 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.generic.server;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
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.common.logging.BufferedAppender;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDataSetTypeDAO;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDataStoreDAO;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataStoreServiceKind;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DatastoreServiceDescription;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ReportingPluginType;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.builders.DataSetTypeBuilder;
import ch.systemsx.cisd.openbis.generic.shared.dto.DataSetTypePE;
import ch.systemsx.cisd.openbis.generic.shared.dto.DataStorePE;
import ch.systemsx.cisd.openbis.generic.shared.dto.DataStoreServicePE;
import ch.systemsx.cisd.openbis.generic.shared.dto.DatastoreServiceDescriptions;
/**
* @author Franz-Josef Elmer
*/
public class DataStoreServiceRegistratorTest extends AssertJUnit
{
private static final String DATASTORE_CODE = "DSS";
private static final Comparator<DataStoreServicePE> SERVICE_COMPARATOR =
new Comparator<DataStoreServicePE>()
{
@Override
public int compare(DataStoreServicePE s1, DataStoreServicePE s2)
{
return s1.getKey().compareTo(s2.getKey());
}
};
private BufferedAppender logRecorder;
private Mockery context;
private IDAOFactory daoFactory;
private IDataStoreDAO dataStoreDAO;
private IDataSetTypeDAO dataSetTypeDAO;
private DataStoreServiceRegistrator dataStoreServiceRegistrator;
@BeforeMethod
public void setUp()
{
logRecorder = new BufferedAppender();
context = new Mockery();
daoFactory = context.mock(IDAOFactory.class);
dataStoreDAO = context.mock(IDataStoreDAO.class);
dataSetTypeDAO = context.mock(IDataSetTypeDAO.class);
context.checking(new Expectations()
{
{
allowing(daoFactory).getDataStoreDAO();
will(returnValue(dataStoreDAO));
allowing(daoFactory).getDataSetTypeDAO();
will(returnValue(dataSetTypeDAO));
}
});
dataStoreServiceRegistrator = new DataStoreServiceRegistrator(daoFactory);
}
@AfterMethod
public void tearDown()
{
logRecorder.reset();
context.assertIsSatisfied();
}
@Test
public void testSetServiceDescriptions()
{
final DataStorePE dataStore = new DataStorePE();
dataStore.setCode(DATASTORE_CODE);
DatastoreServiceDescription r1 =
DatastoreServiceDescription.reporting("R1", "r1", new String[]
{ "A.*", "B" }, DATASTORE_CODE, ReportingPluginType.TABLE_MODEL);
DatastoreServiceDescription p1 =
DatastoreServiceDescription.processing("P1", "p1", new String[]
{ "A.*", "C.*", "D" }, DATASTORE_CODE);
context.checking(new Expectations()
{
{
one(dataSetTypeDAO).listAllEntities();
will(returnValue(Arrays.asList(dataSetType("A1"), dataSetType("B1"),
dataSetType("C1"), dataSetType("D"), dataSetType("D1"))));
one(dataStoreDAO).createOrUpdateDataStore(dataStore);
}
});
dataStoreServiceRegistrator.setServiceDescriptions(dataStore,
new DatastoreServiceDescriptions(Arrays.asList(r1), Arrays.asList(p1)));
List<DataStoreServicePE> services =
new ArrayList<DataStoreServicePE>(dataStore.getServices());
Collections.sort(services, SERVICE_COMPARATOR);
assertEquals("P1", services.get(0).getKey());
assertEquals("p1", services.get(0).getLabel());
assertEquals(DataStoreServiceKind.PROCESSING, services.get(0).getKind());
assertEquals(null, services.get(0).getReportingPluginTypeOrNull());
assertEquals("[A1, C1, D]", extractedDataSetTypes(services.get(0)).toString());
assertEquals("R1", services.get(1).getKey());
assertEquals("r1", services.get(1).getLabel());
assertEquals(DataStoreServiceKind.QUERIES, services.get(1).getKind());
assertEquals(ReportingPluginType.TABLE_MODEL, services.get(1)
.getReportingPluginTypeOrNull());
assertEquals("[A1]", extractedDataSetTypes(services.get(1)).toString());
assertEquals(2, services.size());
assertEquals("The Datastore Server Plugin '[QUERIES; R1; DSS; r1; A.* B ; TABLE_MODEL]' "
+ "is misconfigured. It refers to the dataset types "
+ "which do not exist in openBIS: [B]", logRecorder.getLogContent());
context.assertIsSatisfied();
}
@Test
public void testRegister()
{
final DataStorePE dataStore = new DataStorePE();
dataStore.setCode(DATASTORE_CODE);
DatastoreServiceDescription r1 =
DatastoreServiceDescription.reporting("R1", "r1", new String[]
{ "A.*", "B" }, DATASTORE_CODE, ReportingPluginType.TABLE_MODEL);
DatastoreServiceDescription p1 =
DatastoreServiceDescription.processing("P1", "p1", new String[]
{ "A.*", "C.*", "D" }, DATASTORE_CODE);
context.checking(new Expectations()
{
{
one(dataSetTypeDAO).listAllEntities();
will(returnValue(Arrays.asList(dataSetType("A1"), dataSetType("B1"),
dataSetType("C1"), dataSetType("D"), dataSetType("D1"))));
exactly(3).of(dataStoreDAO).createOrUpdateDataStore(dataStore);
exactly(2).of(dataStoreDAO).tryToFindDataStoreByCode(DATASTORE_CODE);
will(returnValue(dataStore));
exactly(2).of(dataSetTypeDAO).tryToFindDataSetTypeByCode("A3");
will(returnValue(dataSetType("A3")));
one(dataSetTypeDAO).tryToFindDataSetTypeByCode("B");
will(returnValue(dataSetType("B")));
}
});
dataStoreServiceRegistrator.setServiceDescriptions(dataStore,
new DatastoreServiceDescriptions(Arrays.asList(r1), Arrays.asList(p1)));
dataStoreServiceRegistrator.register(new DataSetTypeBuilder().code("A3").getDataSetType());
dataStoreServiceRegistrator.register(new DataSetTypeBuilder().code("B").getDataSetType());
List<DataStoreServicePE> services =
new ArrayList<DataStoreServicePE>(dataStore.getServices());
Collections.sort(services, SERVICE_COMPARATOR);
assertEquals("P1", services.get(0).getKey());
assertEquals("p1", services.get(0).getLabel());
assertEquals(DataStoreServiceKind.PROCESSING, services.get(0).getKind());
assertEquals(null, services.get(0).getReportingPluginTypeOrNull());
assertEquals("[A1, A3, C1, D]", extractedDataSetTypes(services.get(0)).toString());
assertEquals("R1", services.get(1).getKey());
assertEquals("r1", services.get(1).getLabel());
assertEquals(DataStoreServiceKind.QUERIES, services.get(1).getKind());
assertEquals(ReportingPluginType.TABLE_MODEL, services.get(1)
.getReportingPluginTypeOrNull());
assertEquals("[A1, A3, B]", extractedDataSetTypes(services.get(1)).toString());
assertEquals(2, services.size());
context.assertIsSatisfied();
}
private DataSetTypePE dataSetType(String code)
{
DataSetTypePE type = new DataSetTypePE();
type.setCode(code);
return type;
}
private List<String> extractedDataSetTypes(DataStoreServicePE service)
{
Set<DataSetTypePE> datasetTypes = service.getDatasetTypes();
List<String> list = new ArrayList<String>();
for (DataSetTypePE type : datasetTypes)
{
list.add(type.getCode());
}
Collections.sort(list);
return list;
}
}
......@@ -19,6 +19,7 @@ package ch.systemsx.cisd.openbis.generic.shared.basic.dto.builders;
import java.util.ArrayList;
import java.util.List;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataSetKind;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataSetType;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataSetTypePropertyType;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataTypeCode;
......@@ -48,6 +49,12 @@ public class DataSetTypeBuilder extends AbstractEntityTypeBuilder<DataSetType>
return this;
}
public DataSetTypeBuilder kind(DataSetKind dataSetKind)
{
dataSetType.setDataSetKind(dataSetKind);
return this;
}
public DataSetTypeBuilder propertyType(String code, String label, DataTypeCode dataType)
{
DataSetTypePropertyType entityTypePropertyType = new DataSetTypePropertyType();
......
/*
* 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;
import static org.testng.AssertJUnit.assertEquals;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import ch.systemsx.cisd.openbis.generic.server.CommonServiceProvider;
import ch.systemsx.cisd.openbis.generic.shared.IDataStoreService;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataSetKind;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataSetType;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataStoreServiceKind;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DatastoreServiceDescription;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ReportingPluginType;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.builders.DataSetTypeBuilder;
import ch.systemsx.cisd.openbis.generic.shared.dto.DataStoreServerInfo;
import ch.systemsx.cisd.openbis.generic.shared.dto.DatastoreServiceDescriptions;
/**
* @author Franz-Josef Elmer
*/
public class DataStoreServiceRegistrationTest extends SystemTestCase
{
private static final String DOWNLOAD_URL = "blabla";
private static final String SESSION_TOKEN = "123";
private static final String DATASTORE_CODE = "ABC";
private Mockery context;
@BeforeMethod
public void addMockDataStoreService() throws Exception
{
context = new Mockery();
final IDataStoreService dataStoreService = context.mock(IDataStoreService.class);
Object bean = CommonServiceProvider.tryToGetBean("dss-factory");
getDataStoreServices(bean).put("http://localhost:0", dataStoreService);
context.checking(new Expectations()
{
{
allowing(dataStoreService).getVersion(SESSION_TOKEN);
will(returnValue(IDataStoreService.VERSION));
}
});
}
@SuppressWarnings("unchecked")
private Map<String, IDataStoreService> getDataStoreServices(Object bean) throws Exception
{
Field[] fields = bean.getClass().getDeclaredFields();
for (Field field : fields)
{
if (field.getType().getName().equals(Map.class.getName()))
{
field.setAccessible(true);
return (Map<String, IDataStoreService>) field.get(bean);
}
}
throw new IllegalArgumentException("Has no services map: " + bean);
}
@Test
public void test()
{
DataStoreServerInfo dataStoreServerInfo = new DataStoreServerInfo();
dataStoreServerInfo.setDataStoreCode(DATASTORE_CODE);
dataStoreServerInfo.setSessionToken(SESSION_TOKEN);
dataStoreServerInfo.setDownloadUrl(DOWNLOAD_URL);
DatastoreServiceDescription r1 =
DatastoreServiceDescription.reporting("R1", "r1", new String[]
{ "H.*", "UNKNOWN" }, DATASTORE_CODE, ReportingPluginType.TABLE_MODEL);
DatastoreServiceDescription p1 =
DatastoreServiceDescription.processing("P1", "p1", new String[]
{ "H.*", "C.*" }, DATASTORE_CODE);
dataStoreServerInfo.setServicesDescriptions(new DatastoreServiceDescriptions(Arrays
.asList(r1), Arrays.asList(p1)));
// 1. register DSS
etlService.registerDataStoreServer(systemSessionToken, dataStoreServerInfo);
checkDataSetTypes("[HCS_IMAGE, HCS_IMAGE_ANALYSIS_DATA, UNKNOWN]",
"[CONTAINER_TYPE, HCS_IMAGE, HCS_IMAGE_ANALYSIS_DATA]");
// 2. register a new data set type
DataSetType dataSetType =
new DataSetTypeBuilder().code("H").kind(DataSetKind.PHYSICAL).getDataSetType();
commonServer.registerDataSetType(systemSessionToken, dataSetType);
checkDataSetTypes("[H, HCS_IMAGE, HCS_IMAGE_ANALYSIS_DATA, UNKNOWN]",
"[CONTAINER_TYPE, H, HCS_IMAGE, HCS_IMAGE_ANALYSIS_DATA]");
// 3. re-register DSS
etlService.registerDataStoreServer(systemSessionToken, dataStoreServerInfo);
checkDataSetTypes("[H, HCS_IMAGE, HCS_IMAGE_ANALYSIS_DATA, UNKNOWN]",
"[CONTAINER_TYPE, H, HCS_IMAGE, HCS_IMAGE_ANALYSIS_DATA]");
// 4. delete the new data set type and register it again
commonServer.deleteDataSetTypes(systemSessionToken, Arrays.asList("H"));
commonServer.registerDataSetType(systemSessionToken, dataSetType);
checkDataSetTypes("[H, HCS_IMAGE, HCS_IMAGE_ANALYSIS_DATA, UNKNOWN]",
"[CONTAINER_TYPE, H, HCS_IMAGE, HCS_IMAGE_ANALYSIS_DATA]");
}
private void checkDataSetTypes(String expectedReportingDataSetTypes,
String expectedProcessingDataSetTypes)
{
List<DatastoreServiceDescription> services =
commonServer
.listDataStoreServices(systemSessionToken, DataStoreServiceKind.QUERIES);
assertEquals("R1", services.get(0).getKey());
assertEquals("r1", services.get(0).getLabel());
assertEquals(DOWNLOAD_URL, services.get(0).getDownloadURL());
assertEquals(DataStoreServiceKind.QUERIES, services.get(0).getServiceKind());
assertEquals(DATASTORE_CODE, services.get(0).getDatastoreCode());
assertEquals(expectedReportingDataSetTypes, getDataSetTypeCodes(services.get(0)).toString());
assertEquals(1, services.size());
services =
commonServer.listDataStoreServices(systemSessionToken,
DataStoreServiceKind.PROCESSING);
assertEquals("P1", services.get(0).getKey());
assertEquals("p1", services.get(0).getLabel());
assertEquals(DOWNLOAD_URL, services.get(0).getDownloadURL());
assertEquals(DataStoreServiceKind.PROCESSING, services.get(0).getServiceKind());
assertEquals(DATASTORE_CODE, services.get(0).getDatastoreCode());
assertEquals(expectedProcessingDataSetTypes, getDataSetTypeCodes(services.get(0))
.toString());
assertEquals(1, services.size());
}
private List<String> getDataSetTypeCodes(DatastoreServiceDescription description)
{
List<String> result =
new ArrayList<String>(Arrays.asList(description.getDatasetTypeCodes()));
Collections.sort(result);
return result;
}
}
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