1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.watchlist.test.ui

File WatchThisPageAndWholeSpaceTest.java

 

Code metrics

0
57
4
1
218
114
4
0.07
14.25
4
1

Classes

Class Line # Actions
WatchThisPageAndWholeSpaceTest 43 57 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.watchlist.test.ui;
21   
22    import javax.mail.internet.MimeMessage;
23   
24    import org.junit.After;
25    import org.junit.Assert;
26    import org.junit.Before;
27    import org.junit.Rule;
28    import org.junit.Test;
29    import org.xwiki.scheduler.test.po.SchedulerHomePage;
30    import org.xwiki.test.ui.AbstractTest;
31    import org.xwiki.test.ui.SuperAdminAuthenticationRule;
32    import org.xwiki.test.ui.browser.IgnoreBrowser;
33    import org.xwiki.test.ui.browser.IgnoreBrowsers;
34    import org.xwiki.test.ui.po.ViewPage;
35    import org.xwiki.watchlist.test.po.WatchlistUserProfilePage;
36    import org.xwiki.watchlist.test.po.editor.WatchlistPreferencesEditPage;
37   
38    import com.icegreen.greenmail.util.GreenMail;
39    import com.icegreen.greenmail.util.GreenMailUtil;
40   
41    import static org.junit.Assert.assertEquals;
42   
 
43    public class WatchThisPageAndWholeSpaceTest extends AbstractTest
44    {
45    private static final String PASSWORD = "password";
46   
47    private GreenMail greenMail;
48   
49    private String testUserName = "TestUser";
50   
51    private String testUserName2 = "TestUser2";
52   
53    /**
54    * Set the test user's email address to use a localhost domain so that the mail is caught by our GreenMail Mock mail
55    * server.
56    */
57    private String testEmail = "test@localhost";
58   
59    @Rule
60    public SuperAdminAuthenticationRule superAdminAuthenticationRule = new SuperAdminAuthenticationRule(getUtil());
61   
62    private String testSinglePageToWatchSpace = "Test";
63   
64    private String testSinglePageToWatch = "TestWatchThisPage";
65   
66    private String testSpaceToWatch = "TestWatchWholeSpace";
67   
68    private String testSpaceToWatchPage = "Test1";
69   
 
70  1 toggle @Before
71    public void setUp() throws Exception
72    {
73    // Set the SMTP port to the default port used by Greenmail (3025)
74  1 getUtil().updateObject("Mail", "MailConfig", "Mail.SendMailConfigClass", 0, "host", "localhost", "port",
75    "3025", "sendWaitTime", "0");
76   
77    // Start GreenMail test server
78  1 this.greenMail = new GreenMail();
79  1 this.greenMail.start();
80   
81    // Delete the users if they already exists.
82  1 getUtil().deletePage("XWiki", testUserName);
83  1 getUtil().deletePage("XWiki", testUserName2);
84   
85    // Delete the test pages.
86  1 getUtil().deletePage(testSinglePageToWatchSpace, testSinglePageToWatch);
87  1 getUtil().deletePage(testSpaceToWatch, testSpaceToWatchPage);
88   
89    // Create a user for the test.
90  1 getUtil().createUserAndLogin(testUserName, PASSWORD, "email", testEmail);
91   
92  1 WatchlistUserProfilePage watchlistPage = WatchlistUserProfilePage.gotoPage(testUserName);
93   
94    // Disable auto watch.
95  1 WatchlistPreferencesEditPage watchlistPreferences = watchlistPage.editPreferences();
96  1 watchlistPreferences.setAutomaticWatchNone();
97  1 watchlistPreferences.clickSaveAndContinue();
98    }
99   
 
100  1 toggle @After
101    public void tearDown()
102    {
103    // Stop GreenMail test server
104  1 this.greenMail.stop();
105   
106    // Make sure we can restore the settings, so we log back with superadmin to finish the work
107  1 getUtil().loginAsSuperAdmin();
108   
109    // Remove the previous version that the setup has created.
110  1 getUtil().deleteLatestVersion("Mail", "MailConfig");
111    }
112   
 
113  1 toggle @Test
114    @IgnoreBrowsers({
115    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See http://jira.xwiki.org/browse/XE-1146"),
116    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason = "See http://jira.xwiki.org/browse/XE-1177")})
117    public void testWatchThisPageAndWholeSpace() throws Exception
118    {
119    // Clear the list of watched documents and spaces
120  1 getUtil().updateObject("XWiki", this.testUserName, "XWiki.WatchListClass", 0, "spaces", "", "documents", "");
121   
122    // Watch Test.TestWatchThisPage
123  1 ViewPage page =
124    getUtil().createPage(testSinglePageToWatchSpace, testSinglePageToWatch, "TestWatchThisPage ui", null);
125  1 page.watchDocument();
126   
127    // Watch TestWatchWholeSpace.Test1
128  1 page = getUtil().createPage(testSpaceToWatch, testSpaceToWatchPage, "TestWatchWholeSpace ui", null);
129  1 page.watchSpace();
130   
131    // Verify that the watched page & space are present in the watchlist manager
132  1 WatchlistUserProfilePage watchlistPage = WatchlistUserProfilePage.gotoPage(this.testUserName);
133  1 Assert.assertTrue(watchlistPage.getWatchlistMacro()
134    .isWatched(testSinglePageToWatchSpace, testSinglePageToWatch));
135  1 Assert.assertTrue(watchlistPage.getWatchlistMacro().isWatched(testSpaceToWatch));
136   
137    // Ensure that the watchlist notified is set to Daily since we're going to trigger that notifier scheduler job
138  1 WatchlistPreferencesEditPage watchlistPreferences = watchlistPage.editPreferences();
139  1 watchlistPreferences.setNotifierDaily();
140  1 watchlistPreferences.clickSaveAndContinue();
141   
142    // Switch to superadmin user and go to the scheduler home page
143  1 SchedulerHomePage schedulerHomePage = new SchedulerHomePage();
144  1 getUtil().loginAsSuperAdminAndGotoPage(schedulerHomePage.getURL());
145   
146    // Trigger the notification for the Daily job
147  1 schedulerHomePage.clickJobActionTrigger("WatchList daily notifier");
148   
149    // Wait for the email with a timeout
150  1 Assert.assertTrue("Scheduled notification mail not received", this.greenMail.waitForIncomingEmail(70000, 1));
151   
152    // Verify email content
153  1 MimeMessage[] receivedMails = this.greenMail.getReceivedMessages();
154  1 assertEquals(1, receivedMails.length);
155  1 String messageFromXWiki = GreenMailUtil.getBody(receivedMails[0]).replaceAll("=\r?\n", "");
156  1 Assert.assertFalse("should have no exception in " + messageFromXWiki, messageFromXWiki.contains("Exception"));
157  1 Assert.assertTrue("should have test page in the message " + messageFromXWiki,
158    messageFromXWiki.contains(testSinglePageToWatch));
159  1 Assert.assertTrue("should have test space in the message " + messageFromXWiki,
160    messageFromXWiki.contains(testSpaceToWatch));
161   
162    // // Clear the mock inbox for the following step.
163    // GreenMailUser mailUser = this.greenMail.getManagers().getUserManager().getUser("admin@localhost");
164    // this.greenMail.getManagers().getImapHostManager().deleteMailbox(mailUser, "INBOX");
165    // TODO: we might need the commented code above to make sure the mail is destined for our current test user and
166    // not other users created by previous tests.
167   
168  1 testRealtimeNotifier();
169    }
170   
171    /**
172    * Realtime notifications.
173    */
 
