Skip to content

Commit

Permalink
Merge pull request #405 from SWM-NM/feat/#400
Browse files Browse the repository at this point in the history
♻️ [REFACTOR] 코드 결과 Input, Output 수정
  • Loading branch information
aj4941 authored Oct 5, 2023
2 parents 4eb0825 + 7f9937f commit 80de347
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class TestDuringController {

@PostMapping("/output")
@Operation(summary = "코드 실행 결과값 반환", description = "사용자가 특정 코드를 실행할 경우 결과값을 제공합니다.")
public ResponseEntity<OutputDto> getOutputResult
public ResponseEntity<List<OutputDto>> getOutputResult
(@RequestBody TestInputData testInputData) throws Exception {
return new ResponseEntity<>(runCodeService.runCode(testInputData), HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
@Builder
public class OutputDto {
private String result; // 실행 성공 여부
private String output; // 코드 결과
private String errorOutput;
private Double runTime; // 실행 시간
private String output; // 실행 결과물
private Double executeTime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import lombok.*;

import java.util.List;

@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class TestInputData {
private String language;
private String code;
private String input;
private List<String> input;
private List<String> output;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
import org.springframework.stereotype.Service;
import swm_nm.morandi.domain.testDuring.dto.OutputDto;
import swm_nm.morandi.domain.testDuring.dto.TestInputData;
import com.fasterxml.jackson.core.type.TypeReference;
import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class RunCodeService {
public OutputDto runCode(TestInputData testInputData) throws Exception {
public List<OutputDto> runCode(TestInputData testInputData) throws Exception {

CloseableHttpClient httpClient = HttpClients.createDefault();
String url = "http://10.0.102.184:8080";
Expand All @@ -37,8 +40,8 @@ public OutputDto runCode(TestInputData testInputData) throws Exception {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
String responseJson = EntityUtils.toString(response.getEntity());
OutputDto outputDto = objectMapper.readValue(responseJson, OutputDto.class);
return outputDto;
List<OutputDto> outputDtos = objectMapper.readValue(responseJson, new TypeReference<List<OutputDto>>() {});
return outputDtos;
} else {
throw new Exception("HTTP request failed with status code: " + statusCode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
import swm_nm.morandi.domain.testDuring.dto.OutputDto;
import swm_nm.morandi.domain.testDuring.dto.TestInputData;
import swm_nm.morandi.domain.testDuring.service.RunCodeService;
import static org.assertj.core.api.Assertions.assertThat;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,81 +1,6 @@
package swm_nm.morandi.domain.testDuring.service;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
import swm_nm.morandi.domain.testDuring.dto.OutputDto;
import swm_nm.morandi.domain.testDuring.dto.TestInputData;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class RunCodeServiceTest {

@Autowired
private RunCodeService runCodeService;
@Test
@Transactional
void pythonRunCodeTest() throws Exception {
// given
TestInputData testInputData = new TestInputData();
testInputData.setCode(readFileContent("temp.py"));
testInputData.setInput(null);
testInputData.setLanguage("Python");

// when
OutputDto outputDto = runCodeService.runCode(testInputData);

// then
assertThat(outputDto.getOutput()).isEqualTo("168\n");
}

@Test
@Transactional
void cppRunCodeTest() throws Exception {
// given
TestInputData testInputData = new TestInputData();
testInputData.setCode(readFileContent("temp.cpp"));
testInputData.setInput("1 2");
testInputData.setLanguage("Cpp");

// when
OutputDto outputDto = runCodeService.runCode(testInputData);

// then
assertThat(outputDto.getOutput()).isEqualTo("3\n");
}

@Test
@Transactional
void javaRunCodeTest() throws Exception {
// given
TestInputData testInputData = new TestInputData();
testInputData.setCode(readFileContent("Main.java"));
testInputData.setInput("1 2");
testInputData.setLanguage("Java");

// when
OutputDto outputDto = runCodeService.runCode(testInputData);

// then
assertThat(outputDto.getOutput()).isEqualTo("3\n");
}

private String readFileContent(String filePath) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
StringBuilder content = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
content.append(line).append("\n");
}
return content.toString();
}
}
}

0 comments on commit 80de347

Please sign in to comment.