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

[#12779] Fix notifications update logic #12912

Closed
wants to merge 15 commits into from
45 changes: 45 additions & 0 deletions src/it/java/teammates/it/sqllogic/core/AccountRequestsLogicIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.common.util.HibernateUtil;
import teammates.it.test.BaseTestCaseWithSqlDatabaseAccess;
import teammates.sqllogic.core.AccountRequestsLogic;
import teammates.storage.sqlapi.AccountRequestsDb;
Expand Down Expand Up @@ -55,4 +56,48 @@ public void testResetAccountRequest()
assertThrows(EntityDoesNotExistException.class,
() -> accountRequestsLogic.resetAccountRequest(name, institute));
}

@Test
public void testCreateAccountRequest() throws EntityAlreadyExistsException, InvalidParametersException {

______TS("success: create account request");

AccountRequest accountRequest = new AccountRequest("new@email.com", "name", "institute");

accountRequestsLogic.createAccountRequest(accountRequest);

AccountRequest accountRequestFromDb = accountRequestsLogic.getAccountRequest(accountRequest.getId());
assertEquals(accountRequest.getEmail(), accountRequestFromDb.getEmail());
assertEquals(accountRequest.getName(), accountRequestFromDb.getName());
assertEquals(accountRequest.getInstitute(), accountRequestFromDb.getInstitute());

______TS("failure: invalid parameters");

AccountRequest accountRequestWithInvalidEmail = new AccountRequest("invalid email", "name", "institute");

assertThrows(InvalidParametersException.class,
() -> accountRequestsLogic.createAccountRequest(accountRequestWithInvalidEmail));
assertNull(accountRequestsLogic.getAccountRequest(accountRequestWithInvalidEmail.getId()));
}

