Skip to content

Commit

Permalink
[#12048] Create SQL logic for UpdateNotificationAction and add releva…
Browse files Browse the repository at this point in the history
…nt tests for v9 migration (#12085)
  • Loading branch information
hhdqirui authored Feb 25, 2023
1 parent 35de5ed commit 2611338
Show file tree
Hide file tree
Showing 22 changed files with 307 additions and 39 deletions.
62 changes: 62 additions & 0 deletions src/it/java/teammates/it/sqllogic/core/NotificationsLogicIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package teammates.it.sqllogic.core;

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;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.it.test.BaseTestCaseWithSqlDatabaseAccess;
import teammates.sqllogic.core.NotificationsLogic;
import teammates.storage.sqlentity.Notification;

/**
* SUT: {@link NotificationsLogic}.
*/
public class NotificationsLogicIT extends BaseTestCaseWithSqlDatabaseAccess {

private NotificationsLogic notificationsLogic = NotificationsLogic.inst();

@Test
public void testUpdateNotification()
throws EntityAlreadyExistsException, InvalidParametersException, EntityDoesNotExistException {
Instant newStartTime = Instant.parse("2012-01-01T00:00:00Z");
Instant newEndTime = Instant.parse("2098-01-01T00:00:00Z");
NotificationStyle newStyle = NotificationStyle.DARK;
NotificationTargetUser newTargetUser = NotificationTargetUser.INSTRUCTOR;
String newTitle = "An updated deprecation note";
String newMessage = "<p>Deprecation happens in three seconds</p>";

______TS("success: update notification that already exists");
Notification notification = new Notification(Instant.parse("2011-01-01T00:00:00Z"),
Instant.parse("2099-01-01T00:00:00Z"), NotificationStyle.DANGER, NotificationTargetUser.GENERAL,
"A deprecation note", "<p>Deprecation happens in three minutes</p>");
notificationsLogic.createNotification(notification);

UUID notificationId = notification.getNotificationId();
Notification expectedNotification = notificationsLogic.updateNotification(notificationId, newStartTime, newEndTime,
newStyle, newTargetUser, newTitle, newMessage);

assertEquals(notificationId, expectedNotification.getNotificationId());
assertEquals(newStartTime, expectedNotification.getStartTime());
assertEquals(newEndTime, expectedNotification.getEndTime());
assertEquals(newStyle, expectedNotification.getStyle());
assertEquals(newTargetUser, expectedNotification.getTargetUser());
assertEquals(newTitle, expectedNotification.getTitle());
assertEquals(newMessage, expectedNotification.getMessage());

Notification actualNotification = notificationsLogic.getNotification(notificationId);
verifyEquals(expectedNotification, actualNotification);

______TS("failure: update notification that does not exist");
UUID nonExistentId = generateDifferentUuid(notificationId);

assertThrows(EntityDoesNotExistException.class, () -> notificationsLogic.updateNotification(nonExistentId,
newStartTime, newEndTime, newStyle, newTargetUser, newTitle, newMessage));
}

}
4 changes: 4 additions & 0 deletions src/it/java/teammates/it/sqllogic/core/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Contains test cases for {@link teammates.storage.sqlapi} package.
*/
package teammates.it.sqllogic.core;
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ public void testGetNotification() throws EntityAlreadyExistsException, InvalidPa
Notification nonExistentNotification = notificationsDb.getNotification(nonExistentId);
assertNull(nonExistentNotification);
}

}
2 changes: 1 addition & 1 deletion src/it/java/teammates/it/storage/sqlapi/package-info.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
* Contains test cases for {@link teammates.storage.search} package.
* Contains test cases for {@link teammates.storage.sqlapi} package.
*/
package teammates.it.storage.sqlapi;
1 change: 1 addition & 0 deletions src/it/resources/testng-it.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<packages>
<package name="teammates.it" />
<package name="teammates.it.test" />
<package name="teammates.it.sqllogic.core" />
<package name="teammates.it.storage.sqlapi" />
</packages>
</test>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/teammates/common/util/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public final class Const {

public static final int SEARCH_QUERY_SIZE_LIMIT = 50;

public static final String ERROR_CREATE_ENTITY_ALREADY_EXISTS = "Trying to create an entity that exists: %s";
public static final String ERROR_UPDATE_NON_EXISTENT = "Trying to update non-existent Entity: ";

// These constants are used as variable values to mean that the variable is in a 'special' state.

public static final int INT_UNINITIALIZED = -9999;
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/teammates/sqllogic/api/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import java.util.List;
import java.util.UUID;

import teammates.common.datatransfer.NotificationStyle;
import teammates.common.datatransfer.NotificationTargetUser;
import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.sqllogic.core.CoursesLogic;
import teammates.sqllogic.core.NotificationsLogic;
Expand Down Expand Up @@ -104,4 +107,20 @@ public Notification createNotification(Notification notification) throws
public Notification getNotification(UUID notificationId) {
return notificationsLogic.getNotification(notificationId);
}

/**
* Updates a notification.
*
* <p>Preconditions:</p>
* * All parameters are non-null.
* @return updated notification
* @throws InvalidParametersException if the notification is not valid
* @throws EntityDoesNotExistException if the notification does not exist in the database
*/
public Notification updateNotification(UUID notificationId, Instant startTime, Instant endTime,
NotificationStyle style, NotificationTargetUser targetUser, String title,
String message) throws
InvalidParametersException, EntityDoesNotExistException {
return notificationsLogic.updateNotification(notificationId, startTime, endTime, style, targetUser, title, message);
}
}
42 changes: 41 additions & 1 deletion src/main/java/teammates/sqllogic/core/NotificationsLogic.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package teammates.sqllogic.core;

