Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test #42

Merged
merged 19 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
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.post;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.sap.ai.sdk.core.Core.getClient;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -21,7 +23,7 @@
*/
public class ArtifactUnitTest extends WireMockTestServer {
@Test
void testGetArtifact() {
void getArtifacts() {
wireMockServer.stubFor(
get(urlPathEqualTo("/lm/artifacts"))
.withHeader("AI-Resource-Group", equalTo("default"))
Expand Down Expand Up @@ -50,10 +52,13 @@ void testGetArtifact() {

final AiArtifactList artifactList =
new ArtifactApi(getClient(destination)).artifactQuery("default");

assertThat(artifactList).isNotNull();
assertThat(artifactList.getCount()).isEqualTo(1);
assertThat(artifactList.getResources().size()).isEqualTo(1);
AiArtifact artifact = artifactList.getResources().get(0);

final AiArtifact artifact = artifactList.getResources().get(0);

assertThat(artifact.getCreatedAt()).isEqualTo("2024-05-22T07:40:30Z");
assertThat(artifact.getDescription()).isEqualTo("dataset for aicore training");
assertThat(artifact.getId()).isEqualTo("744b0136-ed4b-49b1-bd10-08c236ed5ce7");
Expand All @@ -65,13 +70,13 @@ void testGetArtifact() {
}

@Test
void testPostArtifact() {
void postArtifact() {
wireMockServer.stubFor(
post(urlPathEqualTo("/lm/artifacts"))
.withHeader("AI-Resource-Group", equalTo("default"))
.willReturn(
aResponse()
.withStatus(HttpStatus.SC_OK)
.withStatus(HttpStatus.SC_CREATED)
.withHeader("content-type", "application/json")
.withBody(
"""
Expand All @@ -82,18 +87,92 @@ void testPostArtifact() {
}
""")));

AiArtifactPostData artifactPostData =
final AiArtifactPostData artifactPostData =
AiArtifactPostData.create()
.name("default")
.kind(AiArtifactPostData.KindEnum.DATASET)
.url("ai://default/spam/data")
.scenarioId("foundation-models")
.scenarioId("foundation-models")
.description("dataset for aicore training");
final AiArtifactCreationResponse artifact =
new ArtifactApi(getClient(destination)).artifactCreate("default", artifactPostData);

assertThat(artifact).isNotNull();
assertThat(artifact.getId()).isEqualTo("1a84bb38-4a84-4d12-a5aa-300ae7d33fb4");
assertThat(artifact.getMessage()).isEqualTo("AiArtifact acknowledged");
assertThat(artifact.getUrl()).isEqualTo("ai://default/spam/data");

wireMockServer.verify(
postRequestedFor(urlPathEqualTo("/lm/artifacts"))
.withHeader("AI-Resource-Group", equalTo("default"))
.withRequestBody(
equalToJson(
"""
{
"name": "default",
"kind": "dataset",
"url": "ai://default/spam/data",
"scenarioId": "foundation-models",
"description": "dataset for aicore training",
"labels": []
}
""")));
}

@Test
void getArtifactById() {
wireMockServer.stubFor(
get(urlPathEqualTo("/lm/artifacts/777dea85-e9b1-4a7b-9bea-14769b977633"))
.withHeader("AI-Resource-Group", equalTo("default"))
.willReturn(
aResponse()
.withHeader("content-type", "application/json")
.withBody(
"""
{
"createdAt": "2024-08-23T09:13:21Z",
"description": "",
"id": "777dea85-e9b1-4a7b-9bea-14769b977633",
"kind": "other",
"modifiedAt": "2024-08-23T09:13:21Z",
"name": "test",
"scenarioId": "orchestration",
"url": "https://file-examples.com/wp-content/storage/2017/10/file-sample_150kB.pdf"
}
""")));

final AiArtifact artifact =
new ArtifactApi(getClient(destination))
.artifactGet("default", "777dea85-e9b1-4a7b-9bea-14769b977633");

assertThat(artifact).isNotNull();
assertThat(artifact.getCreatedAt()).isEqualTo("2024-08-23T09:13:21Z");
assertThat(artifact.getDescription()).isEqualTo("");
assertThat(artifact.getId()).isEqualTo("777dea85-e9b1-4a7b-9bea-14769b977633");
assertThat(artifact.getKind()).isEqualTo(AiArtifact.KindEnum.OTHER);
assertThat(artifact.getModifiedAt()).isEqualTo("2024-08-23T09:13:21Z");
assertThat(artifact.getName()).isEqualTo("test");
assertThat(artifact.getScenarioId()).isEqualTo("orchestration");
assertThat(artifact.getUrl())
.isEqualTo("https://file-examples.com/wp-content/storage/2017/10/file-sample_150kB.pdf");
}

@Test
void getArtifactCount() {
wireMockServer.stubFor(
get(urlPathEqualTo("/lm/artifacts/$count"))
.withHeader("AI-Resource-Group", equalTo("default"))
.willReturn(
aResponse()
.withStatus(HttpStatus.SC_OK)
.withHeader("content-type", "application/json")
.withBody("""
4
""")));

final int count = new ArtifactApi(getClient(destination)).artifactCount("default");

assertThat(count).isEqualTo(4);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
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.post;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.sap.ai.sdk.core.Core.getClient;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -13,6 +15,7 @@
import com.sap.ai.sdk.core.client.model.AiConfigurationBaseData;
import com.sap.ai.sdk.core.client.model.AiConfigurationCreationResponse;
import com.sap.ai.sdk.core.client.model.AiConfigurationList;
import com.sap.ai.sdk.core.client.model.AiParameterArgumentBinding;
import org.apache.hc.core5.http.HttpStatus;
import org.junit.jupiter.api.Test;

Expand All @@ -22,7 +25,7 @@
*/
public class ConfigurationUnitTest extends WireMockTestServer {
@Test
void testGetConfigurations() {
void getConfigurations() {
wireMockServer.stubFor(
get(urlPathEqualTo("/lm/configurations"))
.withHeader("AI-Resource-Group", equalTo("default"))
Expand Down Expand Up @@ -59,13 +62,15 @@ void testGetConfigurations() {

final AiConfigurationList configurationList =
new ConfigurationApi(getClient(destination)).configurationQuery("default");

assertThat(configurationList).isNotNull();
assertThat(configurationList.getCount()).isEqualTo(1);
assertThat(configurationList.getResources().size()).isEqualTo(1);
AiConfiguration configuration = configurationList.getResources().get(0);
assertThat(configuration.getCreatedAt()).isEqualTo("2024-04-17T15:19:45Z");
assertThat(configuration.getExecutableId()).isEqualTo("azure-openai");
assertThat(configuration.getId()).isEqualTo("7652a231-ba9b-4fcc-b473-2c355cb21b61");
assertThat(configuration.getInputArtifactBindings().size()).isEqualTo(0);
assertThat(configuration.getName()).isEqualTo("gpt-4-32k");
assertThat(configuration.getParameterBindings().get(0).getKey()).isEqualTo("modelName");
assertThat(configuration.getParameterBindings().get(0).getValue()).isEqualTo("gpt-4-32k");
Expand All @@ -75,13 +80,13 @@ void testGetConfigurations() {
}

@Test
void testPostConfiguration() {
void postConfiguration() {
wireMockServer.stubFor(
post(urlPathEqualTo("/lm/configurations"))
.withHeader("AI-Resource-Group", equalTo("default"))
.willReturn(
aResponse()
.withStatus(HttpStatus.SC_OK)
.withStatus(HttpStatus.SC_CREATED)
.withHeader("content-type", "application/json")
.withBody(
"""
Expand All @@ -91,20 +96,118 @@ void testPostConfiguration() {
}
""")));

AiConfigurationBaseData configurationBaseData =
final AiArtifactArgumentBinding inputArtifactBindingsItem =
AiArtifactArgumentBinding.create()
.key("spam-data")
.artifactId("744b0136-ed4b-49b1-bd10-08c236ed5ce7");
final AiConfigurationBaseData configurationBaseData =
AiConfigurationBaseData.create()
.name("i538344_exec_config")
.executableId("aicore-nvidia")
.scenarioId("foundation-models")
.addInputArtifactBindingsItem(
AiArtifactArgumentBinding.create()
.key("spam-data")
.artifactId("744b0136-ed4b-49b1-bd10-08c236ed5ce7"));
.addInputArtifactBindingsItem(inputArtifactBindingsItem);
final AiConfigurationCreationResponse configuration =
new ConfigurationApi(getClient(destination))
.configurationCreate("default", configurationBaseData);

assertThat(configuration).isNotNull();
assertThat(configuration.getId()).isEqualTo("f88e7581-ade7-45c6-94e9-807889b523ec");
assertThat(configuration.getMessage()).isEqualTo("Configuration created");

wireMockServer.verify(
postRequestedFor(urlPathEqualTo("/lm/configurations"))
.withHeader("AI-Resource-Group", equalTo("default"))
.withRequestBody(
equalToJson(
"""
{
"name": "i538344_exec_config",
"executableId": "aicore-nvidia",
"scenarioId": "foundation-models",
"parameterBindings":[],
"inputArtifactBindings": [
{
"key": "spam-data",
"artifactId": "744b0136-ed4b-49b1-bd10-08c236ed5ce7"
}
]
}
""")));
}

@Test
void getConfigurationCount() {
wireMockServer.stubFor(
get(urlPathEqualTo("/lm/configurations/$count"))
.withHeader("AI-Resource-Group", equalTo("default"))
.willReturn(
aResponse()
.withStatus(HttpStatus.SC_OK)
.withHeader("content-type", "application/json")
.withBody("""
3
""")));

final int configurationCount =
new ConfigurationApi(getClient(destination)).configurationCount("default");

assertThat(configurationCount).isEqualTo(3);
}

@Test
void getConfigurationById() {
wireMockServer.stubFor(
get(urlPathEqualTo("/lm/configurations/6ff6cb80-87db-45f0-b718-4e1d96e66332"))
.withHeader("AI-Resource-Group", equalTo("default"))
.willReturn(
aResponse()
.withStatus(HttpStatus.SC_OK)
.withHeader("content-type", "application/json")
.withBody(
"""
{
"createdAt": "2024-09-11T09:14:31Z",
"executableId": "st-spam-detection-i749902",
"id": "6ff6cb80-87db-45f0-b718-4e1d96e66332",
"inputArtifactBindings": [
{
"artifactId": "c4792df8-da67-44fe-ad99-b5ea74bbb248",
"key": "modeluri"
}
],
"name": "i749902_depl_conf",
"parameterBindings": [
{
"key": "minreplicas",
"value": "1"
}
],
"scenarioId": "scenario-spam-detection-i749902"
}
""")));

final AiConfiguration configuration =
new ConfigurationApi(getClient(destination))
.configurationGet("default", "6ff6cb80-87db-45f0-b718-4e1d96e66332");

assertThat(configuration).isNotNull();
assertThat(configuration.getCreatedAt()).isEqualTo("2024-09-11T09:14:31Z");
assertThat(configuration.getExecutableId()).isEqualTo("st-spam-detection-i749902");
assertThat(configuration.getId()).isEqualTo("6ff6cb80-87db-45f0-b718-4e1d96e66332");
assertThat(configuration.getInputArtifactBindings().size()).isEqualTo(1);
assertThat(configuration.getName()).isEqualTo("i749902_depl_conf");
assertThat(configuration.getParameterBindings().size()).isEqualTo(1);
assertThat(configuration.getScenarioId()).isEqualTo("scenario-spam-detection-i749902");

AiArtifactArgumentBinding aiArtifactArgumentBinding =
configuration.getInputArtifactBindings().get(0);
assertThat(aiArtifactArgumentBinding.getArtifactId())
.isEqualTo("c4792df8-da67-44fe-ad99-b5ea74bbb248");
assertThat(aiArtifactArgumentBinding.getKey()).isEqualTo("modeluri");

AiParameterArgumentBinding aiParameterArgumentBinding =
configuration.getParameterBindings().get(0);
assertThat(aiParameterArgumentBinding.getKey()).isEqualTo("minreplicas");
assertThat(aiParameterArgumentBinding.getValue()).isEqualTo("1");
}
}
Loading