|
| 1 | +package org.togetherjava.jshellapi; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.DisplayName; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.boot.test.context.SpringBootTest; |
| 7 | +import org.springframework.test.context.ActiveProfiles; |
| 8 | +import org.springframework.test.context.ContextConfiguration; |
| 9 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 10 | + |
| 11 | +import org.togetherjava.jshellapi.dto.JShellResult; |
| 12 | +import org.togetherjava.jshellapi.dto.JShellSnippetResult; |
| 13 | +import org.togetherjava.jshellapi.dto.SnippetStatus; |
| 14 | +import org.togetherjava.jshellapi.dto.SnippetType; |
| 15 | +import org.togetherjava.jshellapi.rest.ApiEndpoints; |
| 16 | + |
| 17 | +import java.time.Duration; |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | + |
| 22 | +/** |
| 23 | + * Integrates tests for JShellAPI. |
| 24 | + */ |
| 25 | +@ActiveProfiles("testing") |
| 26 | +@ContextConfiguration(classes = Main.class) |
| 27 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 28 | +public class JShellApiTests { |
| 29 | + |
| 30 | + @Autowired |
| 31 | + private WebTestClient webTestClient; |
| 32 | + |
| 33 | + @Autowired |
| 34 | + private Config testsConfig; |
| 35 | + |
| 36 | + @Test |
| 37 | + @DisplayName("When posting code snippet, evaluate it then return successfully result") |
| 38 | + public void evaluateCodeSnippetTest() { |
| 39 | + |
| 40 | + final String testEvalId = "test"; |
| 41 | + |
| 42 | + // -- first code snippet eval |
| 43 | + executeCodeEvalTest(testEvalId, "int a = 2+2;", 1, "4"); |
| 44 | + |
| 45 | + // -- second code snippet eval |
| 46 | + executeCodeEvalTest(testEvalId, "a * 2", 2, "8"); |
| 47 | + } |
| 48 | + |
| 49 | + private void executeCodeEvalTest(String evalId, String codeSnippet, int expectedId, |
| 50 | + String expectedResult) { |
| 51 | + final JShellSnippetResult jshellCodeSnippet = new JShellSnippetResult(SnippetStatus.VALID, |
| 52 | + SnippetType.ADDITION, expectedId, codeSnippet, expectedResult); |
| 53 | + |
| 54 | + assertThat(testEval(evalId, codeSnippet)) |
| 55 | + .isEqualTo(new JShellResult(List.of(jshellCodeSnippet), null, false, "")); |
| 56 | + } |
| 57 | + |
| 58 | + private JShellResult testEval(String testEvalId, String codeInput) { |
| 59 | + final String endpoint = |
| 60 | + String.join("/", ApiEndpoints.BASE, ApiEndpoints.EVALUATE, testEvalId); |
| 61 | + |
| 62 | + return this.webTestClient.mutate() |
| 63 | + .responseTimeout(Duration.ofSeconds(testsConfig.evalTimeoutSeconds())) |
| 64 | + .build() |
| 65 | + .post() |
| 66 | + .uri(endpoint) |
| 67 | + .bodyValue(codeInput) |
| 68 | + .exchange() |
| 69 | + .expectStatus() |
| 70 | + .isOk() |
| 71 | + .expectBody(JShellResult.class) |
| 72 | + .value((JShellResult evalResult) -> assertThat(evalResult).isNotNull()) |
| 73 | + .returnResult() |
| 74 | + .getResponseBody(); |
| 75 | + } |
| 76 | +} |
0 commit comments