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

[#12048] Migrate tests for GetNotificationActionTest #13210

Merged
merged 8 commits into from
Feb 28, 2025
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package teammates.sqlui.webapi;

import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.when;

import java.util.UUID;

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

import teammates.common.util.Const;
import teammates.storage.sqlentity.Notification;
import teammates.ui.output.NotificationData;
import teammates.ui.webapi.EntityNotFoundException;
import teammates.ui.webapi.GetNotificationAction;
import teammates.ui.webapi.InvalidHttpParameterException;
import teammates.ui.webapi.JsonResult;

/**
* SUT: {@link GetNotificationAction}.
*/
public class GetNotificationActionTest extends BaseActionTest<GetNotificationAction> {

@Override
String getActionUri() {
return Const.ResourceURIs.NOTIFICATION;
}

@Override
String getRequestMethod() {
return GET;
}

@BeforeMethod
public void baseClassSetup() {
loginAsAdmin();
}

@Test
protected void testExecute_withValidNotificationId_shouldReturnData() {
Notification testNotification = getTypicalNotificationWithId();
NotificationData expected = new NotificationData(testNotification);

String[] requestParams = new String[] {
Const.ParamsNames.NOTIFICATION_ID, String.valueOf(testNotification.getId()),
};

when(mockLogic.getNotification(testNotification.getId())).thenReturn(testNotification);

GetNotificationAction action = getAction(requestParams);
JsonResult jsonResult = getJsonResult(action);

NotificationData output = (NotificationData) jsonResult.getOutput();
verifyNotificationEquals(expected, output);

reset(mockLogic);
}

@Test
protected void testExecute_nonExistentNotification_shouldThrowError() {
GetNotificationAction action = getAction(Const.ParamsNames.NOTIFICATION_ID, UUID.randomUUID().toString());
EntityNotFoundException enfe = assertThrows(EntityNotFoundException.class, action::execute);
Comment on lines +61 to +62
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor style suggestion: Consider adding a blank line between the action and the assertions to improve readability.


assertEquals("Notification does not exist.", enfe.getMessage());
}

@Test
protected void testExecute_notificationIdIsNull_shouldThrowError() {
GetNotificationAction action = getAction(Const.ParamsNames.NOTIFICATION_ID, null, new String[] {});
InvalidHttpParameterException ihpe = assertThrows(InvalidHttpParameterException.class, action::execute);
Comment on lines +69 to +70
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above


assertEquals("The [notificationid] HTTP parameter is null.", ihpe.getMessage());
}

private void verifyNotificationEquals(NotificationData expected, NotificationData actual) {
assertEquals(expected.getNotificationId(), actual.getNotificationId());
assertEquals(expected.getStyle(), actual.getStyle());
assertEquals(expected.getTargetUser(), actual.getTargetUser());
assertEquals(expected.getTitle(), actual.getTitle());
assertEquals(expected.getMessage(), actual.getMessage());
assertEquals(expected.getStartTimestamp(), actual.getStartTimestamp());
assertEquals(expected.getEndTimestamp(), actual.getEndTimestamp());
}
}
Loading