|
| 1 | +package org.rostilos.codecrow.analysisengine; |
| 2 | + |
| 3 | +import okhttp3.mockwebserver.MockResponse; |
| 4 | +import okhttp3.mockwebserver.MockWebServer; |
| 5 | +import okhttp3.mockwebserver.RecordedRequest; |
| 6 | +import org.junit.jupiter.api.*; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.concurrent.TimeUnit; |
| 10 | + |
| 11 | +import static org.assertj.core.api.Assertions.assertThat; |
| 12 | + |
| 13 | +/** |
| 14 | + * Integration test for AI inference HTTP client behavior using MockWebServer. |
| 15 | + * Verifies request/response handling, timeouts, retries, and error scenarios. |
| 16 | + */ |
| 17 | +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
| 18 | +class AiClientIT { |
| 19 | + |
| 20 | + private MockWebServer mockServer; |
| 21 | + |
| 22 | + @BeforeEach |
| 23 | + void setup() throws IOException { |
| 24 | + mockServer = new MockWebServer(); |
| 25 | + mockServer.start(); |
| 26 | + } |
| 27 | + |
| 28 | + @AfterEach |
| 29 | + void teardown() throws IOException { |
| 30 | + mockServer.shutdown(); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + @Order(1) |
| 35 | + @DisplayName("Should send correct request format to AI provider") |
| 36 | + void shouldSendCorrectRequestFormat() throws Exception { |
| 37 | + mockServer.enqueue(new MockResponse() |
| 38 | + .setBody("{\"choices\":[{\"message\":{\"content\":\"test response\"}}]}") |
| 39 | + .addHeader("Content-Type", "application/json") |
| 40 | + .setResponseCode(200)); |
| 41 | + |
| 42 | + String baseUrl = mockServer.url("/v1/chat/completions").toString(); |
| 43 | + |
| 44 | + // Simulate HTTP client call |
| 45 | + okhttp3.OkHttpClient client = new okhttp3.OkHttpClient(); |
| 46 | + okhttp3.RequestBody body = okhttp3.RequestBody.create( |
| 47 | + "{\"model\":\"gpt-4\",\"messages\":[{\"role\":\"user\",\"content\":\"analyze code\"}]}", |
| 48 | + okhttp3.MediaType.parse("application/json")); |
| 49 | + okhttp3.Request request = new okhttp3.Request.Builder() |
| 50 | + .url(baseUrl) |
| 51 | + .post(body) |
| 52 | + .addHeader("Authorization", "Bearer test-key") |
| 53 | + .build(); |
| 54 | + |
| 55 | + try (okhttp3.Response response = client.newCall(request).execute()) { |
| 56 | + assertThat(response.isSuccessful()).isTrue(); |
| 57 | + String responseBody = response.body().string(); |
| 58 | + assertThat(responseBody).contains("test response"); |
| 59 | + } |
| 60 | + |
| 61 | + RecordedRequest recorded = mockServer.takeRequest(5, TimeUnit.SECONDS); |
| 62 | + assertThat(recorded).isNotNull(); |
| 63 | + assertThat(recorded.getHeader("Authorization")).isEqualTo("Bearer test-key"); |
| 64 | + assertThat(recorded.getBody().readUtf8()).contains("gpt-4"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + @Order(2) |
| 69 | + @DisplayName("Should handle 429 rate limit response") |
| 70 | + void shouldHandleRateLimitResponse() throws Exception { |
| 71 | + mockServer.enqueue(new MockResponse() |
| 72 | + .setResponseCode(429) |
| 73 | + .addHeader("Retry-After", "5") |
| 74 | + .setBody("{\"error\":{\"message\":\"Rate limited\"}}")); |
| 75 | + |
| 76 | + String baseUrl = mockServer.url("/v1/chat/completions").toString(); |
| 77 | + |
| 78 | + okhttp3.OkHttpClient client = new okhttp3.OkHttpClient(); |
| 79 | + okhttp3.Request request = new okhttp3.Request.Builder() |
| 80 | + .url(baseUrl) |
| 81 | + .post(okhttp3.RequestBody.create("{}", okhttp3.MediaType.parse("application/json"))) |
| 82 | + .build(); |
| 83 | + |
| 84 | + try (okhttp3.Response response = client.newCall(request).execute()) { |
| 85 | + assertThat(response.code()).isEqualTo(429); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + @Order(3) |
| 91 | + @DisplayName("Should handle 500 server error") |
| 92 | + void shouldHandleServerError() throws Exception { |
| 93 | + mockServer.enqueue(new MockResponse() |
| 94 | + .setResponseCode(500) |
| 95 | + .setBody("{\"error\":{\"message\":\"Internal server error\"}}")); |
| 96 | + |
| 97 | + String baseUrl = mockServer.url("/v1/chat/completions").toString(); |
| 98 | + |
| 99 | + okhttp3.OkHttpClient client = new okhttp3.OkHttpClient(); |
| 100 | + okhttp3.Request request = new okhttp3.Request.Builder() |
| 101 | + .url(baseUrl) |
| 102 | + .post(okhttp3.RequestBody.create("{}", okhttp3.MediaType.parse("application/json"))) |
| 103 | + .build(); |
| 104 | + |
| 105 | + try (okhttp3.Response response = client.newCall(request).execute()) { |
| 106 | + assertThat(response.code()).isEqualTo(500); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + @Order(4) |
| 112 | + @DisplayName("Should handle empty response body gracefully") |
| 113 | + void shouldHandleEmptyResponseBody() throws Exception { |
| 114 | + mockServer.enqueue(new MockResponse() |
| 115 | + .setResponseCode(200) |
| 116 | + .setBody("") |
| 117 | + .addHeader("Content-Type", "application/json")); |
| 118 | + |
| 119 | + String baseUrl = mockServer.url("/v1/chat/completions").toString(); |
| 120 | + |
| 121 | + okhttp3.OkHttpClient client = new okhttp3.OkHttpClient(); |
| 122 | + okhttp3.Request request = new okhttp3.Request.Builder() |
| 123 | + .url(baseUrl) |
| 124 | + .post(okhttp3.RequestBody.create("{}", okhttp3.MediaType.parse("application/json"))) |
| 125 | + .build(); |
| 126 | + |
| 127 | + try (okhttp3.Response response = client.newCall(request).execute()) { |
| 128 | + assertThat(response.isSuccessful()).isTrue(); |
| 129 | + assertThat(response.body().string()).isEmpty(); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + @Order(5) |
| 135 | + @DisplayName("Should handle large response payload") |
| 136 | + void shouldHandleLargeResponsePayload() throws Exception { |
| 137 | + String largeContent = "x".repeat(500_000); |
| 138 | + mockServer.enqueue(new MockResponse() |
| 139 | + .setResponseCode(200) |
| 140 | + .setBody("{\"choices\":[{\"message\":{\"content\":\"" + largeContent + "\"}}]}") |
| 141 | + .addHeader("Content-Type", "application/json")); |
| 142 | + |
| 143 | + String baseUrl = mockServer.url("/v1/chat/completions").toString(); |
| 144 | + |
| 145 | + okhttp3.OkHttpClient client = new okhttp3.OkHttpClient(); |
| 146 | + okhttp3.Request request = new okhttp3.Request.Builder() |
| 147 | + .url(baseUrl) |
| 148 | + .post(okhttp3.RequestBody.create("{}", okhttp3.MediaType.parse("application/json"))) |
| 149 | + .build(); |
| 150 | + |
| 151 | + try (okhttp3.Response response = client.newCall(request).execute()) { |
| 152 | + assertThat(response.isSuccessful()).isTrue(); |
| 153 | + assertThat(response.body().string()).contains(largeContent); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments