Skip to content
Merged
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 @@ -21,8 +21,13 @@
import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.tool.definition.ToolDefinition;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.TypeReference;

import java.util.HashSet;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.reflection;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.resource;

Expand All @@ -42,4 +47,97 @@ void core() {
assertThat(runtimeHints).matches(reflection().onType(ToolDefinition.class));
}

@Test
void registerHintsWithNullClassLoader() {
var runtimeHints = new RuntimeHints();
var springAiCore = new SpringAiCoreRuntimeHints();

// Should not throw exception with null ClassLoader
assertThatCode(() -> springAiCore.registerHints(runtimeHints, null)).doesNotThrowAnyException();
}

@Test
void verifyEmbeddingResourceIsRegistered() {
var runtimeHints = new RuntimeHints();
var springAiCore = new SpringAiCoreRuntimeHints();
springAiCore.registerHints(runtimeHints, null);

// Verify the specific embedding properties file is registered
assertThat(runtimeHints).matches(resource().forResource("embedding/embedding-model-dimensions.properties"));
}

@Test
void verifyToolReflectionHintsAreRegistered() {
var runtimeHints = new RuntimeHints();
var springAiCore = new SpringAiCoreRuntimeHints();
springAiCore.registerHints(runtimeHints, null);

Set<TypeReference> registeredTypes = new HashSet<>();
runtimeHints.reflection().typeHints().forEach(typeHint -> registeredTypes.add(typeHint.getType()));

// Verify core tool classes are registered
assertThat(registeredTypes.contains(TypeReference.of(ToolCallback.class))).isTrue();
assertThat(registeredTypes.contains(TypeReference.of(ToolDefinition.class))).isTrue();
}

@Test
void verifyResourceAndReflectionHintsSeparately() {
var runtimeHints = new RuntimeHints();
var springAiCore = new SpringAiCoreRuntimeHints();
springAiCore.registerHints(runtimeHints, null);

// Test resource hints
assertThat(runtimeHints).matches(resource().forResource("embedding/embedding-model-dimensions.properties"));

// Test reflection hints
assertThat(runtimeHints).matches(reflection().onType(ToolCallback.class));
assertThat(runtimeHints).matches(reflection().onType(ToolDefinition.class));
}

@Test
void verifyMultipleRegistrationCallsAreIdempotent() {
var runtimeHints1 = new RuntimeHints();
var runtimeHints2 = new RuntimeHints();
var springAiCore = new SpringAiCoreRuntimeHints();

// Register hints on two separate RuntimeHints instances
springAiCore.registerHints(runtimeHints1, null);
springAiCore.registerHints(runtimeHints2, null);

// Both should have the same hints registered
assertThat(runtimeHints1).matches(resource().forResource("embedding/embedding-model-dimensions.properties"));
assertThat(runtimeHints2).matches(resource().forResource("embedding/embedding-model-dimensions.properties"));

assertThat(runtimeHints1).matches(reflection().onType(ToolCallback.class));
assertThat(runtimeHints2).matches(reflection().onType(ToolCallback.class));
}

@Test
void verifyResourceHintsForIncorrectPaths() {
var runtimeHints = new RuntimeHints();
var springAiCore = new SpringAiCoreRuntimeHints();
springAiCore.registerHints(runtimeHints, null);

// Verify the exact resource path is registered
assertThat(runtimeHints).matches(resource().forResource("embedding/embedding-model-dimensions.properties"));

// Verify that similar but incorrect paths are not matched
assertThat(runtimeHints).doesNotMatch(resource().forResource("embedding-model-dimensions.properties"));
assertThat(runtimeHints).doesNotMatch(resource().forResource("embedding/model-dimensions.properties"));
}

@Test
void ensureBothResourceAndReflectionHintsArePresent() {
var runtimeHints = new RuntimeHints();
var springAiCore = new SpringAiCoreRuntimeHints();
springAiCore.registerHints(runtimeHints, null);

// Ensure both resource and reflection hints are registered
boolean hasResourceHints = runtimeHints.resources() != null;
boolean hasReflectionHints = runtimeHints.reflection().typeHints().spliterator().estimateSize() > 0;

assertThat(hasResourceHints).isTrue();
assertThat(hasReflectionHints).isTrue();
}

}