|
2 | 2 |
|
3 | 3 | import static org.assertj.core.api.Assertions.assertThat; |
4 | 4 | import static org.assertj.core.api.Assertions.assertThatCode; |
5 | | -import static org.mockito.Mockito.doReturn; |
6 | | -import static org.mockito.Mockito.doThrow; |
7 | | -import static org.mockito.Mockito.mock; |
8 | | -import static org.mockito.Mockito.never; |
9 | | -import static org.mockito.Mockito.times; |
10 | | -import static org.mockito.Mockito.verify; |
11 | 5 |
|
12 | 6 | import com.sap.cloud.sdk.cloudplatform.connectivity.DefaultHttpDestination; |
13 | | -import com.sap.cloud.sdk.cloudplatform.connectivity.Destination; |
14 | 7 | import com.sap.cloud.sdk.cloudplatform.connectivity.Header; |
15 | 8 | import com.sap.cloud.sdk.cloudplatform.connectivity.HttpDestination; |
16 | 9 | import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationAccessException; |
17 | | -import com.sap.cloud.sdk.services.openapi.apiclient.ApiClient; |
18 | | -import javax.annotation.Nonnull; |
| 10 | +import java.util.function.Supplier; |
19 | 11 | import lombok.val; |
20 | 12 | import org.junit.jupiter.api.BeforeEach; |
21 | 13 | import org.junit.jupiter.api.Test; |
22 | 14 |
|
23 | 15 | class AiCoreServiceTest { |
24 | | - private static final HttpDestination serviceBindingDestination = |
25 | | - DefaultHttpDestination.builder("https://api.ai.com").build(); |
| 16 | + private static final HttpDestination baseDestination = |
| 17 | + DefaultHttpDestination.builder("https://api.ai.com/v2/").build(); |
26 | 18 |
|
27 | | - private DestinationResolver resolver; |
| 19 | + private Supplier<HttpDestination> destinationResolver; |
28 | 20 | private AiCoreService service; |
29 | 21 |
|
30 | 22 | @BeforeEach |
31 | 23 | void setUp() { |
32 | | - resolver = mock(DestinationResolver.class); |
33 | | - doReturn(serviceBindingDestination).when(resolver).getDestination(); |
| 24 | + destinationResolver = () -> baseDestination; |
34 | 25 |
|
35 | | - service = new AiCoreService(resolver); |
| 26 | + service = new AiCoreService(destinationResolver); |
36 | 27 | } |
37 | 28 |
|
38 | 29 | @Test |
39 | 30 | void testLazyDestinationLoading() { |
40 | | - doThrow(new DestinationAccessException()).when(resolver).getDestination(); |
| 31 | + destinationResolver = |
| 32 | + () -> { |
| 33 | + throw new DestinationAccessException("Test"); |
| 34 | + }; |
41 | 35 |
|
42 | | - assertThatCode(() -> new AiCoreService(resolver)) |
| 36 | + assertThatCode(() -> new AiCoreService(destinationResolver)) |
43 | 37 | .describedAs("This must not perform any destination loading upon initialization") |
44 | 38 | .doesNotThrowAnyException(); |
45 | | - |
46 | | - verify(resolver, never()).getDestination(); |
47 | 39 | } |
48 | 40 |
|
49 | 41 | @Test |
50 | | - void testBaseDestination() { |
51 | | - assertThat(service.getBaseDestination()).isEqualTo(serviceBindingDestination); |
| 42 | + void testDefaultBaseDestination() { |
| 43 | + assertThat(service.getBaseDestination()) |
| 44 | + .describedAs( |
| 45 | + "By default the destination obtained from the destination resolver should be used as-is") |
| 46 | + .isEqualTo(baseDestination); |
52 | 47 | } |
53 | 48 |
|
54 | 49 | @Test |
55 | | - void testGetDestination() { |
56 | | - var destination = service.destination(); |
57 | | - assertThat(destination).isNotEqualTo(serviceBindingDestination); |
58 | | - assertThat(destination.getUri()).hasHost("api.ai.com").hasPath("/v2/"); |
59 | | - assertThat(destination.getHeaders()) |
| 50 | + void testCustomBaseDestination() { |
| 51 | + val customDestination = |
| 52 | + DefaultHttpDestination.builder("https://custom.ai.com/custom/base/path").build(); |
| 53 | + val customService = service.withBaseDestination(customDestination); |
| 54 | + |
| 55 | + assertThat(customService.getBaseDestination().getUri()) |
| 56 | + .hasHost("custom.ai.com") |
| 57 | + .hasPath("/custom/base/path"); |
| 58 | + assertThat(customService.getBaseDestination().getHeaders()) |
| 59 | + .describedAs("The client type should be added to ensure its present in all requests") |
60 | 60 | .containsExactly(new Header("AI-Client-Type", "AI SDK Java")); |
61 | 61 | } |
62 | 62 |
|
63 | 63 | @Test |
64 | | - void testDeploymentDestination() { |
65 | | - var destination = service.forDeployment("123").withResourceGroup("foo").destination(); |
| 64 | + void testToInferenceDestination() { |
| 65 | + val destination = service.toInferenceDestination("foo", "123"); |
66 | 66 |
|
67 | 67 | assertThat(destination.getUri()) |
68 | 68 | .hasHost("api.ai.com") |
69 | 69 | .hasPath("/v2/inference/deployments/123/"); |
70 | | - assertThat(destination.getHeaders()) |
71 | | - .containsExactly( |
72 | | - new Header("AI-Client-Type", "AI SDK Java"), new Header("AI-Resource-Group", "foo")); |
73 | | - |
74 | | - assertThat(service.destination()) |
75 | | - .describedAs("The service object should remain unchanged") |
76 | | - .extracting(HttpDestination::getUri) |
77 | | - .isEqualTo(serviceBindingDestination.getUri()); |
78 | 70 |
|
79 | | - verify(resolver, times(2)).getDestination(); |
| 71 | + assertThat(destination.getHeaders()).containsExactly(new Header("AI-Resource-Group", "foo")); |
80 | 72 | } |
81 | 73 |
|
82 | 74 | @Test |
83 | 75 | void testBuildApiClient() { |
84 | | - var apiClient = service.client(); |
| 76 | + var apiClient = service.getApiClient(); |
85 | 77 |
|
86 | 78 | assertThat(apiClient.getBasePath()).hasToString("https://api.ai.com/v2/"); |
87 | | - |
88 | | - verify(resolver, times(1)).getDestination(); |
89 | | - } |
90 | | - |
91 | | - @Test |
92 | | - void testCustomization() { |
93 | | - val customService = |
94 | | - new AiCoreService() { |
95 | | - @Nonnull |
96 | | - @Override |
97 | | - protected HttpDestination getBaseDestination() { |
98 | | - return DefaultHttpDestination.builder("https://ai").build(); |
99 | | - } |
100 | | - |
101 | | - @Nonnull |
102 | | - @Override |
103 | | - protected ApiClient buildApiClient(@Nonnull Destination destination) { |
104 | | - return new ApiClient().setBasePath("https://fizz.buzz").setUserAgent("SAP"); |
105 | | - } |
106 | | - }; |
107 | | - |
108 | | - val customServiceForDeployment = |
109 | | - customService.forDeployment("deployment").withResourceGroup("group"); |
110 | | - |
111 | | - val client = customServiceForDeployment.client(); |
112 | | - assertThat(client.getBasePath()).isEqualTo("https://fizz.buzz"); |
113 | | - |
114 | | - val destination = customServiceForDeployment.destination().asHttp(); |
115 | | - assertThat(destination.getUri()).hasToString("https://ai/v2/inference/deployments/deployment/"); |
116 | | - |
117 | | - val resourceGroup = customService.resourceGroup; |
118 | | - assertThat(resourceGroup).isEqualTo("group"); |
119 | | - assertThat(destination.getHeaders()).contains(new Header("AI-Resource-Group", "group")); |
120 | 79 | } |
121 | 80 | } |
0 commit comments