Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#11572] Notification Feature - Notification Logic Tests #11696

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 166 additions & 24 deletions src/test/java/teammates/logic/core/NotificationsLogicTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package teammates.logic.core;

import java.time.Instant;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import teammates.common.datatransfer.NotificationStyle;
import teammates.common.datatransfer.NotificationTargetUser;
import teammates.common.datatransfer.attributes.NotificationAttributes;
import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.storage.api.NotificationsDb;

/**
Expand All @@ -17,6 +23,7 @@ public class NotificationsLogicTest extends BaseLogicTest {
private NotificationAttributes n;
private final NotificationsDb notifDb = NotificationsDb.inst();
private final NotificationsLogic notifLogic = NotificationsLogic.inst();
private final Map<String, NotificationAttributes> typicalNotifications = getTypicalDataBundle().notifications;

@Override
protected void prepareTestData() {
Expand All @@ -32,60 +39,195 @@ public void refreshTestData() {
@Test
public void testAll() throws Exception {
testGetNotification();
testGetAllNotifications();
testGetActiveNotificationsByTargetUser();
testCreateNotification();
testUpdateNotification();
testDeleteNotification();
}

private void testGetNotification() throws Exception {
______TS("success: typical case");
private void testGetNotification() {
n = typicalNotifications.get("notification1");

n = getTypicalNotification();
notifDb.createEntity(n);
______TS("success: typical case");

NotificationAttributes retrievedNotif = notifLogic.getNotification(n.getNotificationId());
verifyPresentInDatabase(retrievedNotif);
NotificationAttributes actual = notifLogic.getNotification(n.getNotificationId());
assertNotNull(actual);
verifyNotificationEquals(n, actual);

notifDb.deleteNotification(n.getNotificationId());
______TS("failure: Null parameter");
______TS("failure: null parameter");

assertThrows(AssertionError.class,
() -> notifLogic.getNotification(null));

______TS("failure: notification doesn't exist");
______TS("failure: non-existent notification");

assertNull(notifLogic.getNotification("invalid_notification_id"));
}

private void testGetAllNotifications() {
______TS("success: retrieve all notifications that exist in database");

List<NotificationAttributes> actual = notifLogic.getAllNotifications();
assertNotNull(actual);
typicalNotifications.values().forEach(n -> {
assertTrue(actual.contains(n));
actual.remove(n);
});
}

private void testGetActiveNotificationsByTargetUser() {
______TS("success: valid target user");

List<NotificationAttributes> actual =
notifLogic.getActiveNotificationsByTargetUser(NotificationTargetUser.STUDENT);

assertNotNull(actual);

Set<NotificationAttributes> expected = new HashSet<>();
expected.add(typicalNotifications.get("notification1"));
expected.add(typicalNotifications.get("notification2"));
expected.add(typicalNotifications.get("notification4"));
expected.add(typicalNotifications.get("notification6"));

assertNull(notifLogic.getNotification("nonexistant-notification"));
expected.forEach(n -> {
assertTrue(actual.contains(n));
actual.remove(n);
});
}

private void testCreateNotification() throws Exception {
______TS("success: typical case");

n = getTypicalNotification();
n = getNewNotificationAttributes();
notifLogic.createNotification(n);

verifyPresentInDatabase(n);

notifDb.deleteNotification(n.getNotificationId());
______TS("failure: duplicate notification with the same ID");

assertThrows(EntityAlreadyExistsException.class, () -> notifLogic.createNotification(n));

removeNotificationsFromDb(n);

______TS("failure: Null parameter");

assertThrows(AssertionError.class,
() -> notifLogic.createNotification(null));

______TS("failure: test create with invalid title name");

n = getTypicalNotification();
n = getNewNotificationAttributes();
n.setTitle("");
Exception e = assertThrows(Exception.class, () -> notifLogic.createNotification(n));
assertEquals("The field 'notification title' is empty.", e.getMessage());
verifyAbsentInDatabase(n);
}

private NotificationAttributes getTypicalNotification() {
return NotificationAttributes
.builder("some-notif-id")
.withStartTime(Instant.now())
.withEndTime(Instant.now().plusSeconds(100))
.withStyle(NotificationStyle.WARNING)
.withTargetUser(NotificationTargetUser.INSTRUCTOR)
.withTitle("Maintenance Notice")
.withMessage("Maintenance at 3pm today")
private void testUpdateNotification() throws Exception {
______TS("success: typical case");

n = typicalNotifications.get("notification1");
NotificationAttributes differentNotification = typicalNotifications.get("notification2");

NotificationAttributes.UpdateOptions update1 =
NotificationAttributes.updateOptionsBuilder(n.getNotificationId())
.withTitle(differentNotification.getTitle())
.withMessage(differentNotification.getMessage())
.withStyle(differentNotification.getStyle())
.withTargetUser(differentNotification.getTargetUser())
.withStartTime(differentNotification.getStartTime())
.withEndTime(differentNotification.getEndTime())
.withShown()
.build();

NotificationAttributes actual = notifLogic.updateNotification(update1);
assertEquals(differentNotification.getTitle(), actual.getTitle());
assertEquals(differentNotification.getMessage(), actual.getMessage());
assertEquals(differentNotification.getStyle(), actual.getStyle());
assertEquals(differentNotification.getTargetUser(), actual.getTargetUser());
assertEquals(differentNotification.getStartTime(), actual.getStartTime());
assertEquals(differentNotification.getEndTime(), actual.getEndTime());
assertTrue(actual.isShown());

______TS("failure: invalid update options");

n = typicalNotifications.get("notification1");

NotificationAttributes.UpdateOptions update2 =
NotificationAttributes.updateOptionsBuilder(n.getNotificationId())
.withTitle("")
.build();

assertThrows(InvalidParametersException.class,
() -> notifLogic.updateNotification(update2));

______TS("failure: invalid notification id");

NotificationAttributes.UpdateOptions update3 =
NotificationAttributes.updateOptionsBuilder("invalid-notification-id")
.withTitle("A valid title")
.build();

assertThrows(EntityDoesNotExistException.class,
() -> notifLogic.updateNotification(update3));

______TS("failure: null update options");

assertThrows(AssertionError.class, () -> notifLogic.updateNotification(null));
}

private void testDeleteNotification() {
______TS("success: delete corresponding notification");

n = typicalNotifications.get("notification1");
notifLogic.deleteNotification(n.getNotificationId());

verifyAbsentInDatabase(n);

______TS("failure: silent deletion of the same notification twice");

notifLogic.deleteNotification(n.getNotificationId());

______TS("failure: silent deletion of non-existent notification");

int expectedLength = notifDb.getAllNotifications().size();
notifLogic.deleteNotification("invalid-id");
int actualLength = notifDb.getAllNotifications().size();

assertEquals(expectedLength, actualLength);

______TS("failure: null parameter");

assertThrows(AssertionError.class, () -> notifLogic.deleteNotification(null));
}

private NotificationAttributes getNewNotificationAttributes() {
NotificationAttributes typical = typicalNotifications.get("notification1");
return NotificationAttributes.builder(UUID.randomUUID().toString())
.withTitle(typical.getTitle())
.withMessage(typical.getMessage())
.withStyle(typical.getStyle())
.withTargetUser(typical.getTargetUser())
.withStartTime(typical.getStartTime())
.withEndTime(typical.getEndTime())
.build();
}

private void removeNotificationsFromDb(NotificationAttributes... notifications) {
for (NotificationAttributes notif : notifications) {
notifDb.deleteNotification(notif.getNotificationId());
}
}

private void verifyNotificationEquals(NotificationAttributes expected, NotificationAttributes actual) {
assertEquals(expected.getNotificationId(), actual.getNotificationId());
assertEquals(expected.getMessage(), actual.getMessage());
assertEquals(expected.getStyle(), actual.getStyle());
assertEquals(expected.getTargetUser(), actual.getTargetUser());
assertEquals(expected.getTitle(), actual.getTitle());
assertEquals(expected.getMessage(), actual.getMessage());
assertEquals(expected.getStartTime(), actual.getStartTime());
assertEquals(expected.getEndTime(), actual.getEndTime());
}
}
4 changes: 2 additions & 2 deletions src/test/java/teammates/storage/api/NotificationsDbTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void testGetNotification() throws Exception {
NotificationAttributes actual = notificationsDb.getNotification(n.getNotificationId());
assertNotNull(actual);

______TS("expect null for non-existent account");
______TS("expect null for non-existent notification");
actual = notificationsDb.getNotification("invalid_notification_id");
assertNull(actual);

Expand Down Expand Up @@ -238,7 +238,7 @@ public void testUpdateNotification_singleFieldUpdate_shouldUpdateSuccessfully()
public void testDeleteNotification() throws Exception {
NotificationAttributes n = typicalNotifications.get("notification1");

______TS("silent deletion of non-existant notification");
______TS("silent deletion of non-existent notification");
notificationsDb.deleteNotification("invalid_notification_id");

______TS("typical success case");
Expand Down