You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by il...@apache.org on 2012/08/29 14:42:54 UTC

svn commit: r1378525 - in /incubator/syncope/trunk: ./ client/src/main/java/org/apache/syncope/client/to/ core/src/main/java/org/apache/syncope/core/notification/ core/src/main/java/org/apache/syncope/core/rest/data/ core/src/main/java/org/apache/synco...

Author: ilgrosso
Date: Wed Aug 29 12:42:53 2012
New Revision: 1378525

URL: http://svn.apache.org/viewvc?rev=1378525&view=rev
Log:
Merge from 1_0_X

Modified:
    incubator/syncope/trunk/   (props changed)
    incubator/syncope/trunk/client/src/main/java/org/apache/syncope/client/to/NotificationTaskTO.java
    incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/notification/NotificationManager.java
    incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/rest/data/TaskDataBinder.java
    incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/scheduling/NotificationJob.java
    incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/notification/NotificationTest.java
    incubator/syncope/trunk/pom.xml

Propchange: incubator/syncope/trunk/
------------------------------------------------------------------------------
  Merged /incubator/syncope/branches/1_0_X:r1378500-1378524

Modified: incubator/syncope/trunk/client/src/main/java/org/apache/syncope/client/to/NotificationTaskTO.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/client/src/main/java/org/apache/syncope/client/to/NotificationTaskTO.java?rev=1378525&r1=1378524&r2=1378525&view=diff
==============================================================================
--- incubator/syncope/trunk/client/src/main/java/org/apache/syncope/client/to/NotificationTaskTO.java (original)
+++ incubator/syncope/trunk/client/src/main/java/org/apache/syncope/client/to/NotificationTaskTO.java Wed Aug 29 12:42:53 2012
@@ -36,6 +36,8 @@ public class NotificationTaskTO extends 
 
     private String htmlBody;
 
+    private boolean executed;
+
     private TraceLevel traceLevel;
 
     public NotificationTaskTO() {
@@ -95,6 +97,14 @@ public class NotificationTaskTO extends 
         this.htmlBody = htmlBody;
     }
 
+    public boolean isExecuted() {
+        return executed;
+    }
+
+    public void setExecuted(boolean executed) {
+        this.executed = executed;
+    }
+
     public TraceLevel getTraceLevel() {
         return traceLevel;
     }

