|
1 | 1 | package com.sap.ai.sdk.foundationmodels.openai; |
2 | 2 |
|
3 | 3 | import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 4 | +import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; |
| 5 | +import static com.github.tomakehurst.wiremock.client.WireMock.badRequest; |
4 | 6 | import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; |
| 7 | +import static com.github.tomakehurst.wiremock.client.WireMock.noContent; |
| 8 | +import static com.github.tomakehurst.wiremock.client.WireMock.okXml; |
5 | 9 | import static com.github.tomakehurst.wiremock.client.WireMock.post; |
| 10 | +import static com.github.tomakehurst.wiremock.client.WireMock.serverError; |
6 | 11 | import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; |
7 | 12 | import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; |
8 | 13 | import static org.mockito.ArgumentMatchers.any; |
9 | 14 | import static org.mockito.Mockito.doReturn; |
10 | 15 | import static org.mockito.Mockito.mock; |
11 | 16 | import static org.mockito.Mockito.spy; |
12 | 17 |
|
| 18 | +import com.fasterxml.jackson.core.JsonParseException; |
13 | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
14 | 20 | import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; |
15 | 21 | import com.github.tomakehurst.wiremock.junit5.WireMockTest; |
| 22 | +import com.github.tomakehurst.wiremock.stubbing.Scenario; |
16 | 23 | import com.sap.cloud.sdk.cloudplatform.connectivity.ApacheHttpClient5Accessor; |
17 | 24 | import com.sap.cloud.sdk.cloudplatform.connectivity.ApacheHttpClient5Cache; |
18 | 25 | import com.sap.cloud.sdk.cloudplatform.connectivity.DefaultHttpDestination; |
19 | 26 | import java.io.IOException; |
20 | 27 | import java.io.InputStream; |
21 | 28 | import java.util.Objects; |
22 | 29 | import java.util.function.Function; |
| 30 | +import javax.annotation.Nonnull; |
23 | 31 | import org.apache.hc.client5.http.classic.HttpClient; |
24 | 32 | import org.apache.hc.core5.http.ContentType; |
25 | 33 | import org.apache.hc.core5.http.io.entity.InputStreamEntity; |
26 | 34 | import org.apache.hc.core5.http.message.BasicClassicHttpResponse; |
| 35 | +import org.assertj.core.api.SoftAssertions; |
27 | 36 | import org.junit.jupiter.api.AfterEach; |
28 | 37 | import org.junit.jupiter.api.BeforeEach; |
29 | 38 |
|
@@ -55,6 +64,90 @@ static void stubForEmbedding() { |
55 | 64 | .withHeader("Content-Type", "application/json"))); |
56 | 65 | } |
57 | 66 |
|
| 67 | + static void stubForChatCompletionTool() { |
| 68 | + stubFor( |
| 69 | + post(urlPathEqualTo("/chat/completions")) |
| 70 | + .willReturn( |
| 71 | + aResponse() |
| 72 | + .withHeader("Content-Type", "application/json") |
| 73 | + .withBodyFile("chatCompletionToolResponse.json"))); |
| 74 | + } |
| 75 | + |
| 76 | + static void stubForErrorHandling() { |
| 77 | + final var errorJson = |
| 78 | + """ |
| 79 | + { "error": { "code": null, "message": "foo", "type": "invalid stuff" } } |
| 80 | + """; |
| 81 | + stubFor( |
| 82 | + post(anyUrl()) |
| 83 | + .inScenario("Errors") |
| 84 | + .whenScenarioStateIs(Scenario.STARTED) |
| 85 | + .willReturn(serverError()) |
| 86 | + .willSetStateTo("1")); |
| 87 | + stubFor( |
| 88 | + post(anyUrl()) |
| 89 | + .inScenario("Errors") |
| 90 | + .whenScenarioStateIs("1") |
| 91 | + .willReturn( |
| 92 | + badRequest().withBody(errorJson).withHeader("Content-type", "application/json")) |
| 93 | + .willSetStateTo("2")); |
| 94 | + stubFor( |
| 95 | + post(anyUrl()) |
| 96 | + .inScenario("Errors") |
| 97 | + .whenScenarioStateIs("2") |
| 98 | + .willReturn( |
| 99 | + badRequest() |
| 100 | + .withBody("{ broken json") |
| 101 | + .withHeader("Content-type", "application/json")) |
| 102 | + .willSetStateTo("3")); |
| 103 | + stubFor( |
| 104 | + post(anyUrl()) |
| 105 | + .inScenario("Errors") |
| 106 | + .whenScenarioStateIs("3") |
| 107 | + .willReturn(okXml("<xml></xml>")) |
| 108 | + .willSetStateTo("4")); |
| 109 | + stubFor(post(anyUrl()).inScenario("Errors").whenScenarioStateIs("4").willReturn(noContent())); |
| 110 | + } |
| 111 | + |
| 112 | + static void assertForErrorHandling(@Nonnull final Runnable request) { |
| 113 | + |
| 114 | + final var softly = new SoftAssertions(); |
| 115 | + |
| 116 | + softly |
| 117 | + .assertThatThrownBy(request::run) |
| 118 | + .describedAs("Server errors should be handled") |
| 119 | + .isInstanceOf(OpenAiClientException.class) |
| 120 | + .hasMessageContaining("500"); |
| 121 | + |
| 122 | + softly |
| 123 | + .assertThatThrownBy(request::run) |
| 124 | + .describedAs("Error objects from OpenAI should be interpreted") |
| 125 | + .isInstanceOf(OpenAiClientException.class) |
| 126 | + .hasMessageContaining("error message: 'foo'"); |
| 127 | + |
| 128 | + softly |
| 129 | + .assertThatThrownBy(request::run) |
| 130 | + .describedAs("Failures while parsing error message should be handled") |
| 131 | + .isInstanceOf(OpenAiClientException.class) |
| 132 | + .hasMessageContaining("400") |
| 133 | + .extracting(e -> e.getSuppressed()[0]) |
| 134 | + .isInstanceOf(JsonParseException.class); |
| 135 | + |
| 136 | + softly |
| 137 | + .assertThatThrownBy(request::run) |
| 138 | + .describedAs("Non-JSON responses should be handled") |
| 139 | + .isInstanceOf(OpenAiClientException.class) |
| 140 | + .hasMessageContaining("Failed to parse"); |
| 141 | + |
| 142 | + softly |
| 143 | + .assertThatThrownBy(request::run) |
| 144 | + .describedAs("Empty responses should be handled") |
| 145 | + .isInstanceOf(OpenAiClientException.class) |
| 146 | + .hasMessageContaining("was empty"); |
| 147 | + |
| 148 | + softly.assertAll(); |
| 149 | + } |
| 150 | + |
58 | 151 | @BeforeEach |
59 | 152 | void setup(WireMockRuntimeInfo server) { |
60 | 153 | final DefaultHttpDestination destination = |
@@ -86,13 +179,4 @@ InputStream stubStreamChatCompletion(String responseFile) throws IOException { |
86 | 179 |
|
87 | 180 | return inputStream; |
88 | 181 | } |
89 | | - |
90 | | - static void stubForChatCompletionTool() { |
91 | | - stubFor( |
92 | | - post(urlPathEqualTo("/chat/completions")) |
93 | | - .willReturn( |
94 | | - aResponse() |
95 | | - .withHeader("Content-Type", "application/json") |
96 | | - .withBodyFile("chatCompletionToolResponse.json"))); |
97 | | - } |
98 | 182 | } |
0 commit comments