@Test
public void testUpdateAccountRequest() throws EntityAlreadyExistsException, InvalidParametersException {

______TS("failure: invalid parameters, original unchanged");

String originalEmail = "test@gmail.com";
AccountRequest accountRequest = new AccountRequest(originalEmail, "name", "institute");
accountRequestsLogic.createAccountRequest(accountRequest);

AccountRequest accountRequestWithInvalidEmail = new AccountRequest("invalid email", "name", "institute");

assertThrows(InvalidParametersException.class,
() -> accountRequestsLogic.updateAccountRequest(accountRequestWithInvalidEmail));

HibernateUtil.flushSession();
HibernateUtil.clearSession();
AccountRequest actual = accountRequestsLogic.getAccountRequest(accountRequest.getId());
assertEquals(originalEmail, actual.getEmail());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
import org.testng.annotations.Test;

import teammates.common.datatransfer.SqlDataBundle;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.common.util.HibernateUtil;
import teammates.it.test.BaseTestCaseWithSqlDatabaseAccess;
import teammates.sqllogic.core.DeadlineExtensionsLogic;
import teammates.storage.sqlentity.DeadlineExtension;
import teammates.storage.sqlentity.FeedbackSession;
import teammates.storage.sqlentity.Student;

Expand Down Expand Up @@ -57,4 +60,37 @@ public void testGetExtendedDeadline_extensionDoesNotExist_null() {

assertNull(extendedDeadlineForStudent);
}

@Test
public void testUpdateDeadlineExtension()
throws EntityDoesNotExistException, InvalidParametersException {
FeedbackSession feedbackSession = typicalDataBundle.feedbackSessions.get("session1InCourse1");
Student student = typicalDataBundle.students.get("student1InCourse1");

DeadlineExtension de = typicalDataBundle.deadlineExtensions.get("student1InCourse1Session1");

Instant newEndTime = Instant.parse("2028-04-30T23:00:00Z");
de.setEndTime(newEndTime);
deadlineExtensionsLogic.updateDeadlineExtension(de);

assertEquals(newEndTime, deadlineExtensionsLogic.getExtendedDeadlineForUser(feedbackSession, student));
}

@Test
public void testUpdateDeadlineExtension_invalidParameters_originalUnchanged() {
FeedbackSession feedbackSession = typicalDataBundle.feedbackSessions.get("session1InCourse1");
Student student = typicalDataBundle.students.get("student1InCourse1");

DeadlineExtension de = typicalDataBundle.deadlineExtensions.get("student1InCourse1Session1");
Instant originalEndTime = de.getEndTime();

Instant invalidEndTime = Instant.parse("2011-04-01T22:00:00Z");
assert invalidEndTime.isBefore(feedbackSession.getStartTime());
de.setEndTime(invalidEndTime);

assertThrows(InvalidParametersException.class, () -> deadlineExtensionsLogic.updateDeadlineExtension(de));

assertEquals(originalEndTime, deadlineExtensionsLogic.getExtendedDeadlineForUser(feedbackSession, student));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ public void testCreateFeedbackQuestion() throws InvalidParametersException, Enti
verifyEquals(newQuestion, actualQuestion);
}

@Test
public void testCreateFeedbackQuestion_invalidDetails_throwsInvalidParameterException() {
FeedbackSession fs = typicalDataBundle.feedbackSessions.get("session1InCourse1");
FeedbackTextQuestionDetails newQuestionDetails = new FeedbackTextQuestionDetails("New question text.");
List<FeedbackParticipantType> showTos = new ArrayList<>();
showTos.add(FeedbackParticipantType.INSTRUCTORS);
FeedbackQuestion newQuestion = FeedbackQuestion.makeQuestion(fs, 6, "This is a new text question",
FeedbackParticipantType.STUDENTS, FeedbackParticipantType.OWN_TEAM_MEMBERS, -100,
showTos, showTos, showTos, newQuestionDetails);

newQuestion.setGiverType(FeedbackParticipantType.RECEIVER);

assertThrows(InvalidParametersException.class, () -> fqLogic.createFeedbackQuestion(newQuestion));
assertNull(fqLogic.getFeedbackQuestion(newQuestion.getId()));
}

@Test
public void testGetFeedbackQuestionsForSession() {
FeedbackSession fs = typicalDataBundle.feedbackSessions.get("session1InCourse1");
Expand All @@ -87,10 +103,10 @@ public void testGetFeedbackQuestionsForSession() {
@Test
public void testUpdateFeedbackQuestionCascade() throws InvalidParametersException, EntityDoesNotExistException {
FeedbackQuestion fq1 = typicalDataBundle.feedbackQuestions.get("qn1InSession1InCourse1");
fq1.setDescription("New question description");
String newQuestionDescription = "New question description";
FeedbackQuestionUpdateRequest updateRequest = generateFeedbackQuestionUpdateRequest(
fq1.getQuestionNumber(),
fq1.getDescription(),
newQuestionDescription,
fq1.getQuestionDetailsCopy(),
fq1.getQuestionDetailsCopy().getQuestionType(),
fq1.getGiverType(),
Expand All @@ -106,7 +122,40 @@ public void testUpdateFeedbackQuestionCascade() throws InvalidParametersExceptio

FeedbackQuestion actualFeedbackQuestion = fqLogic.getFeedbackQuestion(fq1.getId());

verifyEquals(fq1, actualFeedbackQuestion);
assertEquals(newQuestionDescription, actualFeedbackQuestion.getDescription());
}

@Test
public void testUpdateFeedbackQuestionCascade_invalidDetails_originalUnchanged()
throws InvalidParametersException, EntityDoesNotExistException {
FeedbackQuestion fq1 = typicalDataBundle.feedbackQuestions.get("qn1InSession1InCourse1");
String originalDescription = fq1.getDescription();

FeedbackQuestionUpdateRequest updateRequest = generateFeedbackQuestionUpdateRequest(
fq1.getQuestionNumber(),
"new question description",
fq1.getQuestionDetailsCopy(),
fq1.getQuestionDetailsCopy().getQuestionType(),
fq1.getGiverType(),
fq1.getRecipientType(),
fq1.getNumOfEntitiesToGiveFeedbackTo(),
fq1.getShowResponsesTo(),
fq1.getShowGiverNameTo(),
fq1.getShowRecipientNameTo()
);
updateRequest.setNumberOfEntitiesToGiveFeedbackToSetting(NumberOfEntitiesToGiveFeedbackToSetting.CUSTOM);

// set invalid question detail
FeedbackTextQuestionDetails ftqd = (FeedbackTextQuestionDetails) updateRequest.getQuestionDetails();
ftqd.setRecommendedLength(0);
updateRequest.setQuestionDetails(ftqd);

assertThrows(InvalidParametersException.class,
() -> fqLogic.updateFeedbackQuestionCascade(fq1.getId(), updateRequest));

HibernateUtil.flushSession();
HibernateUtil.clearSession();
assertEquals(originalDescription, fqLogic.getFeedbackQuestion(fq1.getId()).getDescription());
}

private FeedbackQuestionUpdateRequest generateFeedbackQuestionUpdateRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.testng.annotations.Test;

import teammates.common.datatransfer.SqlDataBundle;
import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.common.util.HibernateUtil;
Expand Down Expand Up @@ -46,6 +47,44 @@ protected void setUp() throws Exception {
HibernateUtil.clearSession();
}

@Test
public void testCreateFeedbackSession() throws InvalidParametersException, EntityAlreadyExistsException {

______TS("failure: invalid parameters");
FeedbackSession fs = typicalDataBundle.feedbackSessions.get("session1InCourse1");
FeedbackSession session = new FeedbackSession(
"", fs.getCourse(), fs.getCreatorEmail(), fs.getInstructions(), fs.getStartTime(), fs.getEndTime(),
fs.getSessionVisibleFromTime(), fs.getResultsVisibleFromTime(), fs.getGracePeriod(),
fs.isOpeningEmailEnabled(), fs.isClosingEmailEnabled(), fs.isPublishedEmailEnabled());

assertThrows(InvalidParametersException.class, () -> fsLogic.createFeedbackSession(session));
assertNull(fsLogic.getFeedbackSession(session.getId()));

______TS("success: typical case");
session.setName(fs.getName());
fsLogic.createFeedbackSession(session);

assertEquals(fs.getName(), fsLogic.getFeedbackSession(session.getId()).getName());
}

@Test
public void testUpdateFeedbackSession() throws InvalidParametersException, EntityDoesNotExistException {

______TS("success: typical case");
FeedbackSession fs = typicalDataBundle.feedbackSessions.get("session1InCourse1");
fs.setName("new name");

fsLogic.updateFeedbackSession(fs);
assertEquals("new name", fsLogic.getFeedbackSession(fs.getId()).getName());

______TS("failure: invalid parameters, original unchanged");
String originalName = fs.getName();
fs.setName("");

assertThrows(InvalidParametersException.class, () -> fsLogic.updateFeedbackSession(fs));
assertEquals(originalName, fsLogic.getFeedbackSession(fs.getId()).getName());
}

@Test
public void testGiverSetThatAnsweredFeedbackQuestion_hasGivers_findsGivers() {
FeedbackSession fs = typicalDataBundle.feedbackSessions.get("session1InCourse1");
Expand Down
77 changes: 77 additions & 0 deletions src/it/java/teammates/it/sqllogic/core/NotificationsLogicIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.common.util.HibernateUtil;
import teammates.it.test.BaseTestCaseWithSqlDatabaseAccess;
import teammates.sqllogic.core.NotificationsLogic;
import teammates.storage.sqlentity.Notification;
Expand All @@ -21,6 +22,48 @@ public class NotificationsLogicIT extends BaseTestCaseWithSqlDatabaseAccess {

private NotificationsLogic notificationsLogic = NotificationsLogic.inst();

@Test
public void testCreateNotification_endTimeIsBeforeStartTime_throwsInvalidParametersException() {
Notification invalidNotification = new Notification(
Instant.parse("2011-02-01T00:00:00Z"),
Instant.parse("2011-01-01T00:00:00Z"),
NotificationStyle.DANGER,
NotificationTargetUser.GENERAL,
"A deprecation note",
"<p>Deprecation happens in three minutes</p>");

assertThrows(InvalidParametersException.class, () -> notificationsLogic.createNotification(invalidNotification));
assertNull(notificationsLogic.getNotification(invalidNotification.getId()));
}

@Test
public void testCreateNotification_emptyTitle_throwsInvalidParametersException() {
Notification invalidNotification = new Notification(
Instant.parse("2011-01-01T00:00:00Z"),
Instant.parse("2099-01-01T00:00:00Z"),
NotificationStyle.DANGER,
NotificationTargetUser.GENERAL,
"",
"<p>Deprecation happens in three minutes</p>");

assertThrows(InvalidParametersException.class, () -> notificationsLogic.createNotification(invalidNotification));
assertNull(notificationsLogic.getNotification(invalidNotification.getId()));
}

@Test
public void testCreateNotification_emptyMessage_throwsInvalidParametersException() {
Notification invalidNotification = new Notification(
Instant.parse("2011-01-01T00:00:00Z"),
Instant.parse("2099-01-01T00:00:00Z"),
NotificationStyle.DANGER,
NotificationTargetUser.GENERAL,
"A deprecation note",
"");

assertThrows(InvalidParametersException.class, () -> notificationsLogic.createNotification(invalidNotification));
assertNull(notificationsLogic.getNotification(invalidNotification.getId()));
}

@Test
public void testUpdateNotification()
throws EntityAlreadyExistsException, InvalidParametersException, EntityDoesNotExistException {
Expand Down Expand Up @@ -59,4 +102,38 @@ public void testUpdateNotification()
newStartTime, newEndTime, newStyle, newTargetUser, newTitle, newMessage));
}

@Test
public void testUpdateNotification_invalidParameters_originalUnchanged() throws Exception {

String originalTitle = "The original title";
String originalMessage = "<p>Deprecation happens in three minutes</p>";
Notification notif = new Notification(
Instant.parse("2011-01-01T00:00:00Z"),
Instant.parse("2099-01-01T00:00:00Z"),
NotificationStyle.DANGER,
NotificationTargetUser.GENERAL,
originalTitle,
originalMessage);
notificationsLogic.createNotification(notif);

String invalidLongTitle = "1234567890".repeat(10);
String validNewMessage = "This should not be reflected.";
assertThrows(
InvalidParametersException.class,
() -> notificationsLogic.updateNotification(
notif.getId(),
Instant.parse("2011-01-01T00:00:00Z"),
Instant.parse("2099-01-01T00:00:00Z"),
NotificationStyle.DANGER,
NotificationTargetUser.GENERAL,
invalidLongTitle,
validNewMessage));

HibernateUtil.flushSession();
HibernateUtil.clearSession();
Notification actual = notificationsLogic.getNotification(notif.getId());
assertEquals(originalTitle, actual.getTitle());
assertEquals(originalMessage, actual.getMessage());
}

}
Loading
Loading