|
8 | 8 | import org.springframework.test.web.reactive.server.WebTestClient;
|
9 | 9 |
|
10 | 10 | import org.togetherjava.jshellapi.dto.JShellResult;
|
| 11 | +import org.togetherjava.jshellapi.dto.JShellSnippetResult; |
| 12 | +import org.togetherjava.jshellapi.dto.SnippetStatus; |
| 13 | +import org.togetherjava.jshellapi.dto.SnippetType; |
11 | 14 | import org.togetherjava.jshellapi.rest.ApiEndpoints;
|
12 | 15 |
|
13 | 16 | import java.time.Duration;
|
| 17 | +import java.util.List; |
14 | 18 |
|
15 | 19 | import static org.assertj.core.api.Assertions.assertThat;
|
16 | 20 |
|
|
20 | 24 | *
|
21 | 25 | * @author Firas Regaieg
|
22 | 26 | */
|
23 |
| -@ContextConfiguration |
| 27 | +@ContextConfiguration(classes = Main.class) |
24 | 28 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
25 | 29 | public class JShellApiTests {
|
26 | 30 |
|
27 | 31 | @Autowired
|
28 | 32 | private WebTestClient webTestClient;
|
29 | 33 |
|
30 |
| - private static final String TEST_EVALUATION_ID = "test"; |
31 |
| - private static final String TEST_CODE_INPUT = "2+2"; |
32 |
| - private static final String TEST_CODE_EXPECTED_OUTPUT = "4"; |
33 |
| - |
34 | 34 | @Test
|
35 | 35 | @DisplayName("When posting code snippet, evaluate it then returns successfully result")
|
36 | 36 | public void evaluateCodeSnippetTest() {
|
37 | 37 |
|
| 38 | + final String testEvalId = "test"; |
| 39 | + |
| 40 | + // -- performing a first code snippet execution |
| 41 | + |
| 42 | + final String firstCodeExpression = "int a = 2+2;"; |
| 43 | + |
| 44 | + final JShellSnippetResult firstCodeSnippet = new JShellSnippetResult(SnippetStatus.VALID, |
| 45 | + SnippetType.ADDITION, 1, firstCodeExpression, "4"); |
| 46 | + final JShellResult firstCodeExpectedResult = |
| 47 | + getJShellResultDefaultInstance(firstCodeSnippet); |
| 48 | + |
| 49 | + assertThat(testEval(testEvalId, firstCodeExpression)).isEqualTo(firstCodeExpectedResult); |
| 50 | + |
| 51 | + // -- performing a second code snippet execution |
| 52 | + |
| 53 | + final String secondCodeExpression = "a * 2"; |
| 54 | + |
| 55 | + final JShellSnippetResult secondCodeSnippet = new JShellSnippetResult(SnippetStatus.VALID, |
| 56 | + SnippetType.ADDITION, 2, secondCodeExpression, "8"); |
| 57 | + |
| 58 | + final JShellResult secondCodeExpectedResult = |
| 59 | + getJShellResultDefaultInstance(secondCodeSnippet); |
| 60 | + |
| 61 | + assertThat(testEval(testEvalId, secondCodeExpression)).isEqualTo(secondCodeExpectedResult); |
| 62 | + } |
| 63 | + |
| 64 | + private JShellResult testEval(String testEvalId, String codeInput) { |
38 | 65 | final String endpoint =
|
39 |
| - String.join("/", ApiEndpoints.BASE, ApiEndpoints.EVALUATE, TEST_EVALUATION_ID); |
| 66 | + String.join("/", ApiEndpoints.BASE, ApiEndpoints.EVALUATE, testEvalId); |
40 | 67 |
|
41 | 68 | JShellResult result = this.webTestClient.mutate()
|
42 | 69 | .responseTimeout(Duration.ofSeconds(6))
|
43 | 70 | .build()
|
44 | 71 | .post()
|
45 | 72 | .uri(endpoint)
|
46 |
| - .bodyValue(TEST_CODE_INPUT) |
| 73 | + .bodyValue(codeInput) |
47 | 74 | .exchange()
|
48 | 75 | .expectStatus()
|
49 | 76 | .isOk()
|
50 | 77 | .expectBody(JShellResult.class)
|
51 |
| - .value(task -> assertThat(task).isNotNull()) |
| 78 | + .value((JShellResult evalResult) -> assertThat(evalResult).isNotNull()) |
52 | 79 | .returnResult()
|
53 | 80 | .getResponseBody();
|
54 | 81 |
|
55 | 82 | assertThat(result).isNotNull();
|
56 | 83 |
|
57 |
| - boolean isValidResult = result.snippetsResults() |
58 |
| - .stream() |
59 |
| - .filter(res -> res.result() != null) |
60 |
| - .anyMatch(res -> res.result().equals(TEST_CODE_EXPECTED_OUTPUT)); |
61 |
| - |
62 |
| - assertThat(isValidResult).isTrue(); |
| 84 | + return result; |
| 85 | + } |
63 | 86 |
|
| 87 | + private static JShellResult getJShellResultDefaultInstance(JShellSnippetResult snippetResult) { |
| 88 | + return new JShellResult(List.of(snippetResult), null, false, ""); |
64 | 89 | }
|
65 | 90 | }
|
0 commit comments