From 305dfdf9d5e94aa8eeb16ca43dc9e52f529fa7b7 Mon Sep 17 00:00:00 2001
From: Joze RIHTARSIC <joze.RIHTARSIC@ext.ec.europa.eu>
Date: Tue, 10 May 2022 09:02:12 +0200
Subject: [PATCH] Add AlertService unit tests.

---
 .gitignore                                    |  71 ++--
 .../edelivery/smp/services/AlertService.java  | 273 ++++++------
 .../smp/services/ConfigurationService.java    |   2 +-
 .../smp/utils/SessionSecurityUtils.java       |   2 +-
 .../smp/data/dao/UserDaoIntegrationTest.java  |   2 +-
 .../AbstractServiceIntegrationTest.java       |   8 +-
 .../smp/services/AlertServiceTest.java        | 402 ++++++++++++++++++
 ...ServiceMultipleDomainsIntegrationTest.java |   8 +-
 ...oupServiceSingleDomainIntegrationTest.java |  20 +-
 .../edelivery/smp/testutil/TestConstants.java |  28 +-
 .../edelivery/smp/testutil/TestDBUtils.java   |   9 +-
 11 files changed, 625 insertions(+), 200 deletions(-)
 create mode 100644 smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/AlertServiceTest.java

diff --git a/.gitignore b/.gitignore
index 072c92ae0..73684590b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,32 +1,41 @@
-common/cipa-commons-busdox/target/
-common/cipa-edelivery-common/target/classes/eu/europa/ec/cipa/common/business/AbstractBusinessImpl.class
-common/cipa-edelivery-common/target/classes/eu/europa/ec/cipa/common/exception/BusinessException.class
-common/cipa-edelivery-common/target/classes/eu/europa/ec/cipa/common/logging/ILogEvent.class
-common/cipa-edelivery-common/target/classes/eu/europa/ec/cipa/common/logging/impl/AbstractLoggerImpl.class
-common/cipa-edelivery-common/target/classes/eu/europa/ec/cipa/common/logging/impl/LoggingServiceImpl.class
-common/cipa-edelivery-common/target/
-common/cipa-peppol-types/target/
-sml/cipa-sml-client-library/target/
-sml/cipa-sml-server-library/target/
-sml/cipa-sml-webapp/target/
-smp/.idea/libraries/Maven__com_sun_jersey_jersey_client_1_17_1.xml
-smp/.idea/libraries/
-smp/cipa-smp-client-library/target/
-smp/cipa-smp-full-webapp/target/
-smp/cipa-smp-readonly-webapp/target/
-smp/cipa-smp-server-library/target/
-smp/.idea/
-smp-api/target/
-smp-api/.idea/copyright/profiles_settings.xml
-smp-api/.idea/libraries/Maven__junit_junit_4_12.xml
-smp-api/.idea/libraries/
-smp-api/.idea/
-*.iml
-addons/cipa-sml-util/.idea/
-addons/cipa-sml-util/target/
-addons/cipa-sml-migration/target/test-classes/global-truststore.jks
-addons/cipa-sml-migration/target/classes/
-addons/cipa-sml-migration/target/
-addons/cipa-sml-migration/.idea/
+## Eclipse config files ##
+.settings/
+.classpath
+.project
+
+## PMD Eclipse plugin files ##
+.pmd
+
+## Maven output folder ##
+target/
+
+#temporary output of the cargo plugin
+cargo
+
+## Compiled java classes ##
+*.class
+*.jar
+*.war
+*.ear
+
+## generic files to ignore
+*~
+*.lock
+*.DS_Store
+*.swp
+*.out
+*.tmp
+*.log
+
+## Intellij IDEA files 
 .idea/
