Skip to content
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
6 changes: 6 additions & 0 deletions reference/grpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
<artifactId>a2a-java-sdk-jsonrpc-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>a2a-java-sdk-server-common</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>a2a-java-sdk-tests-server-common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
public class QuarkusGrpcHandler extends GrpcHandler {

private final Instance<AgentCard> agentCard;
private final Instance<AgentCard> extendedAgentCard;
private final @Nullable Instance<AgentCard> extendedAgentCard;
private final RequestHandler requestHandler;
private final Instance<CallContextFactory> callContextFactoryInstance;
private final Executor executor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package org.a2aproject.sdk.server.grpc.quarkus;

import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicBoolean;

import jakarta.enterprise.inject.Instance;

import org.a2aproject.sdk.server.ResolvedInstance;
import org.a2aproject.sdk.server.FixedInstance;
import org.a2aproject.sdk.server.TestInstances;
import org.a2aproject.sdk.spec.AgentCapabilities;
import org.a2aproject.sdk.spec.AgentCard;
import org.a2aproject.sdk.spec.AgentInterface;
import org.a2aproject.sdk.transport.grpc.handler.CallContextFactory;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Verifies that {@link QuarkusGrpcHandler} does not eagerly resolve {@link Instance} parameters
Expand All @@ -19,26 +29,45 @@ class QuarkusGrpcHandlerLazyResolutionTest {

@Test
void constructorDoesNotResolveAgentCardInstances() {
Instance<AgentCard> throwOnGet = new ResolvedInstance<>(null) {
@Override
public AgentCard get() {
throw new AssertionError("Instance.get() must not be called during construction");
}

@Override
public boolean isUnsatisfied() {
return false;
}
};

@SuppressWarnings("unchecked")
Instance<CallContextFactory> emptyCallContextFactory = (Instance<CallContextFactory>) (Instance<?>) new ResolvedInstance<>(null);
Instance<AgentCard> throwOnGet = TestInstances.throwOnGet();

assertDoesNotThrow(() -> new QuarkusGrpcHandler(
throwOnGet,
throwOnGet,
null,
emptyCallContextFactory,
FixedInstance.empty(),
Runnable::run));
}

@Test
void getExtendedAgentCardResolvesOnAccess() {
AtomicBoolean resolved = new AtomicBoolean(false);
AgentCard expected = AgentCard.builder()
.name("extended").description("test").version("1.0.0")
.supportedInterfaces(Collections.singletonList(new AgentInterface("gRPC", "http://localhost:9999")))
.capabilities(AgentCapabilities.builder().build())
.defaultInputModes(new ArrayList<>())
.defaultOutputModes(new ArrayList<>())
.skills(new ArrayList<>()).build();

Instance<AgentCard> trackingInstance = new FixedInstance<>(expected) {
@Override
public AgentCard get() {
resolved.set(true);
return super.get();
}
};

QuarkusGrpcHandler handler = new QuarkusGrpcHandler(
FixedInstance.empty(),
trackingInstance,
null,
FixedInstance.empty(),
Runnable::run);

assertFalse(resolved.get(), "should not resolve during construction");
AgentCard result = handler.getExtendedAgentCard();
assertTrue(resolved.get(), "should resolve on access");
assertEquals(expected, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
import java.util.List;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import jakarta.enterprise.inject.Instance;

import org.a2aproject.sdk.spec.AgentCard;
import org.a2aproject.sdk.spec.AgentInterface;
import org.a2aproject.sdk.spec.TransportProtocol;
Expand All @@ -25,10 +30,51 @@ public class AgentCardValidator {
public static final String SKIP_GRPC_PROPERTY = "org.a2aproject.sdk.transport.grpc.skipValidation";
public static final String SKIP_REST_PROPERTY = "org.a2aproject.sdk.transport.rest.skipValidation";

/**
* Resolves an {@link AgentCard} from the given {@link Instance} and validates its transport
* configuration exactly once using the default {@link #validateTransportConfiguration} check.
* The {@code transportValidated} guard is reset on failure so validation can be retried on
* the next call.
*
* @param agentCardInstance the CDI instance holding the agent card
* @param transportValidated atomic guard ensuring validation runs once
* @return the resolved agent card
*/
public static AgentCard resolveAndValidateOnce(Instance<AgentCard> agentCardInstance,
AtomicBoolean transportValidated) {
return resolveAndValidateOnce(agentCardInstance::get, transportValidated,
AgentCardValidator::validateTransportConfiguration);
}

/**
* Obtains an {@link AgentCard} from the given supplier and applies the provided validator
* exactly once. The {@code transportValidated} guard is reset on failure so validation can
* be retried on the next call.
*
* @param agentCardSupplier supplier that produces the agent card
* @param transportValidated atomic guard ensuring validation runs once
* @param validator validation logic to apply on first access
* @return the resolved agent card
*/
public static AgentCard resolveAndValidateOnce(Supplier<AgentCard> agentCardSupplier,
AtomicBoolean transportValidated,
Consumer<AgentCard> validator) {
AgentCard card = agentCardSupplier.get();
if (transportValidated.compareAndSet(false, true)) {
try {
validator.accept(card);
} catch (RuntimeException e) {
transportValidated.set(false);
throw e;
}
}
return card;
}

/**
* Validates the transport configuration of an AgentCard against available transports found on the classpath.
* Logs warnings for missing transports and errors for unsupported transports.
*
*
* @param agentCard the agent card to validate
*/
public static void validateTransportConfiguration(AgentCard agentCard) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,44 @@
import org.jspecify.annotations.Nullable;

/**
* An {@link Instance} wrapper for a pre-resolved value, for use in non-CDI contexts
* such as convenience constructors and tests.
* A static {@link Instance} wrapper holding a pre-resolved (or absent) value,
* for use in non-CDI contexts such as convenience constructors and tests.
* A {@code null} value represents an unsatisfied instance.
*
* <p>Usage example (wrapping a concrete value):
* <pre>{@code
* Instance<AgentCard> instance = new FixedInstance<>(myAgentCard);
* }</pre>
*
* <p>Usage example (empty / unsatisfied):
* <pre>{@code
* Instance<AgentCard> empty = FixedInstance.empty();
* }</pre>
*
* @param <T> the bean type
*/
public class ResolvedInstance<T> implements Instance<T> {
public class FixedInstance<T> implements Instance<T> {

@SuppressWarnings("rawtypes")
private static final FixedInstance EMPTY = new FixedInstance<>(null);

private final @Nullable T value;

public ResolvedInstance(@Nullable T value) {
public FixedInstance(@Nullable T value) {
this.value = value;
}

/**
* Returns a {@code FixedInstance} with no value (unsatisfied).
*
* @param <T> the bean type
* @return an empty instance
*/
@SuppressWarnings("unchecked")
public static <T> FixedInstance<T> empty() {
return (FixedInstance<T>) EMPTY;
}

@Override
public T get() {
if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.a2aproject.sdk.server;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
Expand Down Expand Up @@ -229,6 +232,44 @@ void testSkipPropertiesFilterWarnings() {
}
}

@Test
void resolveAndValidateOnceRetriesAfterFailure() {
AgentCard card = createTestAgentCardBuilder().build();
AtomicBoolean guard = new AtomicBoolean(false);
AtomicInteger validationCount = new AtomicInteger(0);

assertThrows(IllegalStateException.class, () ->
AgentCardValidator.resolveAndValidateOnce(
() -> card, guard, c -> {
validationCount.incrementAndGet();
throw new IllegalStateException("transient failure");
}));

assertFalse(guard.get(), "guard should be reset after failure");
assertEquals(1, validationCount.get());

AgentCard result = AgentCardValidator.resolveAndValidateOnce(
() -> card, guard, c -> validationCount.incrementAndGet());

assertTrue(guard.get(), "guard should be set after success");
assertEquals(2, validationCount.get());
assertEquals(card, result);
}

@Test
void resolveAndValidateOnceSkipsValidationOnSubsequentCalls() {
AgentCard card = createTestAgentCardBuilder().build();
AtomicBoolean guard = new AtomicBoolean(false);
AtomicInteger validationCount = new AtomicInteger(0);

AgentCardValidator.resolveAndValidateOnce(
() -> card, guard, c -> validationCount.incrementAndGet());
AgentCardValidator.resolveAndValidateOnce(
() -> card, guard, c -> validationCount.incrementAndGet());

assertEquals(1, validationCount.get(), "validation should run only once");
}

// A simple log handler for testing
private static class TestLogHandler extends Handler {
private final List<String> logMessages = new java.util.ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.a2aproject.sdk.server;

import jakarta.enterprise.inject.Instance;

/**
* Factory methods for {@link Instance} test doubles built on {@link FixedInstance}.
*/
public final class TestInstances {

private TestInstances() {
}

/**
* Returns an {@link Instance} that throws {@link AssertionError} on {@link Instance#get()},
* useful for verifying that a constructor does not eagerly resolve the instance.
*
* @param <T> the bean type
* @return an instance that throws on resolution
*/
public static <T> Instance<T> throwOnGet() {
return new FixedInstance<>(null) {
@Override
public T get() {
throw new AssertionError("Instance.get() must not be called during construction");
}

@Override
public boolean isUnsatisfied() {
return false;
}
};
}
}
Loading
Loading