add replication test for mail service (could only test applying changes to master)

This commit is contained in:
Fredrik Teschke committed 2015-02-13 20:30:32 +01:00
1 parent de39d6a40a
commit 42e1411bb6
22 files changed
+394 -18

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
+28
View File
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>com.sap.sse.mail.replication.test</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
@@ -0,0 +1,7 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
@@ -0,0 +1,3 @@
eclipse.preferences.version=1
pluginProject.extensions=false
resolve.requirebundle=false
@@ -0,0 +1,11 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Test
Bundle-SymbolicName: com.sap.sse.mail.replication.test
Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: SAP
Fragment-Host: com.sap.sse.mail
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Require-Bundle: org.junit4;bundle-version="4.8.2",
com.rabbitmq.client,
com.sap.sse.replication.testsupport
+4
View File
@@ -0,0 +1,4 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.
+13
View File
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>root</artifactId>
<groupId>com.sap.sailing</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>com.sap.sse.mail.replication.test</artifactId>
<packaging>eclipse-plugin</packaging>
</project>
+7
View File
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
+28
View File
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>com.sap.sse.mail.replication.testsupport</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
@@ -0,0 +1,7 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
@@ -0,0 +1,3 @@
eclipse.preferences.version=1
pluginProject.extensions=false
resolve.requirebundle=false
@@ -0,0 +1,17 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Test
Bundle-SymbolicName: com.sap.sse.mail.replication.testsupport
Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: SAP
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Require-Bundle: com.sap.sse.replication,
com.sap.sse.replication.testsupport,
com.sap.sse.mail,
com.sap.sse,
com.sap.sse.common,
org.hamcrest;bundle-version="1.1.0",
org.junit4;bundle-version="4.8.2",
org.mockito.mockito-core;bundle-version="1.10.14",
com.sun.mail.javax.mail;bundle-version="1.4.5"
Export-Package: com.sap.sse.mail.replication.testsupport
@@ -0,0 +1,4 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.
+13
View File
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>root</artifactId>
<groupId>com.sap.sailing</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>com.sap.sse.mail.replication.testsupport</artifactId>
<packaging>eclipse-plugin</packaging>
</project>
@@ -0,0 +1,59 @@
package com.sap.sse.mail.replication.testsupport;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import com.sap.sse.common.mail.MailException;
import com.sap.sse.mail.MailService;
import com.sap.sse.mail.impl.MailServiceImpl;
import com.sap.sse.replication.testsupport.AbstractServerReplicationTestSetUp;
import com.sap.sse.replication.testsupport.AbstractServerWithSingleServiceReplicationTest;
public class AbstractMailReplicationTest extends AbstractServerWithSingleServiceReplicationTest<MailService, MailServiceImpl> {
public AbstractMailReplicationTest() {
super(new MailServerReplicationTestSetUp());
}
public static final Map<MailServiceImpl, Integer> numberOfMailsSent = new HashMap<>();
@Before
public void setUp() {
numberOfMailsSent.clear();
}
public static MailServiceImpl createMailCountingService(final boolean canSendMail) {
return new MailServiceImpl(null) {
@Override
protected void internalSendMail(String toAddress, String subject, ContentSetter contentSetter)
throws MailException {
if (canSendMail) {
Integer old = numberOfMailsSent.get(this);
numberOfMailsSent.put(this, old == null ? 1 : old + 1);
}
}
};
}
public static class MailServerReplicationTestSetUp extends AbstractServerReplicationTestSetUp<MailService, MailServiceImpl> {
public MailServerReplicationTestSetUp() {
super();
}
public MailServerReplicationTestSetUp(int servletPort) {
super(servletPort);
}
@Override
protected MailServiceImpl createNewMaster() throws MailException {
return createMailCountingService(true);
}
@Override
protected MailServiceImpl createNewReplica() throws MailException {
return createMailCountingService(false);
}
}
}
+2 -1
View File
@@ -6,7 +6,8 @@ Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: SAP
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-Activator: com.sap.sse.mail.impl.Activator
Export-Package: com.sap.sse.mail
Export-Package: com.sap.sse.mail,
com.sap.sse.mail.impl
Import-Package: javax.mail,
javax.mail.internet,
org.osgi.framework
@@ -6,6 +6,7 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@@ -26,9 +27,8 @@ import com.sap.sse.replication.OperationExecutionListener;
import com.sap.sse.replication.OperationWithResult;
import com.sap.sse.replication.ReplicationMasterDescriptor;
import com.sap.sse.replication.impl.OperationWithResultWithIdWrapper;
import com.sap.sse.util.ClearStateTestSupport;
public class MailServiceImpl implements ReplicableMailService, ClearStateTestSupport {
public class MailServiceImpl implements ReplicableMailService {
private static final Logger logger = Logger.getLogger(MailServiceImpl.class.getName());
private Properties mailProperties;
@@ -39,7 +39,7 @@ public class MailServiceImpl implements ReplicableMailService, ClearStateTestSup
*/
private ReplicationMasterDescriptor replicatingFromMaster;
private final ConcurrentHashMap<OperationExecutionListener<ReplicableMailService>, OperationExecutionListener<ReplicableMailService>> operationExecutionListeners;
private Set<OperationWithResultWithIdWrapper<?, ?>> operationsSentToMasterForReplication;
private final Set<OperationWithResultWithIdWrapper<?, ?>> operationsSentToMasterForReplication = new HashSet<>();
private ThreadLocal<Boolean> currentlyFillingFromInitialLoadOrApplyingOperationReceivedFromMaster = ThreadLocal
.withInitial(() -> false);
@@ -56,12 +56,17 @@ public class MailServiceImpl implements ReplicableMailService, ClearStateTestSup
}
}
private static interface ContentSetter {
protected static interface ContentSetter {
void setContent(MimeMessage msg) throws MessagingException;
}
private void internalSendMail(String toAddress, String subject, ContentSetter contentSetter) throws MailException {
if (mailProperties != null && mailProperties.containsKey("mail.transport.protocol")) {
private boolean canSendMail() {
return mailProperties != null && mailProperties.containsKey("mail.transport.protocol");
}
//protected for testing purposes
protected void internalSendMail(String toAddress, String subject, ContentSetter contentSetter) throws MailException {
if (canSendMail()) {
if (toAddress != null) {
Session session = Session.getInstance(mailProperties, new SMTPAuthenticator());
MimeMessage msg = new MimeMessage(session);
@@ -201,9 +206,4 @@ public class MailServiceImpl implements ReplicableMailService, ClearStateTestSup
this.currentlyFillingFromInitialLoadOrApplyingOperationReceivedFromMaster
.set(currentlyFillingFromInitialLoadOrApplyingOperationReceivedFromMaster);
}
@Override
public void clearState() throws Exception {
mailProperties.clear();
}
}
@@ -6,10 +6,14 @@ Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: SAP
Fragment-Host: com.sap.sse.security
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Require-Bundle: org.junit4;bundle-version="4.8.2",
Require-Bundle: org.hamcrest;bundle-version="1.1.0",
org.junit4;bundle-version="4.8.2",
com.sap.sse.security.userstore.mongodb,
com.sap.sse.mongodb,
com.rabbitmq.client,
org.mongodb.mongo-java-driver;bundle-version="2.13.0",
com.sap.sse.replication.testsupport,
com.sap.sse.mail.replication.testsupport
com.sap.sse.mail.replication.testsupport,
org.objenesis;bundle-version="2.1.0",
org.mockito.mockito-core;bundle-version="1.10.14"
Import-Package: org.osgi.util.tracker;version="1.5.1"
@@ -4,6 +4,7 @@ import java.io.IOException;
import java.net.MalformedURLException;
import com.sap.sse.mongodb.MongoDBService;
import com.sap.sse.replication.testsupport.AbstractServerReplicationTestSetUp;
import com.sap.sse.replication.testsupport.AbstractServerWithSingleServiceReplicationTest;
import com.sap.sse.security.SecurityService;
import com.sap.sse.security.impl.SecurityServiceImpl;
@@ -11,10 +12,10 @@ import com.sap.sse.security.userstore.mongodb.UserStoreImpl;
public abstract class AbstractSecurityReplicationTest extends AbstractServerWithSingleServiceReplicationTest<SecurityService, SecurityServiceImpl> {
public AbstractSecurityReplicationTest() {
super(new ServerReplicationTestSetUp());
super(new SecurityServerReplicationTestSetUp());
}
private static class ServerReplicationTestSetUp extends com.sap.sse.replication.testsupport.AbstractServerReplicationTestSetUp<SecurityService, SecurityServiceImpl> {
public static class SecurityServerReplicationTestSetUp extends AbstractServerReplicationTestSetUp<SecurityService, SecurityServiceImpl> {
private MongoDBService mongoDBService;
@Override
@@ -0,0 +1,148 @@
package com.sap.sse.security.replication.test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import java.io.IOException;
import java.net.MalformedURLException;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.osgi.util.tracker.ServiceTracker;
import com.sap.sse.common.mail.MailException;
import com.sap.sse.mail.MailService;
import com.sap.sse.mail.impl.MailServiceImpl;
import com.sap.sse.mail.replication.testsupport.AbstractMailReplicationTest;
import com.sap.sse.replication.testsupport.AbstractServerWithMultipleServicesReplicationTest;
import com.sap.sse.security.SecurityService;
import com.sap.sse.security.impl.SecurityServiceImpl;
import com.sap.sse.security.shared.UserManagementException;
import com.sap.sse.security.userstore.mongodb.UserStoreImpl;
/**
* See if transitively triggered email operation ultimately behaves well and results in only one outbound email.
*
* @author Fredrik Teschke
*/
public class SecurityReplicationLeadingToEmailReplicationTest extends AbstractServerWithMultipleServicesReplicationTest {
private static final SecurityServerReplicationTestSetUp securitySetUp = new SecurityServerReplicationTestSetUp();
private static final MailServerReplicationTestSetUp mailSetUp = new MailServerReplicationTestSetUp();
private static MailServiceImpl masterMailService;
private static MailServiceImpl replicaMailService;
@Before
@Override
public void setUp() throws Exception {
masterMailService = AbstractMailReplicationTest.createMailCountingService(true);
replicaMailService = AbstractMailReplicationTest.createMailCountingService(false);
super.setUp();
}
private static class SecurityServerReplicationTestSetUp extends
AbstractSecurityReplicationTest.SecurityServerReplicationTestSetUp {
@Override
protected SecurityServiceImpl createNewMaster() throws MalformedURLException, IOException, InterruptedException {
@SuppressWarnings("unchecked")
ServiceTracker<MailService, MailService> trackerMock = mock(ServiceTracker.class);
doReturn(masterMailService).when(trackerMock).getService();
SecurityServiceImpl result = new SecurityServiceImpl(trackerMock, new UserStoreImpl());
result.clearReplicaState();
return result;
}
@Override
protected SecurityServiceImpl createNewReplica() {
@SuppressWarnings("unchecked")
ServiceTracker<MailService, MailService> trackerMock = mock(ServiceTracker.class);
doReturn(replicaMailService).when(trackerMock).getService();
SecurityServiceImpl result = new SecurityServiceImpl(trackerMock, new UserStoreImpl());
return result;
}
}
private static class MailServerReplicationTestSetUp extends AbstractMailReplicationTest.MailServerReplicationTestSetUp {
private static final int SERVLET_PORT = 9991;
public MailServerReplicationTestSetUp() {
super(SERVLET_PORT);
}
@Override
protected MailServiceImpl createNewMaster() throws MailException {
return masterMailService;
}
@Override
protected MailServiceImpl createNewReplica() throws MailException {
return replicaMailService;
}
}
public SecurityReplicationLeadingToEmailReplicationTest() {
testSetUps.add(securitySetUp);
testSetUps.add(mailSetUp);
}
/**
* See if sending an email (triggered by creating the user on the master) results in only one mail being sent
* (on the master).
*/
@Test
public void triggerEmailSendByAddingUserOnMaster()
throws UserManagementException, MailException, IllegalAccessException, InterruptedException {
SecurityService masterSecurityService = securitySetUp.getMaster();
final String username = "Ernie";
final String email = "ernie@sesame-street.com";
final String password = "BertMyFriend";
final String validationBaseURL = null; //so that validation email is not sent
masterSecurityService.createSimpleUser(username, email, password, validationBaseURL);
masterSecurityService.sendMail(username, "subject", "body");
securitySetUp.getReplicaReplicator().waitUntilQueueIsEmpty();
mailSetUp.getReplicaReplicator().waitUntilQueueIsEmpty();
Thread.sleep(3000);
assertThat("mail was not sent on replica",
AbstractMailReplicationTest.numberOfMailsSent.get(replicaMailService), equalTo(null));
assertThat("mail was sent on master",
AbstractMailReplicationTest.numberOfMailsSent.get(masterMailService), equalTo(1));
}
/**
* Try the other way round - have to ignore for now, no replication test seems to try applying
* operations on the replica.
*/
@Test
@Ignore
public void triggerEmailSendByAddingUserOnReplica()
throws UserManagementException, MailException, IllegalAccessException, InterruptedException {
SecurityService replicaSecurityService = securitySetUp.getReplica();
final String username = "Ernie";
final String email = "ernie@sesame-street.com";
final String password = "BertMyFriend";
final String validationBaseURL = null; //so that validation email is not sent
replicaSecurityService.createSimpleUser(username, email, password, validationBaseURL);
replicaSecurityService.sendMail(username, "subject", "body");
securitySetUp.getReplicaReplicator().waitUntilQueueIsEmpty();
mailSetUp.getReplicaReplicator().waitUntilQueueIsEmpty();
Thread.sleep(3000);
assertThat("mail was not sent on replica",
AbstractMailReplicationTest.numberOfMailsSent.get(replicaMailService), equalTo(null));
assertThat("mail was sent on master",
AbstractMailReplicationTest.numberOfMailsSent.get(masterMailService), equalTo(1));
}
}
@@ -185,6 +185,10 @@ public class SecurityServiceImpl extends RemoteServiceServlet implements Replica
}
}
}
private MailService getMailService() {
return mailServiceTracker == null ? null : mailServiceTracker.getService();
}
@Override
public void sendMail(String username, String subject, String body) throws MailException {
@@ -192,7 +196,12 @@ public class SecurityServiceImpl extends RemoteServiceServlet implements Replica
if (user != null) {
final String toAddress = user.getEmail();
if (toAddress != null) {
mailServiceTracker.getService().sendMail(username, subject, body);
MailService mailService = getMailService();
if (mailService == null) {
logger.warning(String.format("Could not send mail to user %s: no MailService found", username));
} else {
getMailService().sendMail(username, subject, body);
}
}
}
}
+3 -1
View File
@@ -16,7 +16,9 @@
<module>com.sap.sse</module>
<module>com.sap.sse.common</module>
<module>com.sap.sse.shared.android</module>
<module>com.sap.sse.mail</module>
<module>com.sap.sse.mail</module>
<module>com.sap.sse.mail.replication.test</module>
<module>com.sap.sse.mail.replicadtion.testsupport</module>
<module>com.sap.sse.mongodb</module>
<module>com.sap.sse.gwt</module>
<module>com.sap.sse.gwt.adminconsole</module>