<commit_before>package pingis.services.sandbox;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.times;
import static org.mockito.BDDMockito.verify;
import static org.mockito.BDDMockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import pingis.entities.Task;
import pingis.entities.TaskInstance;
import pingis.entities.TaskType;
import pingis.entities.sandbox.Logs;
import pingis.entities.sandbox.ResultStatus;
import pingis.entities.sandbox.Submission;
import pingis.entities.sandbox.SubmissionStatus;
import pingis.entities.sandbox.TestOutput;
import pingis.repositories.sandbox.SubmissionRepository;
import pingis.services.TaskInstanceService;

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {SandboxService.class})
public class SandboxServiceTest {

  @MockBean
  private TaskInstanceService taskInstanceService;

  @MockBean
  private SubmissionSenderService senderService;

  @MockBean
  private SubmissionPackagingService packagingService;

  @MockBean
  private SubmissionRepository submissionRepository;

  @MockBean
  private SimpMessagingTemplate messagingTemplate;

  @Autowired
  private SandboxService sandboxService;

  private Submission submission;
  private UUID submissionId;
  private ArgumentCaptor<Submission> submissionArgumentCaptor;

  private String generateTestOutput(ResultStatus status) throws JsonProcessingException {
    TestOutput top = new TestOutput();
    top.setStatus(status);
    top.setTestResults(new ArrayList<>());

    Logs logs = new Logs();
    logs.setStderr("stderr".getBytes());
    logs.setStdout("stdout".getBytes());
    top.setLogs(logs);

    return new ObjectMapper().writeValueAsString(top);
  }

  private void generateSubmission() {
    submission = new Submission();
    submissionId = UUID.randomUUID();

    submission.setId(submissionId);
    submission.setStatus(SubmissionStatus.PENDING);

    TaskInstance taskInstance = new TaskInstance(null, "code",
        new Task(0, TaskType.TEST, null, "name", "desc",
            "code", 0, 0));
    submission.setTaskInstance(taskInstance);
  }

  @Before
  public void setup() {
    generateSubmission();
    submissionArgumentCaptor = ArgumentCaptor.forClass(Submission.class);
  }

  private boolean updateSubmission(UUID submissionId, ResultStatus status) throws IOException {
    String testOutput = generateTestOutput(status);

    return sandboxService.updateSubmissionResult(submissionId, testOutput,
        "stdout", "stderr", "validations", "vmLog",
        "FINISHED", 0);
  }

  private void checkSubmission(UUID submissionId, Submission actual) {
    assertEquals("stdout", actual.getStdout());
    assertEquals("stderr", actual.getStderr());
    assertEquals("validations", actual.getValidations());
    assertEquals("vmLog", actual.getVmLog());
    assertEquals(SubmissionStatus.FINISHED, actual.getStatus());
    assertEquals(0, (int) actual.getExitCode());
    assertEquals(submissionId, actual.getId());
  }

  @Test
  public void doubleUpdateResult() throws Exception {
    when(submissionRepository.findById(submissionId)).thenReturn(Optional.of(submission));

    boolean firstResult = updateSubmission(submissionId, ResultStatus.PASSED);
    boolean secondResult = updateSubmission(submissionId, ResultStatus.PASSED);

    verify(submissionRepository).save(any(Submission.class));

    assertTrue(firstResult);
    assertFalse(secondResult);

    verify(submissionRepository, times(2)).findById(submissionId);
    verifyNoMoreInteractions(submissionRepository);
  }

  @Test
  public void nonExistentUpdateResult() throws Exception {
    when(submissionRepository.findById(submissionId)).thenReturn(Optional.empty());

    boolean result = updateSubmission(submissionId, null);

    assertFalse(result);
    verify(submissionRepository, times(1)).findById(submissionId);
    verifyNoMoreInteractions(submissionRepository);
  }

  @Test
  public void successfullyUpdateResult() throws Exception {
    when(submissionRepository.findById(submissionId)).thenReturn(Optional.of(submission));

    boolean result = updateSubmission(submissionId, ResultStatus.TESTS_FAILED);

    verify(submissionRepository).save(submissionArgumentCaptor.capture());

    assertTrue(result);
    checkSubmission(submissionId, submissionArgumentCaptor.getValue());

    verify(submissionRepository).findById(submissionId);
    verify(taskInstanceService).markAsDone(submission.getTaskInstance());
    verifyNoMoreInteractions(submissionRepository, taskInstanceService);
  }

  @Test
  public void updateResultWithCompileFailure() throws Exception {
    when(submissionRepository.findById(submissionId)).thenReturn(Optional.of(submission));

    boolean result = updateSubmission(submissionId, ResultStatus.COMPILE_FAILED);

    verify(submissionRepository).save(submissionArgumentCaptor.capture());

    assertTrue(result);
    checkSubmission(submissionId, submissionArgumentCaptor.getValue());

    verify(submissionRepository).findById(submissionId);<commit_msg>Modified a test<commit_after>package pingis.services.sandbox;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.times;
import static org.mockito.BDDMockito.verify;
import static org.mockito.BDDMockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import pingis.entities.Task;
import pingis.entities.TaskInstance;
import pingis.entities.TaskType;
import pingis.entities.sandbox.Logs;
import pingis.entities.sandbox.ResultStatus;
import pingis.entities.sandbox.Submission;
import pingis.entities.sandbox.SubmissionStatus;
import pingis.entities.sandbox.TestOutput;
import pingis.repositories.sandbox.SubmissionRepository;
import pingis.services.TaskInstanceService;

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {SandboxService.class})
public class SandboxServiceTest {

