Skip to content

Commit e7c2450

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

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

JShellAPI/src/test/java/org/togetherjava/jshellapi/JShellApiTests.java

Lines changed: 37 additions & 14 deletions
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,65 @@
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 = getJShellResultDefaultInstance(firstCodeSnippet);
47+
48+
assertThat(testEval(testEvalId, firstCodeExpression)).isEqualTo(firstCodeExpectedResult);
49+
50+
// -- performing a second code snippet execution
51+
52+
final String secondCodeExpression = "a * 2";
53+
54+
final JShellSnippetResult secondCodeSnippet = new JShellSnippetResult(SnippetStatus.VALID,
55+
SnippetType.ADDITION, 2, secondCodeExpression, "8");
56+
57+
final JShellResult secondCodeExpectedResult = getJShellResultDefaultInstance(secondCodeSnippet);
58+
59+
assertThat(testEval(testEvalId, secondCodeExpression)).isEqualTo(secondCodeExpectedResult);
60+
}
61+
62+
private JShellResult testEval(String testEvalId, String codeInput) {
3863
final String endpoint =
39-
String.join("/", ApiEndpoints.BASE, ApiEndpoints.EVALUATE, TEST_EVALUATION_ID);
64+
String.join("/", ApiEndpoints.BASE, ApiEndpoints.EVALUATE, testEvalId);
4065

4166
JShellResult result = this.webTestClient.mutate()
4267
.responseTimeout(Duration.ofSeconds(6))
4368
.build()
4469
.post()
4570
.uri(endpoint)
46-
.bodyValue(TEST_CODE_INPUT)
71+
.bodyValue(codeInput)
4772
.exchange()
4873
.expectStatus()
4974
.isOk()
5075
.expectBody(JShellResult.class)
51-
.value(task -> assertThat(task).isNotNull())
76+
.value((JShellResult evalResult) -> assertThat(evalResult).isNotNull())
5277
.returnResult()
5378
.getResponseBody();
5479

5580
assertThat(result).isNotNull();
5681

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();
82+
return result;
83+
}
6384

85+
private static JShellResult getJShellResultDefaultInstance(JShellSnippetResult snippetResult) {
86+
return new JShellResult(List.of(snippetResult), null, false, "");
6487
}
6588
}

0 commit comments

Comments
 (0)