PageRenderTime 14ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/sigmah/src/test/java/org/sigmah/server/schedule/ReportMailerTest.java

http://sigma-h.googlecode.com/
Java | 149 lines | 96 code | 46 blank | 7 comment | 0 complexity | d999783649f1494ad282ff4415ca2eb6 MD5 | raw file
  1/*
  2 * All Sigmah code is released under the GNU General Public License v3
  3 * See COPYRIGHT.txt and LICENSE.txt.
  4 */
  5
  6package org.sigmah.server.schedule;
  7
  8import org.junit.Assert;
  9import org.junit.Test;
 10import org.sigmah.server.domain.ReportDefinition;
 11import org.sigmah.server.domain.ReportSubscription;
 12import org.sigmah.shared.domain.User;
 13import org.sigmah.shared.report.model.DateRange;
 14import org.sigmah.shared.report.model.Report;
 15import org.sigmah.shared.report.model.ReportFrequency;
 16
 17import java.util.Calendar;
 18import java.util.Date;
 19
 20/**
 21 * @author Alex Bertram
 22 */
 23public class ReportMailerTest {
 24
 25    @Test
 26    public void testDailyCheck() {
 27
 28        Report sub = new Report();
 29        sub.setFrequency(ReportFrequency.Daily);
 30
 31        Assert.assertTrue("Daily report always goes out", ReportMailerHelper.mailToday(new Date(), sub));
 32
 33    }
 34
 35    @Test
 36    public void testWeeklyCheck() {
 37
 38        Report report = new Report();
 39        report.setFrequency(ReportFrequency.Weekly);
 40        report.setDay(0); // Sunday
 41
 42        Calendar cal = Calendar.getInstance();
 43        cal.set(Calendar.YEAR, 2009);
 44        cal.set(Calendar.MONTH, 9);
 45        cal.set(Calendar.DATE, 4);
 46
 47        Assert.assertTrue("Sunday report goes out on Sunday", ReportMailerHelper.mailToday(cal.getTime(), report));
 48
 49        cal.set(Calendar.DATE, 5);
 50
 51        Assert.assertFalse("Sunday report does not out on Monday", ReportMailerHelper.mailToday(cal.getTime(), report));
 52
 53        report.setDay(1);
 54
 55        Assert.assertTrue("Monday report goes out on Monday", ReportMailerHelper.mailToday(cal.getTime(), report));
 56    }
 57
 58    @Test
 59    public void testMonthly() {
 60
 61        Report report = new Report();
 62        report.setFrequency(ReportFrequency.Monthly);
 63        report.setDay(11);
 64
 65        Calendar cal = Calendar.getInstance();
 66        cal.set(Calendar.YEAR, 2009);
 67        cal.set(Calendar.MONTH, 3);
 68        cal.set(Calendar.DATE, 11);
 69
 70        Assert.assertTrue("Monthly report scheduled for each the 11th goes out on the 11th",
 71                ReportMailerHelper.mailToday(cal.getTime(), report));
 72
 73
 74        cal.set(Calendar.DATE, 30);
 75    }
 76
 77    @Test
 78    public void testLastDayOfMonth() {
 79
 80        Report report = new Report();
 81        report.setFrequency(ReportFrequency.Monthly);
 82        report.setDay(Report.LAST_DAY_OF_MONTH);
 83
 84        Calendar cal = Calendar.getInstance();
 85        cal.set(Calendar.YEAR, 2009);
 86        cal.set(Calendar.MONTH, 3);
 87        cal.set(Calendar.DATE, 30);
 88
 89        Assert.assertTrue("Report goes out on 4-April",
 90                ReportMailerHelper.mailToday(cal.getTime(), report));
 91
 92        cal.set(Calendar.MONTH, 0);
 93        cal.set(Calendar.DATE, 31);
 94
 95        Assert.assertTrue("Report goes out on 31-Jan",
 96                ReportMailerHelper.mailToday(cal.getTime(), report));
 97
 98    }
 99
100    @Test
101    public void testDateParameters() {
102
103        Report report = new Report();
104        report.setFrequency(ReportFrequency.Monthly);
105
106
107        Calendar today = Calendar.getInstance();
108        today.set(Calendar.DATE, 5);
109        today.set(Calendar.MONTH, 0);
110        today.set(Calendar.YEAR, 2009);
111
112        DateRange dateRange = ReportMailerHelper.computeDateRange(report, today.getTime());
113
114        Calendar month = Calendar.getInstance();
115        month.setTime(dateRange.getMaxDate());
116
117        Assert.assertEquals(11, month.get(Calendar.MONTH));
118        Assert.assertEquals(31, month.get(Calendar.DATE));
119        Assert.assertEquals(2008, month.get(Calendar.YEAR));
120
121    }
122
123    @Test
124    public void testTextEmail() {
125
126        User user = new User();
127        user.setEmail("akbertram@gmail.com");
128        user.setName("alex");
129        user.setLocale("fr");
130
131        ReportSubscription sub = new ReportSubscription();
132        sub.setTemplate(new ReportDefinition());
133        sub.getTemplate().setId(1);
134        sub.setUser(user);
135
136        Report report = new Report();
137        report.setTitle("Rapport RRM Mensuelle");
138        report.setFrequency(ReportFrequency.Weekly);
139        report.setDay(1);
140
141        String text = ReportMailerHelper.composeTextEmail(sub, report);
142
143        System.out.println(text);
144
145        Assert.assertTrue("user name is present", text.contains(user.getName()));
146
147    }
148
149}