|
| 1 | +package org.patinanetwork.codebloom.common.submissions; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | +import static org.mockito.ArgumentMatchers.any; |
| 6 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 7 | +import static org.mockito.ArgumentMatchers.anyString; |
| 8 | +import static org.mockito.ArgumentMatchers.eq; |
| 9 | +import static org.mockito.Mockito.mock; |
| 10 | +import static org.mockito.Mockito.never; |
| 11 | +import static org.mockito.Mockito.verify; |
| 12 | +import static org.mockito.Mockito.when; |
| 13 | + |
| 14 | +import java.time.LocalDateTime; |
| 15 | +import java.util.List; |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.patinanetwork.codebloom.common.db.models.leaderboard.Leaderboard; |
| 19 | +import org.patinanetwork.codebloom.common.db.models.potd.POTD; |
| 20 | +import org.patinanetwork.codebloom.common.db.models.question.Question; |
| 21 | +import org.patinanetwork.codebloom.common.db.models.question.QuestionDifficulty; |
| 22 | +import org.patinanetwork.codebloom.common.db.models.question.bank.QuestionBank; |
| 23 | +import org.patinanetwork.codebloom.common.db.models.question.topic.LeetcodeTopicEnum; |
| 24 | +import org.patinanetwork.codebloom.common.db.models.question.topic.QuestionTopic; |
| 25 | +import org.patinanetwork.codebloom.common.db.models.user.User; |
| 26 | +import org.patinanetwork.codebloom.common.db.models.user.UserWithScore; |
| 27 | +import org.patinanetwork.codebloom.common.db.repos.job.JobRepository; |
| 28 | +import org.patinanetwork.codebloom.common.db.repos.leaderboard.LeaderboardRepository; |
| 29 | +import org.patinanetwork.codebloom.common.db.repos.potd.POTDRepository; |
| 30 | +import org.patinanetwork.codebloom.common.db.repos.question.QuestionRepository; |
| 31 | +import org.patinanetwork.codebloom.common.db.repos.question.questionbank.QuestionBankRepository; |
| 32 | +import org.patinanetwork.codebloom.common.db.repos.question.topic.QuestionTopicRepository; |
| 33 | +import org.patinanetwork.codebloom.common.db.repos.user.UserRepository; |
| 34 | +import org.patinanetwork.codebloom.common.db.repos.user.options.UserFilterOptions; |
| 35 | +import org.patinanetwork.codebloom.common.leetcode.models.LeetcodeQuestion; |
| 36 | +import org.patinanetwork.codebloom.common.leetcode.models.LeetcodeSubmission; |
| 37 | +import org.patinanetwork.codebloom.common.leetcode.models.LeetcodeTopicTag; |
| 38 | +import org.patinanetwork.codebloom.common.leetcode.throttled.ThrottledLeetcodeClient; |
| 39 | +import org.patinanetwork.codebloom.common.reporter.throttled.ThrottledReporter; |
| 40 | +import org.patinanetwork.codebloom.common.submissions.object.AcceptedSubmission; |
| 41 | + |
| 42 | +public class SubmissionHandlerTest { |
| 43 | + private final QuestionRepository questionRepository = mock(QuestionRepository.class); |
| 44 | + private final ThrottledLeetcodeClient leetcodeClient = mock(ThrottledLeetcodeClient.class); |
| 45 | + private final LeaderboardRepository leaderboardRepository = mock(LeaderboardRepository.class); |
| 46 | + private final POTDRepository potdRepository = mock(POTDRepository.class); |
| 47 | + private final UserRepository userRepository = mock(UserRepository.class); |
| 48 | + private final QuestionTopicRepository questionTopicRepository = mock(QuestionTopicRepository.class); |
| 49 | + private final JobRepository jobRepository = mock(JobRepository.class); |
| 50 | + private final ThrottledReporter throttledReporter = mock(ThrottledReporter.class); |
| 51 | + private final QuestionBankRepository questionBankRepository = mock(QuestionBankRepository.class); |
| 52 | + |
| 53 | + private SubmissionsHandler submissionsHandler; |
| 54 | + |
| 55 | + private final User user = mock(User.class); |
| 56 | + private final Leaderboard leaderboard = mock(Leaderboard.class); |
| 57 | + private final UserWithScore userWithScore = mock(UserWithScore.class); |
| 58 | + private final POTD potd = mock(POTD.class); |
| 59 | + |
| 60 | + private final LeetcodeSubmission submission = |
| 61 | + new LeetcodeSubmission(1, "Hello World", "hello-world", LocalDateTime.now(), "Accepted"); |
| 62 | + |
| 63 | + private final LeetcodeTopicTag leetcodeTopicTag = |
| 64 | + LeetcodeTopicTag.builder().name("String").slug("string").build(); |
| 65 | + |
| 66 | + private final LeetcodeQuestion lcQuestion = LeetcodeQuestion.builder() |
| 67 | + .questionId(1) |
| 68 | + .titleSlug("hello-world") |
| 69 | + .questionTitle("Hello World") |
| 70 | + .difficulty("Easy") |
| 71 | + .question("Print Hello World") |
| 72 | + .acceptanceRate(1.0f) |
| 73 | + .topics(List.of(leetcodeTopicTag)) |
| 74 | + .build(); |
| 75 | + |
| 76 | + private void fullPath() { |
| 77 | + when(user.getId()).thenReturn("fb632fce-173e-11f1-8350-6f6b49d02c1c"); |
| 78 | + when(leaderboard.getId()).thenReturn("7c572df6-173f-11f1-8331-d3ac52e05366"); |
| 79 | + when(userWithScore.getTotalScore()).thenReturn(100); |
| 80 | + when(userRepository.getUserWithScoreByIdAndLeaderboardId( |
| 81 | + "fb632fce-173e-11f1-8350-6f6b49d02c1c", |
| 82 | + "7c572df6-173f-11f1-8331-d3ac52e05366", |
| 83 | + UserFilterOptions.DEFAULT)) |
| 84 | + .thenReturn(userWithScore); |
| 85 | + |
| 86 | + when(potdRepository.getCurrentPOTD()).thenReturn(null); |
| 87 | + |
| 88 | + when(questionRepository.questionExistsBySubmissionId("1")).thenReturn(false); |
| 89 | + |
| 90 | + when(questionRepository.getQuestionBySlugAndUserId("hello-world", "fb632fce-173e-11f1-8350-6f6b49d02c1c")) |
| 91 | + .thenReturn(null); |
| 92 | + |
| 93 | + when(leaderboard.getCreatedAt()).thenReturn(submission.getTimestamp().minusHours(1)); |
| 94 | + when(leaderboardRepository.getRecentLeaderboardMetadata()).thenReturn(leaderboard); |
| 95 | + |
| 96 | + when(leetcodeClient.findQuestionBySlugFast("hello-world")).thenReturn(lcQuestion); |
| 97 | + } |
| 98 | + |
| 99 | + @BeforeEach |
| 100 | + void setup() { |
| 101 | + submissionsHandler = new SubmissionsHandler( |
| 102 | + questionRepository, |
| 103 | + leetcodeClient, |
| 104 | + leaderboardRepository, |
| 105 | + potdRepository, |
| 106 | + userRepository, |
| 107 | + questionTopicRepository, |
| 108 | + throttledReporter, |
| 109 | + jobRepository, |
| 110 | + questionBankRepository); |
| 111 | + |
| 112 | + fullPath(); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void testHandleSubmissionSuccess() { |
| 117 | + List<AcceptedSubmission> result = submissionsHandler.handleSubmissions(List.of(submission), user, true); |
| 118 | + |
| 119 | + assertEquals(1, result.size()); |
| 120 | + |
| 121 | + verify(potd, never()).getMultiplier(); |
| 122 | + verify(leetcodeClient).findQuestionBySlugFast("hello-world"); |
| 123 | + verify(jobRepository).createJob(any()); |
| 124 | + verify(leaderboardRepository) |
| 125 | + .updateUserPointsFromLeaderboard( |
| 126 | + eq("7c572df6-173f-11f1-8331-d3ac52e05366"), |
| 127 | + eq("fb632fce-173e-11f1-8350-6f6b49d02c1c"), |
| 128 | + anyInt()); |
| 129 | + assertTrue(result.get(0).points() > 0); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void testNoBank() { |
| 134 | + QuestionBank bankQuestion = QuestionBank.builder() |
| 135 | + .questionSlug(lcQuestion.getTitleSlug()) |
| 136 | + .questionDifficulty(QuestionDifficulty.valueOf(lcQuestion.getDifficulty())) |
| 137 | + .questionTitle(lcQuestion.getQuestionTitle()) |
| 138 | + .questionNumber(lcQuestion.getQuestionId()) |
| 139 | + .questionLink("https://leetcode.com/problems/" + lcQuestion.getTitleSlug()) |
| 140 | + .description(lcQuestion.getQuestion()) |
| 141 | + .acceptanceRate(lcQuestion.getAcceptanceRate()) |
| 142 | + .topics(lcQuestion.getTopics().stream() |
| 143 | + .map(t -> QuestionTopic.builder() |
| 144 | + .topicSlug(t.getSlug()) |
| 145 | + .topic(LeetcodeTopicEnum.fromValue(t.getSlug())) |
| 146 | + .build()) |
| 147 | + .toList()) |
| 148 | + .build(); |
| 149 | + when(questionBankRepository.getQuestionBySlug(anyString())).thenReturn(bankQuestion); |
| 150 | + |
| 151 | + List<AcceptedSubmission> result = submissionsHandler.handleSubmissions(List.of(submission), user, true); |
| 152 | + assertEquals(1, result.size()); |
| 153 | + |
| 154 | + verify(leetcodeClient, never()).findQuestionBySlugFast(anyString()); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + void testFindQuestionsSlow() { |
| 159 | + when(leetcodeClient.findQuestionBySlug("hello-world")).thenReturn(lcQuestion); |
| 160 | + |
| 161 | + List<AcceptedSubmission> result = submissionsHandler.handleSubmissions(List.of(submission), user, false); |
| 162 | + assertEquals(1, result.size()); |
| 163 | + |
| 164 | + verify(leetcodeClient).findQuestionBySlug("hello-world"); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + void testQuestionNotAccepted() { |
| 169 | + submission.setStatusDisplay("Rejected"); |
| 170 | + |
| 171 | + List<AcceptedSubmission> result = submissionsHandler.handleSubmissions(List.of(submission), user, true); |
| 172 | + assertEquals(0, result.size()); |
| 173 | + |
| 174 | + submission.setStatusDisplay("Accepted"); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + void testSubmissionAlreadyExists() { |
| 179 | + when(questionRepository.questionExistsBySubmissionId(String.valueOf(submission.getId()))) |
| 180 | + .thenReturn(true); |
| 181 | + |
| 182 | + List<AcceptedSubmission> result = submissionsHandler.handleSubmissions(List.of(submission), user, true); |
| 183 | + assertEquals(0, result.size()); |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + void testQuestionNotNull() { |
| 188 | + Question q = mock(Question.class); |
| 189 | + when(questionRepository.getQuestionBySlugAndUserId(submission.getTitleSlug(), user.getId())) |
| 190 | + .thenReturn(q); |
| 191 | + |
| 192 | + List<AcceptedSubmission> result = submissionsHandler.handleSubmissions(List.of(submission), user, true); |
| 193 | + assertEquals(1, result.size()); |
| 194 | + |
| 195 | + assertTrue(result.get(0).points() == 0); |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + void testTooLate() { |
| 200 | + when(leaderboard.getCreatedAt()).thenReturn(submission.getTimestamp().plusHours(1)); |
| 201 | + |
| 202 | + List<AcceptedSubmission> result = submissionsHandler.handleSubmissions(List.of(submission), user, true); |
| 203 | + assertEquals(1, result.size()); |
| 204 | + |
| 205 | + assertTrue(result.get(0).points() == 0); |
| 206 | + } |
| 207 | + |
| 208 | + @Test |
| 209 | + void testIsValidPOTD() { |
| 210 | + when(potdRepository.getCurrentPOTD()).thenReturn(potd); |
| 211 | + when(potd.getCreatedAt()).thenReturn(LocalDateTime.now()); |
| 212 | + when(potd.getSlug()).thenReturn("hello-world"); |
| 213 | + when(potd.getMultiplier()).thenReturn(1.5f); |
| 214 | + |
| 215 | + List<AcceptedSubmission> result = submissionsHandler.handleSubmissions(List.of(submission), user, true); |
| 216 | + assertEquals(1, result.size()); |
| 217 | + |
| 218 | + verify(potd).getMultiplier(); |
| 219 | + } |
| 220 | +} |
0 commit comments