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

Create SQL logic for GetNotificationAction and add relevant tests for v9 migration #12080

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
19 changes: 19 additions & 0 deletions src/it/java/teammates/it/storage/sqlapi/NotificationDbIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,23 @@ public void testCreateNotification() throws EntityAlreadyExistsException, Invali
Notification actualNotification = notificationsDb.getNotification(notificationId);
verifyEquals(newNotification, actualNotification);
}

@Test
public void testGetNotification() throws EntityAlreadyExistsException, InvalidParametersException {
______TS("success: get a notification that already exists");
Notification newNotification = 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>");

notificationsDb.createNotification(newNotification);

UUID notificationId = newNotification.getNotificationId();
Notification actualNotification = notificationsDb.getNotification(notificationId);
verifyEquals(newNotification, actualNotification);

______TS("success: get a notification that does not exist");
UUID nonExistentId = generateDifferentUuid(notificationId);
Notification nonExistentNotification = notificationsDb.getNotification(nonExistentId);
assertNull(nonExistentNotification);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package teammates.it.test;

import java.util.UUID;

import org.testcontainers.containers.PostgreSQLContainer;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
Expand Down Expand Up @@ -98,4 +100,14 @@ private void equalizeIrrelevantData(Notification expected, Notification actual)
expected.setUpdatedAt(actual.getUpdatedAt());
}

/**
* Generates a UUID that is different from the given {@code uuid}.
*/
protected UUID generateDifferentUuid(UUID uuid) {
UUID ret = UUID.randomUUID();
while (ret.equals(uuid)) {
ret = UUID.randomUUID();
}
return ret;
}
}
12 changes: 12 additions & 0 deletions src/main/java/teammates/sqllogic/api/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.InvalidParametersException;
Expand Down Expand Up @@ -92,4 +93,15 @@ public Notification createNotification(Notification notification) throws
return notificationsLogic.createNotification(notification);
}

/**
* Gets a notification by ID.
*
* <p>Preconditions:</p>
* * All parameters are non-null.
*
* @return Null if no match found.
*/
public Notification getNotification(UUID notificationId) {
return notificationsLogic.getNotification(notificationId);
}
}
13 changes: 13 additions & 0 deletions src/main/java/teammates/sqllogic/core/NotificationsLogic.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package teammates.sqllogic.core;

import java.util.UUID;

import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.InvalidParametersException;
import teammates.storage.sqlapi.NotificationsDb;
Expand Down Expand Up @@ -37,4 +39,15 @@ public Notification createNotification(Notification notification)
throws InvalidParametersException, EntityAlreadyExistsException {
return notificationsDb.createNotification(notification);
}

/**
* Gets notification associated with the {@code notificationId}.
*
* @return null if no match found.
*/
public Notification getNotification(UUID notificationId) {
assert notificationId != null;

return notificationsDb.getNotification(notificationId);
}
}
14 changes: 14 additions & 0 deletions src/main/java/teammates/ui/webapi/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.Type;
import java.util.Optional;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

Expand Down Expand Up @@ -200,6 +201,19 @@ long getLongRequestParamValue(String paramName) {
}
}

/**
* Returns the first value for the specified parameter expected to be present in the HTTP request as UUID.
*/
UUID getUuidRequestParamValue(String paramName) {
String value = getNonNullRequestParamValue(paramName);
try {
return UUID.fromString(value);
} catch (IllegalArgumentException e) {
throw new InvalidHttpParameterException(
"Expected UUID value for " + paramName + " parameter, but found: [" + value + "]", e);
}
}

/**
* Returns the request body payload.
*/
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/teammates/ui/webapi/GetNotificationAction.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package teammates.ui.webapi;

import teammates.common.datatransfer.attributes.NotificationAttributes;
import java.util.UUID;

import teammates.common.util.Const;
import teammates.storage.sqlentity.Notification;
import teammates.ui.output.NotificationData;
import teammates.ui.request.InvalidHttpRequestBodyException;

Expand All @@ -12,9 +14,9 @@ public class GetNotificationAction extends AdminOnlyAction {

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

NotificationAttributes notification = logic.getNotification(notificationId);
Notification notification = sqlLogic.getNotification(notificationId);

if (notification == null) {
throw new EntityNotFoundException("Notification does not exist.");
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/teammates/storage/sqlapi/NotificationsDbTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.mockito.Mockito.when;

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

import org.hibernate.Session;
import org.hibernate.SessionFactory;
Expand Down Expand Up @@ -79,4 +80,36 @@ public void testCreateNotification_emptyMessage_throwsInvalidParametersException
assertThrows(InvalidParametersException.class, () -> notificationsDb.createNotification(invalidNotification));
verify(session, never()).persist(invalidNotification);
}

@Test
public void testGetNotification_success() throws EntityAlreadyExistsException, InvalidParametersException {
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>");
notification.setNotificationId(UUID.randomUUID());
notificationsDb.createNotification(notification);
verify(session, times(1)).persist(notification);

when(session.get(Notification.class, notification.getNotificationId())).thenReturn(notification);
Notification actualNotification = notificationsDb.getNotification(notification.getNotificationId());

assertEquals(notification, actualNotification);
}

@Test
public void testGetNotification_entityDoesNotExist() throws EntityAlreadyExistsException, InvalidParametersException {
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>");
notification.setNotificationId(UUID.randomUUID());
notificationsDb.createNotification(notification);
verify(session, times(1)).persist(notification);

UUID nonExistentId = UUID.fromString("00000000-0000-1000-0000-000000000000");

when(session.get(Notification.class, nonExistentId)).thenReturn(null);
Notification actualNotification = notificationsDb.getNotification(nonExistentId);

assertNull(actualNotification);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package teammates.ui.webapi;

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

import teammates.common.datatransfer.attributes.NotificationAttributes;
Expand All @@ -10,6 +11,7 @@
/**
* SUT: {@link GetNotificationAction}.
*/
@Ignore
public class GetNotificationActionTest extends BaseActionTest<GetNotificationAction> {

@Override
Expand Down