174  1 toggle private void testRealtimeNotifier() throws Exception
175    {
176    // Log back in with the user to test realtime notifications.
177  1 getUtil().login(testUserName, PASSWORD);
178   
179    // Set the notifier to 'Realtime'.
180  1 WatchlistUserProfilePage profilePage = WatchlistUserProfilePage.gotoPage(testUserName);
181  1 WatchlistUserProfilePage watchlistPage = profilePage.switchToWatchlist();
182   
183  1 WatchlistPreferencesEditPage watchlistPreferences = watchlistPage.editPreferences();
184  1 watchlistPreferences.setNotifierRealtime();
185  1 watchlistPreferences.clickSaveAndContinue();
186   
187    // Make a change in a watched document.
188    // Note: Taking a shortcut and just using the save action.
189  1 String content = "New content that watchlist should ignore.";
190  1 getUtil().gotoPage(testSinglePageToWatchSpace, testSinglePageToWatch, "save", "content", content);
191   
192    // Wait for an email that should never come.
193  1 Assert.assertFalse("Mail should not be received for own changes", this.greenMail.waitForIncomingEmail(3000, 2));
194   
195    // Create a second user that should trigger a realtime notification to the first user.
196  1 getUtil().createUserAndLogin(testUserName2, PASSWORD);
197   
198    // Make a change in a document watched by the first user.
199    // Note: Taking a shortcut and just using the save action.
200  1 String newContent = "New content that watchlist should notify about.";
201  1 content += "\n" + newContent;
202  1 getUtil().gotoPage(testSinglePageToWatchSpace, testSinglePageToWatch, "save", "content", content);
203   
204    // Wait for the email with a timeout.
205  1 Assert.assertTrue("Realtime notification mail not received", this.greenMail.waitForIncomingEmail(70000, 2));
206   
207    // Verify email content.
208  1 MimeMessage[] receivedMails = this.greenMail.getReceivedMessages();
209    // Second mail we`ve received, including the previous one from the daily notifier.
210  1 assertEquals(2, receivedMails.length);
211  1 String messageFromXWiki = GreenMailUtil.getBody(receivedMails[1]).replaceAll("=\r?\n", "");
212  1 Assert.assertFalse("should have no exception in " + messageFromXWiki, messageFromXWiki.contains("Exception"));
213  1 Assert.assertTrue("should have test page in the message " + messageFromXWiki,
214    messageFromXWiki.contains("TestWatchThisPage"));
215  1 Assert.assertTrue("should have second user in the message " + messageFromXWiki,
216    messageFromXWiki.contains(testUserName2));
217    }
218    }