  @MockBean
  private TaskInstanceService taskInstanceService;

  @MockBean
  private SubmissionSenderService senderService;

  @MockBean
  private SubmissionPackagingService packagingService;

  @MockBean
  private SubmissionRepository submissionRepository;

  @MockBean
  private SimpMessagingTemplate messagingTemplate;

  @Autowired
  private SandboxService sandboxService;

  private Submission submission;
  private UUID submissionId;
  private ArgumentCaptor<Submission> submissionArgumentCaptor;

  private String generateTestOutput(ResultStatus status) throws JsonProcessingException {
    TestOutput top = new TestOutput();
    top.setStatus(status);
    top.setTestResults(new ArrayList<>());

    Logs logs = new Logs();
    logs.setStderr("stderr".getBytes());
    logs.setStdout("stdout".getBytes());
    top.setLogs(logs);

    return new ObjectMapper().writeValueAsString(top);
  }

  private void generateSubmission() {
    submission = new Submission();
    submissionId = UUID.randomUUID();

    submission.setId(submissionId);
    submission.setStatus(SubmissionStatus.PENDING);

    TaskInstance taskInstance = new TaskInstance(null, "code",
        new Task(0, TaskType.TEST, null, "name", "desc",
            "code", 0, 0));
    submission.setTaskInstance(taskInstance);
  }

  @Before
  public void setup() {
    generateSubmission();
    submissionArgumentCaptor = ArgumentCaptor.forClass(Submission.class);
  }

  private boolean updateSubmission(UUID submissionId, ResultStatus status) throws IOException {
    String testOutput = generateTestOutput(status);

    return sandboxService.updateSubmissionResult(submissionId, testOutput,
        "stdout", "stderr", "validations", "vmLog",
        "FINISHED", 0);
  }

  private void checkSubmission(UUID submissionId, Submission actual) {
    assertEquals("stdout", actual.getStdout());
    assertEquals("stderr", actual.getStderr());
    assertEquals("validations", actual.getValidations());
    assertEquals("vmLog", actual.getVmLog());
    assertEquals(SubmissionStatus.FINISHED, actual.getStatus());
    assertEquals(0, (int) actual.getExitCode());
    assertEquals(submissionId, actual.getId());
  }

  @Test
  public void doubleUpdateResult() throws Exception {
    when(submissionRepository.findById(submissionId)).thenReturn(Optional.of(submission));

    boolean firstResult = updateSubmission(submissionId, ResultStatus.PASSED);
    boolean secondResult = updateSubmission(submissionId, ResultStatus.PASSED);

    verify(submissionRepository).save(any(Submission.class));

    assertTrue(firstResult);
    assertFalse(secondResult);

    verify(submissionRepository, times(2)).findById(submissionId);
    verifyNoMoreInteractions(submissionRepository);
  }

  @Test
  public void nonExistentUpdateResult() throws Exception {
    when(submissionRepository.findById(submissionId)).thenReturn(Optional.empty());

    boolean result = updateSubmission(submissionId, null);

    assertFalse(result);
    verify(submissionRepository, times(1)).findById(submissionId);
    verifyNoMoreInteractions(submissionRepository);
  }

  @Test
  public void successfullyUpdateResult() throws Exception {
    when(submissionRepository.findById(submissionId)).thenReturn(Optional.of(submission));

    boolean result = updateSubmission(submissionId, ResultStatus.TESTS_FAILED);

    verify(submissionRepository).save(submissionArgumentCaptor.capture());

    assertTrue(result);
    checkSubmission(submissionId, submissionArgumentCaptor.getValue());

    verify(submissionRepository).findById(submissionId);
    verify(taskInstanceService).markAsDone(submission.getTaskInstance());
    verifyNoMoreInteractions(submissionRepository, taskInstanceService);
  }

  @Test
  public void updateResultWithTestCompileFailure() throws Exception {
    when(submissionRepository.findById(submissionId)).thenReturn(Optional.of(submission));

    boolean result = updateSubmission(submissionId, ResultStatus.COMPILE_FAILED);

    verify(submissionRepository).save(submissionArgumentCaptor.capture());

    assertTrue(result);
    checkSubmission(submissionId, submissionArgumentCaptor.getValue());

    verify(submissionRepository).findById(submissionId);
    verify(taskInstanceService).markAsDone(any());
{'fraction_non_alphanumeric': 0.09945309213294068, 'fraction_numerical': 0.0013462347496844761, 'mean_word_length': 6.062388591800357, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6201206', 'n_tokens_mistral': 3312, 'n_tokens_neox': 3223, 'n_words': 576}