add replication test for file storage mgmt service

This commit is contained in:
Fredrik Teschke committed 2015-01-29 19:50:17 +01:00
1 parent 1a0b2ffa25
commit 2989b4a335
20 files changed
+299 -79

No files matched your search

@@ -0,0 +1,76 @@
package com.sap.sse.filestorage.propertystore.test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.sap.sse.common.TypeBasedServiceFinder;
import com.sap.sse.common.impl.SingleTypeBasedServiceFinderImpl;
import com.sap.sse.filestorage.FileStorageService;
import com.sap.sse.filestorage.FileStorageServiceProperty;
import com.sap.sse.filestorage.FileStorageServicePropertyStore;
import com.sap.sse.filestorage.impl.FileStorageManagementServiceImpl;
import com.sap.sse.filestorage.impl.MongoFileStorageServicePropertyStoreImpl;
import com.sap.sse.filestorage.test.util.Util;
import com.sap.sse.filestorage.testsupport.DummyFileStorageService;
import com.sap.sse.mongodb.MongoDBConfiguration;
public class MongoStoreTest {
private MongoDBConfiguration config = MongoDBConfiguration.getDefaultTestConfiguration();
@Before
public void setup() {
config.getService().getDB().dropDatabase();
}
@After
public void tearDown() {
config.getService().getDB().dropDatabase();
}
@Test
public void testStoreAndReadProperties() {
FileStorageServicePropertyStore store = new MongoFileStorageServicePropertyStoreImpl(config.getService());
store.writeProperty("s1", "p1", "1");
store.writeProperty("s1", "p2", "2");
store.writeProperty("s2", "p3", "3");
Map<String, String> properties = store.readAllProperties("s1");
assertThat("two properties read for service 1", properties.size(), equalTo(2));
assertThat("property 1 is read correctly for service 1", properties.get("p1"), equalTo("1"));
properties = store.readAllProperties("s2");
assertThat("one property read for service 2", properties.size(), equalTo(1));
}
@Test
public void testManagementServiceUsesStore() {
FileStorageServicePropertyStore store = new MongoFileStorageServicePropertyStoreImpl(config.getService());
FileStorageService service = new DummyFileStorageService();
TypeBasedServiceFinder<FileStorageService> serviceFinder = new SingleTypeBasedServiceFinderImpl<>(
service, DummyFileStorageService.NAME);
FileStorageManagementServiceImpl mgmtService = new FileStorageManagementServiceImpl(serviceFinder, store);
mgmtService.setActiveFileStorageService(service);
mgmtService.setFileStorageServiceProperty(service, DummyFileStorageService.PROPERTY_NAME, "123");
// now setup new services
service = new DummyFileStorageService();
serviceFinder = new SingleTypeBasedServiceFinderImpl<>(service, DummyFileStorageService.NAME);
mgmtService = new FileStorageManagementServiceImpl(serviceFinder, store);
assertThat("active service is read from DB", mgmtService.getActiveFileStorageService(), equalTo(service));
// simulate OSGi service addition
mgmtService.onServiceAdded(service);
FileStorageServiceProperty property = Util.findProperty(service, DummyFileStorageService.PROPERTY_NAME);
assertThat("stored property set for service", property.getValue(), equalTo("123"));
}
}
@@ -0,0 +1,52 @@
package com.sap.sse.filestorage.replication.test;
import java.io.IOException;
import java.net.MalformedURLException;
import com.sap.sse.common.impl.SingleTypeBasedServiceFinderImpl;
import com.sap.sse.filestorage.FileStorageManagementService;
import com.sap.sse.filestorage.FileStorageService;
import com.sap.sse.filestorage.impl.EmptyFileStorageServicePropertyStoreImpl;
import com.sap.sse.filestorage.impl.FileStorageManagementServiceImpl;
import com.sap.sse.filestorage.testsupport.DummyFileStorageService;
import com.sap.sse.mongodb.MongoDBService;
import com.sap.sse.replication.testsupport.AbstractServerReplicationTest;
public abstract class AbstractFileStorageManagementServiceReplicationTest extends
AbstractServerReplicationTest<FileStorageManagementService, FileStorageManagementServiceImpl> {
private MongoDBService mongoDBService;
@Override
protected void persistenceSetUp(boolean dropDB) {
mongoDBService = MongoDBService.INSTANCE;
if (dropDB) {
mongoDBService.getDB().dropDatabase();
}
}
protected FileStorageManagementServiceImpl createNewService() {
return new FileStorageManagementServiceImpl(
new SingleTypeBasedServiceFinderImpl<>(new DummyFileStorageService(),
DummyFileStorageService.NAME), EmptyFileStorageServicePropertyStoreImpl.INSTANCE);
}
@Override
protected FileStorageManagementServiceImpl createNewMaster() throws MalformedURLException, IOException,
InterruptedException {
FileStorageManagementServiceImpl result = createNewService();
FileStorageService serviceOnMaster = result.getAvailableFileStorageServices()[0];
result.setActiveFileStorageService(serviceOnMaster);
result.setFileStorageServiceProperty(serviceOnMaster, DummyFileStorageService.PROPERTY_NAME, "123");
return result;
}
@Override
protected FileStorageManagementServiceImpl createNewReplica() {
return createNewService();
}
@Override
protected void persistenceTearDown() {
mongoDBService.getDB().dropDatabase();
}
}
@@ -0,0 +1,45 @@
package com.sap.sse.filestorage.replication.test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import com.sap.sse.common.NoCorrespondingServiceRegisteredException;
import com.sap.sse.filestorage.FileStorageService;
import com.sap.sse.filestorage.test.util.Util;
import com.sap.sse.filestorage.testsupport.DummyFileStorageService;
public class SimpleFileStorageManagementServiceReplicationTest extends
AbstractFileStorageManagementServiceReplicationTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void testReplicationOfExistingActiveServiceAndProperties() throws InterruptedException,
IllegalAccessException {
FileStorageService serviceOnMaster = master.getActiveFileStorageService();
// see if initial load worked
FileStorageService serviceOnReplica = replica.getActiveFileStorageService();
assertThat("active service on replica same as on master after initial load", serviceOnReplica.getName(),
equalTo(serviceOnMaster.getName()));
assertThat("service property on replica set after initial load",
Util.findProperty(serviceOnReplica, DummyFileStorageService.PROPERTY_NAME).getValue(), equalTo("123"));
// now change things and see if replication works
master.setActiveFileStorageService(null);
master.setFileStorageServiceProperty(serviceOnMaster, DummyFileStorageService.PROPERTY_NAME, "456");
replicaReplicator.waitUntilQueueIsEmpty();
Thread.sleep(3000);
thrown.expect(NoCorrespondingServiceRegisteredException.class);
replica.getActiveFileStorageService();
assertThat("service property on replica set after replication",
Util.findProperty(serviceOnReplica, DummyFileStorageService.PROPERTY_NAME).getValue(), equalTo("456"));
}
}
@@ -1,4 +1,4 @@
package com.sap.sse.filestorage.test;
package com.sap.sse.filestorage.services.test;
import java.io.IOException;
import java.io.InputStream;
@@ -1,44 +0,0 @@
package com.sap.sse.filestorage.test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.sap.sse.filestorage.FileStorageServicePropertyStore;
import com.sap.sse.filestorage.impl.MongoFileStorageServicePropertyStoreImpl;
import com.sap.sse.mongodb.MongoDBConfiguration;
public class MongoStoreTest {
private MongoDBConfiguration config = MongoDBConfiguration.getDefaultTestConfiguration();
@Before
public void setup() {
config.getService().getDB().dropDatabase();
}
@After
public void tearDown() {
config.getService().getDB().dropDatabase();
}
@Test
public void testStoreAndReadProperties() {
FileStorageServicePropertyStore store = new MongoFileStorageServicePropertyStoreImpl(config.getService());
store.writeProperty("s1", "p1", "1");
store.writeProperty("s1", "p2", "2");
store.writeProperty("s2", "p3", "3");
Map<String, String> properties = store.readAllProperties("s1");
assertThat("two properties read for service 1", properties.size(), equalTo(2));
assertThat("property 1 is read correctly for service 1", properties.get("p1"), equalTo("1"));
properties = store.readAllProperties("s2");
assertThat("one property read for service 2", properties.size(), equalTo(1));
}
}
@@ -0,0 +1,13 @@
package com.sap.sse.filestorage.test.util;
import java.util.Arrays;
import com.sap.sse.filestorage.FileStorageService;
import com.sap.sse.filestorage.FileStorageServiceProperty;
public class Util {
public static FileStorageServiceProperty findProperty(FileStorageService service, String name) {
return Arrays.asList(service.getProperties()).stream().filter(p -> p.getName().equals(name)).findFirst()
.get();
}
}