Skip to content

Commit

Permalink
Merge branch 'refs/heads/refactor-core3' into query-filter
Browse files Browse the repository at this point in the history
# Conflicts:
#	core/src/main/java/com/sap/ai/sdk/core/AiCoreService.java
#	core/src/test/java/com/sap/ai/sdk/core/client/WireMockTestServer.java
  • Loading branch information
CharlesDuboisSAP committed Oct 16, 2024
2 parents 350a555 + d4d98fc commit 0c088a8
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 50 deletions.
17 changes: 2 additions & 15 deletions core/src/main/java/com/sap/ai/sdk/core/AiCoreDeployment.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Destination destination() {
final var dest = service.baseDestinationHandler.apply(service);
final var builder = service.builderHandler.apply(service, dest);
destinationSetUrl(builder, dest);
destinationSetHeaders(builder, dest);
destinationSetHeaders(builder);
return builder.build();
}

Expand Down Expand Up @@ -77,10 +77,8 @@ protected void destinationSetUrl(
* Update and set the default request headers for the destination.
*
* @param builder The destination builder.
* @param dest The original destination reference.
*/
protected void destinationSetHeaders(
@Nonnull final DefaultHttpDestination.Builder builder, @Nonnull final Destination dest) {
protected void destinationSetHeaders(@Nonnull final DefaultHttpDestination.Builder builder) {
builder.property(AI_RESOURCE_GROUP, getResourceGroup());
}

Expand All @@ -95,17 +93,6 @@ public AiCoreDeployment withResourceGroup(@Nonnull final String resourceGroup) {
return new AiCoreDeployment(service, deploymentId, resourceGroup);
}

/**
* Set the destination.
*
* @param destination The destination.
* @return A new instance of the AI Core service.
*/
@Nonnull
public AiCoreDeployment withDestination(@Nonnull final Destination destination) {
return new AiCoreDeployment(service.withDestination(destination), deploymentId, resourceGroup);
}

/**
* Get the deployment id.
*
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/com/sap/ai/sdk/core/AiCoreService.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Destination destination() {
* Set a specific base destination.
*
* @param destination The destination to be used for AI Core service calls.
* @return A new instance of the AI Core service.
* @return A new instance of the AI Core Service based on the provided destination.
*/
@Nonnull
public AiCoreService withDestination(@Nonnull final Destination destination) {
Expand All @@ -75,7 +75,7 @@ public AiCoreService withDestination(@Nonnull final Destination destination) {
* Set a specific deployment by id.
*
* @param deploymentId The deployment id to be used for AI Core service calls.
* @return A new instance of the AI Core service.
* @return A new instance of the AI Core Deployment.
*/
@Nonnull
public AiCoreDeployment forDeployment(@Nonnull final String deploymentId) {
Expand All @@ -87,7 +87,7 @@ public AiCoreDeployment forDeployment(@Nonnull final String deploymentId) {
* the first one is returned.
*
* @param modelName The model name to be used for AI Core service calls.
* @return A new instance of the AI Core service.
* @return A new instance of the AI Core Deployment.
* @throws NoSuchElementException if no running deployment is found for the model.
*/
@Nonnull
Expand All @@ -105,7 +105,7 @@ public AiCoreDeployment forDeploymentByModel(@Nonnull final String modelName)
* the first one is returned.
*
* @param scenarioId The scenario id to be used for AI Core service calls.
* @return A new instance of the AI Core service.
* @return A new instance of the AI Core Deployment.
* @throws NoSuchElementException if no running deployment is found for the scenario.
*/
@Nonnull
Expand Down
24 changes: 4 additions & 20 deletions core/src/test/java/com/sap/ai/sdk/core/AiCoreServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void testLazyEvaluation() {
}

@Test
void testSimpleCase() {
void testDefaultCase() {
// setup
final var accessor = mock(ServiceBindingAccessor.class);
DestinationResolver.setAccessor(accessor);
Expand Down Expand Up @@ -95,25 +95,6 @@ void testBaseDestination() {
assertThat(client.getBasePath()).isEqualTo("https://foo.bar/v2/");
}

@Test
void testDeployment() {
final var accessor = mock(ServiceBindingAccessor.class);
DestinationResolver.setAccessor(accessor);
doReturn(List.of(BINDING)).when(accessor).getServiceBindings();

// execution without errors
final var destination = new AiCoreService().destination();
final var client = new AiCoreService().client();

// verification
assertThat(destination.get(DestinationProperty.URI)).contains("https://srv/v2/");
assertThat(destination.get(DestinationProperty.AUTH_TYPE)).isEmpty();
assertThat(destination.get(DestinationProperty.NAME)).singleElement(STRING).contains("aicore");
assertThat(destination.get(AI_CLIENT_TYPE_KEY)).contains(AI_CLIENT_TYPE_VALUE);
assertThat(client.getBasePath()).isEqualTo("https://srv/v2/");
verify(accessor, times(2)).getServiceBindings();
}

@Test
void testCustomization() {
final var customService =
Expand All @@ -139,5 +120,8 @@ protected ApiClient getApiClient(@Nonnull Destination destination) {

final var destination = customServiceForDeployment.destination().asHttp();
assertThat(destination.getUri()).hasToString("https://ai/v2/inference/deployments/deployment/");

final var resourceGroup = customServiceForDeployment.getResourceGroup();
assertThat(resourceGroup).isEqualTo("group");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.sap.ai.sdk.core.AiCoreService;
import com.sap.cloud.sdk.cloudplatform.connectivity.DefaultHttpDestination;
import com.sap.cloud.sdk.cloudplatform.connectivity.Destination;
import com.sap.cloud.sdk.services.openapi.apiclient.ApiClient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -17,14 +16,14 @@ public abstract class WireMockTestServer {
wireMockConfig().dynamicPort();

public static WireMockServer wireMockServer;
public static Destination destination;
public static ApiClient client;

@BeforeAll
static void setup() {
wireMockServer = new WireMockServer(WIREMOCK_CONFIGURATION);
wireMockServer.start();
destination = DefaultHttpDestination.builder(wireMockServer.baseUrl()).build();

final var destination = DefaultHttpDestination.builder(wireMockServer.baseUrl()).build();
client = new AiCoreService().withDestination(destination).client();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.jsonResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
Expand Down Expand Up @@ -43,7 +44,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import org.apache.hc.core5.http.HttpStatus;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.web.client.HttpClientErrorException;
Expand Down Expand Up @@ -81,16 +81,13 @@ public class OrchestrationUnitTest {

@BeforeEach
void setup(WireMockRuntimeInfo server) {

stubFor(
get(urlPathEqualTo("/v2/lm/deployments"))
.withHeader("AI-Resource-Group", equalTo("default"))
.withHeader("AI-Resource-Group", equalTo("my-resource-group"))
.withHeader("AI-Client-Type", equalTo("AI SDK Java"))
.willReturn(
aResponse()
.withStatus(HttpStatus.SC_OK)
.withHeader("content-type", "application/json")
.withBody(
"""
okJson(
"""
{
"resources": [
{
Expand All @@ -103,10 +100,12 @@ void setup(WireMockRuntimeInfo server) {

final DefaultHttpDestination destination =
DefaultHttpDestination.builder(server.getHttpBaseUrl()).build();

final var apiClient =
new AiCoreService()
.withDestination(destination)
.forDeploymentByScenario("orchestration")
.withResourceGroup("my-resource-group")
.client();
client = new OrchestrationCompletionApi(apiClient);
}
Expand Down

0 comments on commit 0c088a8

Please sign in to comment.