import static teammates.common.util.Const.ERROR_UPDATE_NON_EXISTENT;

import java.time.Instant;
import java.util.UUID;

import teammates.common.datatransfer.NotificationStyle;
import teammates.common.datatransfer.NotificationTargetUser;
import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.storage.sqlapi.NotificationsDb;
import teammates.storage.sqlentity.Notification;
Expand All @@ -24,7 +30,10 @@ public static NotificationsLogic inst() {
return instance;
}

void initLogicDependencies(NotificationsDb notificationsDb) {
/**
* Initialise dependencies for {@code NotificationLogic} object.
*/
public void initLogicDependencies(NotificationsDb notificationsDb) {
this.notificationsDb = notificationsDb;
}

Expand All @@ -50,4 +59,35 @@ public Notification getNotification(UUID notificationId) {

return notificationsDb.getNotification(notificationId);
}

/**
* Updates/Creates the notification using {@link Notification}.
*
* @return updated notification
* @throws InvalidParametersException if attributes to update are not valid
* @throws EntityDoesNotExistException if notification cannot be found with given Id
*/
public Notification updateNotification(UUID notificationId, Instant startTime, Instant endTime,
NotificationStyle style, NotificationTargetUser targetUser, String title,
String message)
throws InvalidParametersException, EntityDoesNotExistException {
Notification notification = notificationsDb.getNotification(notificationId);

if (notification == null) {
throw new EntityDoesNotExistException(ERROR_UPDATE_NON_EXISTENT + Notification.class);
}

notification.setStartTime(startTime);
notification.setEndTime(endTime);
notification.setStyle(style);
notification.setTargetUser(targetUser);
notification.setTitle(title);
notification.setMessage(message);

if (!notification.isValid()) {
throw new InvalidParametersException(notification.getInvalidityInfo());
}

return notification;
}
}
2 changes: 2 additions & 0 deletions src/main/java/teammates/storage/sqlapi/AccountRequestDb.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package teammates.storage.sqlapi;

import static teammates.common.util.Const.ERROR_CREATE_ENTITY_ALREADY_EXISTS;

import java.time.Instant;
import java.util.List;

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/teammates/storage/sqlapi/AccountsDb.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package teammates.storage.sqlapi;

import static teammates.common.util.Const.ERROR_CREATE_ENTITY_ALREADY_EXISTS;
import static teammates.common.util.Const.ERROR_UPDATE_NON_EXISTENT;

import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/teammates/storage/sqlapi/CoursesDb.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package teammates.storage.sqlapi;

import static teammates.common.util.Const.ERROR_CREATE_ENTITY_ALREADY_EXISTS;
import static teammates.common.util.Const.ERROR_UPDATE_NON_EXISTENT;

import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package teammates.storage.sqlapi;

