From 3765c13ac74ccb97831dc5d91762f945be2d22a8 Mon Sep 17 00:00:00 2001 From: toximu Date: Mon, 29 Jun 2026 18:48:50 +0300 Subject: [PATCH] fix submitting solution and tests --- .../judge/problem/ProblemController.java | 4 +--- .../backend/judge/problem/ProblemService.java | 16 +++++++++---- .../codzilla/backend/Sandbox/SandboxTest.java | 24 ++++++++++++------- .../controller/ProblemControllerFileTest.java | 2 +- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/codzilla/backend/judge/problem/ProblemController.java b/src/main/java/com/codzilla/backend/judge/problem/ProblemController.java index 06f1a0a..ab27a37 100644 --- a/src/main/java/com/codzilla/backend/judge/problem/ProblemController.java +++ b/src/main/java/com/codzilla/backend/judge/problem/ProblemController.java @@ -65,11 +65,9 @@ public ResponseEntity submitFileByMatch( } Long problemId = match.getProblem().getId(); - String languageStr = match.getOptions().get(Category.Language); - int languageId = Enum.valueOf(Language.class, languageStr).getValue(); UUID userId = user.getId(); - String result = problemService.submitSolution(userId, matchId , problemId, sourceCode, languageId); + String result = problemService.submitSolution(userId, match, problemId, sourceCode); return ResponseEntity.ok(result); } diff --git a/src/main/java/com/codzilla/backend/judge/problem/ProblemService.java b/src/main/java/com/codzilla/backend/judge/problem/ProblemService.java index 16a4c5a..6ba3e1c 100644 --- a/src/main/java/com/codzilla/backend/judge/problem/ProblemService.java +++ b/src/main/java/com/codzilla/backend/judge/problem/ProblemService.java @@ -1,5 +1,8 @@ package com.codzilla.backend.judge.problem; +import com.codzilla.backend.MatchRoom.Match; +import com.codzilla.backend.PreMatch.model.Category; +import com.codzilla.backend.PreMatch.model.Language; import com.codzilla.backend.PreMatch.model.ProblemType; import com.codzilla.backend.S3.S3Repository; import com.codzilla.backend.judge.client.Artefactik0Client; @@ -61,18 +64,18 @@ public Problem registerSqlProblem(RegisterSqlProblemRequest request) { } - public String submitSolution(UUID userId, UUID matchId , Long problemId, String sourceCode, int languageId) { + public String submitSolution(UUID userId, Match match , Long problemId, String sourceCode) { Problem problem = problemRepository.findById(problemId) .orElseThrow(() -> new RuntimeException("Problem not found: " + problemId)); return switch (problem.getType()) { - case ALGORITHM, MATH, DATA_STRUCTURES -> submitAlgo(userId, problem, sourceCode, languageId , matchId); - case SQL -> submitSql(userId, problem, sourceCode , matchId); + case ALGORITHM, MATH, DATA_STRUCTURES -> submitAlgo(userId, problem, sourceCode, match); + case SQL -> submitSql(userId, problem, sourceCode , match.getId()); }; } - private String submitAlgo(UUID userId, Problem problem, String sourceCode, int languageId , UUID matchId) { + private String submitAlgo(UUID userId, Problem problem, String sourceCode, Match match) { List tests = artefactik0Client.getTests(problem.getExternalId()); @@ -81,10 +84,13 @@ private String submitAlgo(UUID userId, Problem problem, String sourceCode, int l "No tests in Artefactik0 for problem " + problem.getId()); } + String languageStr = match.getOptions().get(Category.Language); + int languageId = Enum.valueOf(Language.class, languageStr).getValue(); + Submission sub = new Submission(); sub.setProblemId(problem.getId()); sub.setUserId(userId); - sub.setMatchId(matchId); + sub.setMatchId(match.getId()); sub.setLanguageId(languageId); sub.setStatus(Submission.Status.IN_QUEUE); Submission saved = submissionRepository.save(sub); diff --git a/src/test/java/com/codzilla/backend/Sandbox/SandboxTest.java b/src/test/java/com/codzilla/backend/Sandbox/SandboxTest.java index 3f5657f..87ef2d1 100644 --- a/src/test/java/com/codzilla/backend/Sandbox/SandboxTest.java +++ b/src/test/java/com/codzilla/backend/Sandbox/SandboxTest.java @@ -1,5 +1,7 @@ package com.codzilla.backend.Sandbox; +import com.codzilla.backend.MatchRoom.Match; +import com.codzilla.backend.PreMatch.model.Category; import com.codzilla.backend.PreMatch.model.ProblemType; import com.codzilla.backend.judge.client.Artefactik0Client; import com.codzilla.backend.judge.judge0.Judge0Client; @@ -11,11 +13,9 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.testcontainers.shaded.org.checkerframework.checker.units.qual.C; -import java.util.Collections; -import java.util.List; -import java.util.Optional; -import java.util.UUID; +import java.util.*; import static org.assertj.core.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; @@ -50,6 +50,7 @@ public class SandboxTest { private Problem problem; private Artefactik0Client.TestCase testCase; + private Match match; @BeforeEach void setUp() { @@ -62,6 +63,9 @@ void setUp() { testCase = new Artefactik0Client.TestCase(); testCase.setInput("1 2"); testCase.setOutput("3"); + + match = new Match(UUID.randomUUID(), UUID.randomUUID()); + match.setOptions(Map.of(Category.Language, "PY")); } @Test @@ -91,7 +95,7 @@ void submitSolution_shouldThrowWhenProblemNotFound() { when(problemRepository.findById(99L)).thenReturn(Optional.empty()); assertThatThrownBy(() -> - problemService.submitSolution(UUID.randomUUID(), null, 99L, "code", 71)) + problemService.submitSolution(UUID.randomUUID(), null, 99L, "code")) .isInstanceOf(RuntimeException.class) .hasMessageContaining("Problem not found"); } @@ -124,7 +128,7 @@ void submitSolution_shouldReturnToken() { when(submissionRepository.save(any())).thenReturn(savedSub); when(submissionTestRepository.save(any())).thenAnswer(i -> i.getArgument(0)); - String result = problemService.submitSolution(UUID.randomUUID(), null, 1L, "print(3)", 71); + String result = problemService.submitSolution(UUID.randomUUID(), match, 1L, "print(3)"); assertThat(result).isEqualTo("42"); } @@ -147,7 +151,9 @@ void submitSolution_shouldRunAllTests() { when(submissionRepository.save(any())).thenReturn(savedSub); when(submissionTestRepository.save(any())).thenAnswer(i -> i.getArgument(0)); - problemService.submitSolution(UUID.randomUUID(), null, 1L, "print(3)", 71); + + + problemService.submitSolution(UUID.randomUUID(), match, 1L, "print(3)"); verify(judge0Client, times(2)).submitAsync(anyString(), anyInt(), anyString(), isNull()); } @@ -170,7 +176,7 @@ void submitSolution_shouldSaveSubmissionWithInQueueStatus() { }); when(submissionTestRepository.save(any())).thenAnswer(i -> i.getArgument(0)); - problemService.submitSolution(UUID.randomUUID(), null, 1L, "print(3)", 71); + problemService.submitSolution(UUID.randomUUID(), match, 1L, "print(3)"); assertThat(saved[0]).isNotNull(); assertThat(saved[0].getStatus()).isEqualTo(Submission.Status.IN_QUEUE); @@ -190,7 +196,7 @@ void submitSolution_shouldHandleJudge0Failure() { when(submissionRepository.save(any())).thenReturn(savedSub); assertThatThrownBy(() -> - problemService.submitSolution(UUID.randomUUID(), null, 1L, "print(3)", 71)) + problemService.submitSolution(UUID.randomUUID(), match, 1L, "print(3)")) .isInstanceOf(RuntimeException.class) .hasMessageContaining("Judge0 unavailable"); } diff --git a/src/test/java/com/codzilla/backend/Sandbox/controller/ProblemControllerFileTest.java b/src/test/java/com/codzilla/backend/Sandbox/controller/ProblemControllerFileTest.java index f63b5c0..1055073 100644 --- a/src/test/java/com/codzilla/backend/Sandbox/controller/ProblemControllerFileTest.java +++ b/src/test/java/com/codzilla/backend/Sandbox/controller/ProblemControllerFileTest.java @@ -99,7 +99,7 @@ void submitFile_shouldWork() throws Exception { ); when(matchService.getMatchById(matchId)).thenReturn(match); - when(problemService.submitSolution(any(UUID.class), any(UUID.class), anyLong(), anyString(), anyInt())) + when(problemService.submitSolution(any(UUID.class), any(Match.class), anyLong(), anyString())) .thenReturn("Submitted!"); mockMvc.perform(multipart("/problems/submit/file")