diff --git a/logback.xml b/logback.xml index 2bbaaa365..c29966f70 100644 --- a/logback.xml +++ b/logback.xml @@ -12,8 +12,8 @@ - - + + diff --git a/sample-code/spring-app/pom.xml b/sample-code/spring-app/pom.xml index 946a5aeac..9996d555a 100644 --- a/sample-code/spring-app/pom.xml +++ b/sample-code/spring-app/pom.xml @@ -66,6 +66,18 @@ com.sap.ai.sdk core + + + com.sap.cloud.sdk.cloudplatform + connectivity-oauth + + + + + com.sap.cloud.sdk.cloudplatform + connectivity-oauth + 5.27.0-SNAPSHOT + com.sap.ai.sdk.foundationmodels @@ -241,6 +253,52 @@ com.fasterxml.jackson.dataformat jackson-dataformat-yaml + + + com.sap.cloud.environment.servicebinding.api + java-access-api + test + + + com.sap.cloud.environment.servicebinding.api + java-core-api + test + + + com.auth0 + java-jwt + test + + + com.sap.cloud.sdk.cloudplatform + connectivity-apache-httpclient5 + test + + + com.sap.cloud.environment.servicebinding + java-sap-vcap-services + test + + + io.vavr + vavr + test + + + com.sap.cloud.sdk.cloudplatform + security + test + + + org.apache.httpcomponents.core5 + httpcore5 + test + + + org.apache.httpcomponents.client5 + httpclient5 + test + diff --git a/sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/AgentToolIntegrationLayerTest.java b/sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/AgentToolIntegrationLayerTest.java new file mode 100644 index 000000000..4d35a91c5 --- /dev/null +++ b/sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/AgentToolIntegrationLayerTest.java @@ -0,0 +1,111 @@ +package com.sap.ai.sdk.app.controllers; + +import static com.sap.cloud.environment.servicebinding.api.ServiceIdentifier.IDENTITY_AUTHENTICATION; +import static org.apache.hc.core5.http.ContentType.APPLICATION_JSON; + +import com.auth0.jwt.JWT; +import com.sap.cloud.environment.servicebinding.SapVcapServicesServiceBindingAccessor; +import com.sap.cloud.environment.servicebinding.api.DefaultServiceBindingAccessor; +import com.sap.cloud.sdk.cloudplatform.connectivity.ApacheHttpClient5Accessor; +import com.sap.cloud.sdk.cloudplatform.connectivity.BtpServiceOptions.AuthenticationServiceOptions; +import com.sap.cloud.sdk.cloudplatform.connectivity.BtpServiceOptions.IasOptions; +import com.sap.cloud.sdk.cloudplatform.connectivity.OnBehalfOf; +import com.sap.cloud.sdk.cloudplatform.connectivity.ServiceBindingDestinationLoader; +import com.sap.cloud.sdk.cloudplatform.connectivity.ServiceBindingDestinationOptions; +import com.sap.cloud.sdk.cloudplatform.security.AuthToken; +import com.sap.cloud.sdk.cloudplatform.security.AuthTokenAccessor; +import io.vavr.control.Try; +import java.util.Collections; +import lombok.SneakyThrows; +import org.apache.hc.client5.http.classic.methods.HttpPost; +import org.apache.hc.client5.http.impl.classic.BasicHttpClientResponseHandler; +import org.apache.hc.core5.http.io.entity.StringEntity; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * How to run this test: + * + *

1. Get the VCAP_SERVICES: Navigate to BTP + * > agent-approuter > environment variables and copy the VCAP_SERVICES part + * starting with {"identity". + * + *

2. Get the JSON_WEB_TOKEN: Copy your password while logging into Cloud Identity. Make a Bruno request to + * https://ma.accounts400.ondemand.com/oauth2/token with the following URL encoded body: + * + *

+ *   grant_type=client_credentials
+ *   &client_id=<approuter-client-id>
+ *   &client_secret=<approuter-client-secret>
+ *   &username=<your-email>
+ *   &password=<Cloud-Indentity-password>
+ * 
+ */ +class AgentToolIntegrationLayerTest { + + private static final String VCAP_SERVICES = +""" +"""; + + private static final String JSON_WEB_TOKEN = +""" +"""; + + String payload = +""" +{ + "message": { + "role": "user", + "parts": [ + { + "kind": "data", + "data": { + "userQuery": "Plan a travel itinerary for me for Berlin" + } + } + ], + "kind": "message" + } +} +"""; + + @BeforeEach + void prepareInboundAccessToken() { + DefaultServiceBindingAccessor.setInstance( + new SapVcapServicesServiceBindingAccessor( + Collections.singletonMap("VCAP_SERVICES", VCAP_SERVICES)::get)); + AuthTokenAccessor.setAuthTokenFacade( + () -> Try.success(new AuthToken(JWT.decode(JSON_WEB_TOKEN)))); + } + + @Test + @SneakyThrows + void testAgentToolIntegrationLayer() { + var options = + ServiceBindingDestinationOptions.forService(IDENTITY_AUTHENTICATION) + .withOption( + AuthenticationServiceOptions.withTargetUri( + "https://gen-ai-hub-sdk-8pengs8d.eu12.sapdas.cloud.sap")) + .withOption(IasOptions.withApplicationName("Agent2Joule")) + .onBehalfOf(OnBehalfOf.NAMED_USER_CURRENT_TENANT) + .build(); + + var destination = ServiceBindingDestinationLoader.defaultLoaderChain().getDestination(options); + + String atilUrl = + "/api/content/v1/capabilities/com.sap.ai.sdk/my_assistant_capability/scenarios/plan_travel/v1/message:send?assistant=my_assistant_i563080"; + var httpPost = new HttpPost(atilUrl); + httpPost.setEntity(new StringEntity(payload, APPLICATION_JSON.withCharset((String) null))); + httpPost.setHeader( + "x-callback-target", "https://echo-server.cfapps.eu12-001.hana.ondemand.com"); + httpPost.setHeader("x-global-user-id", "01361f5a-9ca7-4593-9c54-2425a127edff"); + httpPost.setHeader("Accept-Language", "en"); + + var response = + ApacheHttpClient5Accessor.getHttpClient(destination) + .execute(httpPost, new BasicHttpClientResponseHandler()); + System.out.println(response); + } +}