1import static io.restassured.RestAssured.given;
2import static org.junit.jupiter.api.Assertions.assertTrue;
3
4import com.google.gson.JsonObject;
5import com.google.gson.JsonParser;
6import java.nio.file.Files;
7import java.nio.file.Path;
8import java.util.List;
9import java.util.regex.Pattern;
10import org.junit.jupiter.api.Test;
11
12// Your lesson 15 harness in Java: every case in cases.jsonl becomes a test.
13public class CasesTest {
14static final String URL = System.getenv("OPENAI_BASE_URL") + "/chat/completions";
15static final String KEY = System.getenv("OPENAI_API_KEY");
16
17static String ask(String question) {
18JsonObject msg = new JsonObject();
19msg.addProperty("role", "user");
20msg.addProperty("content", question);
21JsonObject body = new JsonObject();
22body.addProperty("model", "acme-practice-1");
23body.add("messages", JsonParser.parseString("[" + msg + "]"));
24return given().header("Authorization", "Bearer " + KEY).contentType("application/json").body(body.toString())
25.when().post(URL).then().statusCode(200).extract().path("choices[0].message.content");
26}
27
28static List<JsonObject> cases() throws Exception {
29return Files.readAllLines(Path.of("cases.jsonl")).stream().filter(l -> !l.isBlank())
30.map(l -> JsonParser.parseString(l).getAsJsonObject()).toList();
31}
32
33@Test
34void firstCase() throws Exception {
35JsonObject c = cases().get(0);
36String reply = ask(c.get("question").getAsString());
37assertTrue(Pattern.compile(c.get("must_match").getAsString(), Pattern.CASE_INSENSITIVE).matcher(reply).find(), reply);
38}
39
40// Run every case, not just the first, and apply must_not_match when a case has one.
41}
42