Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@ public ResponseEntity<String> 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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Artefactik0Client.TestCase> tests =
artefactik0Client.getTests(problem.getExternalId());

Expand All @@ -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);
Expand Down
24 changes: 15 additions & 9 deletions src/test/java/com/codzilla/backend/Sandbox/SandboxTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.*;
Expand Down Expand Up @@ -50,6 +50,7 @@ public class SandboxTest {

private Problem problem;
private Artefactik0Client.TestCase testCase;
private Match match;

@BeforeEach
void setUp() {
Expand All @@ -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
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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");
}
Expand All @@ -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());
}
Expand All @@ -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);
Expand All @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading