Skip to content

Commit

Permalink
Merge branch 'master' into branch-migrate-tests-week4
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonqiu212 authored Mar 2, 2025
2 parents e8d4ad3 + efd2eaf commit b11db4f
Show file tree
Hide file tree
Showing 7 changed files with 509 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/teammates/ui/webapi/GetTimeZonesAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* Action: get supported time zones.
*/
class GetTimeZonesAction extends Action {
public class GetTimeZonesAction extends Action {

@Override
AuthType getMinAuthLevel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* Gets usage statistics for a specified time period.
*/
class GetUsageStatisticsAction extends Action {
public class GetUsageStatisticsAction extends Action {

private static final Duration MAX_SEARCH_WINDOW = Duration.ofDays(184L); // covering six whole months

Expand Down
192 changes: 192 additions & 0 deletions src/test/java/teammates/sqlui/webapi/CreateNotificationActionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package teammates.sqlui.webapi;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.when;

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.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.NotificationCreateRequest;
import teammates.ui.webapi.CreateNotificationAction;

/**
* SUT: {@link CreateNotificationAction}.
*/
public class CreateNotificationActionTest extends BaseActionTest<CreateNotificationAction> {
private static final String GOOGLE_ID = "user-googleId";
private static final String INVALID_TITLE = "";
NotificationCreateRequest testReq;
private Notification testNotification;

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

@Override
String getRequestMethod() {
return POST;
}

@BeforeMethod
void setUp() {
loginAsAdmin();
testNotification = getTypicalNotificationWithId();
}

@Test
void testExecute_addNotification_success() throws Exception {
long startTime = testNotification.getStartTime().toEpochMilli();
long endTime = testNotification.getEndTime().toEpochMilli();
NotificationStyle style = testNotification.getStyle();
NotificationTargetUser targetUser = testNotification.getTargetUser();
String title = testNotification.getTitle();
String message = testNotification.getMessage();

when(mockLogic.createNotification(isA(Notification.class))).thenAnswer(invocation -> invocation.getArgument(0));

NotificationCreateRequest req = getTypicalCreateRequest();
CreateNotificationAction action = getAction(req);
NotificationData res = (NotificationData) action.execute().getOutput();

when(mockLogic.getNotification(UUID.fromString(res.getNotificationId()))).thenReturn(testNotification);

Notification createdNotification = mockLogic.getNotification(UUID.fromString(res.getNotificationId()));

// check that notification returned has same properties as notification created
assertEquals(createdNotification.getStartTime().toEpochMilli(), res.getStartTimestamp());
assertEquals(createdNotification.getEndTime().toEpochMilli(), res.getEndTimestamp());
assertEquals(createdNotification.getStyle(), res.getStyle());
assertEquals(createdNotification.getTargetUser(), res.getTargetUser());
assertEquals(createdNotification.getTitle(), res.getTitle());
assertEquals(createdNotification.getMessage(), res.getMessage());

// check DB correctly processed request
assertEquals(startTime, createdNotification.getStartTime().toEpochMilli());
assertEquals(endTime, createdNotification.getEndTime().toEpochMilli());
assertEquals(style, createdNotification.getStyle());
assertEquals(targetUser, createdNotification.getTargetUser());
assertEquals(title, createdNotification.getTitle());
assertEquals(message, createdNotification.getMessage());
}

@Test
void testSpecificAccessControl_admin_canAccess() {
verifyCanAccess();
}

@Test
void testSpecificAccessControl_instructor_cannotAccess() {
logoutUser();
loginAsInstructor(GOOGLE_ID);
verifyCannotAccess();
}

@Test
void testSpecificAccessControl_student_cannotAccess() {
logoutUser();
loginAsStudent(GOOGLE_ID);
verifyCannotAccess();
}

@Test
void testSpecificAccessControl_loggedOut_cannotAccess() {
logoutUser();
verifyCannotAccess();
}

@Test
void testExecute_invalidStyle_throwsInvalidHttpParameterException() {
testReq = getTypicalCreateRequest();
testReq.setStyle(null);

InvalidHttpRequestBodyException ex = verifyHttpRequestBodyFailure(testReq);

assertEquals("Notification style cannot be null", ex.getMessage());
}

@Test
void testExecute_invalidTargetUser_throwsInvalidHttpParameterException() {
testReq = getTypicalCreateRequest();
testReq.setTargetUser(null);

InvalidHttpRequestBodyException ex = verifyHttpRequestBodyFailure(testReq);

assertEquals("Notification target user cannot be null", ex.getMessage());
}

@Test
void testExecute_invalidTitle_throwsInvalidHttpParameterException() {
testReq = getTypicalCreateRequest();
testReq.setTitle(null);

InvalidHttpRequestBodyException ex = verifyHttpRequestBodyFailure(testReq);

assertEquals("Notification title cannot be null", ex.getMessage());
}

@Test
void testExecute_invalidMessage_throwsInvalidHttpParameterException() {
testReq = getTypicalCreateRequest();
testReq.setMessage(null);

InvalidHttpRequestBodyException ex = verifyHttpRequestBodyFailure(testReq);

assertEquals("Notification message cannot be null", ex.getMessage());
}

@Test
void testExecute_negativeStartTimestamp_throwsInvalidHttpParameterException() {
testReq = getTypicalCreateRequest();
testReq.setStartTimestamp(-1);

InvalidHttpRequestBodyException ex = verifyHttpRequestBodyFailure(testReq);

assertEquals("Start timestamp should be greater than zero", ex.getMessage());
}

@Test
void testExecute_negativeEndTimestamp_throwsInvalidHttpParameterException() {
testReq = getTypicalCreateRequest();
testReq.setEndTimestamp(-1);

InvalidHttpRequestBodyException ex = verifyHttpRequestBodyFailure(testReq);

assertEquals("End timestamp should be greater than zero", ex.getMessage());
}

@Test
void testExecute_invalidParameter_throwsInvalidHttpParameterException() throws Exception {
testReq = getTypicalCreateRequest();
testReq.setTitle(INVALID_TITLE);

when(mockLogic.createNotification(any())).thenThrow(new InvalidParametersException("Invalid title"));
InvalidHttpRequestBodyException ex = verifyHttpRequestBodyFailure(testReq);

assertEquals("Invalid title", ex.getMessage());
}

private NotificationCreateRequest getTypicalCreateRequest() {
NotificationCreateRequest req = new NotificationCreateRequest();

req.setStartTimestamp(testNotification.getStartTime().toEpochMilli());
req.setEndTimestamp(testNotification.getEndTime().toEpochMilli());
req.setStyle(testNotification.getStyle());
req.setTargetUser(testNotification.getTargetUser());
req.setTitle(testNotification.getTitle());
req.setMessage(testNotification.getMessage());

return req;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.UUID;

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

import teammates.common.datatransfer.InstructorPrivileges;
Expand All @@ -21,11 +22,10 @@
*/
public class DeleteFeedbackQuestionActionTest extends BaseActionTest<DeleteFeedbackQuestionAction> {

private final Instructor typicalInstructor = getTypicalInstructor();
private final Course typicalCourse = typicalInstructor.getCourse();
private final FeedbackSession typicalFeedbackSession = getTypicalFeedbackSessionForCourse(typicalCourse);
private final FeedbackQuestion typicalFeedbackQuestion =
getTypicalFeedbackQuestionForSession(typicalFeedbackSession);
private Instructor typicalInstructor;
private Course typicalCourse;
private FeedbackSession typicalFeedbackSession;
private FeedbackQuestion typicalFeedbackQuestion;

@Override
protected String getActionUri() {
Expand All @@ -37,6 +37,14 @@ protected String getRequestMethod() {
return DELETE;
}

@BeforeMethod
void setUp() {
typicalInstructor = getTypicalInstructor();
typicalCourse = typicalInstructor.getCourse();
typicalFeedbackSession = getTypicalFeedbackSessionForCourse(typicalCourse);
typicalFeedbackQuestion = getTypicalFeedbackQuestionForSession(typicalFeedbackSession);
}

@Test
void testExecute_feedbackQuestionExists_success() {
when(mockLogic.getFeedbackQuestion(typicalFeedbackQuestion.getId())).thenReturn(typicalFeedbackQuestion);
Expand Down
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);

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);

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

0 comments on commit b11db4f

Please sign in to comment.