-code-coverage
\ No newline at end of file
+*.iml
+*.ipr
+*.iws
+
+## Files generated by checksum plugin
+*.sha256
+
+## maven-shade-plugin generated files
+dependency-reduced-pom.xml
+/.idea/
diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/AlertService.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/AlertService.java
index b16650985..c314ac2ef 100644
--- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/AlertService.java
+++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/AlertService.java
@@ -54,9 +54,9 @@ public class AlertService {
         AlertLevelEnum alertLevel = configurationService.getAlertBeforeExpirePasswordLevel();
         AlertTypeEnum alertType = AlertTypeEnum.CREDENTIAL_IMMINENT_EXPIRATION;
 
-        alertCredentialExpiration(mailSubject, mailTo,
-                credentialType, credentialId, expiredOn,
-                alertLevel, alertType);
+        DBAlert alert = createAlert(mailSubject, mailTo, alertLevel, alertType);
+
+        alertCredentialExpiration(alert, credentialType, credentialId, expiredOn);
     }
 
     public void alertUsernamePasswordExpired(DBUser user) {
@@ -73,9 +73,9 @@ public class AlertService {
         AlertLevelEnum alertLevel = configurationService.getAlertExpiredPasswordLevel();
         AlertTypeEnum alertType = AlertTypeEnum.CREDENTIAL_EXPIRED;
 
-        alertCredentialExpiration(mailSubject, mailTo,
-                credentialType, credentialId, expiredOn,
-                alertLevel, alertType);
+        DBAlert alert = createAlert(mailSubject, mailTo, alertLevel, alertType);
+
+        alertCredentialExpiration(alert, credentialType, credentialId, expiredOn);
     }
 
     public void alertBeforeAccessTokenExpire(DBUser user) {
@@ -94,9 +94,8 @@ public class AlertService {
         AlertLevelEnum alertLevel = configurationService.getAlertBeforeExpireAccessTokenLevel();
         AlertTypeEnum alertType = AlertTypeEnum.CREDENTIAL_IMMINENT_EXPIRATION;
 
-        alertCredentialExpiration(mailSubject, mailTo,
-                credentialType, credentialId, expiredOn,
-                alertLevel, alertType);
+        DBAlert alert = createAlert(mailSubject, mailTo, alertLevel, alertType);
+        alertCredentialExpiration(alert, credentialType, credentialId, expiredOn);
     }
 
     public void alertAccessTokenExpired(DBUser user) {
@@ -115,17 +114,15 @@ public class AlertService {
         AlertLevelEnum alertLevel = configurationService.getAlertExpiredAccessTokenLevel();
         AlertTypeEnum alertType = AlertTypeEnum.CREDENTIAL_EXPIRED;
 
-        alertCredentialExpiration(mailSubject, mailTo,
-                credentialType, credentialId, expiredOn,
-                alertLevel, alertType);
+        DBAlert alert = createAlert(mailSubject, mailTo, alertLevel, alertType);
+        alertCredentialExpiration(alert, credentialType, credentialId, expiredOn);
     }
 
 
     public void alertBeforeCertificateExpire(DBUser user) {
-        LOG.info("Alert Certificate [{}] for user [{}] is about to expire on [{}]",
+        LOG.info("Alert Certificate [{}] for user [{}] is about to expire",
                 user.getCertificate().getCertificateId(),
-                user.getUsername(),
-                ISO_LOCAL_DATE_TIME.format(user.getAccessTokenExpireOn()));
+                user.getUsername());
 
         String mailTo = user.getEmailAddress();
         CredentialTypeEnum credentialType = CredentialTypeEnum.CERTIFICATE;
@@ -137,16 +134,14 @@ public class AlertService {
         AlertLevelEnum alertLevel = configurationService.getAlertBeforeExpireCertificateLevel();
         AlertTypeEnum alertType = AlertTypeEnum.CREDENTIAL_IMMINENT_EXPIRATION;
 
-        alertCredentialExpiration(mailSubject, mailTo,
-                credentialType, credentialId, expiredOn,
-                alertLevel, alertType);
+        DBAlert alert = createAlert(mailSubject, mailTo, alertLevel, alertType);
+        alertCredentialExpiration(alert, credentialType, credentialId, expiredOn);
     }
 
     public void alertCertificateExpired(DBUser user) {
-        LOG.info("Alert Certificate [{}] for user [{}] expired on [{}]",
+        LOG.info("Alert Certificate [{}] for user [{}] expired",
                 user.getCertificate().getCertificateId(),
-                user.getUsername(),
-                ISO_LOCAL_DATE_TIME.format(user.getAccessTokenExpireOn()));
+                user.getUsername());
 
         String mailTo = user.getEmailAddress();
         CredentialTypeEnum credentialType = CredentialTypeEnum.CERTIFICATE;
@@ -158,168 +153,184 @@ public class AlertService {
         AlertLevelEnum alertLevel = configurationService.getAlertExpiredCertificateLevel();
         AlertTypeEnum alertType = AlertTypeEnum.CREDENTIAL_EXPIRED;
 
-        alertCredentialExpiration(mailSubject, mailTo,
-                credentialType, credentialId, expiredOn,
-                alertLevel, alertType);
-    }
-
-
-    public void alertCredentialExpiration(String mailSubject,
-                                          String mailTo,
-                                          CredentialTypeEnum credentialType,
-                                          String credentialId,
-                                          OffsetDateTime expirationDate,
-                                          AlertLevelEnum level,
-                                          AlertTypeEnum alertType) {
-
-        // create alert
-        OffsetDateTime reportDate = OffsetDateTime.now();
-        String serverName = HttpUtils.getServerAddress();
-
-        DBAlert alert = new DBAlert();
-        alert.setProcessed(false);
-        alert.setMailSubject(mailSubject);
-        alert.setMailTo(mailTo);
-        alert.setReportingTime(reportDate);
-        alert.setAlertType(alertType);
-        alert.setAlertLevel(level);
-        alert.addProperty(CredentialsExpirationProperties.CREDENTIAL_TYPE.name(), credentialType.name());
-        alert.addProperty(CredentialsExpirationProperties.CREDENTIAL_ID.name(), credentialId);
-        alert.addProperty(CredentialsExpirationProperties.EXPIRATION_DATETIME.name(), expirationDate);
-        alert.addProperty(CredentialsExpirationProperties.REPORTING_DATETIME.name(), reportDate);
-        alert.addProperty(CredentialsExpirationProperties.ALERT_LEVEL.name(), level.name());
-        alert.addProperty(CredentialsExpirationProperties.SERVER_NAME.name(), serverName);
-        alertDao.persistFlushDetach(alert);
-        // submit alerts
-        submitAlertMail(alert);
-    }
-
-    public void submitAlertMail(DBAlert alert) {
-        String mailTo = alert.getMailTo();
-        if (StringUtils.isBlank(mailTo)) {
-            LOG.warn("Can not send mail (empty mail) for alert [{}]!", alert);
-            return;
-        }
-
-        String mailFrom = configurationService.getAlertEmailFrom();
-        PropertiesMailModel props = new PropertiesMailModel(alert);
-        mailService.sendMail(props, mailFrom, alert.getMailTo());
-
-        alert.setProcessed(true);
-        alert.setProcessedTime(OffsetDateTime.now());
-        alertDao.update(alert);
+        DBAlert alert = createAlert(mailSubject, mailTo, alertLevel, alertType);
+        alertCredentialExpiration(alert, credentialType, credentialId, expiredOn);
     }
 
-    public void alertCredentialVerificationFailed(DBUser user,CredentialTypeEnum credentialType) {
+    public void alertCredentialVerificationFailed(DBUser user, CredentialTypeEnum credentialType) {
         Boolean loginFailureEnabled = configurationService.getAlertUserLoginFailureEnabled();
         if (!loginFailureEnabled) {
-            LOG.debug("Alert Login failure is disabled!" );
+            LOG.debug("Alert Login failure is disabled!");
             return;
         }
 
         String mailTo = user.getEmailAddress();
-        String mailSubject = configurationService.getAlertBeforeUserSuspendedSubject();
-        AlertLevelEnum level = configurationService.getAlertUserSuspendedLevel();
+        String mailSubject = configurationService.getAlertUserSuspendedSubject();
+        AlertLevelEnum alertLevel = configurationService.getAlertUserSuspendedLevel();
         AlertTypeEnum alertType = AlertTypeEnum.CREDENTIAL_VERIFICATION_FAILED;
-        Integer failureCount = user.getSequentialLoginFailureCount();
-        OffsetDateTime lastFailedLoginDate = user.getLastFailedLoginAttempt();
-        String credentialId = user.getUsername();
 
-        alertCredentialVerificationFailed(mailSubject, mailTo,
+        Integer failureCount;
+        OffsetDateTime lastFailedLoginDate;
+        String credentialId;
+
+        if(credentialType == CredentialTypeEnum.ACCESS_TOKEN) {
+            failureCount = user.getSequentialTokenLoginFailureCount();
+            lastFailedLoginDate = user.getLastTokenFailedLoginAttempt();
+            credentialId = user.getAccessTokenIdentifier();
+        }else if(credentialType == CredentialTypeEnum.USERNAME_PASSWORD) {
+            failureCount = user.getSequentialLoginFailureCount();
+            lastFailedLoginDate = user.getLastFailedLoginAttempt();
+            credentialId = user.getUsername();
+        } else {
+            LOG.error("Alert for suspended credentials type [{}] is not supported", credentialType);
+            return;
+        }
+        DBAlert alert = createAlert(mailSubject, mailTo, alertLevel, alertType);
+        alertCredentialVerificationFailed(alert,
                 credentialType, credentialId,
-                failureCount, lastFailedLoginDate,
-                level, alertType);
+                failureCount, lastFailedLoginDate);
     }
 
     public void alertCredentialsSuspended(DBUser user, CredentialTypeEnum credentialType) {
         Boolean suspensionAlertEnabled = configurationService.getAlertUserSuspendedEnabled();
         if (!suspensionAlertEnabled) {
-            LOG.debug("Alert suspended is disabled!" );
+            LOG.debug("Alert suspended is disabled!");
             return;
         }
 
         String mailTo = user.getEmailAddress();
-        String mailSubject = configurationService.getAlertBeforeUserSuspendedSubject();
-        AlertLevelEnum level = configurationService.getAlertUserSuspendedLevel();
+        String mailSubject = configurationService.getAlertUserSuspendedSubject();
+        AlertLevelEnum alertLevel = configurationService.getAlertUserSuspendedLevel();
         AlertTypeEnum alertType = AlertTypeEnum.CREDENTIAL_SUSPENDED;
-        Integer failureCount = user.getSequentialLoginFailureCount();
-        OffsetDateTime lastFailedLoginDate = user.getLastFailedLoginAttempt();
-        OffsetDateTime suspendedUtil = lastFailedLoginDate.plusSeconds(configurationService.getLoginSuspensionTimeInSeconds());
-        String credentialId = user.getUsername();
 
-        alertCredentialSuspended(mailSubject, mailTo,
+        Integer failureCount;
+        OffsetDateTime lastFailedLoginDate;
+        OffsetDateTime suspendedUtil;
+        String credentialId;
+        if(credentialType == CredentialTypeEnum.ACCESS_TOKEN) {
+            failureCount = user.getSequentialTokenLoginFailureCount();
+            lastFailedLoginDate = user.getLastTokenFailedLoginAttempt();
+            suspendedUtil = lastFailedLoginDate.plusSeconds(configurationService.getAccessTokenLoginSuspensionTimeInSeconds());
+            credentialId = user.getAccessTokenIdentifier();
+        }else if(credentialType == CredentialTypeEnum.USERNAME_PASSWORD) {
+            failureCount = user.getSequentialLoginFailureCount();
+            lastFailedLoginDate = user.getLastFailedLoginAttempt();
+            suspendedUtil = lastFailedLoginDate.plusSeconds(configurationService.getLoginSuspensionTimeInSeconds());
+            credentialId = user.getUsername();
+        } else {
+            LOG.error("Alert for suspended credentials type [{}] is not supported", credentialType);
+            return;
+        }
+        DBAlert alert = createAlert(mailSubject, mailTo, alertLevel, alertType);
+
+        alertCredentialSuspended(alert,
                 credentialType, credentialId,
-                failureCount, lastFailedLoginDate, suspendedUtil,
-                level, alertType);
+                failureCount, lastFailedLoginDate, suspendedUtil);
     }
 
-    public void alertCredentialVerificationFailed(String mailSubject,
-                                         String mailTo,
-                                         CredentialTypeEnum credentialType,
-                                         String credentialId,
-                                         Integer failedLoginCount,
-                                         OffsetDateTime lastFailedLoginDate,
-                                         AlertLevelEnum level,
-                                         AlertTypeEnum alertType) {
-
-        Boolean suspensionAlertEnabled = configurationService.getAlertUserLoginFailureEnabled();
-        if (!suspensionAlertEnabled) {
-            LOG.debug("Alert suspended is disabled!" );
-            return;
-        }
+    public void alertCredentialExpiration(DBAlert alert,
+                                          CredentialTypeEnum credentialType,
+                                          String credentialId,
+                                          OffsetDateTime expirationDate
+                                          ) {
 
-        OffsetDateTime reportDate = OffsetDateTime.now();
         String serverName = HttpUtils.getServerAddress();
+        // add alert properties
+        alert.addProperty(CredentialsExpirationProperties.CREDENTIAL_TYPE.name(), credentialType.name());
+        alert.addProperty(CredentialsExpirationProperties.CREDENTIAL_ID.name(), credentialId);
+        alert.addProperty(CredentialsExpirationProperties.EXPIRATION_DATETIME.name(), expirationDate);
+        alert.addProperty(CredentialsExpirationProperties.REPORTING_DATETIME.name(), alert.getReportingTime());
+        alert.addProperty(CredentialsExpirationProperties.ALERT_LEVEL.name(), alert.getAlertLevel().name());
+        alert.addProperty(CredentialsExpirationProperties.SERVER_NAME.name(), serverName);
+        alertDao.persistFlushDetach(alert);
+        // submit alerts
+        submitAlertMail(alert);
+    }
 
-        DBAlert alert = new DBAlert();
-        alert.setProcessed(false);
-        alert.setMailSubject(mailSubject);
-        alert.setMailTo(mailTo);
-        alert.setReportingTime(reportDate);
-        alert.setAlertType(alertType);
-        alert.setAlertLevel(level);
+    public void alertCredentialVerificationFailed(DBAlert alert,
+                                                  CredentialTypeEnum credentialType,
+                                                  String credentialId,
+                                                  Integer failedLoginCount,
+                                                  OffsetDateTime lastFailedLoginDate
+                                                  ) {
+        String serverName = HttpUtils.getServerAddress();
+        // add alert properties
         alert.addProperty(CredentialVerificationFailedProperties.CREDENTIAL_TYPE.name(), credentialType.name());
         alert.addProperty(CredentialVerificationFailedProperties.CREDENTIAL_ID.name(), credentialId);
         alert.addProperty(CredentialVerificationFailedProperties.FAILED_LOGIN_ATTEMPT.name(), failedLoginCount.toString());
         alert.addProperty(CredentialVerificationFailedProperties.LAST_LOGIN_FAILURE_DATETIME.name(), lastFailedLoginDate);
-        alert.addProperty(CredentialVerificationFailedProperties.REPORTING_DATETIME.name(), reportDate);
-        alert.addProperty(CredentialVerificationFailedProperties.ALERT_LEVEL.name(), level.name());
+        alert.addProperty(CredentialVerificationFailedProperties.REPORTING_DATETIME.name(), alert.getReportingTime());
+        alert.addProperty(CredentialVerificationFailedProperties.ALERT_LEVEL.name(), alert.getAlertLevel().name());
         alert.addProperty(CredentialVerificationFailedProperties.SERVER_NAME.name(), serverName);
         alertDao.persistFlushDetach(alert);
         // submit alerts
         submitAlertMail(alert);
     }
-    public void alertCredentialSuspended(String mailSubject,
-                                         String mailTo,
+
+    public void alertCredentialSuspended(DBAlert alert,
                                          CredentialTypeEnum credentialType,
                                          String credentialId,
                                          Integer failedLoginCount,
                                          OffsetDateTime lastFailedLoginDate,
-                                         OffsetDateTime suspendedUtil,
-                                         AlertLevelEnum level,
-                                         AlertTypeEnum alertType) {
+                                         OffsetDateTime suspendedUtil) {
 
-        OffsetDateTime reportDate = OffsetDateTime.now();
         String serverName = HttpUtils.getServerAddress();
-
-        DBAlert alert = new DBAlert();
-        alert.setProcessed(false);
-        alert.setMailSubject(mailSubject);
-        alert.setMailTo(mailTo);
-        alert.setReportingTime(reportDate);
-        alert.setAlertType(alertType);
-        alert.setAlertLevel(level);
+        // add alert properties
         alert.addProperty(CredentialSuspendedProperties.CREDENTIAL_TYPE.name(), credentialType.name());
         alert.addProperty(CredentialSuspendedProperties.CREDENTIAL_ID.name(), credentialId);
         alert.addProperty(CredentialSuspendedProperties.FAILED_LOGIN_ATTEMPT.name(), failedLoginCount.toString());
         alert.addProperty(CredentialSuspendedProperties.LAST_LOGIN_FAILURE_DATETIME.name(), lastFailedLoginDate);
         alert.addProperty(CredentialSuspendedProperties.SUSPENDED_UNTIL_DATETIME.name(), suspendedUtil);
-        alert.addProperty(CredentialSuspendedProperties.REPORTING_DATETIME.name(), reportDate);
-        alert.addProperty(CredentialSuspendedProperties.ALERT_LEVEL.name(), level.name());
+        alert.addProperty(CredentialSuspendedProperties.REPORTING_DATETIME.name(), alert.getReportingTime());
+        alert.addProperty(CredentialSuspendedProperties.ALERT_LEVEL.name(), alert.getAlertLevel().name());
         alert.addProperty(CredentialSuspendedProperties.SERVER_NAME.name(), serverName);
         alertDao.persistFlushDetach(alert);
         // submit alerts
         submitAlertMail(alert);
     }
 
+    /**
+     *  Create Alert DB entity
+     *
+     * @param mailSubject
+     * @param mailTo
+     * @param level
+     * @param alertType
+     * @return
+     */
+    protected DBAlert createAlert(String mailSubject,
+                                String mailTo,
+                                AlertLevelEnum level,
+                                AlertTypeEnum alertType) {
+
+        DBAlert alert = new DBAlert();
+        alert.setProcessed(false);
+        alert.setMailSubject(mailSubject);
+        alert.setMailTo(mailTo);
+        alert.setReportingTime(OffsetDateTime.now());
+        alert.setAlertType(alertType);
+        alert.setAlertLevel(level);
+        return alert;
+    }
+
+    /**
+     * Submit mail  for the alert
+     * @param alert
+     */
+    public void submitAlertMail(DBAlert alert) {
+        String mailTo = alert.getMailTo();
+        if (StringUtils.isBlank(mailTo)) {
+            LOG.warn("Can not send mail (empty mail) for alert [{}]!", alert);
+            return;
+        }
+
+        String mailFrom = configurationService.getAlertEmailFrom();
+        PropertiesMailModel props = new PropertiesMailModel(alert);
+        mailService.sendMail(props, mailFrom, alert.getMailTo());
+
+        alert.setProcessed(true);
+        alert.setProcessedTime(OffsetDateTime.now());
+        alertDao.update(alert);
+    }
+
 }
diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ConfigurationService.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ConfigurationService.java
index 3d26225b4..f1d11243c 100644
--- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ConfigurationService.java
+++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ConfigurationService.java
@@ -351,7 +351,7 @@ public class ConfigurationService {
         return AlertLevelEnum.valueOf(level);
     }
 
-    public String getAlertBeforeUserSuspendedSubject() {
+    public String getAlertUserSuspendedSubject() {
         return (String) configurationDAO.getCachedPropertyValue(ALERT_USER_SUSPENDED_MAIL_SUBJECT);
     }
 
diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/utils/SessionSecurityUtils.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/utils/SessionSecurityUtils.java
index ae803480f..4753cda3c 100644
--- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/utils/SessionSecurityUtils.java
+++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/utils/SessionSecurityUtils.java
@@ -27,7 +27,7 @@ public class SessionSecurityUtils {
      * '
      * Currently authentication tokens supported to create na UI session.
      */
-    public static final List<Class> sessionAuthenticationClasses = Arrays.asList(SMPAuthenticationToken.class,
+    protected static final List<Class> sessionAuthenticationClasses = Arrays.asList(SMPAuthenticationToken.class,
             CasAuthenticationToken.class);
 
     /**
diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/data/dao/UserDaoIntegrationTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/data/dao/UserDaoIntegrationTest.java
index 1e016c0af..68be572b2 100644
--- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/data/dao/UserDaoIntegrationTest.java
+++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/data/dao/UserDaoIntegrationTest.java
@@ -136,7 +136,7 @@ public class UserDaoIntegrationTest extends AbstractBaseDao {
         testInstance.persistFlushDetach(u);
 
         //test
-        Optional<DBUser> ou = testInstance.findUserByIdentifier(TestConstants.USERNAME_1);
+        Optional<DBUser> ou = testInstance.findUserByIdentifier(TestConstants.USERNAME_TOKEN_1);
         assertNotSame(u , ou.get());
         assertEquals(u, ou.get());
         assertEquals(u.getEmailAddress(), ou.get().getEmailAddress());
diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/AbstractServiceIntegrationTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/AbstractServiceIntegrationTest.java
index 1544e94e4..9bd050a3b 100644
--- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/AbstractServiceIntegrationTest.java
+++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/AbstractServiceIntegrationTest.java
@@ -11,6 +11,7 @@ import eu.europa.ec.edelivery.smp.data.model.DBServiceGroup;
 import eu.europa.ec.edelivery.smp.data.model.DBServiceMetadata;
 import eu.europa.ec.edelivery.smp.data.model.DBUser;
 import eu.europa.ec.edelivery.smp.data.ui.enums.SMPPropertyEnum;
+import eu.europa.ec.edelivery.smp.services.mail.MailService;
 import eu.europa.ec.edelivery.smp.services.ui.UIKeystoreService;
 import eu.europa.ec.edelivery.smp.services.ui.UITruststoreService;
 import eu.europa.ec.edelivery.smp.sml.SmlConnector;
@@ -41,13 +42,14 @@ import static eu.europa.ec.edelivery.smp.testutil.TestConstants.*;
 
 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(classes = {H2JPATestConfig.class,
-        CaseSensitivityNormalizer.class,SmlConnector.class,ServiceMetadataSigner.class,
+        CaseSensitivityNormalizer.class,SmlConnector.class,ServiceMetadataSigner.class, MailService.class,
         ServiceGroupService.class, DomainService.class, ServiceMetadataService.class,
-        ServiceGroupDao.class,ServiceMetadataDao.class, DomainDao.class, UserDao.class,DBAssertion.class, ConfigurationDao.class,
+        ServiceGroupDao.class,ServiceMetadataDao.class, DomainDao.class, UserDao.class,DBAssertion.class, ConfigurationDao.class, AlertDao.class,
         UITruststoreService.class, UIKeystoreService.class, ConversionTestConfig.class, SMLIntegrationService.class,
         CRLVerifierService.class,
         ConfigurationService.class,
-        ServicesBeansConfiguration.class})
+        ServicesBeansConfiguration.class,
+        AlertService.class})
 @Sql(scripts = {"classpath:cleanup-database.sql",
         "classpath:basic_conf_data-h2.sql"
 }, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, config = @SqlConfig
diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/AlertServiceTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/AlertServiceTest.java
new file mode 100644
index 000000000..d9cadbf71
--- /dev/null
+++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/AlertServiceTest.java
@@ -0,0 +1,402 @@
+package eu.europa.ec.edelivery.smp.services;
+
+import eu.europa.ec.edelivery.smp.data.dao.AlertDao;
+import eu.europa.ec.edelivery.smp.data.model.DBAlert;
+import eu.europa.ec.edelivery.smp.data.model.DBUser;
+import eu.europa.ec.edelivery.smp.data.ui.enums.AlertLevelEnum;
+import eu.europa.ec.edelivery.smp.data.ui.enums.AlertTypeEnum;
+import eu.europa.ec.edelivery.smp.data.ui.enums.CredentialTypeEnum;
+import eu.europa.ec.edelivery.smp.services.mail.MailModel;
+import eu.europa.ec.edelivery.smp.services.mail.MailService;
+import eu.europa.ec.edelivery.smp.services.mail.prop.CredentialSuspendedProperties;
+import eu.europa.ec.edelivery.smp.services.mail.prop.CredentialVerificationFailedProperties;
+import eu.europa.ec.edelivery.smp.services.mail.prop.CredentialsExpirationProperties;
+import eu.europa.ec.edelivery.smp.testutil.TestDBUtils;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mockito;
+
+import java.time.OffsetDateTime;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Properties;
+import java.util.stream.Collectors;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+
+public class AlertServiceTest {
+
+    AlertDao alertDao = Mockito.mock(AlertDao.class);
+    MailService mailService = Mockito.mock(MailService.class);
+    ConfigurationService configurationService = Mockito.mock(ConfigurationService.class);
+
+
+    AlertService testInstance = new AlertService(alertDao, mailService, configurationService);
+
+    @Test
+    public void testCreateAlert() {
+        String mailSubject = "mailSubject";
+        String mailTo = "mailTo";
+        AlertLevelEnum level = AlertLevelEnum.MEDIUM;
+        AlertTypeEnum alertType = AlertTypeEnum.CREDENTIAL_EXPIRED;
+
+        DBAlert alert = testInstance.createAlert(mailSubject, mailTo, level, alertType);
+
+        assertNotNull(alert);
+        assertNull(alert.getId());
+        assertEquals(mailSubject, alert.getMailSubject());
+        assertEquals(mailTo, alert.getMailTo());
+        assertEquals(level, alert.getAlertLevel());
+        assertEquals(alertType, alert.getAlertType());
+        assertFalse(alert.isProcessed());
+        assertNotNull(alert.getReportingTime());
+
+    }
+
+    @Test
+    public void testSubmitAlertMailNoMail() {
+
+        DBAlert alert = new DBAlert();
+
+        testInstance.submitAlertMail(alert);
+
+        verify(mailService, Mockito.never()).sendMail(Mockito.any(), Mockito.anyString(), Mockito.anyString());
+    }
+
+    @Test
+    public void alertBeforeUsernamePasswordExpire() {
+        // given
+        DBUser user = TestDBUtils.createDBUser("alertBeforeUsernamePasswordExpire");
+        String mailSubject = "mail subject";
+        String mailFrom = "mail.from@test.eu";
+        AlertLevelEnum alertLevel = AlertLevelEnum.MEDIUM;
+        user.setPasswordExpireOn(OffsetDateTime.now().plusDays(1));
+        doReturn(mailSubject).when(configurationService).getAlertBeforeExpirePasswordMailSubject();
+        doReturn(alertLevel).when(configurationService).getAlertBeforeExpirePasswordLevel();
+        doReturn(mailFrom).when(configurationService).getAlertEmailFrom();
+
+        AlertTypeEnum alertType = AlertTypeEnum.CREDENTIAL_IMMINENT_EXPIRATION;
+        List<String> expectedTemplateProperties = Arrays.asList(CredentialsExpirationProperties.values()).stream()
+                .map(CredentialsExpirationProperties::name).collect(Collectors.toList());
+        // when
+        testInstance.alertBeforeUsernamePasswordExpire(user);
+        // then
+        assertAlertSend(alertType, user.getEmailAddress(), mailFrom, mailSubject,
+                expectedTemplateProperties);
+
+        verify(configurationService, times(1)).getAlertBeforeExpirePasswordMailSubject();
+        verify(configurationService, times(1)).getAlertBeforeExpirePasswordLevel();
+        verify(configurationService, times(1)).getAlertEmailFrom();
+    }
+
+    @Test
+    public void alertUsernamePasswordExpired() {
+        // given
+        DBUser user = TestDBUtils.createDBUser("alertUsernamePasswordExpired");
+        String mailSubject = "mail subject";
+        String mailFrom = "mail.from@test.eu";
+        AlertLevelEnum alertLevel = AlertLevelEnum.MEDIUM;
+        user.setPasswordExpireOn(OffsetDateTime.now().plusDays(1));
+        doReturn(mailSubject).when(configurationService).getAlertExpiredPasswordMailSubject();
+        doReturn(alertLevel).when(configurationService).getAlertExpiredPasswordLevel();
+        doReturn(mailFrom).when(configurationService).getAlertEmailFrom();
+        AlertTypeEnum alertType = AlertTypeEnum.CREDENTIAL_EXPIRED;
+        List<String> expectedTemplateProperties = Arrays.asList(CredentialsExpirationProperties.values()).stream()
+                .map(CredentialsExpirationProperties::name).collect(Collectors.toList());
+        // when
+        testInstance.alertUsernamePasswordExpired(user);
+        // then
+        assertAlertSend(alertType, user.getEmailAddress(), mailFrom, mailSubject,
+                expectedTemplateProperties);
+
+        verify(configurationService, times(1)).getAlertExpiredPasswordMailSubject();
+        verify(configurationService, times(1)).getAlertExpiredPasswordLevel();
+        verify(configurationService, times(1)).getAlertEmailFrom();
+    }
+
+    @Test
+    public void alertBeforeAccessTokenExpire() {
+        // given
+        DBUser user = TestDBUtils.createDBUser("alertBeforeAccessTokenExpire");
+        String mailSubject = "mail subject";
+        String mailFrom = "mail.from@test.eu";
+        AlertLevelEnum alertLevel = AlertLevelEnum.MEDIUM;
+        user.setAccessTokenExpireOn(OffsetDateTime.now().plusDays(1));
+        doReturn(mailSubject).when(configurationService).getAlertBeforeExpireAccessTokenMailSubject();
+        doReturn(alertLevel).when(configurationService).getAlertBeforeExpireAccessTokenLevel();
+        doReturn(mailFrom).when(configurationService).getAlertEmailFrom();
+
+        AlertTypeEnum alertType = AlertTypeEnum.CREDENTIAL_IMMINENT_EXPIRATION;
+        List<String> expectedTemplateProperties = Arrays.asList(CredentialsExpirationProperties.values()).stream()
+                .map(CredentialsExpirationProperties::name).collect(Collectors.toList());
+        // when
+        testInstance.alertBeforeAccessTokenExpire(user);
+        // then
+        assertAlertSend(alertType, user.getEmailAddress(), mailFrom, mailSubject,
+                expectedTemplateProperties);
+
+        verify(configurationService, times(1)).getAlertBeforeExpireAccessTokenMailSubject();
+        verify(configurationService, times(1)).getAlertBeforeExpireAccessTokenLevel();
+        verify(configurationService, times(1)).getAlertEmailFrom();
+    }
+
+    @Test
+    public void alertAccessTokenExpired() {
+        // given
+        DBUser user = TestDBUtils.createDBUser("alertAccessTokenExpired");
+        String mailSubject = "mail subject";
+        String mailFrom = "mail.from@test.eu";
+        AlertLevelEnum alertLevel = AlertLevelEnum.MEDIUM;
+        user.setAccessTokenExpireOn(OffsetDateTime.now().plusDays(1));
+        doReturn(mailSubject).when(configurationService).getAlertExpiredAccessTokenMailSubject();
+        doReturn(alertLevel).when(configurationService).getAlertExpiredAccessTokenLevel();
+        doReturn(mailFrom).when(configurationService).getAlertEmailFrom();
+        AlertTypeEnum alertType = AlertTypeEnum.CREDENTIAL_EXPIRED;
+        List<String> expectedTemplateProperties = Arrays.asList(CredentialsExpirationProperties.values()).stream()
+                .map(CredentialsExpirationProperties::name).collect(Collectors.toList());
+        // when
+        testInstance.alertAccessTokenExpired(user);
+        // then
+        assertAlertSend(alertType, user.getEmailAddress(), mailFrom, mailSubject,
+                expectedTemplateProperties);
+
+        verify(configurationService, times(1)).getAlertExpiredAccessTokenMailSubject();
+        verify(configurationService, times(1)).getAlertExpiredAccessTokenLevel();
+        verify(configurationService, times(1)).getAlertEmailFrom();
+    }
+
+    @Test
+    public void alertBeforeCertificateExpire() {
+        // given
+        DBUser user = TestDBUtils.createDBUser("user", "alertBeforeCertificateExpire");
+        String mailSubject = "mail subject";
+        String mailFrom = "mail.from@test.eu";
+        AlertLevelEnum alertLevel = AlertLevelEnum.MEDIUM;
+        doReturn(mailSubject).when(configurationService).getAlertBeforeExpireCertificateMailSubject();
+        doReturn(alertLevel).when(configurationService).getAlertBeforeExpireCertificateLevel();
+        doReturn(mailFrom).when(configurationService).getAlertEmailFrom();
+        AlertTypeEnum alertType = AlertTypeEnum.CREDENTIAL_IMMINENT_EXPIRATION;
+        List<String> expectedTemplateProperties = Arrays.asList(CredentialsExpirationProperties.values()).stream()
+                .map(CredentialsExpirationProperties::name).collect(Collectors.toList());
+        // when
+        testInstance.alertBeforeCertificateExpire(user);
+        // then
+        assertAlertSend(alertType, user.getEmailAddress(), mailFrom, mailSubject,
+                expectedTemplateProperties);
+
+        verify(configurationService, times(1)).getAlertBeforeExpireCertificateMailSubject();
+        verify(configurationService, times(1)).getAlertBeforeExpireCertificateLevel();
+        verify(configurationService, times(1)).getAlertEmailFrom();
+    }
+
+    @Test
+    public void alertCertificateExpired() {
+        // given
+        DBUser user = TestDBUtils.createDBUser("user", "alertCertificateExpired");
+        String mailSubject = "mail subject";
+        String mailFrom = "mail.from@test.eu";
+        AlertLevelEnum alertLevel = AlertLevelEnum.MEDIUM;
+        doReturn(mailSubject).when(configurationService).getAlertExpiredCertificateMailSubject();
+        doReturn(alertLevel).when(configurationService).getAlertExpiredCertificateLevel();
+        doReturn(mailFrom).when(configurationService).getAlertEmailFrom();
+        AlertTypeEnum alertType = AlertTypeEnum.CREDENTIAL_EXPIRED;
+        List<String> expectedTemplateProperties = Arrays.asList(CredentialsExpirationProperties.values()).stream()
+                .map(CredentialsExpirationProperties::name).collect(Collectors.toList());
+
+        // when
+        testInstance.alertCertificateExpired(user);
+        // then
+        assertAlertSend(alertType, user.getEmailAddress(), mailFrom, mailSubject,
+                expectedTemplateProperties);
+
+        verify(configurationService, times(1)).getAlertExpiredCertificateMailSubject();
+        verify(configurationService, times(1)).getAlertExpiredCertificateLevel();
+        verify(configurationService, times(1)).getAlertEmailFrom();
+    }
+
+    @Test
+    public void submitAlertMail() {
+        String mailTo = "test.mail@domain.eu";
+        String mailFrom = "test.mail@domain.eu";
+        String mailSubject = "mailSubject";
+        AlertTypeEnum template = AlertTypeEnum.CREDENTIAL_IMMINENT_EXPIRATION;
+        DBAlert alert = new DBAlert();
+        alert.setAlertType(template);
+        alert.setMailTo(mailTo);
+        alert.setMailSubject(mailSubject);
+        alert.addProperty("test", "testValue");
+        doReturn(mailFrom).when(configurationService).getAlertEmailFrom();
+
+        testInstance.submitAlertMail(alert);
+
+        ArgumentCaptor<MailModel<Properties>> argModel = ArgumentCaptor.forClass(MailModel.class);
+        ArgumentCaptor<String> argMailTo = ArgumentCaptor.forClass(String.class);
+        ArgumentCaptor<String> argFrom = ArgumentCaptor.forClass(String.class);
+
+        verify(mailService, times(1))
+                .sendMail(argModel.capture(), argFrom.capture(), argMailTo.capture());
+        verify(alertDao, times(1)).update(alert);
+
+        assertEquals(mailTo, argMailTo.getValue());
+        assertEquals(mailFrom, argFrom.getValue());
+        assertEquals(mailSubject, argModel.getValue().getSubject());
+        assertEquals(template.getTemplate(), argModel.getValue().getTemplatePath());
+        assertEquals(1, argModel.getValue().getModel().size());
+    }
+
+    @Test
+    public void alertUsernameCredentialVerificationFailed() {
+        DBUser user = TestDBUtils.createDBUser("user", "alertCertificateExpired");
+        String mailSubject = "mail subject";
+        String mailFrom = "mail.from@test.eu";
+        user.setSequentialLoginFailureCount(5);
+        user.setLastFailedLoginAttempt(OffsetDateTime.now());
+        AlertLevelEnum alertLevel = AlertLevelEnum.MEDIUM;
+
+        doReturn(true).when(configurationService).getAlertUserLoginFailureEnabled();
+        doReturn(mailSubject).when(configurationService).getAlertUserSuspendedSubject();
+        doReturn(alertLevel).when(configurationService).getAlertUserSuspendedLevel();
+        doReturn(mailFrom).when(configurationService).getAlertEmailFrom();
+        doReturn(123456).when(configurationService).getLoginSuspensionTimeInSeconds();
+        AlertTypeEnum alertType = AlertTypeEnum.CREDENTIAL_VERIFICATION_FAILED;
+        List<String> expectedTemplateProperties = Arrays.asList(CredentialVerificationFailedProperties.values()).stream()
+                .map(CredentialVerificationFailedProperties::name).collect(Collectors.toList());
+
+        // when
+        testInstance.alertCredentialVerificationFailed(user, CredentialTypeEnum.USERNAME_PASSWORD);
+        // then
+        assertAlertSend(alertType, user.getEmailAddress(), mailFrom, mailSubject,
+                expectedTemplateProperties);
+
+        verify(configurationService, times(1)).getAlertUserLoginFailureEnabled();
+        verify(configurationService, times(1)).getAlertUserSuspendedSubject();
+        verify(configurationService, times(1)).getAlertUserSuspendedLevel();
+        verify(configurationService, times(1)).getAlertEmailFrom();
+    }
+
+    @Test
+    public void alertTokenCredentialVerificationFailed() {
+        DBUser user = TestDBUtils.createDBUser("user", "alertCertificateExpired");
+        String mailSubject = "mail subject";
+        String mailFrom = "mail.from@test.eu";
+        user.setSequentialTokenLoginFailureCount(5);
+        user.setLastTokenFailedLoginAttempt(OffsetDateTime.now());
+        AlertLevelEnum alertLevel = AlertLevelEnum.MEDIUM;
+
+        doReturn(true).when(configurationService).getAlertUserLoginFailureEnabled();
+        doReturn(mailSubject).when(configurationService).getAlertUserSuspendedSubject();
+        doReturn(alertLevel).when(configurationService).getAlertUserSuspendedLevel();
+        doReturn(mailFrom).when(configurationService).getAlertEmailFrom();
+        doReturn(123456).when(configurationService).getLoginSuspensionTimeInSeconds();
+        AlertTypeEnum alertType = AlertTypeEnum.CREDENTIAL_VERIFICATION_FAILED;
+        List<String> expectedTemplateProperties = Arrays.asList(CredentialVerificationFailedProperties.values()).stream()
+                .map(CredentialVerificationFailedProperties::name).collect(Collectors.toList());
+
+        // when
+        testInstance.alertCredentialVerificationFailed(user, CredentialTypeEnum.ACCESS_TOKEN);
+        // then
+        assertAlertSend(alertType, user.getEmailAddress(), mailFrom, mailSubject,
+                expectedTemplateProperties);
+
+        verify(configurationService, times(1)).getAlertUserLoginFailureEnabled();
+        verify(configurationService, times(1)).getAlertUserSuspendedSubject();
+        verify(configurationService, times(1)).getAlertUserSuspendedLevel();
+        verify(configurationService, times(1)).getAlertEmailFrom();
+    }
+
+    @Test
+    public void alertUsernameCredentialsSuspended() {
+        DBUser user = TestDBUtils.createDBUser("user", "alertCertificateExpired");
+        String mailSubject = "mail subject";
+        String mailFrom = "mail.from@test.eu";
+        user.setSequentialLoginFailureCount(5);
+        user.setLastFailedLoginAttempt(OffsetDateTime.now());
+        AlertLevelEnum alertLevel = AlertLevelEnum.MEDIUM;
+
+        doReturn(true).when(configurationService).getAlertUserSuspendedEnabled();
+        doReturn(mailSubject).when(configurationService).getAlertUserSuspendedSubject();
+        doReturn(alertLevel).when(configurationService).getAlertUserSuspendedLevel();
+        doReturn(mailFrom).when(configurationService).getAlertEmailFrom();
+        doReturn(123456).when(configurationService).getLoginSuspensionTimeInSeconds();
+        AlertTypeEnum alertType = AlertTypeEnum.CREDENTIAL_SUSPENDED;
+        List<String> expectedTemplateProperties = Arrays.asList(CredentialSuspendedProperties.values()).stream()
+                .map(CredentialSuspendedProperties::name).collect(Collectors.toList());
+
+        // when
+        testInstance.alertCredentialsSuspended(user, CredentialTypeEnum.USERNAME_PASSWORD);
+        // then
+        assertAlertSend(alertType, user.getEmailAddress(), mailFrom, mailSubject,
+                expectedTemplateProperties);
+
+        verify(configurationService, times(1)).getAlertUserSuspendedEnabled();
+        verify(configurationService, times(1)).getAlertUserSuspendedSubject();
+        verify(configurationService, times(1)).getAlertUserSuspendedLevel();
+        verify(configurationService, times(1)).getAlertEmailFrom();
+    }
+
+    @Test
+    public void alertTokenCredentialsSuspended() {
+        DBUser user = TestDBUtils.createDBUser("user", "alertCertificateExpired");
+        String mailSubject = "mail subject";
+        String mailFrom = "mail.from@test.eu";
+        user.setSequentialTokenLoginFailureCount(5);
+        user.setLastTokenFailedLoginAttempt(OffsetDateTime.now());
+        AlertLevelEnum alertLevel = AlertLevelEnum.MEDIUM;
+
+        doReturn(true).when(configurationService).getAlertUserSuspendedEnabled();
+        doReturn(mailSubject).when(configurationService).getAlertUserSuspendedSubject();
+        doReturn(alertLevel).when(configurationService).getAlertUserSuspendedLevel();
+        doReturn(mailFrom).when(configurationService).getAlertEmailFrom();
+        doReturn(123456).when(configurationService).getLoginSuspensionTimeInSeconds();
+        AlertTypeEnum alertType = AlertTypeEnum.CREDENTIAL_SUSPENDED;
+        List<String> expectedTemplateProperties = Arrays.asList(CredentialSuspendedProperties.values()).stream()
+                .map(CredentialSuspendedProperties::name).collect(Collectors.toList());
+
+        // when
+        testInstance.alertCredentialsSuspended(user, CredentialTypeEnum.ACCESS_TOKEN);
+        // then
+        assertAlertSend(alertType, user.getEmailAddress(), mailFrom, mailSubject,
+                expectedTemplateProperties);
+
+        verify(configurationService, times(1)).getAlertUserSuspendedEnabled();
+        verify(configurationService, times(1)).getAlertUserSuspendedSubject();
+        verify(configurationService, times(1)).getAlertUserSuspendedLevel();
+        verify(configurationService, times(1)).getAlertEmailFrom();
+    }
+
+
+    public void assertAlertSend(AlertTypeEnum alertType, String mailTo, String mailFrom, String mailSubject,
+                                List<String> templateProperties) {
+
+        ArgumentCaptor<MailModel<Properties>> argModel = ArgumentCaptor.forClass(MailModel.class);
+        ArgumentCaptor<String> argMailFrom = ArgumentCaptor.forClass(String.class);
+        ArgumentCaptor<String> argMailTo = ArgumentCaptor.forClass(String.class);
+        ArgumentCaptor<DBAlert> argAlert = ArgumentCaptor.forClass(DBAlert.class);
+        ArgumentCaptor<DBAlert> argAlertUpdate = ArgumentCaptor.forClass(DBAlert.class);
+
+
+        verify(alertDao, times(1)).persistFlushDetach(argAlert.capture());
+        verify(mailService, times(1))
+                .sendMail(argModel.capture(), argMailFrom.capture(), argMailTo.capture());
+
+        verify(alertDao, times(1)).update(argAlertUpdate.capture());
+
+        assertEquals(mailTo, argMailTo.getValue());
+        assertEquals(mailFrom, argMailFrom.getValue());
+
+
+        MailModel<Properties> model = argModel.getValue();
+        assertEquals(alertType.getTemplate(), model.getTemplatePath());
+        assertEquals(mailSubject, model.getSubject());
+
+        // test to contain all properties
+        for (String prop : templateProperties) {
+
+            assertTrue(prop, model.getModel().keySet().contains(prop));
+        }
+        assertEquals(templateProperties.size(), model.getModel().size());
+
+
+    }
+}
\ No newline at end of file
diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ServiceGroupServiceMultipleDomainsIntegrationTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ServiceGroupServiceMultipleDomainsIntegrationTest.java
index 6e4a24915..48ab30626 100644
--- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ServiceGroupServiceMultipleDomainsIntegrationTest.java
+++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ServiceGroupServiceMultipleDomainsIntegrationTest.java
@@ -85,8 +85,8 @@ public class ServiceGroupServiceMultipleDomainsIntegrationTest extends AbstractS
         assertFalse(dbsg.isPresent()); // test if exists - it must not :)
 
         // when
-        boolean bCreated = testInstance.saveServiceGroup(inServiceGroup, TEST_DOMAIN_CODE_2, TestConstants.USERNAME_1,
-                TestConstants.USERNAME_1);
+        boolean bCreated = testInstance.saveServiceGroup(inServiceGroup, TEST_DOMAIN_CODE_2, TestConstants.USERNAME_TOKEN_1,
+                TestConstants.USERNAME_TOKEN_1);
         Optional<DBServiceGroup> optRes = dbAssertion.findAndInitServiceGroup(TEST_SG_ID_PL, TEST_SG_SCHEMA_2);
 
         // then
@@ -111,8 +111,8 @@ public class ServiceGroupServiceMultipleDomainsIntegrationTest extends AbstractS
         assertNotEquals(extension, newExtension); // extension updated
 
         // when
-        boolean bCreated = testInstance.saveServiceGroup(inServiceGroup, TEST_DOMAIN_CODE_1, TestConstants.USERNAME_1,
-                TestConstants.USERNAME_1);
+        boolean bCreated = testInstance.saveServiceGroup(inServiceGroup, TEST_DOMAIN_CODE_1, ""+ TestConstants.USERNAME_TOKEN_1,
+                TestConstants.USERNAME_TOKEN_1);
         serviceGroupDao.clearPersistenceContext();
 
         Optional<DBServiceGroup> optRes = dbAssertion.findAndInitServiceGroup(TEST_SG_ID_2, TEST_SG_SCHEMA_2);
diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ServiceGroupServiceSingleDomainIntegrationTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ServiceGroupServiceSingleDomainIntegrationTest.java
index 8d450fa49..5c61019cf 100644
--- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ServiceGroupServiceSingleDomainIntegrationTest.java
+++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ServiceGroupServiceSingleDomainIntegrationTest.java
@@ -100,8 +100,8 @@ public class ServiceGroupServiceSingleDomainIntegrationTest extends AbstractServ
         DBDomain domain = domainDao.getTheOnlyDomain().get();
         assertNotNull(domain);
         // when
-        boolean bCreated = testInstance.saveServiceGroup(inServiceGroup, null, TestConstants.USERNAME_1,
-                TestConstants.USERNAME_1);
+        boolean bCreated = testInstance.saveServiceGroup(inServiceGroup, null, TestConstants.USERNAME_TOKEN_1,
+                TestConstants.USERNAME_TOKEN_1);
 
         Optional<DBServiceGroup> optRes= serviceGroupDao.findServiceGroup(TEST_SG_ID_PL, TEST_SG_SCHEMA_2);
 
@@ -122,8 +122,8 @@ public class ServiceGroupServiceSingleDomainIntegrationTest extends AbstractServ
        assertNotNull(domain);
 
        // when
-       boolean bCreated = testInstance.saveServiceGroup(inServiceGroup, domain.getDomainCode(), TestConstants.USERNAME_1,
-               TestConstants.USERNAME_1);
+       boolean bCreated = testInstance.saveServiceGroup(inServiceGroup, domain.getDomainCode(), TestConstants.USERNAME_TOKEN_1,
+               TestConstants.USERNAME_TOKEN_1);
 
 
        Optional<DBServiceGroup> optRes= serviceGroupDao.findServiceGroup(TEST_SG_ID_PL, TEST_SG_SCHEMA_2);
@@ -148,8 +148,8 @@ public class ServiceGroupServiceSingleDomainIntegrationTest extends AbstractServ
         assertFalse(Arrays.equals(extension, newExtension)); // extension updated
 
         // when
-        boolean bCreated = testInstance.saveServiceGroup(inServiceGroup, domain.getDomainCode(), TestConstants.USERNAME_1,
-                TestConstants.USERNAME_1);
+        boolean bCreated = testInstance.saveServiceGroup(inServiceGroup, domain.getDomainCode(), TestConstants.USERNAME_TOKEN_1,
+                TestConstants.USERNAME_TOKEN_1);
 
 
         Optional<DBServiceGroup> optRes= serviceGroupDao.findServiceGroup(TEST_SG_ID_PL, TEST_SG_SCHEMA_2);
@@ -174,8 +174,8 @@ public class ServiceGroupServiceSingleDomainIntegrationTest extends AbstractServ
     public void saveAndDeletePositiveScenario() throws IOException {
         // given
         ServiceGroup inServiceGroup = unmarshal(loadDocumentAsString(TestConstants.SERVICE_GROUP_POLAND_XML_PATH));
-        boolean bCreated = testInstance.saveServiceGroup(inServiceGroup, null, TestConstants.USERNAME_1,
-                TestConstants.USERNAME_1);
+        boolean bCreated = testInstance.saveServiceGroup(inServiceGroup, null, TestConstants.USERNAME_TOKEN_1,
+                TestConstants.USERNAME_TOKEN_1);
         assertTrue(bCreated);
         serviceGroupDao.clearPersistenceContext();
 
@@ -277,7 +277,7 @@ public class ServiceGroupServiceSingleDomainIntegrationTest extends AbstractServ
         expectedExeption.expectMessage(DOMAIN_NOT_EXISTS.getMessage(domain));
 
         //execute
-        testInstance.saveServiceGroup(inServiceGroup, domain, USERNAME_1, USERNAME_1);
+        testInstance.saveServiceGroup(inServiceGroup, domain, USERNAME_TOKEN_1, USERNAME_TOKEN_1);
     }
 
     @Test
@@ -290,7 +290,7 @@ public class ServiceGroupServiceSingleDomainIntegrationTest extends AbstractServ
                 DomainService.DOMAIN_ID_PATTERN.pattern()));
 
         //execute
-        testInstance.saveServiceGroup(inServiceGroup, domain, USERNAME_1, USERNAME_1);
+        testInstance.saveServiceGroup(inServiceGroup, domain, USERNAME_TOKEN_1, USERNAME_TOKEN_1);
     }
 
     @Test
diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/testutil/TestConstants.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/testutil/TestConstants.java
index c67dbbd7e..f8ad8b6a7 100644
--- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/testutil/TestConstants.java
+++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/testutil/TestConstants.java
@@ -18,8 +18,8 @@ public class TestConstants {
     public static final String TEST_SG_ID_3 = "0007:002:utest";
     public static final String TEST_SG_ID_4 = "0007:004:utest";
     public static final String TEST_SG_ID_NO_SCHEME = "No-Scheme-Party-Id";
-    public static final String TEST_SG_ID_PL= "urn:poland:ncpb:utest";
-    public static final String TEST_SG_ID_PL2= "urn:Poland:ncpb";
+    public static final String TEST_SG_ID_PL = "urn:poland:ncpb:utest";
+    public static final String TEST_SG_ID_PL2 = "urn:Poland:ncpb";
 
 
     public static final String TEST_SG_SCHEMA_1 = "iso6523-actorid-upis";
@@ -27,24 +27,26 @@ public class TestConstants {
     public static final String TEST_SG_SCHEMA_PL2 = "eHealth-participantId-qns";
 
 
-
-
     public static final String TEST_DOC_SCHEMA_1 = "busdox-docid-qns";
     public static final String TEST_DOC_SCHEMA_2 = "ehealth-resid-qns";
-    public static final String TEST_DOC_SCHEMA_PL2="eHealth-resId-qns";
+    public static final String TEST_DOC_SCHEMA_PL2 = "eHealth-resId-qns";
 
     public static final String TEST_DOC_ID_1 = "urn:oasis:names:specification:ubl:schema:xsd:Invoice-12::Invoice##urn:www.cenbii.eu:transaction:biicoretrdm010:ver1.0:#urn:www.peppol.eu:bis:peppol4a:ver1.0::2.0";
     public static final String TEST_DOC_ID_2 = "docid.007";
     public static final String TEST_DOC_ID_PL2 = "DocId.007";
 
 
+    public static final String TOKEN_PREFIX = "token-";
     public static final String USERNAME_1 = "test-user_001";
     public static final String USERNAME_2 = "test-user_002";
     public static final String USERNAME_3 = "test-user_003";
+    public static final String USERNAME_TOKEN_1 = TOKEN_PREFIX + USERNAME_1;
+    public static final String USERNAME_TOKEN_2 = TOKEN_PREFIX + USERNAME_2;
+    public static final String USERNAME_TOKEN_3 = TOKEN_PREFIX + USERNAME_3;
 
-    public static final String USER_CERT_1="CN=utest common name 01,O=org,C=BE:0000000000000066";
-    public static final String USER_CERT_2="CN=utest common name 02,O=org,C=BE:0000000000000077";
-    public static final String USER_CERT_3="CN=utest common name 03,O=org,C=BE:0000000000000077";
+    public static final String USER_CERT_1 = "CN=utest common name 01,O=org,C=BE:0000000000000066";
+    public static final String USER_CERT_2 = "CN=utest common name 02,O=org,C=BE:0000000000000077";
+    public static final String USER_CERT_3 = "CN=utest common name 03,O=org,C=BE:0000000000000077";
 
 
     public static final String SERVICE_GROUP_POLAND_XML_PATH = "/examples/services/ServiceGroupPoland.xml";
@@ -52,16 +54,16 @@ public class TestConstants {
     public static final String SERVICE_METADATA_XML_PATH = "/examples/services/ServiceMetadataPoland.xml";
     public static final String SIGNED_SERVICE_METADATA_XML_PATH = "/examples/services/SignedServiceMetadataPoland.xml";
 
-    public static final ParticipantIdentifierType SERVICE_GROUP_ID = asParticipantId("participant-scheme-qns::urn:eu:ncpb",false);
+    public static final ParticipantIdentifierType SERVICE_GROUP_ID = asParticipantId("participant-scheme-qns::urn:eu:ncpb", false);
 
     public static final String ADMIN_USERNAME = "test_admin";
-    public static final String CERT_USER="CN=common name,O=org,C=BE:0000000000000066";
-    public static final String CERT_USER_ENCODED="CN%3Dcommon%20name%2CO%3Dorg%2CC%3DBE%3A0000000000000066";
+    public static final String CERT_USER = "CN=common name,O=org,C=BE:0000000000000066";
+    public static final String CERT_USER_ENCODED = "CN%3Dcommon%20name%2CO%3Dorg%2CC%3DBE%3A0000000000000066";
 
     // parameter: custom string as conntent
-    public static final String SIMPLE_EXTENSION_XML ="<Extension xmlns=\"http://docs.oasis-open.org/bdxr/ns/SMP/2016/05\"><ex:dummynode xmlns:ex=\"http://test.eu\">Sample not mandatory extension: %s</ex:dummynode></Extension>";
+    public static final String SIMPLE_EXTENSION_XML = "<Extension xmlns=\"http://docs.oasis-open.org/bdxr/ns/SMP/2016/05\"><ex:dummynode xmlns:ex=\"http://test.eu\">Sample not mandatory extension: %s</ex:dummynode></Extension>";
     //5 parameters: ParticipantScheme, ParticipantIdentifier, DocumentScheme, DocumentIdentifier, custom value
-    public static final String SIMPLE_DOCUMENT_XML ="<ServiceMetadata xmlns=\"http://docs.oasis-open.org/bdxr/ns/SMP/2016/05\"><ServiceInformation><ParticipantIdentifier scheme=\"%s\">%s</ParticipantIdentifier><DocumentIdentifier scheme=\"%s\">%s</DocumentIdentifier><ProcessList><Process><ProcessIdentifier scheme=\"cenbii-procid-ubl\">urn:www.cenbii.eu:profile:bii04:ver1.0</ProcessIdentifier><ServiceEndpointList><Endpoint transportProfile=\"bdxr-transport-ebms3-as4-v1p0\"><EndpointURI>http://localhost:8080/domibus-weblogic/services/msh</EndpointURI><RequireBusinessLevelSignature>true</RequireBusinessLevelSignature><ServiceActivationDate>2003-01-01T00:00:00</ServiceActivationDate><ServiceExpirationDate>2099-05-01T00:00:00</ServiceExpirationDate>" +
+    public static final String SIMPLE_DOCUMENT_XML = "<ServiceMetadata xmlns=\"http://docs.oasis-open.org/bdxr/ns/SMP/2016/05\"><ServiceInformation><ParticipantIdentifier scheme=\"%s\">%s</ParticipantIdentifier><DocumentIdentifier scheme=\"%s\">%s</DocumentIdentifier><ProcessList><Process><ProcessIdentifier scheme=\"cenbii-procid-ubl\">urn:www.cenbii.eu:profile:bii04:ver1.0</ProcessIdentifier><ServiceEndpointList><Endpoint transportProfile=\"bdxr-transport-ebms3-as4-v1p0\"><EndpointURI>http://localhost:8080/domibus-weblogic/services/msh</EndpointURI><RequireBusinessLevelSignature>true</RequireBusinessLevelSignature><ServiceActivationDate>2003-01-01T00:00:00</ServiceActivationDate><ServiceExpirationDate>2099-05-01T00:00:00</ServiceExpirationDate>" +
             "<Certificate>VGhpcyBpcyB0ZXN0IGNlcnRpZmljYXRlIGlzIHlvdSBiZWxpZXZlIG9yIG5vdC4=</Certificate><ServiceDescription>Sample description of %s</ServiceDescription><TechnicalContactUrl>https://example.com</TechnicalContactUrl></Endpoint></ServiceEndpointList></Process></ProcessList></ServiceInformation></ServiceMetadata>";
 
 }
diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/testutil/TestDBUtils.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/testutil/TestDBUtils.java
index 4e0bbf18f..04a057bac 100644
--- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/testutil/TestDBUtils.java
+++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/testutil/TestDBUtils.java
@@ -8,8 +8,7 @@ import eu.europa.ec.edelivery.smp.data.ui.enums.AlertTypeEnum;
 import java.time.OffsetDateTime;
 import java.util.UUID;
 
-import static eu.europa.ec.edelivery.smp.testutil.TestConstants.SIMPLE_DOCUMENT_XML;
-import static eu.europa.ec.edelivery.smp.testutil.TestConstants.SIMPLE_EXTENSION_XML;
+import static eu.europa.ec.edelivery.smp.testutil.TestConstants.*;
 
 public class TestDBUtils {
 
@@ -95,10 +94,10 @@ public class TestDBUtils {
         DBUser dbuser = new DBUser();
         dbuser.setUsername(userName);
         dbuser.setRole("test");
-        dbuser.setEmailAddress("test@test.eu");
+        dbuser.setEmailAddress(userName + "@test.eu");
         dbuser.setPasswordChanged(OffsetDateTime.now());
         dbuser.setPassword(UUID.randomUUID().toString());
-        dbuser.setAccessTokenIdentifier(userName);
+        dbuser.setAccessTokenIdentifier(TOKEN_PREFIX + userName);
         dbuser.setAccessToken(UUID.randomUUID().toString());
         return dbuser;
     }
@@ -124,7 +123,7 @@ public class TestDBUtils {
     }
 
     public static DBUser createDBUserByCertificate(String certId, OffsetDateTime validFrom, OffsetDateTime validTo) {
-        return createDBUser("test-" + certId, certId, validFrom,validTo);
+        return createDBUser("test-" + certId, certId, validFrom, validTo);
     }
 
     public static DBUser createDBUser(String userName, String certId) {
-- 
GitLab