Skip to content

Commit

Permalink
[#12048] Migrate Admin Notifications E2E Test (#12793)
Browse files Browse the repository at this point in the history
* feat: add resources for admin notifications e2e test

* fix: set created at on notification creation

* feat: migrate admin notifications e2e test

* fix: remove created at check for notifications

* fix: remove extra comments

* fix: remove explicit created at

* fix: null check for created at

* fix lint

---------

Co-authored-by: Cedric Ong <[email protected]>
Co-authored-by: Cedric Ong <[email protected]>
  • Loading branch information
3 people authored Feb 25, 2024
1 parent cbcab99 commit 500e742
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 34 deletions.
55 changes: 33 additions & 22 deletions src/e2e/java/teammates/e2e/cases/AdminNotificationsPageE2ETest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,33 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.UUID;

import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

import teammates.common.datatransfer.NotificationStyle;
import teammates.common.datatransfer.NotificationTargetUser;
import teammates.common.datatransfer.attributes.NotificationAttributes;
import teammates.common.util.AppUrl;
import teammates.common.util.Const;
import teammates.e2e.pageobjects.AdminNotificationsPage;
import teammates.storage.sqlentity.Notification;
import teammates.ui.output.NotificationData;

/**
* SUT: {@link Const.WebPageURIs#ADMIN_NOTIFICATIONS_PAGE}.
*/
public class AdminNotificationsPageE2ETest extends BaseE2ETestCase {
private NotificationAttributes[] notifications = new NotificationAttributes[2];
private Notification[] notifications = new Notification[2];

@Override
protected void prepareTestData() {
testData = loadDataBundle("/AdminNotificationsPageE2ETest.json");
removeAndRestoreDataBundle(testData);

notifications[0] = testData.notifications.get("notification1");
notifications[1] = testData.notifications.get("notification2");
sqlTestData = removeAndRestoreSqlDataBundle(
loadSqlDataBundle("/AdminNotificationsPageE2ETest_SqlEntities.json"));
notifications[0] = sqlTestData.notifications.get("notification1");
notifications[1] = sqlTestData.notifications.get("notification2");
}

@Test
Expand All @@ -40,32 +43,40 @@ public void testAll() {
// This is because the page will display all notifications in the database, which is not predictable
notificationsPage.verifyNotificationsTableRow(notifications[0]);
notificationsPage.verifyNotificationsTableRow(notifications[1]);
verifyPresentInDatabase(notifications[0]);
verifyPresentInDatabase(notifications[1]);
NotificationData notif = BACKDOOR.getNotificationData(notifications[0].getId().toString());
assertEquals(notif.getNotificationId(), notifications[0].getId().toString());
assertEquals(notif.getMessage(), notifications[0].getMessage());
assertEquals(notif.getTitle(), notifications[0].getTitle());
notif = BACKDOOR.getNotificationData(notifications[1].getId().toString());
assertEquals(notif.getNotificationId(), notifications[1].getId().toString());
assertEquals(notif.getMessage(), notifications[1].getMessage());
assertEquals(notif.getTitle(), notifications[1].getTitle());

______TS("add new notification");
int currentYear = LocalDate.now().getYear();
NotificationAttributes newNotification = NotificationAttributes
.builder("placeholder-notif-id")
.withStartTime(LocalDateTime.of(currentYear + 8, 1, 2, 12, 0).atZone(ZoneId.of("UTC")).toInstant())
.withEndTime(LocalDateTime.of(currentYear + 8, 1, 3, 12, 0).atZone(ZoneId.of("UTC")).toInstant())
.withStyle(NotificationStyle.SUCCESS)
.withTargetUser(NotificationTargetUser.GENERAL)
.withTitle("E2E test notification 1")
.withMessage("<p>E2E test notification message</p>")
.build();
Notification newNotification = new Notification(
LocalDateTime.of(currentYear + 5, 2, 2, 12, 0).atZone(ZoneId.of("UTC")).toInstant(),
LocalDateTime.of(currentYear + 5, 2, 3, 12, 0).atZone(ZoneId.of("UTC")).toInstant(),
NotificationStyle.INFO,
NotificationTargetUser.STUDENT,
"New E2E test notification 1",
"<p>New E2E test notification message</p>"
);

notificationsPage.addNotification(newNotification);
notificationsPage.verifyStatusMessage("Notification created successfully.");

// Replace placeholder ID with actual ID of created notification
notificationsPage.sortNotificationsTableByDescendingCreateTime();
String newestNotificationId = notificationsPage.getFirstRowNotificationId();
newNotification.setNotificationId(newestNotificationId);
newNotification.setId(UUID.fromString(newestNotificationId));

// Checks that notification is in the database first
// so that newNotification is updated with the created time before checking table row
verifyPresentInDatabase(newNotification);
notif = BACKDOOR.getNotificationData(newestNotificationId);
assertEquals(notif.getNotificationId(), newestNotificationId);
assertEquals(notif.getMessage(), newNotification.getMessage());
assertEquals(notif.getTitle(), newNotification.getTitle());
notificationsPage.verifyNotificationsTableRow(newNotification);

______TS("edit notification");
Expand All @@ -87,14 +98,14 @@ public void testAll() {
______TS("delete notification");
notificationsPage.deleteNotification(newNotification);
notificationsPage.verifyStatusMessage("Notification has been deleted.");
verifyAbsentInDatabase(newNotification);

notif = BACKDOOR.getNotificationData(newestNotificationId);
assertNull(notif);
}

@AfterClass
public void classTeardown() {
for (NotificationAttributes notification : testData.notifications.values()) {
BACKDOOR.deleteNotification(notification.getNotificationId());
for (Notification notification : sqlTestData.notifications.values()) {
BACKDOOR.deleteNotification(notification.getId().toString());
}
}

Expand Down
21 changes: 10 additions & 11 deletions src/e2e/java/teammates/e2e/pageobjects/AdminNotificationsPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import teammates.common.datatransfer.NotificationStyle;
import teammates.common.datatransfer.NotificationTargetUser;
import teammates.common.datatransfer.attributes.NotificationAttributes;
import teammates.storage.sqlentity.Notification;

/**
* Page Object Model for the admin notifications page.
Expand Down Expand Up @@ -66,12 +66,12 @@ protected boolean containsExpectedPageContents() {
return getPageSource().contains("Notifications");
}

public void verifyNotificationsTableRow(NotificationAttributes notification) {
WebElement notificationRow = notificationsTable.findElement(By.id(notification.getNotificationId()));
public void verifyNotificationsTableRow(Notification notification) {
WebElement notificationRow = notificationsTable.findElement(By.id(notification.getId().toString()));
verifyTableRowValues(notificationRow, getNotificationTableDisplayDetails(notification));
}

public void addNotification(NotificationAttributes notification) {
public void addNotification(Notification notification) {
clickAddNotificationButton();
waitForElementPresence(By.id("btn-create-notification"));

Expand All @@ -81,8 +81,8 @@ public void addNotification(NotificationAttributes notification) {
waitForPageToLoad(true);
}

public void editNotification(NotificationAttributes notification) {
WebElement notificationRow = notificationsTable.findElement(By.id(notification.getNotificationId()));
public void editNotification(Notification notification) {
WebElement notificationRow = notificationsTable.findElement(By.id(notification.getId().toString()));
WebElement editButton = notificationRow.findElement(By.className("btn-light"));
editButton.click();
waitForElementPresence(By.id("btn-edit-notification"));
Expand All @@ -93,16 +93,16 @@ public void editNotification(NotificationAttributes notification) {
waitForPageToLoad(true);
}

public void deleteNotification(NotificationAttributes notification) {
WebElement notificationRow = notificationsTable.findElement(By.id(notification.getNotificationId()));
public void deleteNotification(Notification notification) {
WebElement notificationRow = notificationsTable.findElement(By.id(notification.getId().toString()));
WebElement deleteButton = notificationRow.findElement(By.className("btn-danger"));

deleteButton.click();
waitForConfirmationModalAndClickOk();
waitForPageToLoad(true);
}

public void fillNotificationForm(NotificationAttributes notification) {
public void fillNotificationForm(Notification notification) {
selectDropdownOptionByText(notificationTargetUserDropdown, getTargetUserText(notification.getTargetUser()));
selectDropdownOptionByText(notificationStyleDropdown, getNotificationStyle(notification.getStyle()));
fillTextBox(notificationTitleTextBox, notification.getTitle());
Expand Down Expand Up @@ -153,14 +153,13 @@ private void setDateTime(WebElement dateBox, WebElement timeBox, Instant startIn
selectDropdownOptionByText(timeBox.findElement(By.tagName("select")), getInputTimeString(startInstant));
}

private String[] getNotificationTableDisplayDetails(NotificationAttributes notification) {
private String[] getNotificationTableDisplayDetails(Notification notification) {
return new String[] {
notification.getTitle(),
getTableDisplayDateString(notification.getStartTime()),
getTableDisplayDateString(notification.getEndTime()),
notification.getTargetUser().toString(),
getNotificationStyle(notification.getStyle()),
getTableDisplayDateString(notification.getCreatedAt()),
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"notifications": {
"notification1": {
"id": "00000000-0000-4000-8000-000000001103",
"startTime": "2011-01-01T00:00:00Z",
"endTime": "2099-01-01T00:00:00Z",
"createdAt": "2011-01-01T00:00:00Z",
"style": "DANGER",
"targetUser": "GENERAL",
"title": "A deprecation note",
"message": "<p>Deprecation happens in three minutes</p>",
"shown": false
},
"notification2": {
"id": "00000000-0000-4000-8000-000000001103",
"startTime": "2011-02-02T00:00:00Z",
"endTime": "2099-02-02T00:00:00Z",
"createdAt": "2011-01-01T00:00:00Z",
"style": "SUCCESS",
"targetUser": "STUDENT",
"title": "A note for update",
"message": "<p>Exciting features</p>",
"shown": false
}
}
}
5 changes: 4 additions & 1 deletion src/main/java/teammates/ui/output/NotificationData.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package teammates.ui.output;

import org.threeten.bp.Instant;

import teammates.common.datatransfer.NotificationStyle;
import teammates.common.datatransfer.NotificationTargetUser;
import teammates.common.datatransfer.attributes.NotificationAttributes;
Expand Down Expand Up @@ -36,7 +38,8 @@ public NotificationData(Notification notification) {
this.notificationId = notification.getId().toString();
this.startTimestamp = notification.getStartTime().toEpochMilli();
this.endTimestamp = notification.getEndTime().toEpochMilli();
this.createdAt = notification.getCreatedAt().toEpochMilli();
this.createdAt = notification.getCreatedAt() == null
? Instant.now().toEpochMilli() : notification.getCreatedAt().toEpochMilli();
this.style = notification.getStyle();
this.targetUser = notification.getTargetUser();
this.title = notification.getTitle();
Expand Down

0 comments on commit 500e742

Please sign in to comment.