-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into branch-migrate-tests-week4
- Loading branch information
Showing
7 changed files
with
509 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
192 changes: 192 additions & 0 deletions
192
src/test/java/teammates/sqlui/webapi/CreateNotificationActionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/test/java/teammates/sqlui/webapi/GetNotificationActionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.