From ff83226ee512b7be66635d0465191da00da40461 Mon Sep 17 00:00:00 2001 From: hhdqirui <53338059+hhdqirui@users.noreply.github.com> Date: Sat, 11 Feb 2023 17:19:08 +0800 Subject: [PATCH 01/14] Create sql logic for CreateNotificationAction and add tests --- .../it/storage/sqlapi/NotificationDbIT.java | 87 ++++++++++++++ .../java/teammates/sqllogic/api/Logic.java | 18 +++ .../sqllogic/core/NotificationsLogic.java | 40 +++++++ .../storage/sqlapi/NotificationsDb.java | 10 +- .../storage/sqlentity/Notification.java | 4 + .../teammates/ui/output/NotificationData.java | 13 +++ .../ui/webapi/CreateNotificationAction.java | 6 +- .../storage/sqlapi/NotificationsDbTest.java | 106 ++++++++++++++++++ 8 files changed, 277 insertions(+), 7 deletions(-) create mode 100644 src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java create mode 100644 src/main/java/teammates/sqllogic/core/NotificationsLogic.java create mode 100644 src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java diff --git a/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java b/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java new file mode 100644 index 00000000000..fc8ef04918a --- /dev/null +++ b/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java @@ -0,0 +1,87 @@ +package teammates.it.storage.sqlapi; + +import org.testng.annotations.Test; +import teammates.common.datatransfer.NotificationStyle; +import teammates.common.datatransfer.NotificationTargetUser; +import teammates.common.exception.EntityAlreadyExistsException; +import teammates.common.exception.InvalidParametersException; +import teammates.it.test.BaseTestCaseWithSqlDatabaseAccess; +import teammates.storage.sqlapi.NotificationsDb; +import teammates.storage.sqlentity.Notification; + +import java.time.Instant; +import java.util.UUID; + +/** + * SUT: {@link NotificationsDb}. + */ +public class NotificationDbIT extends BaseTestCaseWithSqlDatabaseAccess { + + private final NotificationsDb notificationsDb = NotificationsDb.inst(); + + @Test + public void testCreateNotification() throws EntityAlreadyExistsException, InvalidParametersException { + ______TS("success: create notification that does not exists"); + Notification newNotification = new Notification.NotificationBuilder("A deprecation note") + .withStartTime(Instant.parse("2011-01-01T00:00:00Z")) + .withEndTime(Instant.parse("2099-01-01T00:00:00Z")) + .withStyle(NotificationStyle.DANGER) + .withTargetUser(NotificationTargetUser.GENERAL) + .withMessage("

Deprecation happens in three minutes

") + .build(); + + notificationsDb.createNotification(newNotification); + + UUID notificationId = newNotification.getNotificationId(); + Notification actualNotification = notificationsDb.getNotification(notificationId); + verifyEquals(newNotification, actualNotification); + + ______TS("failure: create notification that already exists, exception thrown"); + Notification identicalNotification = new Notification.NotificationBuilder("A deprecation note") + .withStartTime(Instant.parse("2011-01-01T00:00:00Z")) + .withEndTime(Instant.parse("2099-01-01T00:00:00Z")) + .withStyle(NotificationStyle.DANGER) + .withTargetUser(NotificationTargetUser.GENERAL) + .withMessage("

Deprecation happens in three minutes

") + .build(); + identicalNotification.setNotificationId(notificationId); + + assertNotSame(newNotification, identicalNotification); + EntityAlreadyExistsException ex = assertThrows(EntityAlreadyExistsException.class, + () -> notificationsDb.createNotification(identicalNotification)); + assertEquals(ex.getMessage(), "Trying to create an entity that exists: " + identicalNotification.toString()); + + ______TS("failure: invalid parameter, start time is before end time"); + Notification invalidNotification1 = new Notification.NotificationBuilder("A deprecation note") + .withStartTime(Instant.parse("2011-02-01T00:00:00Z")) + .withEndTime(Instant.parse("2011-01-01T00:00:00Z")) + .withStyle(NotificationStyle.DANGER) + .withTargetUser(NotificationTargetUser.GENERAL) + .withMessage("

Deprecation happens in three minutes

") + .build(); + + assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification1)); + + ______TS("failure: empty title"); + Notification invalidNotification2 = new Notification.NotificationBuilder("") + .withStartTime(Instant.parse("2011-01-01T00:00:00Z")) + .withEndTime(Instant.parse("2099-01-01T00:00:00Z")) + .withStyle(NotificationStyle.DANGER) + .withTargetUser(NotificationTargetUser.GENERAL) + .withMessage("

Deprecation happens in three minutes

") + .build(); + + assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification2)); + + ______TS("failure: empty message"); + Notification invalidNotification3 = new Notification.NotificationBuilder("A deprecation note") + .withStartTime(Instant.parse("2011-01-01T00:00:00Z")) + .withEndTime(Instant.parse("2099-01-01T00:00:00Z")) + .withStyle(NotificationStyle.DANGER) + .withTargetUser(NotificationTargetUser.GENERAL) + .withMessage("") + .build(); + + assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification3)); + } +} diff --git a/src/main/java/teammates/sqllogic/api/Logic.java b/src/main/java/teammates/sqllogic/api/Logic.java index ec0bd785243..2ad457e70c7 100644 --- a/src/main/java/teammates/sqllogic/api/Logic.java +++ b/src/main/java/teammates/sqllogic/api/Logic.java @@ -3,7 +3,9 @@ import teammates.common.exception.EntityAlreadyExistsException; import teammates.common.exception.InvalidParametersException; import teammates.sqllogic.core.CoursesLogic; +import teammates.sqllogic.core.NotificationsLogic; import teammates.storage.sqlentity.Course; +import teammates.storage.sqlentity.Notification; /** * Provides the business logic for production usage of the system. @@ -15,6 +17,7 @@ public class Logic { final CoursesLogic coursesLogic = CoursesLogic.inst(); // final FeedbackSessionsLogic feedbackSessionsLogic = FeedbackSessionsLogic.inst(); + final NotificationsLogic notificationsLogic = NotificationsLogic.inst(); Logic() { // prevent initialization @@ -45,4 +48,19 @@ public Course getCourse(String courseId) { public Course createCourse(Course course) throws InvalidParametersException, EntityAlreadyExistsException { return coursesLogic.createCourse(course); } + + /** + * Creates a notification. + * + *

Preconditions:

+ * * All parameters are non-null. + * + * @return created notification + * @throws InvalidParametersException if the notification is not valid + * @throws EntityAlreadyExistsException if the notification exists in the database + */ + public Notification createNotification(Notification notification) throws + InvalidParametersException, EntityAlreadyExistsException { + return notificationsLogic.createNotification(notification); + } } diff --git a/src/main/java/teammates/sqllogic/core/NotificationsLogic.java b/src/main/java/teammates/sqllogic/core/NotificationsLogic.java new file mode 100644 index 00000000000..5fbb9a25149 --- /dev/null +++ b/src/main/java/teammates/sqllogic/core/NotificationsLogic.java @@ -0,0 +1,40 @@ +package teammates.sqllogic.core; + +import teammates.common.exception.EntityAlreadyExistsException; +import teammates.common.exception.InvalidParametersException; +import teammates.storage.sqlapi.NotificationsDb; +import teammates.storage.sqlentity.Notification; + +/** + * Handles the logic related to notifications. + */ +public final class NotificationsLogic { + + private static final NotificationsLogic instance = new NotificationsLogic(); + + private final NotificationsDb notificationsDb = NotificationsDb.inst(); + + private NotificationsLogic() { + // prevent initialization + } + + public static NotificationsLogic inst() { + return instance; + } + + void initLogicDependencies() { + // No dependency to other logic class + } + + /** + * Creates a notification. + * + * @return the created notification + * @throws InvalidParametersException if the notification is not valid + * @throws EntityAlreadyExistsException if the notification already exists in the database. + */ + public Notification createNotification(Notification notification) + throws InvalidParametersException, EntityAlreadyExistsException { + return notificationsDb.createNotification(notification); + } +} diff --git a/src/main/java/teammates/storage/sqlapi/NotificationsDb.java b/src/main/java/teammates/storage/sqlapi/NotificationsDb.java index fb170661586..d88aff0d062 100644 --- a/src/main/java/teammates/storage/sqlapi/NotificationsDb.java +++ b/src/main/java/teammates/storage/sqlapi/NotificationsDb.java @@ -6,6 +6,8 @@ import teammates.common.util.HibernateUtil; import teammates.storage.sqlentity.Notification; +import java.util.UUID; + /** * Handles CRUD operations for notifications. * @@ -35,7 +37,7 @@ public Notification createNotification(Notification notification) throw new InvalidParametersException(notification.getInvalidityInfo()); } - if (getNotification(notification.getNotificationId().toString()) != null) { + if (notification.getNotificationId() != null && getNotification(notification.getNotificationId()) != null) { throw new EntityAlreadyExistsException(String.format(ERROR_CREATE_ENTITY_ALREADY_EXISTS, notification.toString())); } @@ -47,7 +49,7 @@ public Notification createNotification(Notification notification) /** * Gets a notification by its unique ID. */ - public Notification getNotification(String notificationId) { + public Notification getNotification(UUID notificationId) { assert notificationId != null; return HibernateUtil.getSessionFactory().getCurrentSession().get(Notification.class, notificationId); @@ -66,7 +68,7 @@ public Notification updateNotification(Notification notification) throw new InvalidParametersException(notification.getInvalidityInfo()); } - if (getNotification(notification.getNotificationId().toString()) == null) { + if (getNotification(notification.getNotificationId()) == null) { throw new EntityDoesNotExistException(ERROR_UPDATE_NON_EXISTENT); } @@ -78,7 +80,7 @@ public Notification updateNotification(Notification notification) * *

Fails silently if there is no such notification. */ - public void deleteNotification(String notificationId) { + public void deleteNotification(UUID notificationId) { assert notificationId != null; Notification notification = getNotification(notificationId); diff --git a/src/main/java/teammates/storage/sqlentity/Notification.java b/src/main/java/teammates/storage/sqlentity/Notification.java index 91e0c0e8be8..51e0c4a6dbc 100644 --- a/src/main/java/teammates/storage/sqlentity/Notification.java +++ b/src/main/java/teammates/storage/sqlentity/Notification.java @@ -111,6 +111,10 @@ public UUID getNotificationId() { return notificationId; } + public void setNotificationId(UUID notificationId) { + this.notificationId = notificationId; + } + public Instant getStartTime() { return startTime; } diff --git a/src/main/java/teammates/ui/output/NotificationData.java b/src/main/java/teammates/ui/output/NotificationData.java index ecc9384a7c2..f65ad0b694f 100644 --- a/src/main/java/teammates/ui/output/NotificationData.java +++ b/src/main/java/teammates/ui/output/NotificationData.java @@ -3,6 +3,7 @@ import teammates.common.datatransfer.NotificationStyle; import teammates.common.datatransfer.NotificationTargetUser; import teammates.common.datatransfer.attributes.NotificationAttributes; +import teammates.storage.sqlentity.Notification; /** * The API output format of a notification. @@ -31,6 +32,18 @@ public NotificationData(NotificationAttributes notificationAttributes) { this.shown = notificationAttributes.isShown(); } + public NotificationData(Notification notification) { + this.notificationId = notification.getNotificationId().toString(); + this.startTimestamp = notification.getStartTime().toEpochMilli(); + this.endTimestamp = notification.getEndTime().toEpochMilli(); + this.createdAt = notification.getCreatedAt().toEpochMilli(); + this.style = notification.getStyle(); + this.targetUser = notification.getTargetUser(); + this.title = notification.getTitle(); + this.message = notification.getMessage(); + this.shown = notification.isShown(); + } + public String getNotificationId() { return this.notificationId; } diff --git a/src/main/java/teammates/ui/webapi/CreateNotificationAction.java b/src/main/java/teammates/ui/webapi/CreateNotificationAction.java index 1a086d21596..68ca73c0cb5 100644 --- a/src/main/java/teammates/ui/webapi/CreateNotificationAction.java +++ b/src/main/java/teammates/ui/webapi/CreateNotificationAction.java @@ -9,6 +9,7 @@ import teammates.common.exception.EntityAlreadyExistsException; import teammates.common.exception.InvalidParametersException; import teammates.common.util.Logger; +import teammates.storage.sqlentity.Notification; import teammates.ui.output.NotificationData; import teammates.ui.request.InvalidHttpRequestBodyException; import teammates.ui.request.NotificationCreateRequest; @@ -26,17 +27,16 @@ public JsonResult execute() throws InvalidHttpRequestBodyException, InvalidOpera Instant startTime = Instant.ofEpochMilli(notificationRequest.getStartTimestamp()); Instant endTime = Instant.ofEpochMilli(notificationRequest.getEndTimestamp()); - NotificationAttributes newNotification = NotificationAttributes.builder(UUID.randomUUID().toString()) + Notification newNotification = new Notification.NotificationBuilder(notificationRequest.getTitle()) .withStartTime(startTime) .withEndTime(endTime) .withStyle(notificationRequest.getStyle()) .withTargetUser(notificationRequest.getTargetUser()) - .withTitle(notificationRequest.getTitle()) .withMessage(notificationRequest.getMessage()) .build(); try { - return new JsonResult(new NotificationData(logic.createNotification(newNotification))); + return new JsonResult(new NotificationData(sqlLogic.createNotification(newNotification))); } catch (InvalidParametersException e) { throw new InvalidHttpRequestBodyException(e); } catch (EntityAlreadyExistsException e) { diff --git a/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java b/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java new file mode 100644 index 00000000000..114cacae0b1 --- /dev/null +++ b/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java @@ -0,0 +1,106 @@ +package teammates.storage.sqlapi; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; +import teammates.common.datatransfer.NotificationStyle; +import teammates.common.datatransfer.NotificationTargetUser; +import teammates.common.exception.EntityAlreadyExistsException; +import teammates.common.exception.InvalidParametersException; +import teammates.common.util.HibernateUtil; +import teammates.storage.sqlentity.Notification; +import teammates.test.BaseTestCase; + +import java.time.Instant; +import java.util.UUID; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * SUT: {@code NotificationsDb}. + */ +public class NotificationsDbTest extends BaseTestCase { + + private NotificationsDb notificationsDb = NotificationsDb.inst(); + + private Session session; + + @BeforeMethod + public void setUp() { + session = mock(Session.class); + SessionFactory sessionFactory = mock(SessionFactory.class); + HibernateUtil.setSessionFactory(sessionFactory); + when(sessionFactory.getCurrentSession()).thenReturn(session); + } + + @Test + public void testCreateNotification_success() throws EntityAlreadyExistsException, InvalidParametersException { + Notification newNotification = new Notification.NotificationBuilder("A deprecation note") + .withStartTime(Instant.parse("2011-01-01T00:00:00Z")) + .withEndTime(Instant.parse("2099-01-01T00:00:00Z")) + .withStyle(NotificationStyle.DANGER) + .withTargetUser(NotificationTargetUser.GENERAL) + .withMessage("

Deprecation happens in three minutes

") + .build(); + UUID notificationId = UUID.randomUUID(); + newNotification.setNotificationId(notificationId); + when(session.get(Notification.class, notificationId.toString())).thenReturn(null); + + notificationsDb.createNotification(newNotification); + + verify(session, times(1)).persist(newNotification); + } + + @Test + public void testCreateNotification_invalidNonNullParameters() { + ______TS("failure: start time is before end time"); + Notification invalidNotification1 = new Notification.NotificationBuilder("A deprecation note") + .withStartTime(Instant.parse("2011-02-01T00:00:00Z")) + .withEndTime(Instant.parse("2011-01-01T00:00:00Z")) + .withStyle(NotificationStyle.DANGER) + .withTargetUser(NotificationTargetUser.GENERAL) + .withMessage("

Deprecation happens in three minutes

") + .build(); + UUID notificationId = UUID.randomUUID(); + invalidNotification1.setNotificationId(notificationId); + when(session.get(Notification.class, notificationId.toString())).thenReturn(invalidNotification1); + + assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification1)); + verify(session, never()).persist(invalidNotification1); + + ______TS("failure: empty title"); + Notification invalidNotification2 = new Notification.NotificationBuilder("") + .withStartTime(Instant.parse("2011-01-01T00:00:00Z")) + .withEndTime(Instant.parse("2099-01-01T00:00:00Z")) + .withStyle(NotificationStyle.DANGER) + .withTargetUser(NotificationTargetUser.GENERAL) + .withMessage("

Deprecation happens in three minutes

") + .build(); + notificationId = UUID.randomUUID(); + invalidNotification2.setNotificationId(notificationId); + when(session.get(Notification.class, notificationId.toString())).thenReturn(invalidNotification2); + + assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification2)); + verify(session, never()).persist(invalidNotification2); + + ______TS("failure: empty message"); + Notification invalidNotification3 = new Notification.NotificationBuilder("A deprecation note") + .withStartTime(Instant.parse("2011-01-01T00:00:00Z")) + .withEndTime(Instant.parse("2099-01-01T00:00:00Z")) + .withStyle(NotificationStyle.DANGER) + .withTargetUser(NotificationTargetUser.GENERAL) + .withMessage("") + .build(); + notificationId = UUID.randomUUID(); + invalidNotification3.setNotificationId(notificationId); + when(session.get(Notification.class, notificationId.toString())).thenReturn(invalidNotification3); + + assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification3)); + verify(session, never()).persist(invalidNotification3); + } +} From b203f5090257d875f1038d5acd60463defb8a10c Mon Sep 17 00:00:00 2001 From: hhdqirui <53338059+hhdqirui@users.noreply.github.com> Date: Sat, 11 Feb 2023 17:39:19 +0800 Subject: [PATCH 02/14] Fix lint tests --- .../it/storage/sqlapi/NotificationDbIT.java | 7 ++++--- .../sqllogic/core/NotificationsLogic.java | 2 +- .../storage/sqlapi/NotificationsDb.java | 4 ++-- .../ui/webapi/CreateNotificationAction.java | 2 -- .../storage/sqlapi/NotificationsDbTest.java | 19 ++++++++++--------- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java b/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java index fc8ef04918a..85464d93593 100644 --- a/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java +++ b/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java @@ -1,6 +1,10 @@ package teammates.it.storage.sqlapi; +import java.time.Instant; +import java.util.UUID; + import org.testng.annotations.Test; + import teammates.common.datatransfer.NotificationStyle; import teammates.common.datatransfer.NotificationTargetUser; import teammates.common.exception.EntityAlreadyExistsException; @@ -9,9 +13,6 @@ import teammates.storage.sqlapi.NotificationsDb; import teammates.storage.sqlentity.Notification; -import java.time.Instant; -import java.util.UUID; - /** * SUT: {@link NotificationsDb}. */ diff --git a/src/main/java/teammates/sqllogic/core/NotificationsLogic.java b/src/main/java/teammates/sqllogic/core/NotificationsLogic.java index 5fbb9a25149..a5a64c99c40 100644 --- a/src/main/java/teammates/sqllogic/core/NotificationsLogic.java +++ b/src/main/java/teammates/sqllogic/core/NotificationsLogic.java @@ -14,7 +14,7 @@ public final class NotificationsLogic { private final NotificationsDb notificationsDb = NotificationsDb.inst(); - private NotificationsLogic() { + private NotificationsLogic() { // prevent initialization } diff --git a/src/main/java/teammates/storage/sqlapi/NotificationsDb.java b/src/main/java/teammates/storage/sqlapi/NotificationsDb.java index d88aff0d062..41818e37fe6 100644 --- a/src/main/java/teammates/storage/sqlapi/NotificationsDb.java +++ b/src/main/java/teammates/storage/sqlapi/NotificationsDb.java @@ -1,13 +1,13 @@ package teammates.storage.sqlapi; +import java.util.UUID; + import teammates.common.exception.EntityAlreadyExistsException; import teammates.common.exception.EntityDoesNotExistException; import teammates.common.exception.InvalidParametersException; import teammates.common.util.HibernateUtil; import teammates.storage.sqlentity.Notification; -import java.util.UUID; - /** * Handles CRUD operations for notifications. * diff --git a/src/main/java/teammates/ui/webapi/CreateNotificationAction.java b/src/main/java/teammates/ui/webapi/CreateNotificationAction.java index 68ca73c0cb5..ad77e1bcee3 100644 --- a/src/main/java/teammates/ui/webapi/CreateNotificationAction.java +++ b/src/main/java/teammates/ui/webapi/CreateNotificationAction.java @@ -1,11 +1,9 @@ package teammates.ui.webapi; import java.time.Instant; -import java.util.UUID; import org.apache.http.HttpStatus; -import teammates.common.datatransfer.attributes.NotificationAttributes; import teammates.common.exception.EntityAlreadyExistsException; import teammates.common.exception.InvalidParametersException; import teammates.common.util.Logger; diff --git a/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java b/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java index 114cacae0b1..cd02ba59d13 100644 --- a/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java +++ b/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java @@ -1,9 +1,19 @@ package teammates.storage.sqlapi; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.time.Instant; +import java.util.UUID; + import org.hibernate.Session; import org.hibernate.SessionFactory; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; + import teammates.common.datatransfer.NotificationStyle; import teammates.common.datatransfer.NotificationTargetUser; import teammates.common.exception.EntityAlreadyExistsException; @@ -12,15 +22,6 @@ import teammates.storage.sqlentity.Notification; import teammates.test.BaseTestCase; -import java.time.Instant; -import java.util.UUID; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - /** * SUT: {@code NotificationsDb}. */ From 42bd5ade02f4d424892723f121e5f1ef9d634bf9 Mon Sep 17 00:00:00 2001 From: hhdqirui <53338059+hhdqirui@users.noreply.github.com> Date: Sat, 11 Feb 2023 19:25:03 +0800 Subject: [PATCH 03/14] Update db tests --- .../storage/sqlapi/NotificationsDbTest.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java b/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java index cd02ba59d13..dc2e1df1d8e 100644 --- a/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java +++ b/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java @@ -58,8 +58,7 @@ public void testCreateNotification_success() throws EntityAlreadyExistsException } @Test - public void testCreateNotification_invalidNonNullParameters() { - ______TS("failure: start time is before end time"); + public void testCreateNotification_invalidNonNullParameters_endTimeIsBeforeStartTime() { Notification invalidNotification1 = new Notification.NotificationBuilder("A deprecation note") .withStartTime(Instant.parse("2011-02-01T00:00:00Z")) .withEndTime(Instant.parse("2011-01-01T00:00:00Z")) @@ -73,8 +72,10 @@ public void testCreateNotification_invalidNonNullParameters() { assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification1)); verify(session, never()).persist(invalidNotification1); + } - ______TS("failure: empty title"); + @Test + public void testCreateNotification_invalidNonNullParameters_emptyTitle() { Notification invalidNotification2 = new Notification.NotificationBuilder("") .withStartTime(Instant.parse("2011-01-01T00:00:00Z")) .withEndTime(Instant.parse("2099-01-01T00:00:00Z")) @@ -82,14 +83,16 @@ public void testCreateNotification_invalidNonNullParameters() { .withTargetUser(NotificationTargetUser.GENERAL) .withMessage("

Deprecation happens in three minutes

") .build(); - notificationId = UUID.randomUUID(); + UUID notificationId = UUID.randomUUID(); invalidNotification2.setNotificationId(notificationId); when(session.get(Notification.class, notificationId.toString())).thenReturn(invalidNotification2); assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification2)); verify(session, never()).persist(invalidNotification2); + } - ______TS("failure: empty message"); + @Test + public void testCreateNotification_invalidNonNullParameters_emptyMessage() { Notification invalidNotification3 = new Notification.NotificationBuilder("A deprecation note") .withStartTime(Instant.parse("2011-01-01T00:00:00Z")) .withEndTime(Instant.parse("2099-01-01T00:00:00Z")) @@ -97,7 +100,7 @@ public void testCreateNotification_invalidNonNullParameters() { .withTargetUser(NotificationTargetUser.GENERAL) .withMessage("") .build(); - notificationId = UUID.randomUUID(); + UUID notificationId = UUID.randomUUID(); invalidNotification3.setNotificationId(notificationId); when(session.get(Notification.class, notificationId.toString())).thenReturn(invalidNotification3); From 0a21088bad54fab02c3619e571785f576df72a1b Mon Sep 17 00:00:00 2001 From: hhdqirui <53338059+hhdqirui@users.noreply.github.com> Date: Sat, 11 Feb 2023 19:34:13 +0800 Subject: [PATCH 04/14] Update code for notification logic dependency initilisation --- src/main/java/teammates/sqllogic/core/LogicStarter.java | 3 +++ .../java/teammates/sqllogic/core/NotificationsLogic.java | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/teammates/sqllogic/core/LogicStarter.java b/src/main/java/teammates/sqllogic/core/LogicStarter.java index a29e981cf31..6ae17c4d8a1 100644 --- a/src/main/java/teammates/sqllogic/core/LogicStarter.java +++ b/src/main/java/teammates/sqllogic/core/LogicStarter.java @@ -6,6 +6,7 @@ import teammates.common.util.Logger; import teammates.storage.sqlapi.CoursesDb; import teammates.storage.sqlapi.FeedbackSessionsDb; +import teammates.storage.sqlapi.NotificationsDb; /** * Setup in web.xml to register logic classes at application startup. @@ -20,9 +21,11 @@ public class LogicStarter implements ServletContextListener { public static void initializeDependencies() { CoursesLogic coursesLogic = CoursesLogic.inst(); FeedbackSessionsLogic fsLogic = FeedbackSessionsLogic.inst(); + NotificationsLogic notificationsLogic = NotificationsLogic.inst(); coursesLogic.initLogicDependencies(CoursesDb.inst(), fsLogic); fsLogic.initLogicDependencies(FeedbackSessionsDb.inst(), coursesLogic); + notificationsLogic.initLogicDependencies(NotificationsDb.inst()); log.info("Initialized dependencies between logic classes"); } diff --git a/src/main/java/teammates/sqllogic/core/NotificationsLogic.java b/src/main/java/teammates/sqllogic/core/NotificationsLogic.java index a5a64c99c40..9b7fdeba25d 100644 --- a/src/main/java/teammates/sqllogic/core/NotificationsLogic.java +++ b/src/main/java/teammates/sqllogic/core/NotificationsLogic.java @@ -12,7 +12,7 @@ public final class NotificationsLogic { private static final NotificationsLogic instance = new NotificationsLogic(); - private final NotificationsDb notificationsDb = NotificationsDb.inst(); + private NotificationsDb notificationsDb; private NotificationsLogic() { // prevent initialization @@ -22,8 +22,8 @@ public static NotificationsLogic inst() { return instance; } - void initLogicDependencies() { - // No dependency to other logic class + void initLogicDependencies(NotificationsDb notificationsDb) { + this.notificationsDb = notificationsDb; } /** From eeaad8981b79e5a77be4df6d1c48d0aa46746577 Mon Sep 17 00:00:00 2001 From: hhdqirui <53338059+hhdqirui@users.noreply.github.com> Date: Sat, 11 Feb 2023 19:39:41 +0800 Subject: [PATCH 05/14] Update notification db it --- .../it/storage/sqlapi/NotificationDbIT.java | 33 ------------------- 1 file changed, 33 deletions(-) diff --git a/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java b/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java index 85464d93593..26f2b57f031 100644 --- a/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java +++ b/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java @@ -51,38 +51,5 @@ public void testCreateNotification() throws EntityAlreadyExistsException, Invali EntityAlreadyExistsException ex = assertThrows(EntityAlreadyExistsException.class, () -> notificationsDb.createNotification(identicalNotification)); assertEquals(ex.getMessage(), "Trying to create an entity that exists: " + identicalNotification.toString()); - - ______TS("failure: invalid parameter, start time is before end time"); - Notification invalidNotification1 = new Notification.NotificationBuilder("A deprecation note") - .withStartTime(Instant.parse("2011-02-01T00:00:00Z")) - .withEndTime(Instant.parse("2011-01-01T00:00:00Z")) - .withStyle(NotificationStyle.DANGER) - .withTargetUser(NotificationTargetUser.GENERAL) - .withMessage("

Deprecation happens in three minutes

") - .build(); - - assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification1)); - - ______TS("failure: empty title"); - Notification invalidNotification2 = new Notification.NotificationBuilder("") - .withStartTime(Instant.parse("2011-01-01T00:00:00Z")) - .withEndTime(Instant.parse("2099-01-01T00:00:00Z")) - .withStyle(NotificationStyle.DANGER) - .withTargetUser(NotificationTargetUser.GENERAL) - .withMessage("

Deprecation happens in three minutes

") - .build(); - - assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification2)); - - ______TS("failure: empty message"); - Notification invalidNotification3 = new Notification.NotificationBuilder("A deprecation note") - .withStartTime(Instant.parse("2011-01-01T00:00:00Z")) - .withEndTime(Instant.parse("2099-01-01T00:00:00Z")) - .withStyle(NotificationStyle.DANGER) - .withTargetUser(NotificationTargetUser.GENERAL) - .withMessage("") - .build(); - - assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification3)); } } From dce3922b3109e6cc6768715435b980db20679957 Mon Sep 17 00:00:00 2001 From: hhdqirui <53338059+hhdqirui@users.noreply.github.com> Date: Sat, 11 Feb 2023 23:00:51 +0800 Subject: [PATCH 06/14] Fix lint tests --- src/main/java/teammates/sqllogic/core/NotificationsLogic.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/teammates/sqllogic/core/NotificationsLogic.java b/src/main/java/teammates/sqllogic/core/NotificationsLogic.java index 9b7fdeba25d..a2440327eec 100644 --- a/src/main/java/teammates/sqllogic/core/NotificationsLogic.java +++ b/src/main/java/teammates/sqllogic/core/NotificationsLogic.java @@ -23,7 +23,7 @@ public static NotificationsLogic inst() { } void initLogicDependencies(NotificationsDb notificationsDb) { - this.notificationsDb = notificationsDb; + this.notificationsDb = notificationsDb; } /** From 5371692b46355230fbc034a20bc2c5a2307ede07 Mon Sep 17 00:00:00 2001 From: hhdqirui <53338059+hhdqirui@users.noreply.github.com> Date: Sun, 12 Feb 2023 00:02:54 +0800 Subject: [PATCH 07/14] Update tests --- .../storage/sqlapi/NotificationsDbTest.java | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java b/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java index dc2e1df1d8e..cc48dae388d 100644 --- a/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java +++ b/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java @@ -59,7 +59,7 @@ public void testCreateNotification_success() throws EntityAlreadyExistsException @Test public void testCreateNotification_invalidNonNullParameters_endTimeIsBeforeStartTime() { - Notification invalidNotification1 = new Notification.NotificationBuilder("A deprecation note") + Notification invalidNotification = new Notification.NotificationBuilder("A deprecation note") .withStartTime(Instant.parse("2011-02-01T00:00:00Z")) .withEndTime(Instant.parse("2011-01-01T00:00:00Z")) .withStyle(NotificationStyle.DANGER) @@ -67,16 +67,16 @@ public void testCreateNotification_invalidNonNullParameters_endTimeIsBeforeStart .withMessage("

Deprecation happens in three minutes

") .build(); UUID notificationId = UUID.randomUUID(); - invalidNotification1.setNotificationId(notificationId); - when(session.get(Notification.class, notificationId.toString())).thenReturn(invalidNotification1); + invalidNotification.setNotificationId(notificationId); + when(session.get(Notification.class, notificationId.toString())).thenReturn(invalidNotification); - assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification1)); - verify(session, never()).persist(invalidNotification1); + assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification)); + verify(session, never()).persist(invalidNotification); } @Test public void testCreateNotification_invalidNonNullParameters_emptyTitle() { - Notification invalidNotification2 = new Notification.NotificationBuilder("") + Notification invalidNotification = new Notification.NotificationBuilder("") .withStartTime(Instant.parse("2011-01-01T00:00:00Z")) .withEndTime(Instant.parse("2099-01-01T00:00:00Z")) .withStyle(NotificationStyle.DANGER) @@ -84,16 +84,16 @@ public void testCreateNotification_invalidNonNullParameters_emptyTitle() { .withMessage("

Deprecation happens in three minutes

") .build(); UUID notificationId = UUID.randomUUID(); - invalidNotification2.setNotificationId(notificationId); - when(session.get(Notification.class, notificationId.toString())).thenReturn(invalidNotification2); + invalidNotification.setNotificationId(notificationId); + when(session.get(Notification.class, notificationId.toString())).thenReturn(invalidNotification); - assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification2)); - verify(session, never()).persist(invalidNotification2); + assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification)); + verify(session, never()).persist(invalidNotification); } @Test public void testCreateNotification_invalidNonNullParameters_emptyMessage() { - Notification invalidNotification3 = new Notification.NotificationBuilder("A deprecation note") + Notification invalidNotification = new Notification.NotificationBuilder("A deprecation note") .withStartTime(Instant.parse("2011-01-01T00:00:00Z")) .withEndTime(Instant.parse("2099-01-01T00:00:00Z")) .withStyle(NotificationStyle.DANGER) @@ -101,10 +101,10 @@ public void testCreateNotification_invalidNonNullParameters_emptyMessage() { .withMessage("") .build(); UUID notificationId = UUID.randomUUID(); - invalidNotification3.setNotificationId(notificationId); - when(session.get(Notification.class, notificationId.toString())).thenReturn(invalidNotification3); + invalidNotification.setNotificationId(notificationId); + when(session.get(Notification.class, notificationId.toString())).thenReturn(invalidNotification); - assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification3)); - verify(session, never()).persist(invalidNotification3); + assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification)); + verify(session, never()).persist(invalidNotification); } } From eb14a64877ec85fe6b3ea416ac55f1c76229665e Mon Sep 17 00:00:00 2001 From: hhdqirui <53338059+hhdqirui@users.noreply.github.com> Date: Sun, 12 Feb 2023 17:19:43 +0800 Subject: [PATCH 08/14] Remove irrelavant checks and update tests --- .../it/storage/sqlapi/NotificationDbIT.java | 15 --------------- .../teammates/storage/sqlapi/NotificationsDb.java | 5 ----- .../storage/sqlapi/NotificationsDbTest.java | 13 ------------- 3 files changed, 33 deletions(-) diff --git a/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java b/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java index 26f2b57f031..38a0a73ce21 100644 --- a/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java +++ b/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java @@ -36,20 +36,5 @@ public void testCreateNotification() throws EntityAlreadyExistsException, Invali UUID notificationId = newNotification.getNotificationId(); Notification actualNotification = notificationsDb.getNotification(notificationId); verifyEquals(newNotification, actualNotification); - - ______TS("failure: create notification that already exists, exception thrown"); - Notification identicalNotification = new Notification.NotificationBuilder("A deprecation note") - .withStartTime(Instant.parse("2011-01-01T00:00:00Z")) - .withEndTime(Instant.parse("2099-01-01T00:00:00Z")) - .withStyle(NotificationStyle.DANGER) - .withTargetUser(NotificationTargetUser.GENERAL) - .withMessage("

Deprecation happens in three minutes

") - .build(); - identicalNotification.setNotificationId(notificationId); - - assertNotSame(newNotification, identicalNotification); - EntityAlreadyExistsException ex = assertThrows(EntityAlreadyExistsException.class, - () -> notificationsDb.createNotification(identicalNotification)); - assertEquals(ex.getMessage(), "Trying to create an entity that exists: " + identicalNotification.toString()); } } diff --git a/src/main/java/teammates/storage/sqlapi/NotificationsDb.java b/src/main/java/teammates/storage/sqlapi/NotificationsDb.java index 41818e37fe6..e460b1dd8d3 100644 --- a/src/main/java/teammates/storage/sqlapi/NotificationsDb.java +++ b/src/main/java/teammates/storage/sqlapi/NotificationsDb.java @@ -37,11 +37,6 @@ public Notification createNotification(Notification notification) throw new InvalidParametersException(notification.getInvalidityInfo()); } - if (notification.getNotificationId() != null && getNotification(notification.getNotificationId()) != null) { - throw new EntityAlreadyExistsException(String.format(ERROR_CREATE_ENTITY_ALREADY_EXISTS, - notification.toString())); - } - persist(notification); return notification; } diff --git a/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java b/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java index cc48dae388d..c994f71cc76 100644 --- a/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java +++ b/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java @@ -7,7 +7,6 @@ import static org.mockito.Mockito.when; import java.time.Instant; -import java.util.UUID; import org.hibernate.Session; import org.hibernate.SessionFactory; @@ -48,9 +47,6 @@ public void testCreateNotification_success() throws EntityAlreadyExistsException .withTargetUser(NotificationTargetUser.GENERAL) .withMessage("

Deprecation happens in three minutes

") .build(); - UUID notificationId = UUID.randomUUID(); - newNotification.setNotificationId(notificationId); - when(session.get(Notification.class, notificationId.toString())).thenReturn(null); notificationsDb.createNotification(newNotification); @@ -66,9 +62,6 @@ public void testCreateNotification_invalidNonNullParameters_endTimeIsBeforeStart .withTargetUser(NotificationTargetUser.GENERAL) .withMessage("

Deprecation happens in three minutes

") .build(); - UUID notificationId = UUID.randomUUID(); - invalidNotification.setNotificationId(notificationId); - when(session.get(Notification.class, notificationId.toString())).thenReturn(invalidNotification); assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification)); verify(session, never()).persist(invalidNotification); @@ -83,9 +76,6 @@ public void testCreateNotification_invalidNonNullParameters_emptyTitle() { .withTargetUser(NotificationTargetUser.GENERAL) .withMessage("

Deprecation happens in three minutes

") .build(); - UUID notificationId = UUID.randomUUID(); - invalidNotification.setNotificationId(notificationId); - when(session.get(Notification.class, notificationId.toString())).thenReturn(invalidNotification); assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification)); verify(session, never()).persist(invalidNotification); @@ -100,9 +90,6 @@ public void testCreateNotification_invalidNonNullParameters_emptyMessage() { .withTargetUser(NotificationTargetUser.GENERAL) .withMessage("") .build(); - UUID notificationId = UUID.randomUUID(); - invalidNotification.setNotificationId(notificationId); - when(session.get(Notification.class, notificationId.toString())).thenReturn(invalidNotification); assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification)); verify(session, never()).persist(invalidNotification); From 345ea100ade26dd66ed5bd7a72a12ab21eba40c9 Mon Sep 17 00:00:00 2001 From: hhdqirui <53338059+hhdqirui@users.noreply.github.com> Date: Sun, 12 Feb 2023 18:31:48 +0800 Subject: [PATCH 09/14] Add missing implementation --- .../it/test/BaseTestCaseWithSqlDatabaseAccess.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/it/java/teammates/it/test/BaseTestCaseWithSqlDatabaseAccess.java b/src/it/java/teammates/it/test/BaseTestCaseWithSqlDatabaseAccess.java index c025e2c4e8b..78f6072420f 100644 --- a/src/it/java/teammates/it/test/BaseTestCaseWithSqlDatabaseAccess.java +++ b/src/it/java/teammates/it/test/BaseTestCaseWithSqlDatabaseAccess.java @@ -7,12 +7,14 @@ import org.testng.annotations.BeforeSuite; import org.testng.annotations.Test; +import teammates.common.datatransfer.attributes.NotificationAttributes; import teammates.common.util.HibernateUtil; import teammates.common.util.JsonUtils; import teammates.sqllogic.api.Logic; import teammates.sqllogic.core.LogicStarter; import teammates.storage.sqlentity.BaseEntity; import teammates.storage.sqlentity.Course; +import teammates.storage.sqlentity.Notification; import teammates.test.BaseTestCase; /** @@ -61,6 +63,11 @@ protected void verifyEquals(BaseEntity expected, BaseEntity actual) { Course actualCourse = (Course) actual; equalizeIrrelevantData(expectedCourse, actualCourse); assertEquals(JsonUtils.toJson(expectedCourse), JsonUtils.toJson(actualCourse)); + } else if (expected instanceof Notification) { + Notification expectedNotification = (Notification) expected; + Notification actualNotification = (Notification) actual; + equalizeIrrelevantData(expectedNotification, actualNotification); + assertEquals(JsonUtils.toJson(expectedNotification), JsonUtils.toJson(actualNotification)); } } @@ -86,4 +93,10 @@ private void equalizeIrrelevantData(Course expected, Course actual) { expected.setUpdatedAt(actual.getUpdatedAt()); } + private void equalizeIrrelevantData(Notification expected, Notification actual) { + // Ignore time field as it is stamped at the time of creation in testing + expected.setCreatedAt(actual.getCreatedAt()); + expected.setUpdatedAt(actual.getUpdatedAt()); + } + } From 8abb214392ba0f9eb94257d1335400627654fa25 Mon Sep 17 00:00:00 2001 From: hhdqirui <53338059+hhdqirui@users.noreply.github.com> Date: Sun, 12 Feb 2023 18:37:08 +0800 Subject: [PATCH 10/14] Remove import --- .../teammates/it/test/BaseTestCaseWithSqlDatabaseAccess.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/it/java/teammates/it/test/BaseTestCaseWithSqlDatabaseAccess.java b/src/it/java/teammates/it/test/BaseTestCaseWithSqlDatabaseAccess.java index 78f6072420f..b472d5ce35b 100644 --- a/src/it/java/teammates/it/test/BaseTestCaseWithSqlDatabaseAccess.java +++ b/src/it/java/teammates/it/test/BaseTestCaseWithSqlDatabaseAccess.java @@ -7,7 +7,6 @@ import org.testng.annotations.BeforeSuite; import org.testng.annotations.Test; -import teammates.common.datatransfer.attributes.NotificationAttributes; import teammates.common.util.HibernateUtil; import teammates.common.util.JsonUtils; import teammates.sqllogic.api.Logic; From aa41f2cde1dc8eec144b57fb457c6a4c48d7fd87 Mon Sep 17 00:00:00 2001 From: hhdqirui <53338059+hhdqirui@users.noreply.github.com> Date: Mon, 13 Feb 2023 14:12:36 +0800 Subject: [PATCH 11/14] Update notification builder --- .../it/storage/sqlapi/NotificationDbIT.java | 3 ++- .../teammates/storage/sqlentity/Notification.java | 9 +++++---- .../ui/webapi/CreateNotificationAction.java | 3 ++- .../storage/sqlapi/NotificationsDbTest.java | 12 ++++++++---- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java b/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java index 38a0a73ce21..479045dd96b 100644 --- a/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java +++ b/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java @@ -23,11 +23,12 @@ public class NotificationDbIT extends BaseTestCaseWithSqlDatabaseAccess { @Test public void testCreateNotification() throws EntityAlreadyExistsException, InvalidParametersException { ______TS("success: create notification that does not exists"); - Notification newNotification = new Notification.NotificationBuilder("A deprecation note") + Notification newNotification = new Notification.NotificationBuilder() .withStartTime(Instant.parse("2011-01-01T00:00:00Z")) .withEndTime(Instant.parse("2099-01-01T00:00:00Z")) .withStyle(NotificationStyle.DANGER) .withTargetUser(NotificationTargetUser.GENERAL) + .withTitle("A deprecation note") .withMessage("

Deprecation happens in three minutes

") .build(); diff --git a/src/main/java/teammates/storage/sqlentity/Notification.java b/src/main/java/teammates/storage/sqlentity/Notification.java index 51e0c4a6dbc..19677357960 100644 --- a/src/main/java/teammates/storage/sqlentity/Notification.java +++ b/src/main/java/teammates/storage/sqlentity/Notification.java @@ -248,10 +248,6 @@ public static class NotificationBuilder { private String message; private boolean shown; - public NotificationBuilder(String title) { - this.title = title; - } - public NotificationBuilder withStartTime(Instant startTime) { this.startTime = startTime; return this; @@ -272,6 +268,11 @@ public NotificationBuilder withTargetUser(NotificationTargetUser targetUser) { return this; } + public NotificationBuilder withTitle(String title) { + this.title = title; + return this; + } + public NotificationBuilder withMessage(String message) { this.message = message; return this; diff --git a/src/main/java/teammates/ui/webapi/CreateNotificationAction.java b/src/main/java/teammates/ui/webapi/CreateNotificationAction.java index ad77e1bcee3..5a5e2c370dd 100644 --- a/src/main/java/teammates/ui/webapi/CreateNotificationAction.java +++ b/src/main/java/teammates/ui/webapi/CreateNotificationAction.java @@ -25,11 +25,12 @@ public JsonResult execute() throws InvalidHttpRequestBodyException, InvalidOpera Instant startTime = Instant.ofEpochMilli(notificationRequest.getStartTimestamp()); Instant endTime = Instant.ofEpochMilli(notificationRequest.getEndTimestamp()); - Notification newNotification = new Notification.NotificationBuilder(notificationRequest.getTitle()) + Notification newNotification = new Notification.NotificationBuilder() .withStartTime(startTime) .withEndTime(endTime) .withStyle(notificationRequest.getStyle()) .withTargetUser(notificationRequest.getTargetUser()) + .withTitle(notificationRequest.getTitle()) .withMessage(notificationRequest.getMessage()) .build(); diff --git a/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java b/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java index c994f71cc76..c9a4fbf65f3 100644 --- a/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java +++ b/src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java @@ -40,11 +40,12 @@ public void setUp() { @Test public void testCreateNotification_success() throws EntityAlreadyExistsException, InvalidParametersException { - Notification newNotification = new Notification.NotificationBuilder("A deprecation note") + Notification newNotification = new Notification.NotificationBuilder() .withStartTime(Instant.parse("2011-01-01T00:00:00Z")) .withEndTime(Instant.parse("2099-01-01T00:00:00Z")) .withStyle(NotificationStyle.DANGER) .withTargetUser(NotificationTargetUser.GENERAL) + .withTitle("A deprecation note") .withMessage("

Deprecation happens in three minutes

") .build(); @@ -55,11 +56,12 @@ public void testCreateNotification_success() throws EntityAlreadyExistsException @Test public void testCreateNotification_invalidNonNullParameters_endTimeIsBeforeStartTime() { - Notification invalidNotification = new Notification.NotificationBuilder("A deprecation note") + Notification invalidNotification = new Notification.NotificationBuilder() .withStartTime(Instant.parse("2011-02-01T00:00:00Z")) .withEndTime(Instant.parse("2011-01-01T00:00:00Z")) .withStyle(NotificationStyle.DANGER) .withTargetUser(NotificationTargetUser.GENERAL) + .withTitle("A deprecation note") .withMessage("

Deprecation happens in three minutes

") .build(); @@ -69,11 +71,12 @@ public void testCreateNotification_invalidNonNullParameters_endTimeIsBeforeStart @Test public void testCreateNotification_invalidNonNullParameters_emptyTitle() { - Notification invalidNotification = new Notification.NotificationBuilder("") + Notification invalidNotification = new Notification.NotificationBuilder() .withStartTime(Instant.parse("2011-01-01T00:00:00Z")) .withEndTime(Instant.parse("2099-01-01T00:00:00Z")) .withStyle(NotificationStyle.DANGER) .withTargetUser(NotificationTargetUser.GENERAL) + .withTitle("") .withMessage("

Deprecation happens in three minutes

") .build(); @@ -83,11 +86,12 @@ public void testCreateNotification_invalidNonNullParameters_emptyTitle() { @Test public void testCreateNotification_invalidNonNullParameters_emptyMessage() { - Notification invalidNotification = new Notification.NotificationBuilder("A deprecation note") + Notification invalidNotification = new Notification.NotificationBuilder() .withStartTime(Instant.parse("2011-01-01T00:00:00Z")) .withEndTime(Instant.parse("2099-01-01T00:00:00Z")) .withStyle(NotificationStyle.DANGER) .withTargetUser(NotificationTargetUser.GENERAL) + .withTitle("A deprecation note") .withMessage("") .build(); From 0865c01f773eec121ccbfa4f4c71ec304a1e2b48 Mon Sep 17 00:00:00 2001 From: hhdqirui <53338059+hhdqirui@users.noreply.github.com> Date: Mon, 13 Feb 2023 14:40:11 +0800 Subject: [PATCH 12/14] Update notification builder --- src/main/java/teammates/storage/sqlentity/Notification.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/teammates/storage/sqlentity/Notification.java b/src/main/java/teammates/storage/sqlentity/Notification.java index 19677357960..4c3fc729f96 100644 --- a/src/main/java/teammates/storage/sqlentity/Notification.java +++ b/src/main/java/teammates/storage/sqlentity/Notification.java @@ -240,6 +240,7 @@ public boolean equals(Object other) { */ public static class NotificationBuilder { + private UUID notificationId; private Instant startTime; private Instant endTime; private NotificationStyle style; @@ -248,6 +249,11 @@ public static class NotificationBuilder { private String message; private boolean shown; + public NotificationBuilder withNotificationId(UUID notificationId) { + this.notificationId = notificationId; + return this; + } + public NotificationBuilder withStartTime(Instant startTime) { this.startTime = startTime; return this; From 22a4bd6e493da59baa4261692afc43e4875fc4d1 Mon Sep 17 00:00:00 2001 From: hhdqirui <53338059+hhdqirui@users.noreply.github.com> Date: Mon, 13 Feb 2023 14:52:24 +0800 Subject: [PATCH 13/14] Update notification builder --- src/main/java/teammates/storage/sqlentity/Notification.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/teammates/storage/sqlentity/Notification.java b/src/main/java/teammates/storage/sqlentity/Notification.java index 4c3fc729f96..3a621506d9d 100644 --- a/src/main/java/teammates/storage/sqlentity/Notification.java +++ b/src/main/java/teammates/storage/sqlentity/Notification.java @@ -72,6 +72,7 @@ public class Notification extends BaseEntity { * Instantiates a new notification from {@code NotificationBuilder}. */ public Notification(NotificationBuilder builder) { + this.setNotificationId(builder.notificationId); this.setStartTime(builder.startTime); this.setEndTime(builder.endTime); this.setStyle(builder.style); From b17db61925950c6b0948b79846c1f855ac1e0ceb Mon Sep 17 00:00:00 2001 From: hhdqirui <53338059+hhdqirui@users.noreply.github.com> Date: Mon, 13 Feb 2023 16:10:58 +0800 Subject: [PATCH 14/14] Fix typo --- src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java b/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java index 479045dd96b..6dc03d501bf 100644 --- a/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java +++ b/src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java @@ -22,7 +22,7 @@ public class NotificationDbIT extends BaseTestCaseWithSqlDatabaseAccess { @Test public void testCreateNotification() throws EntityAlreadyExistsException, InvalidParametersException { - ______TS("success: create notification that does not exists"); + ______TS("success: create notification that does not exist"); Notification newNotification = new Notification.NotificationBuilder() .withStartTime(Instant.parse("2011-01-01T00:00:00Z")) .withEndTime(Instant.parse("2099-01-01T00:00:00Z"))