Modified: incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/notification/NotificationManager.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/notification/NotificationManager.java?rev=1378525&r1=1378524&r2=1378525&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/notification/NotificationManager.java (original)
+++ incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/notification/NotificationManager.java Wed Aug 29 12:42:53 2012
@@ -168,7 +168,7 @@ public class NotificationManager {
 
         final Map<String, Object> model = new HashMap<String, Object>();
         model.put("user", userDataBinder.getUserTO(user));
-        model.put("syncopeConf", this.findAll());
+        model.put("syncopeConf", this.findAllSyncopeConfs());
 
         String htmlBody;
         String textBody;
@@ -251,12 +251,12 @@ public class NotificationManager {
     }
 
     /**
-     * Store execution of a NotificationTask and update latest execution status of related NotificationTask.
+     * Store execution of a NotificationTask.
      *
      * @param execution task execution.
      * @return merged task execution.
      */
-    public TaskExec storeExecAndUpdateLatestExecStatus(final TaskExec execution) {
+    public TaskExec storeExec(final TaskExec execution) {
         NotificationTask task = taskDAO.find(execution.getTask().getId());
         task.addExec(execution);
         task.setExecuted(true);
@@ -265,7 +265,18 @@ public class NotificationManager {
         return task.getExecs().get(0);
     }
 
-    public Map<String, String> findAll() {
+    /**
+     * Mark NotificationTask with provided id as executed.
+     * 
+     * @param taskId task to be updated
+     */
+    public void setTaskExecuted(final Long taskId) {
+        NotificationTask task = taskDAO.find(taskId);
+        task.setExecuted(true);
+        taskDAO.save(task);
+    }
+
+    public Map<String, String> findAllSyncopeConfs() {
         Map<String, String> syncopeConfMap = new HashMap<String, String>();
         for (SyncopeConf conf : confDAO.findAll()) {
             syncopeConfMap.put(conf.getKey(), conf.getValue());

Modified: incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/rest/data/TaskDataBinder.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/rest/data/TaskDataBinder.java?rev=1378525&r1=1378524&r2=1378525&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/rest/data/TaskDataBinder.java (original)
+++ incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/rest/data/TaskDataBinder.java Wed Aug 29 12:42:53 2012
@@ -32,6 +32,7 @@ import org.apache.syncope.client.validat
 import org.apache.syncope.client.validation.SyncopeClientException;
 import org.apache.syncope.core.init.JobInstanceLoader;
 import org.apache.syncope.core.persistence.beans.ExternalResource;
+import org.apache.syncope.core.persistence.beans.NotificationTask;
 import org.apache.syncope.core.persistence.beans.PropagationTask;
 import org.apache.syncope.core.persistence.beans.SchedTask;
 import org.apache.syncope.core.persistence.beans.SyncTask;
@@ -149,7 +150,8 @@ public class TaskDataBinder {
 
             case SYNC:
                 if (!(taskTO instanceof SyncTaskTO)) {
-                    throw new ClassCastException("taskUtil is type SyncTask but taskTO is not SyncTaskTO: " + taskTO.getClass().getName());
+                    throw new ClassCastException("taskUtil is type SyncTask but taskTO is not SyncTaskTO: " + taskTO.
+                            getClass().getName());
                 }
                 SyncTaskTO syncTaskTO = (SyncTaskTO) taskTO;
 
@@ -176,7 +178,8 @@ public class TaskDataBinder {
                         getName());
             }
             if (!(taskTO instanceof SyncTaskTO)) {
-                throw new ClassCastException("taskUtil is type SyncTask but taskTO is not SyncTaskTO: " + taskTO.getClass().getName());
+                throw new ClassCastException("taskUtil is type SyncTask but taskTO is not SyncTaskTO: " + taskTO.
+                        getClass().getName());
             }
 
             fill((SyncTask) task, (SyncTaskTO) taskTO);
@@ -227,7 +230,7 @@ public class TaskDataBinder {
         taskTO.setStartDate(latestExec == null
                 ? null
                 : latestExec.getStartDate());
-        
+
         taskTO.setEndDate(latestExec == null
                 ? null
                 : latestExec.getEndDate());
@@ -265,6 +268,9 @@ public class TaskDataBinder {
                 break;
 
             case NOTIFICATION:
+                if (((NotificationTask) task).isExecuted() && StringUtils.isBlank(taskTO.getLatestExecStatus())) {
+                    taskTO.setLatestExecStatus("[EXECUTED]");
+                }
                 break;
 
             default:

Modified: incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/scheduling/NotificationJob.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/scheduling/NotificationJob.java?rev=1378525&r1=1378524&r2=1378525&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/scheduling/NotificationJob.java (original)
+++ incubator/syncope/trunk/core/src/main/java/org/apache/syncope/core/scheduling/NotificationJob.java Wed Aug 29 12:42:53 2012
@@ -48,22 +48,22 @@ import org.apache.syncope.types.TraceLev
  * @see NotificationTask
  */
 public class NotificationJob implements StatefulJob {
-
+    
     enum Status {
-
+        
         SENT,
         NOT_SENT
-
+        
     }
 
     /**
      * Logger.
      */
     private static final Logger LOG = LoggerFactory.getLogger(NotificationJob.class);
-
+    
     @Autowired
     private AuditManager auditManager;
-
+    
     @Autowired
     private NotificationManager notificationManager;
 
@@ -78,15 +78,15 @@ public class NotificationJob implements 
      */
     @Autowired
     private ConfDAO confDAO;
-
+    
     private String smtpHost;
-
+    
     private int smtpPort;
-
+    
     private String smtpUsername;
-
+    
     private String smtpPassword;
-
+    
     private void init() {
         smtpHost = confDAO.find("smtp.host", "").getValue();
         smtpPort = 25;
@@ -97,31 +97,31 @@ public class NotificationJob implements 
         }
         smtpUsername = confDAO.find("smtp.username", "").getValue();
         smtpPassword = confDAO.find("smtp.password", "").getValue();
-
+        
         LOG.debug("SMTP details fetched: {}:{} / {}:[PASSWORD_NOT_SHOWN]",
                 new Object[]{smtpHost, smtpPort, smtpUsername});
     }
-
+    
     public TaskExec executeSingle(final NotificationTask task) {
         init();
-
+        
         TaskExec execution = new TaskExec();
         execution.setTask(task);
         execution.setStartDate(new Date());
-
+        
         if (StringUtils.isBlank(smtpHost) || StringUtils.isBlank(task.getSender())
                 || StringUtils.isBlank(task.getSubject()) || task.getRecipients().isEmpty()
                 || StringUtils.isBlank(task.getHtmlBody()) || StringUtils.isBlank(task.getTextBody())) {
-
+            
             String message = "Could not fetch all required information for " + "sending e-mails:\n" + smtpHost + ":"
                     + smtpPort + "\n" + task.getRecipients() + "\n" + task.getSender() + "\n" + task.getSubject()
                     + "\n" + task.getHtmlBody() + "\n" + task.getTextBody();
             LOG.error(message);
-
+            
             execution.setStatus(Status.NOT_SENT.name());
-
+            
             if (task.getTraceLevel().ordinal() >= TraceLevel.FAILURES.ordinal()) {
-
+                
                 execution.setMessage(message);
             }
         } else {
@@ -130,7 +130,7 @@ public class NotificationJob implements 
                         + task.getSender() + "\n" + task.getSubject() + "\n" + task.getHtmlBody() + "\n"
                         + task.getTextBody() + "\n");
             }
-
+            
             for (String to : task.getRecipients()) {
                 try {
                     JavaMailSenderImpl sender = new JavaMailSenderImpl();
@@ -143,18 +143,18 @@ public class NotificationJob implements 
                     if (StringUtils.isNotBlank(smtpPassword)) {
                         sender.setPassword(smtpPassword);
                     }
-
+                    
                     MimeMessage message = sender.createMimeMessage();
                     MimeMessageHelper helper = new MimeMessageHelper(message, true);
                     helper.setTo(to);
                     helper.setFrom(task.getSender());
                     helper.setSubject(task.getSubject());
                     helper.setText(task.getTextBody(), task.getHtmlBody());
-
+                    
                     sender.send(message);
-
+                    
                     execution.setStatus(Status.SENT.name());
-
+                    
                     StringBuilder report = new StringBuilder();
                     switch (task.getTraceLevel()) {
                         case ALL:
@@ -164,11 +164,11 @@ public class NotificationJob implements 
                                     append(task.getTextBody()).append('\n').append('\n').
                                     append(task.getHtmlBody()).append('\n');
                             break;
-
+                        
                         case SUMMARY:
                             report.append("E-mail sent to ").append(to).append('\n');
                             break;
-
+                        
                         case FAILURES:
                         case NONE:
                         default:
@@ -176,51 +176,53 @@ public class NotificationJob implements 
                     if (report.length() > 0) {
                         execution.setMessage(report.toString());
                     }
-
+                    
                     auditManager.audit(Category.notification, NotificationSubCategory.sent, Result.success,
                             "Successfully sent notification to " + to);
                 } catch (Exception e) {
                     LOG.error("Could not send e-mail", e);
-
+                    
                     execution.setStatus(Status.NOT_SENT.name());
                     StringWriter exceptionWriter = new StringWriter();
                     exceptionWriter.write(e.getMessage() + "\n\n");
                     e.printStackTrace(new PrintWriter(exceptionWriter));
-
+                    
                     if (task.getTraceLevel().ordinal() >= TraceLevel.FAILURES.ordinal()) {
                         execution.setMessage(exceptionWriter.toString());
                     }
-
+                    
                     auditManager.audit(Category.notification, NotificationSubCategory.sent, Result.failure,
                             "Could not send notification to " + to, e);
                 }
-
+                
                 execution.setEndDate(new Date());
             }
         }
-
+        
         if (hasToBeRegistered(execution)) {
-            execution = notificationManager.storeExecAndUpdateLatestExecStatus(execution);
+            execution = notificationManager.storeExec(execution);
+        } else {
+            notificationManager.setTaskExecuted(execution.getTask().getId());
         }
-
+        
         return execution;
     }
-
+    
     @Override
     public void execute(final JobExecutionContext context)
             throws JobExecutionException {
-
+        
         LOG.debug("Waking up...");
-
+        
         for (NotificationTask task : taskDAO.findToExec(NotificationTask.class)) {
             LOG.debug("Found notification task {} to be executed: starting...", task);
             executeSingle(task);
             LOG.debug("Notification task {} executed", task);
         }
-
+        
         LOG.debug("Sleeping again...");
     }
-
+    
     private boolean hasToBeRegistered(final TaskExec execution) {
         NotificationTask task = (NotificationTask) execution.getTask();
 

Modified: incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/notification/NotificationTest.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/notification/NotificationTest.java?rev=1378525&r1=1378524&r2=1378525&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/notification/NotificationTest.java (original)
+++ incubator/syncope/trunk/core/src/test/java/org/apache/syncope/core/notification/NotificationTest.java Wed Aug 29 12:42:53 2012
@@ -31,9 +31,11 @@ import javax.mail.Folder;
 import javax.mail.Message;
 import javax.mail.Session;
 import javax.mail.Store;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.syncope.client.search.MembershipCond;
 import org.apache.syncope.client.search.NodeCond;
 import org.apache.syncope.client.to.MembershipTO;
+import org.apache.syncope.client.to.NotificationTaskTO;
 import org.apache.syncope.client.to.UserTO;
 import org.apache.syncope.core.persistence.beans.Entitlement;
 import org.apache.syncope.core.persistence.beans.Notification;
@@ -48,6 +50,7 @@ import org.apache.syncope.core.rest.cont
 import org.apache.syncope.core.rest.controller.UserController;
 import org.apache.syncope.core.scheduling.NotificationJob;
 import org.apache.syncope.types.IntMappingType;
+import org.apache.syncope.types.TraceLevel;
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
@@ -152,6 +155,24 @@ public class NotificationTest {
         SecurityContextHolder.getContext().setAuthentication(authentication);
     }
 
+    @Before
+    public void setupSMTP() {
+        try {
+            SyncopeConf smtpHostConf = confDAO.find("smtp.host");
+            smtpHostConf.setValue(smtpHost);
+            confDAO.save(smtpHostConf);
+
+            SyncopeConf smtpPortConf = confDAO.find("smtp.port");
+            smtpPortConf.setValue(Integer.toString(smtpPort));
+            confDAO.save(smtpPortConf);
+        } catch (Exception e) {
+            LOG.error("Unexpected exception", e);
+            fail("Unexpected exception while setting SMTP host and port");
+        }
+
+        confDAO.flush();
+    }
+
     private boolean verifyMail(final String sender, final String subject) {
         LOG.info("Waiting for notification to be sent...");
         try {
@@ -216,23 +237,79 @@ public class NotificationTest {
 
         notificationDAO.flush();
 
-        // 2. use test SMTP server
+        // 2. create user
+        UserTO userTO = UserTestITCase.getSampleTO(mailAddress);
+        MembershipTO membershipTO = new MembershipTO();
+        membershipTO.setRoleId(7);
+        userTO.addMembership(membershipTO);
+
         try {
-            SyncopeConf smtpHostConf = confDAO.find("smtp.host");
-            smtpHostConf.setValue(smtpHost);
-            confDAO.save(smtpHostConf);
+            userController.create(new MockHttpServletResponse(), userTO);
+        } catch (Exception e) {
+            LOG.error("Unexpected exception", e);
+            fail("Unexpected exception while creating");
+        }
 
-            SyncopeConf smtpPortConf = confDAO.find("smtp.port");
-            smtpPortConf.setValue(Integer.toString(smtpPort));
-            confDAO.save(smtpPortConf);
+        // 3. force Quartz job execution and verify e-mail
+        try {
+            notificationJob.execute(null);
+        } catch (SchedulerException e) {
+            LOG.error("Unexpected exception", e);
+            fail("Unexpected exception while triggering notification job");
+        }
+        assertTrue(verifyMail(sender, subject));
+
+        // 4. get NotificationTask id
+        Long taskId = null;
+        for (NotificationTask task : taskDAO.findAll(NotificationTask.class)) {
+            if (sender.equals(task.getSender())) {
+                taskId = task.getId();
+            }
+        }
+        assertNotNull(taskId);
+
+        // 5. execute Notification task and verify e-mail
+        try {
+            taskController.execute(taskId, false);
         } catch (Exception e) {
             LOG.error("Unexpected exception", e);
-            fail("Unexpected exception while setting SMTP host and port");
+            fail("Unexpected exception while executing notification task");
         }
+        assertTrue(verifyMail(sender, subject));
+    }
 
-        confDAO.flush();
+    @Test
+    public void issueSYNCOPE192() {
+        // 1. create suitable notification for subsequent tests
+        Notification notification = new Notification();
+        notification.addEvent("create");
+
+        MembershipCond membCond = new MembershipCond();
+        membCond.setRoleId(7L);
+        notification.setAbout(NodeCond.getLeafCond(membCond));
+
+        membCond = new MembershipCond();
+        membCond.setRoleId(8L);
+        notification.setRecipients(NodeCond.getLeafCond(membCond));
+        notification.setSelfAsRecipient(true);
+
+        notification.setRecipientAttrName("email");
+        notification.setRecipientAttrType(IntMappingType.UserSchema);
+
+        Random random = new Random(System.currentTimeMillis());
+        String sender = "syncope192-" + random.nextLong() + "@syncope.apache.org";
+        notification.setSender(sender);
+        String subject = "Test notification " + random.nextLong();
+        notification.setSubject(subject);
+        notification.setTemplate("optin");
+        notification.setTraceLevel(TraceLevel.NONE);
 
-        // 3. create user
+        Notification actual = notificationDAO.save(notification);
+        assertNotNull(actual);
+
+        notificationDAO.flush();
+
+        // 2. create user
         UserTO userTO = UserTestITCase.getSampleTO(mailAddress);
         MembershipTO membershipTO = new MembershipTO();
         membershipTO.setRoleId(7);
@@ -245,33 +322,35 @@ public class NotificationTest {
             fail("Unexpected exception while creating");
         }
 
-        // 4. force Quartz job execution and verify e-mail
+        // 3. force Quartz job execution and verify e-mail
         try {
             notificationJob.execute(null);
         } catch (SchedulerException e) {
             LOG.error("Unexpected exception", e);
             fail("Unexpected exception while triggering notification job");
         }
-
         assertTrue(verifyMail(sender, subject));
 
-        List<NotificationTask> tasks = taskDAO.findAll(NotificationTask.class);
+        // 4. get NotificationTask id
         Long taskId = null;
-        for (NotificationTask task : tasks) {
+        for (NotificationTask task : taskDAO.findAll(NotificationTask.class)) {
             if (sender.equals(task.getSender())) {
                 taskId = task.getId();
             }
         }
         assertNotNull(taskId);
 
-        // 5. execute Notification task and verify e-mail
+        // 5. verify that last exec status was updated
+        NotificationTaskTO task = null;
         try {
-            taskController.execute(taskId, false);
+            task = (NotificationTaskTO) taskController.read(taskId);
         } catch (Exception e) {
             LOG.error("Unexpected exception", e);
-            fail("Unexpected exception while executing notification task");
+            fail("Unexpected exception while reading notification task");
         }
-
-        assertTrue(verifyMail(sender, subject));
+        assertNotNull(task);
+        assertTrue(task.getExecutions().isEmpty());
+        assertTrue(task.isExecuted());
+        assertTrue(StringUtils.isNotBlank(task.getLatestExecStatus()));
     }
 }

Modified: incubator/syncope/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/pom.xml?rev=1378525&r1=1378524&r2=1378525&view=diff
==============================================================================
--- incubator/syncope/trunk/pom.xml (original)
+++ incubator/syncope/trunk/pom.xml Wed Aug 29 12:42:53 2012
@@ -755,7 +755,7 @@ under the License.
         <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-surefire-plugin</artifactId>
-          <version>2.12</version>
+          <version>2.12.2</version>
           <configuration>
             <redirectTestOutputToFile>true</redirectTestOutputToFile>
           </configuration>
@@ -805,7 +805,7 @@ under the License.
         <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-failsafe-plugin</artifactId>
-          <version>2.12</version>
+          <version>2.12.2</version>
           <configuration>
             <redirectTestOutputToFile>true</redirectTestOutputToFile>
             <encoding>utf-8</encoding>
@@ -828,7 +828,7 @@ under the License.
         <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-dependency-plugin</artifactId>
-          <version>2.4</version>
+          <version>2.5</version>
           <configuration>
             <artifactItems>
               <artifactItem>