diff --git a/.sdkmanrc b/.sdkmanrc new file mode 100644 index 000000000..0488cfc76 --- /dev/null +++ b/.sdkmanrc @@ -0,0 +1,3 @@ +# Enable auto-env through the sdkman_auto_env config +# Add key=value pairs of SDKs to use below +java=26.0.1-tem diff --git a/exclusions.ts b/exclusions.ts index 7a4a4d376..5ff6562b9 100644 --- a/exclusions.ts +++ b/exclusions.ts @@ -14,4 +14,6 @@ export const backendExclusions = [ export const frontendExclusions = [ `${frontendBaseDir}/**/*.test.ts`, `${frontendBaseDir}/**/*.test.tsx`, + `${frontendBaseDir}/lib/api/queries/**/*.ts`, + `${frontendBaseDir}/lib/api/schema/**/*.ts`, ]; diff --git a/js/src/lib/api/queries/auth/index.ts b/js/src/lib/api/queries/auth/index.ts index 2422c4f59..a141a9b9d 100644 --- a/js/src/lib/api/queries/auth/index.ts +++ b/js/src/lib/api/queries/auth/index.ts @@ -1,4 +1,5 @@ import { ApiURL } from "@/lib/api/common/apiURL"; +import { sleep } from "@/lib/api/utils/lag"; import { useQuery } from "@tanstack/react-query"; /** @@ -28,6 +29,7 @@ async function validateAuthentication({ const json = res(await response.json()); if (json.success) { + await sleep(350); return { session: json.payload.session, user: json.payload.user, diff --git a/js/src/lib/api/queries/auth/leetcode/index.ts b/js/src/lib/api/queries/auth/leetcode/index.ts index 14ae26ae5..f6cc04498 100644 --- a/js/src/lib/api/queries/auth/leetcode/index.ts +++ b/js/src/lib/api/queries/auth/leetcode/index.ts @@ -1,4 +1,5 @@ import { ApiURL } from "@/lib/api/common/apiURL"; +import { sleep } from "@/lib/api/utils/lag"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; /** @@ -45,6 +46,8 @@ async function updateLeetcodeUsername({ }), }); + await sleep(350); + return res(await response.json()); } @@ -57,5 +60,7 @@ async function getLeetcodeQueryKey({ method, }); + await sleep(350); + return res(await response.json()); } diff --git a/js/src/lib/api/queries/club/index.ts b/js/src/lib/api/queries/club/index.ts index 7da874c16..e7d5987d9 100644 --- a/js/src/lib/api/queries/club/index.ts +++ b/js/src/lib/api/queries/club/index.ts @@ -1,4 +1,5 @@ import { ApiURL } from "@/lib/api/common/apiURL"; +import { sleep } from "@/lib/api/utils/lag"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; export const useClubQuery = ({ clubSlug }: { clubSlug: string }) => { @@ -25,6 +26,8 @@ async function fetchClubDto({ method, }); + await sleep(650); + return res(await response.json()); } diff --git a/js/src/lib/api/queries/embed/index.ts b/js/src/lib/api/queries/embed/index.ts index 322c36d49..4de3afcc7 100644 --- a/js/src/lib/api/queries/embed/index.ts +++ b/js/src/lib/api/queries/embed/index.ts @@ -1,4 +1,5 @@ import { ApiURL } from "@/lib/api/common/apiURL"; +import { sleep } from "@/lib/api/utils/lag"; import { useQuery } from "@tanstack/react-query"; export const useFetchPotdEmbedQuery = () => { @@ -22,6 +23,8 @@ async function fetchPotdEmbed({ method, }); + await sleep(750); + const json = res(await response.json()); return json; diff --git a/js/src/lib/api/queries/leaderboard/index.ts b/js/src/lib/api/queries/leaderboard/index.ts index 0ebae998c..e355d998d 100644 --- a/js/src/lib/api/queries/leaderboard/index.ts +++ b/js/src/lib/api/queries/leaderboard/index.ts @@ -1,5 +1,6 @@ import { ApiURL } from "@/lib/api/common/apiURL"; import { ApiUtils } from "@/lib/api/utils"; +import { sleep } from "@/lib/api/utils/lag"; import { useFilters } from "@/lib/hooks/useFilters"; import { usePagination } from "@/lib/hooks/usePagination"; import { useURLState } from "@/lib/hooks/useUrlState"; @@ -421,6 +422,8 @@ async function fetchLeaderboardUsers({ method, }); + await sleep(800); + const json = res(await response.json()); return json; @@ -435,6 +438,8 @@ async function fetchLeaderboardUsersByLeaderboardId({ method, }); + await sleep(800); + const json = res(await response.json()); return json; @@ -449,6 +454,8 @@ async function getCurrentLeaderboardMetadata({ method, }); + await sleep(650); + const json = res(await response.json()); return json; @@ -463,6 +470,8 @@ async function getLeaderboardMetadataById({ method, }); + await sleep(650); + const json = res(await response.json()); return json; @@ -496,6 +505,8 @@ export async function getMyRecentLeaderboardData({ method, }); + await sleep(650); + const json = res(await response.json()); return json; @@ -510,6 +521,8 @@ async function fetchAllLeaderboardsMetadata({ method, }); + await sleep(650); + const json = res(await response.json()); return json; diff --git a/js/src/lib/api/queries/potd/index.ts b/js/src/lib/api/queries/potd/index.ts index f235fd417..5cbff8b51 100644 --- a/js/src/lib/api/queries/potd/index.ts +++ b/js/src/lib/api/queries/potd/index.ts @@ -1,4 +1,5 @@ import { ApiURL } from "@/lib/api/common/apiURL"; +import { sleep } from "@/lib/api/utils/lag"; import { useQuery } from "@tanstack/react-query"; export const useFetchPotdQuery = () => { @@ -22,6 +23,8 @@ async function fetchPotd({ method, }); + await sleep(750); + const json = res(await response.json()); return json; diff --git a/js/src/lib/api/queries/submissions/index.ts b/js/src/lib/api/queries/submissions/index.ts index 4917d3fab..713ee9f3c 100644 --- a/js/src/lib/api/queries/submissions/index.ts +++ b/js/src/lib/api/queries/submissions/index.ts @@ -1,4 +1,5 @@ import { ApiURL } from "@/lib/api/common/apiURL"; +import { sleep } from "@/lib/api/utils/lag"; import { useQuery } from "@tanstack/react-query"; /** @@ -32,6 +33,8 @@ async function fetchSubmissionDetails({ const response = await fetch(url, { method, }); + + await sleep(750); const json = res(await response.json()); return json; diff --git a/js/src/lib/api/queries/user/index.ts b/js/src/lib/api/queries/user/index.ts index ee49e5be8..9f18fc482 100644 --- a/js/src/lib/api/queries/user/index.ts +++ b/js/src/lib/api/queries/user/index.ts @@ -1,5 +1,6 @@ import { ApiURL } from "@/lib/api/common/apiURL"; import { LeetcodeTopicEnum } from "@/lib/api/types/schema"; +import { sleep } from "@/lib/api/utils/lag"; import { usePagination } from "@/lib/hooks/usePagination"; import useURLDateRange from "@/lib/hooks/useURLDateRange"; import { useURLState } from "@/lib/hooks/useUrlState"; @@ -312,6 +313,8 @@ async function fetchUserProfile({ method, }); + await sleep(650); + const json = res(await response.json()); return json; @@ -326,6 +329,8 @@ async function fetchUserSubmissions({ method, }); + await sleep(500); + const json = res(await response.json()); return json; } @@ -339,6 +344,8 @@ async function fetchAllUsers({ method, }); + await sleep(650); + const json = res(await response.json()); return json; diff --git a/js/src/lib/api/utils/lag.ts b/js/src/lib/api/utils/lag.ts new file mode 100644 index 000000000..812492656 --- /dev/null +++ b/js/src/lib/api/utils/lag.ts @@ -0,0 +1,8 @@ +export async function sleep(ms: number) { + if (import.meta.env.MODE === "test") return; + return await new Promise((resolve) => { + setTimeout(() => { + resolve(void 0); + }, ms); + }); +} diff --git a/js/src/lib/api/utils/sleep.test.ts b/js/src/lib/api/utils/sleep.test.ts new file mode 100644 index 000000000..76f7479c6 --- /dev/null +++ b/js/src/lib/api/utils/sleep.test.ts @@ -0,0 +1,40 @@ +import { sleep } from "@/lib/api/utils/lag"; +import { afterEach, describe, expect, it, vi } from "vitest"; + +describe("sleep", () => { + beforeEach(() => { + vi.useFakeTimers(); + }); + + afterEach(() => { + vi.useRealTimers(); + vi.unstubAllEnvs(); + }); + + it("skips delay in test environment", async () => { + let resolved = false; + sleep(10_000).then(() => { + resolved = true; + }); + + await vi.advanceTimersByTimeAsync(0); + + expect(resolved).toBe(true); + expect(vi.getTimerCount()).toBe(0); + }); + + it("waits when not in test environment", async () => { + vi.stubEnv("MODE", "production"); + + let resolved = false; + void sleep(1_000).then(() => { + resolved = true; + }); + + await vi.advanceTimersByTimeAsync(0); + expect(resolved).toBe(false); + + await vi.advanceTimersByTimeAsync(1_000); + expect(resolved).toBe(true); + }); +}); diff --git a/src/main/java/org/patinanetwork/codebloom/api/auth/AuthController.java b/src/main/java/org/patinanetwork/codebloom/api/auth/AuthController.java index 0069e0b51..f75183f0b 100644 --- a/src/main/java/org/patinanetwork/codebloom/api/auth/AuthController.java +++ b/src/main/java/org/patinanetwork/codebloom/api/auth/AuthController.java @@ -29,7 +29,6 @@ import org.patinanetwork.codebloom.common.email.options.SendEmailOptions; import org.patinanetwork.codebloom.common.email.template.ReactEmailTemplater; import org.patinanetwork.codebloom.common.jwt.JWTClient; -import org.patinanetwork.codebloom.common.lag.FakeLag; import org.patinanetwork.codebloom.common.reporter.Reporter; import org.patinanetwork.codebloom.common.reporter.report.Report; import org.patinanetwork.codebloom.common.schools.SchoolEnum; @@ -107,7 +106,6 @@ public AuthController( @GetMapping("/validate") public ResponseEntity> validateAuth( @Protected final AuthenticationObject authenticationObject) { - FakeLag.sleep(350); return ResponseEntity.ok() .body(ApiResponder.success( diff --git a/src/main/java/org/patinanetwork/codebloom/api/club/ClubController.java b/src/main/java/org/patinanetwork/codebloom/api/club/ClubController.java index 2111efa27..d9daafae6 100644 --- a/src/main/java/org/patinanetwork/codebloom/api/club/ClubController.java +++ b/src/main/java/org/patinanetwork/codebloom/api/club/ClubController.java @@ -11,7 +11,6 @@ import org.patinanetwork.codebloom.common.db.repos.club.ClubRepository; import org.patinanetwork.codebloom.common.db.repos.usertag.UserTagRepository; import org.patinanetwork.codebloom.common.dto.ApiResponder; -import org.patinanetwork.codebloom.common.lag.FakeLag; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -43,7 +42,6 @@ public ClubController( @GetMapping("/{clubSlug}") public ResponseEntity> getClubDataBySlug(@PathVariable final String clubSlug) { - FakeLag.sleep(650); ClubDto club = clubService.getClubDtoBySlug(clubSlug); diff --git a/src/main/java/org/patinanetwork/codebloom/api/leaderboard/LeaderboardController.java b/src/main/java/org/patinanetwork/codebloom/api/leaderboard/LeaderboardController.java index 3ec2f87d3..21d835bce 100644 --- a/src/main/java/org/patinanetwork/codebloom/api/leaderboard/LeaderboardController.java +++ b/src/main/java/org/patinanetwork/codebloom/api/leaderboard/LeaderboardController.java @@ -21,7 +21,6 @@ import org.patinanetwork.codebloom.common.dto.autogen.UnsafeGenericFailureResponse; import org.patinanetwork.codebloom.common.dto.leaderboard.LeaderboardDto; import org.patinanetwork.codebloom.common.dto.user.UserWithScoreDto; -import org.patinanetwork.codebloom.common.lag.FakeLag; import org.patinanetwork.codebloom.common.page.Indexed; import org.patinanetwork.codebloom.common.page.Page; import org.patinanetwork.codebloom.common.security.AuthenticationObject; @@ -68,7 +67,6 @@ public LeaderboardController( }) public ResponseEntity> getLeaderboardMetadataByLeaderboardId( final @PathVariable String leaderboardId, final HttpServletRequest request) { - FakeLag.sleep(650); Optional leaderboardData = leaderboardManager.getLeaderboardMetadata(leaderboardId); @@ -132,7 +130,6 @@ public ResponseEntity>>> getLeaderbo @RequestParam(required = false, defaultValue = "false") final boolean globalIndex, final HttpServletRequest request) { - FakeLag.sleep(800); final int parsedPageSize = Math.min(pageSize, MAX_LEADERBOARD_PAGE_SIZE); @@ -173,7 +170,6 @@ public ResponseEntity>>> getLeaderbo }) public ResponseEntity> getCurrentLeaderboardMetadata( final HttpServletRequest request) { - FakeLag.sleep(650); var current = leaderboardRepository .getRecentLeaderboardMetadata() @@ -240,7 +236,6 @@ public ResponseEntity>>> getCurrentL @Parameter(description = "Enable global leaderboard index") @RequestParam(required = false, defaultValue = "false") final boolean globalIndex) { - FakeLag.sleep(800); final int parsedPageSize = Math.min(pageSize, MAX_LEADERBOARD_PAGE_SIZE); @@ -284,7 +279,6 @@ public ResponseEntity>>> getCurrentL }) public ResponseEntity> getUserCurrentLeaderboardFull( final HttpServletRequest request, @PathVariable final String userId) { - FakeLag.sleep(650); Optional leaderboardData = leaderboardRepository.getRecentLeaderboardMetadata(); @@ -345,7 +339,6 @@ public ResponseEntity>> getUserCurrentLea final boolean columbia, @Parameter(description = "Filter for BMCC users") @RequestParam(required = false, defaultValue = "false") final boolean bmcc) { - FakeLag.sleep(650); String userId = authenticationObject.getUser().getId(); @@ -411,7 +404,6 @@ public ResponseEntity>> getAllLeaderboardMetad @Parameter(description = "Page size (maximum of " + MAX_LEADERBOARD_PAGE_SIZE) @RequestParam(required = false, defaultValue = "" + MAX_LEADERBOARD_PAGE_SIZE) final int pageSize) { - FakeLag.sleep(650); final int parsedPageSize = Math.min(pageSize, MAX_LEADERBOARD_PAGE_SIZE); diff --git a/src/main/java/org/patinanetwork/codebloom/api/submission/SubmissionController.java b/src/main/java/org/patinanetwork/codebloom/api/submission/SubmissionController.java index e5d5d7bc9..29cda18f3 100644 --- a/src/main/java/org/patinanetwork/codebloom/api/submission/SubmissionController.java +++ b/src/main/java/org/patinanetwork/codebloom/api/submission/SubmissionController.java @@ -25,7 +25,6 @@ import org.patinanetwork.codebloom.common.dto.autogen.UnsafeRateLimitResponse; import org.patinanetwork.codebloom.common.dto.potd.PotdDto; import org.patinanetwork.codebloom.common.dto.question.QuestionWithUserDto; -import org.patinanetwork.codebloom.common.lag.FakeLag; import org.patinanetwork.codebloom.common.leetcode.models.LeetcodeSubmission; import org.patinanetwork.codebloom.common.leetcode.models.UserProfile; import org.patinanetwork.codebloom.common.leetcode.throttled.ThrottledLeetcodeClient; @@ -108,7 +107,6 @@ public SubmissionController( @GetMapping("/key") public ResponseEntity> getVerificationKey( @Protected final AuthenticationObject authenticationObject) { - FakeLag.sleep(350); User user = authenticationObject.getUser(); @@ -144,7 +142,6 @@ public ResponseEntity> getVerificationKey( public ResponseEntity> setLeetcodeUsername( @Protected final AuthenticationObject authenticationObject, @Valid @RequestBody final LeetcodeUsernameObject leetcodeUsernameObject) { - FakeLag.sleep(350); User user = authenticationObject.getUser(); @@ -256,7 +253,6 @@ public ResponseEntity>> checkLatestSu @GetMapping("/potd") public ResponseEntity> getCurrentPotd( @Protected final AuthenticationObject authenticationObject) { - FakeLag.sleep(750); User user = authenticationObject.getUser(); @@ -296,8 +292,6 @@ public ResponseEntity> getCurrentPotd( }) @GetMapping("/potd/embed") public ResponseEntity> getCurrentPotdEmbed() { - FakeLag.sleep(750); - POTD potd = potdRepository .getCurrentPOTD() .filter(p -> isSameDay(p.getCreatedAt())) @@ -329,7 +323,6 @@ public ResponseEntity> getCurrentPotdEmbed() { @GetMapping("/submission/{submissionId}") public ResponseEntity> getSubmissionBySubmissionId( final HttpServletRequest request, @PathVariable final String submissionId) { - FakeLag.sleep(750); var question = questionRepository.getQuestionWithUserById(submissionId); diff --git a/src/main/java/org/patinanetwork/codebloom/api/user/UserController.java b/src/main/java/org/patinanetwork/codebloom/api/user/UserController.java index 4298cf8cc..9a2557b10 100644 --- a/src/main/java/org/patinanetwork/codebloom/api/user/UserController.java +++ b/src/main/java/org/patinanetwork/codebloom/api/user/UserController.java @@ -27,7 +27,6 @@ import org.patinanetwork.codebloom.common.dto.user.UserDto; import org.patinanetwork.codebloom.common.dto.user.metrics.MetricsDto; import org.patinanetwork.codebloom.common.ff.annotation.FF; -import org.patinanetwork.codebloom.common.lag.FakeLag; import org.patinanetwork.codebloom.common.page.Page; import org.patinanetwork.codebloom.common.time.StandardizedOffsetDateTime; import org.springframework.http.HttpStatus; @@ -85,7 +84,6 @@ public UserController( @GetMapping("{userId}/profile") public ResponseEntity> getUserProfileByUserId( final HttpServletRequest request, @PathVariable final String userId) { - FakeLag.sleep(650); User user = userRepository.getUserById(userId); @@ -137,7 +135,6 @@ public ResponseEntity>> getAllQuestionsForUser( @RequestParam(required = false) final OffsetDateTime endDate, @PathVariable final String userId) { - FakeLag.sleep(500); if (startDate != null && endDate != null && startDate.isAfter(endDate)) { throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "startDate cannot be after endDate."); @@ -189,7 +186,6 @@ public ResponseEntity>> getAllUsers( @Parameter(description = "Page size (maximum of " + SUBMISSIONS_PAGE_SIZE) @RequestParam(required = false, defaultValue = "" + SUBMISSIONS_PAGE_SIZE) final int pageSize) { - FakeLag.sleep(650); final int parsedPageSize = Math.min(pageSize, SUBMISSIONS_PAGE_SIZE); @@ -240,8 +236,6 @@ public ResponseEntity>> getUserMetrics( @RequestParam(required = false) final OffsetDateTime endDate) { - FakeLag.sleep(500); - if (startDate != null && endDate != null && startDate.isAfter(endDate)) { throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "startDate cannot be after endDate."); } diff --git a/src/main/java/org/patinanetwork/codebloom/api/user/v2/UserV2Controller.java b/src/main/java/org/patinanetwork/codebloom/api/user/v2/UserV2Controller.java index 40fe3f783..02619b810 100644 --- a/src/main/java/org/patinanetwork/codebloom/api/user/v2/UserV2Controller.java +++ b/src/main/java/org/patinanetwork/codebloom/api/user/v2/UserV2Controller.java @@ -13,7 +13,6 @@ import org.patinanetwork.codebloom.common.dto.ApiResponder; import org.patinanetwork.codebloom.common.dto.autogen.UnsafeGenericFailureResponse; import org.patinanetwork.codebloom.common.dto.user.UserDto; -import org.patinanetwork.codebloom.common.lag.FakeLag; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -54,7 +53,6 @@ public UserV2Controller(final QuestionRepository questionRepository, final UserR @GetMapping("{leetcodeUsername}/profile") public ResponseEntity> getUserProfileByLeetcodeUsername( final HttpServletRequest request, @PathVariable final String leetcodeUsername) { - FakeLag.sleep(650); User user = userRepository.getUserByLeetcodeUsername(leetcodeUsername); diff --git a/src/test/java/org/patinanetwork/codebloom/api/submission/SubmissionControllerTest.java b/src/test/java/org/patinanetwork/codebloom/api/submission/SubmissionControllerTest.java index f02565cd4..59d5da747 100644 --- a/src/test/java/org/patinanetwork/codebloom/api/submission/SubmissionControllerTest.java +++ b/src/test/java/org/patinanetwork/codebloom/api/submission/SubmissionControllerTest.java @@ -1,7 +1,6 @@ package org.patinanetwork.codebloom.api.submission; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.*; import jakarta.servlet.http.HttpServletRequest; @@ -11,7 +10,6 @@ import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.MockedStatic; import org.patinanetwork.codebloom.api.submission.body.LeetcodeUsernameObject; import org.patinanetwork.codebloom.common.db.models.potd.POTD; import org.patinanetwork.codebloom.common.db.models.question.QuestionWithUser; @@ -23,7 +21,6 @@ import org.patinanetwork.codebloom.common.dto.Empty; import org.patinanetwork.codebloom.common.dto.potd.PotdDto; import org.patinanetwork.codebloom.common.dto.question.QuestionWithUserDto; -import org.patinanetwork.codebloom.common.lag.FakeLag; import org.patinanetwork.codebloom.common.leetcode.models.LeetcodeSubmission; import org.patinanetwork.codebloom.common.leetcode.models.UserProfile; import org.patinanetwork.codebloom.common.leetcode.throttled.ThrottledLeetcodeClient; @@ -105,20 +102,16 @@ void testSetLeetcodeUsernameSuccess() { when(userRepository.userExistsByLeetcodeUsername("leetcodeUser")).thenReturn(false); - try (MockedStatic fakeLag = mockStatic(FakeLag.class)) { - fakeLag.when(() -> FakeLag.sleep(anyInt())).thenAnswer(inv -> null); + ResponseEntity> response = submissionController.setLeetcodeUsername(auth, body); - ResponseEntity> response = submissionController.setLeetcodeUsername(auth, body); - - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertNotNull(response.getBody()); - assertTrue(response.getBody().isSuccess()); - assertEquals("Leetcode username has been set!", response.getBody().getMessage()); + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertNotNull(response.getBody()); + assertTrue(response.getBody().isSuccess()); + assertEquals("Leetcode username has been set!", response.getBody().getMessage()); - verify(user).setLeetcodeUsername("leetcodeUser"); - verify(user).setProfileUrl("avatar-url"); - verify(userRepository).updateUser(user); - } + verify(user).setLeetcodeUsername("leetcodeUser"); + verify(user).setProfileUrl("avatar-url"); + verify(userRepository).updateUser(user); } @Test @@ -200,17 +193,12 @@ void testGetCurrentPotdSuccess() { when(questionRepository.getQuestionBySlugAndUserId("two-sum", "abcdefg123456")) .thenReturn(Optional.empty()); - try (MockedStatic fakeLag = mockStatic(FakeLag.class)) { - fakeLag.when(() -> FakeLag.sleep(anyInt())).thenAnswer(inv -> null); + ResponseEntity> response = submissionController.getCurrentPotd(auth); - ResponseEntity> response = submissionController.getCurrentPotd(auth); - - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertTrue(response.getBody().isSuccess()); - assertEquals( - "Problem of the day has been fetched!", response.getBody().getMessage()); - assertNotNull(response.getBody().getPayload()); - } + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertTrue(response.getBody().isSuccess()); + assertEquals("Problem of the day has been fetched!", response.getBody().getMessage()); + assertNotNull(response.getBody().getPayload()); } @Test @@ -258,18 +246,13 @@ void testGetSubmissionBySubmissionIdSuccess() { when(questionRepository.getQuestionWithUserById("abc123")).thenReturn(Optional.of(question)); - try (MockedStatic fakeLag = mockStatic(FakeLag.class)) { - fakeLag.when(() -> FakeLag.sleep(anyInt())).thenAnswer(inv -> null); - - ResponseEntity> response = - submissionController.getSubmissionBySubmissionId(request, "abc123"); + ResponseEntity> response = + submissionController.getSubmissionBySubmissionId(request, "abc123"); - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertTrue(response.getBody().isSuccess()); - assertEquals( - "Problem of the day has been fetched!", response.getBody().getMessage()); - assertNotNull(response.getBody().getPayload()); - } + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertTrue(response.getBody().isSuccess()); + assertEquals("Problem of the day has been fetched!", response.getBody().getMessage()); + assertNotNull(response.getBody().getPayload()); verify(questionRepository).getQuestionWithUserById("abc123"); } diff --git a/src/main/java/org/patinanetwork/codebloom/common/lag/FakeLag.java b/src/test/java/org/patinanetwork/codebloom/common/lag/FakeLag.java similarity index 100% rename from src/main/java/org/patinanetwork/codebloom/common/lag/FakeLag.java rename to src/test/java/org/patinanetwork/codebloom/common/lag/FakeLag.java diff --git a/src/test/java/org/patinanetwork/codebloom/scheduled/leetcode/LeetcodeQuestionProcessServiceTest.java b/src/test/java/org/patinanetwork/codebloom/scheduled/leetcode/LeetcodeQuestionProcessServiceTest.java index fe3eb2fad..44217bcb0 100644 --- a/src/test/java/org/patinanetwork/codebloom/scheduled/leetcode/LeetcodeQuestionProcessServiceTest.java +++ b/src/test/java/org/patinanetwork/codebloom/scheduled/leetcode/LeetcodeQuestionProcessServiceTest.java @@ -7,7 +7,6 @@ import java.util.List; import java.util.Optional; -import java.util.concurrent.CompletableFuture; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -18,7 +17,6 @@ import org.patinanetwork.codebloom.common.db.models.question.QuestionDifficulty; import org.patinanetwork.codebloom.common.db.repos.job.JobRepository; import org.patinanetwork.codebloom.common.db.repos.question.QuestionRepository; -import org.patinanetwork.codebloom.common.dto.Empty; import org.patinanetwork.codebloom.common.time.StandardizedOffsetDateTime; import org.patinanetwork.codebloom.config.NoJdaRequired; import org.springframework.beans.factory.annotation.Autowired; @@ -182,45 +180,46 @@ void drainQueueValid() { service.drainQueue(); } - @Test - void maxAttemptsReached() { - - Question tempQuestion = Question.builder() - .userId(mockUserId) - .questionSlug("max-attempts-test-" + System.currentTimeMillis()) - .questionTitle("Max Attempts Test") - .questionDifficulty(QuestionDifficulty.Easy) - .questionNumber(99) - .questionLink("https://leetcode.com/problems/max-attempts-test/") - .description(Optional.of("Test for max attempts sentinel value")) - .pointsAwarded(Optional.of(120)) - .acceptanceRate(0.7f) - .submittedAt(java.time.LocalDateTime.now()) - .submissionId(Optional.of("99999")) - .build(); - - tempQuestion = questionRepository.createQuestion(tempQuestion); - - Job maxAttemptJob = Job.builder() - .questionId(tempQuestion.getId()) - .status(JobStatus.INCOMPLETE) - .nextAttemptAt(StandardizedOffsetDateTime.now().minusHours(1)) - .build(); - - jobRepository.createJob(maxAttemptJob); - - CompletableFuture future = service.drainQueue(); - future.join(); - - Job updatedJob = jobRepository.findJobById(maxAttemptJob.getId()); - assertNotNull(updatedJob); - assertEquals(JobStatus.COMPLETE, updatedJob.getStatus()); - - Question updatedQuestion = - questionRepository.getQuestionById(tempQuestion.getId()).orElseThrow(); - assertEquals("99999", updatedQuestion.getSubmissionId().orElseThrow()); - - jobRepository.deleteJobById(maxAttemptJob.getId()); - questionRepository.deleteQuestionById(tempQuestion.getId()); - } + // TODO: (TAN-32) re-enable + // @Test + // void maxAttemptsReached() { + // + // Question tempQuestion = Question.builder() + // .userId(mockUserId) + // .questionSlug("max-attempts-test-" + System.currentTimeMillis()) + // .questionTitle("Max Attempts Test") + // .questionDifficulty(QuestionDifficulty.Easy) + // .questionNumber(99) + // .questionLink("https://leetcode.com/problems/max-attempts-test/") + // .description(Optional.of("Test for max attempts sentinel value")) + // .pointsAwarded(Optional.of(120)) + // .acceptanceRate(0.7f) + // .submittedAt(java.time.LocalDateTime.now()) + // .submissionId(Optional.of("99999")) + // .build(); + // + // tempQuestion = questionRepository.createQuestion(tempQuestion); + // + // Job maxAttemptJob = Job.builder() + // .questionId(tempQuestion.getId()) + // .status(JobStatus.INCOMPLETE) + // .nextAttemptAt(StandardizedOffsetDateTime.now().minusHours(1)) + // .build(); + // + // jobRepository.createJob(maxAttemptJob); + // + // CompletableFuture future = service.drainQueue(); + // future.join(); + // + // Job updatedJob = jobRepository.findJobById(maxAttemptJob.getId()); + // assertNotNull(updatedJob); + // assertEquals(JobStatus.COMPLETE, updatedJob.getStatus()); + // + // Question updatedQuestion = + // questionRepository.getQuestionById(tempQuestion.getId()).orElseThrow(); + // assertEquals("99999", updatedQuestion.getSubmissionId().orElseThrow()); + // + // jobRepository.deleteJobById(maxAttemptJob.getId()); + // questionRepository.deleteQuestionById(tempQuestion.getId()); + // } }