Skip to content

Commit f25c22b

Browse files
authored
Increase test coverage (#147)
1 parent bc51cd1 commit f25c22b

File tree

2 files changed

+67
-14
lines changed

2 files changed

+67
-14
lines changed

orchestration/src/test/java/com/sap/ai/sdk/orchestration/OrchestrationUnitTest.java

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
88
import static com.github.tomakehurst.wiremock.client.WireMock.get;
99
import static com.github.tomakehurst.wiremock.client.WireMock.jsonResponse;
10+
import static com.github.tomakehurst.wiremock.client.WireMock.noContent;
1011
import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
12+
import static com.github.tomakehurst.wiremock.client.WireMock.okXml;
1113
import static com.github.tomakehurst.wiremock.client.WireMock.post;
1214
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
1315
import static com.github.tomakehurst.wiremock.client.WireMock.serverError;
@@ -21,8 +23,10 @@
2123
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2224
import static org.mockito.Mockito.mock;
2325

26+
import com.fasterxml.jackson.core.JsonParseException;
2427
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
2528
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
29+
import com.github.tomakehurst.wiremock.stubbing.Scenario;
2630
import com.sap.ai.sdk.core.AiCoreService;
2731
import com.sap.ai.sdk.orchestration.client.model.AzureContentSafety;
2832
import com.sap.ai.sdk.orchestration.client.model.AzureThreshold;
@@ -47,6 +51,7 @@
4751
import java.util.Objects;
4852
import java.util.function.Function;
4953
import javax.annotation.Nonnull;
54+
import org.assertj.core.api.SoftAssertions;
5055
import org.junit.jupiter.api.BeforeEach;
5156
import org.junit.jupiter.api.Test;
5257

@@ -339,26 +344,74 @@ private static MaskingModuleConfig createMaskingConfig(
339344
}
340345

341346
@Test
342-
void testGenericErrorHandling() {
343-
stubFor(post(anyUrl()).willReturn(serverError()));
344-
345-
assertThatThrownBy(() -> client.executeRequest(mock(CompletionPostRequest.class)))
346-
.isInstanceOf(OrchestrationClientException.class)
347-
.hasMessageContaining("500 Server Error");
348-
}
349-
350-
@Test
351-
void testOrchestrationErrorParsing() {
347+
void testErrorHandling() {
352348
stubFor(
353349
post(anyUrl())
350+
.inScenario("Errors")
351+
.whenScenarioStateIs(Scenario.STARTED)
352+
.willReturn(serverError())
353+
.willSetStateTo("1"));
354+
stubFor(
355+
post(anyUrl())
356+
.inScenario("Errors")
357+
.whenScenarioStateIs("1")
354358
.willReturn(
355359
badRequest()
356360
.withHeader("Content-Type", "application/json")
357-
.withBodyFile("errorResponse.json")));
361+
.withBodyFile("errorResponse.json"))
362+
.willSetStateTo("2"));
363+
stubFor(
364+
post(anyUrl())
365+
.inScenario("Errors")
366+
.whenScenarioStateIs("2")
367+
.willReturn(
368+
badRequest()
369+
.withBody("{ broken json")
370+
.withHeader("Content-type", "application/json"))
371+
.willSetStateTo("3"));
372+
stubFor(
373+
post(anyUrl())
374+
.inScenario("Errors")
375+
.whenScenarioStateIs("3")
376+
.willReturn(okXml("<xml></xml>"))
377+
.willSetStateTo("4"));
378+
stubFor(post(anyUrl()).inScenario("Errors").whenScenarioStateIs("4").willReturn(noContent()));
379+
380+
final var softly = new SoftAssertions();
381+
final Runnable request = () -> client.executeRequest(mock(CompletionPostRequest.class));
382+
383+
softly
384+
.assertThatThrownBy(request::run)
385+
.describedAs("Server errors should be handled")
386+
.isInstanceOf(OrchestrationClientException.class)
387+
.hasMessageContaining("500");
358388

359-
assertThatThrownBy(() -> client.executeRequest(mock(CompletionPostRequest.class)))
389+
softly
390+
.assertThatThrownBy(request::run)
391+
.describedAs("Error objects from Orchestration should be interpreted")
360392
.isInstanceOf(OrchestrationClientException.class)
361-
.hasMessageContaining("400 Bad Request")
362393
.hasMessageContaining("'orchestration_config' is a required property");
394+
395+
softly
396+
.assertThatThrownBy(request::run)
397+
.describedAs("Failures while parsing error message should be handled")
398+
.isInstanceOf(OrchestrationClientException.class)
399+
.hasMessageContaining("400")
400+
.extracting(e -> e.getSuppressed()[0])
401+
.isInstanceOf(JsonParseException.class);
402+
403+
softly
404+
.assertThatThrownBy(request::run)
405+
.describedAs("Non-JSON responses should be handled")
406+
.isInstanceOf(OrchestrationClientException.class)
407+
.hasMessageContaining("Failed to parse");
408+
409+
softly
410+
.assertThatThrownBy(request::run)
411+
.describedAs("Empty responses should be handled")
412+
.isInstanceOf(OrchestrationClientException.class)
413+
.hasMessageContaining("was empty");
414+
415+
softly.assertAll();
363416
}
364417
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
<enforcer.skipBanGeneratedModulesReference>false</enforcer.skipBanGeneratedModulesReference>
7575
<!-- Test coverage -->
7676
<coverage.instruction>74%</coverage.instruction>
77-
<coverage.branch>62%</coverage.branch>
77+
<coverage.branch>67%</coverage.branch>
7878
<coverage.complexity>67%</coverage.complexity>
7979
<coverage.line>75%</coverage.line>
8080
<coverage.method>80%</coverage.method>

0 commit comments

Comments
 (0)