import static teammates.common.util.Const.ERROR_CREATE_ENTITY_ALREADY_EXISTS;
import static teammates.common.util.Const.ERROR_UPDATE_NON_EXISTENT;

import org.hibernate.Session;

import teammates.common.exception.EntityAlreadyExistsException;
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/teammates/storage/sqlapi/EntitiesDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
*/
class EntitiesDb<E extends BaseEntity> {

static final String ERROR_CREATE_ENTITY_ALREADY_EXISTS = "Trying to create an entity that exists: %s";
static final String ERROR_UPDATE_NON_EXISTENT = "Trying to update non-existent Entity: ";

static final Logger log = Logger.getLogger();

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package teammates.storage.sqlapi;

import static teammates.common.util.Const.ERROR_UPDATE_NON_EXISTENT;

import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.common.util.HibernateUtil;
Expand Down
19 changes: 0 additions & 19 deletions src/main/java/teammates/storage/sqlapi/NotificationsDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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;
Expand Down Expand Up @@ -49,24 +48,6 @@ public Notification getNotification(UUID notificationId) {
return HibernateUtil.get(Notification.class, notificationId);
}

/**
* Updates a notification with {@link Notification}.
*/
public Notification updateNotification(Notification notification)
throws InvalidParametersException, EntityDoesNotExistException {
assert notification != null;

if (!notification.isValid()) {
throw new InvalidParametersException(notification.getInvalidityInfo());
}

if (getNotification(notification.getNotificationId()) == null) {
throw new EntityDoesNotExistException(ERROR_UPDATE_NON_EXISTENT);
}

return merge(notification);
}

/**
* Deletes a notification.
*
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/teammates/storage/sqlapi/UsersDb.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package teammates.storage.sqlapi;

import static teammates.common.util.Const.ERROR_CREATE_ENTITY_ALREADY_EXISTS;
import static teammates.common.util.Const.ERROR_UPDATE_NON_EXISTENT;

import java.time.Instant;

import org.hibernate.Session;
Expand Down
21 changes: 8 additions & 13 deletions src/main/java/teammates/ui/webapi/UpdateNotificationAction.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package teammates.ui.webapi;

import java.time.Instant;
import java.util.UUID;

import teammates.common.datatransfer.attributes.NotificationAttributes;
import teammates.common.datatransfer.attributes.NotificationAttributes.UpdateOptions;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.common.util.Const;
import teammates.storage.sqlentity.Notification;
import teammates.ui.output.NotificationData;
import teammates.ui.request.InvalidHttpRequestBodyException;
import teammates.ui.request.NotificationUpdateRequest;
Expand All @@ -18,23 +18,18 @@ public class UpdateNotificationAction extends AdminOnlyAction {

@Override
public JsonResult execute() throws InvalidHttpRequestBodyException {
String notificationId = getNonNullRequestParamValue(Const.ParamsNames.NOTIFICATION_ID);
UUID notificationId = getUuidRequestParamValue(Const.ParamsNames.NOTIFICATION_ID);
NotificationUpdateRequest notificationRequest = getAndValidateRequestBody(NotificationUpdateRequest.class);

Instant startTime = Instant.ofEpochMilli(notificationRequest.getStartTimestamp());
Instant endTime = Instant.ofEpochMilli(notificationRequest.getEndTimestamp());

UpdateOptions newNotification = NotificationAttributes.updateOptionsBuilder(notificationId)
.withStartTime(startTime)
.withEndTime(endTime)
.withStyle(notificationRequest.getStyle())
.withTargetUser(notificationRequest.getTargetUser())
.withTitle(notificationRequest.getTitle())
.withMessage(notificationRequest.getMessage())
.build();

try {
return new JsonResult(new NotificationData(logic.updateNotification(newNotification)));
Notification updateNotification = sqlLogic.updateNotification(notificationId, startTime, endTime,
notificationRequest.getStyle(), notificationRequest.getTargetUser(), notificationRequest.getTitle(),
notificationRequest.getMessage());

return new JsonResult(new NotificationData(updateNotification));
} catch (InvalidParametersException e) {
throw new InvalidHttpRequestBodyException(e);
} catch (EntityDoesNotExistException ednee) {
Expand Down
Loading

0 comments on commit 2611338

Please sign in to comment.