Skip to content

Commit cccd5f1

Browse files
committed
[Testing][JShellAPI] adding a second subtest for same endpoint with additional checks
1 parent 6ab35c4 commit cccd5f1

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

Diff for: JShellAPI/src/test/java/org/togetherjava/jshellapi/JShellApiTests.java

+39-14
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
import org.springframework.test.web.reactive.server.WebTestClient;
99

1010
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;
1114
import org.togetherjava.jshellapi.rest.ApiEndpoints;
1215

1316
import java.time.Duration;
17+
import java.util.List;
1418

1519
import static org.assertj.core.api.Assertions.assertThat;
1620

@@ -20,46 +24,67 @@
2024
*
2125
* @author Firas Regaieg
2226
*/
23-
@ContextConfiguration
27+
@ContextConfiguration(classes = Main.class)
2428
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
2529
public class JShellApiTests {
2630

2731
@Autowired
2832
private WebTestClient webTestClient;
2933

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-
3434
@Test
3535
@DisplayName("When posting code snippet, evaluate it then returns successfully result")
3636
public void evaluateCodeSnippetTest() {
3737

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) {
3865
final String endpoint =
39-
String.join("/", ApiEndpoints.BASE, ApiEndpoints.EVALUATE, TEST_EVALUATION_ID);
66+
String.join("/", ApiEndpoints.BASE, ApiEndpoints.EVALUATE, testEvalId);
4067

4168
JShellResult result = this.webTestClient.mutate()
4269
.responseTimeout(Duration.ofSeconds(6))
4370
.build()
4471
.post()
4572
.uri(endpoint)
46-
.bodyValue(TEST_CODE_INPUT)
73+
.bodyValue(codeInput)
4774
.exchange()
4875
.expectStatus()
4976
.isOk()
5077
.expectBody(JShellResult.class)
51-
.value(task -> assertThat(task).isNotNull())
78+
.value((JShellResult evalResult) -> assertThat(evalResult).isNotNull())
5279
.returnResult()
5380
.getResponseBody();
5481

5582
assertThat(result).isNotNull();
5683

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+
}
6386

87+
private static JShellResult getJShellResultDefaultInstance(JShellSnippetResult snippetResult) {
88+
return new JShellResult(List.of(snippetResult), null, false, "");
6489
}
6590
}

0 commit comments

Comments
 (0)