From 7f9937ff61a6f2ec00b3f5f42b7b1d2949c17114 Mon Sep 17 00:00:00 2001 From: "aj4941@naver.com" Date: Fri, 6 Oct 2023 00:58:13 +0900 Subject: [PATCH] =?UTF-8?q?:recycle:=20[REFACTOR]=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EA=B2=B0=EA=B3=BC=20Input,=20Output=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/TestDuringController.java | 2 +- .../domain/testDuring/dto/OutputDto.java | 5 +- .../domain/testDuring/dto/TestInputData.java | 5 +- .../testDuring/service/RunCodeService.java | 9 ++- .../controller/TestDuringControllerTest.java | 1 - .../service/RunCodeServiceTest.java | 75 ------------------- 6 files changed, 13 insertions(+), 84 deletions(-) diff --git a/src/main/java/swm_nm/morandi/domain/testDuring/controller/TestDuringController.java b/src/main/java/swm_nm/morandi/domain/testDuring/controller/TestDuringController.java index 2acdc18c..8478b017 100644 --- a/src/main/java/swm_nm/morandi/domain/testDuring/controller/TestDuringController.java +++ b/src/main/java/swm_nm/morandi/domain/testDuring/controller/TestDuringController.java @@ -29,7 +29,7 @@ public class TestDuringController { @PostMapping("/output") @Operation(summary = "코드 실행 결과값 반환", description = "사용자가 특정 코드를 실행할 경우 결과값을 제공합니다.") - public ResponseEntity getOutputResult + public ResponseEntity> getOutputResult (@RequestBody TestInputData testInputData) throws Exception { return new ResponseEntity<>(runCodeService.runCode(testInputData), HttpStatus.OK); } diff --git a/src/main/java/swm_nm/morandi/domain/testDuring/dto/OutputDto.java b/src/main/java/swm_nm/morandi/domain/testDuring/dto/OutputDto.java index b6e57061..ffe8c24c 100644 --- a/src/main/java/swm_nm/morandi/domain/testDuring/dto/OutputDto.java +++ b/src/main/java/swm_nm/morandi/domain/testDuring/dto/OutputDto.java @@ -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; } diff --git a/src/main/java/swm_nm/morandi/domain/testDuring/dto/TestInputData.java b/src/main/java/swm_nm/morandi/domain/testDuring/dto/TestInputData.java index 9c0be15f..0c79e723 100644 --- a/src/main/java/swm_nm/morandi/domain/testDuring/dto/TestInputData.java +++ b/src/main/java/swm_nm/morandi/domain/testDuring/dto/TestInputData.java @@ -2,6 +2,8 @@ import lombok.*; +import java.util.List; + @Getter @Setter @NoArgsConstructor @AllArgsConstructor @@ -9,5 +11,6 @@ public class TestInputData { private String language; private String code; - private String input; + private List input; + private List output; } \ No newline at end of file diff --git a/src/main/java/swm_nm/morandi/domain/testDuring/service/RunCodeService.java b/src/main/java/swm_nm/morandi/domain/testDuring/service/RunCodeService.java index 76a26f21..3703155d 100644 --- a/src/main/java/swm_nm/morandi/domain/testDuring/service/RunCodeService.java +++ b/src/main/java/swm_nm/morandi/domain/testDuring/service/RunCodeService.java @@ -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 runCode(TestInputData testInputData) throws Exception { CloseableHttpClient httpClient = HttpClients.createDefault(); String url = "http://10.0.102.184:8080"; @@ -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 outputDtos = objectMapper.readValue(responseJson, new TypeReference>() {}); + return outputDtos; } else { throw new Exception("HTTP request failed with status code: " + statusCode); } diff --git a/src/test/java/swm_nm/morandi/domain/testDuring/controller/TestDuringControllerTest.java b/src/test/java/swm_nm/morandi/domain/testDuring/controller/TestDuringControllerTest.java index dc5fec51..888b0d5a 100644 --- a/src/test/java/swm_nm/morandi/domain/testDuring/controller/TestDuringControllerTest.java +++ b/src/test/java/swm_nm/morandi/domain/testDuring/controller/TestDuringControllerTest.java @@ -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; diff --git a/src/test/java/swm_nm/morandi/domain/testDuring/service/RunCodeServiceTest.java b/src/test/java/swm_nm/morandi/domain/testDuring/service/RunCodeServiceTest.java index c13d5360..f4dbb552 100644 --- a/src/test/java/swm_nm/morandi/domain/testDuring/service/RunCodeServiceTest.java +++ b/src/test/java/swm_nm/morandi/domain/testDuring/service/RunCodeServiceTest.java @@ -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(); - } - } } \ No newline at end of file