Files
moyi-model-intelligence-hub/control-plane-java/src/test/java/com/moyi/controlplane/gateway/GatewayDeploymentTest.java
T

342 lines
22 KiB
Java
Raw Normal View History

package com.moyi.controlplane.gateway;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.moyi.controlplane.catalog.CatalogService;
import com.moyi.controlplane.common.ApiException;
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
import com.moyi.controlplane.task.AsyncTaskService;
import com.moyi.controlplane.usage.UsageAuditService;
import com.moyi.controlplane.wallet.ChatBillingService;
import com.moyi.controlplane.gateway.request.ApiRequestRiskEngine;
import com.moyi.controlplane.gateway.request.ApiRequestRiskPolicy;
import com.moyi.controlplane.gateway.request.GatewayRequestExecutionService;
import org.springframework.mock.web.MockHttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.jdbc.core.JdbcTemplate;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
class GatewayDeploymentTest {
private static GatewayController controller(CatalogService catalog, OpenAiCompatibleProvider provider,
GatewayGuard guard, ChatBillingService billing, ObjectMapper json) {
doReturn(mock(GatewayGuard.Admission.class)).when(guard).acquire(anyMap(), any(UUID.class), anyLong());
var requests = new GatewayRequestExecutionService(billing,
new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()));
return new GatewayController(catalog, provider, guard, requests, json);
}
@Test void failedTaskClaimReleasesRedisLeaseAfterDatabaseRollback() {
var redis=mock(StringRedisTemplate.class);doReturn(1L).when(redis).execute(any(RedisScript.class),anyList(),any(Object[].class));
var policies=mock(EnterprisePolicies.class);UUID key=UUID.randomUUID(),user=UUID.randomUUID();
when(policies.taskLimit(key)).thenReturn(new EnterprisePolicies.UserLimit(user,120,16));
var guard=new GatewayGuard(redis,60,300,4,"moyi:test:");guard.setEnterprisePolicies(policies);
org.springframework.transaction.support.TransactionSynchronizationManager.initSynchronization();
try {
assertTrue(guard.tryTaskUserSlot(key,"isolated-task",3600000));
clearInvocations(redis);
for(var callback:org.springframework.transaction.support.TransactionSynchronizationManager.getSynchronizations()) callback.afterCompletion(org.springframework.transaction.support.TransactionSynchronization.STATUS_ROLLED_BACK);
verify(redis).execute(any(RedisScript.class),eq(List.of("moyi:test:gateway:slots:user:"+user)),eq("isolated-task"));
} finally {org.springframework.transaction.support.TransactionSynchronizationManager.clearSynchronization();}
}
@Test
void capacitySnapshotReadsBothLiveAndWaitingBucketsAndDoesNotInventZeroOnFailure() {
StringRedisTemplate redis = mock(StringRedisTemplate.class);
doReturn(List.of(2L, 3L, 0L, 1L)).when(redis).execute(any(RedisScript.class), anyList());
GatewayGuard guard = new GatewayGuard(redis, 60, 300, 4, "moyi:test:");
var snapshot = guard.capacitySnapshot(List.of("model-a", "model-b"));
assertTrue(snapshot.available());
assertEquals(new GatewayGuard.Capacity(2, 3), snapshot.groups().get("model-a"));
assertEquals(new GatewayGuard.Capacity(0, 1), snapshot.groups().get("model-b"));
verify(redis).execute(any(RedisScript.class), eq(List.of(
"moyi:test:gateway:slots:model:model-a", "moyi:test:gateway:waiting:model:model-a",
"moyi:test:gateway:slots:model:model-b", "moyi:test:gateway:waiting:model:model-b")));
doThrow(new org.springframework.dao.DataAccessResourceFailureException("offline")).when(redis).execute(any(RedisScript.class), anyList());
assertFalse(guard.capacitySnapshot(List.of("model-a")).available());
assertTrue(guard.capacitySnapshot(List.of("model-a")).groups().isEmpty());
}
@Test
void queueTimeoutCleansItsWaitingEntry() {
StringRedisTemplate redis = mock(StringRedisTemplate.class);
doReturn(0L).when(redis).execute(any(RedisScript.class), anyList(), any(Object[].class));
GatewayGuard guard = new GatewayGuard(redis, 60, 300, 1, 1, 15, 10, "moyi:test:");
assertThrows(GatewayLimitException.class, () -> guard.acquire(Map.of("id", "model-a"), UUID.randomUUID(), 15));
verify(redis).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:waiting:model:model-a")), any(Object[].class));
}
@Test
void redisRateKeysAreEnvironmentScoped() {
StringRedisTemplate redis = mock(StringRedisTemplate.class);
doReturn(0L).when(redis).execute(any(RedisScript.class), anyList(), any(Object[].class));
UUID keyId = UUID.randomUUID();
new GatewayGuard(redis, 60, 300, 4, "moyi:test:").checkRate(keyId, false);
verify(redis).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:rate:invoke:" + keyId)), any(Object[].class));
}
@Test
void keyConcurrencyUsesDedicatedRedisBucket() {
StringRedisTemplate redis = mock(StringRedisTemplate.class);
doReturn(1L).when(redis).execute(any(RedisScript.class), anyList(), any(Object[].class));
UUID keyId = UUID.randomUUID();
try (GatewayGuard.Slot ignored = new GatewayGuard(redis, 60, 300, 4, 2, "moyi:test:").acquireKey(keyId)) {
// The lease is intentionally held for the duration of this assertion.
}
verify(redis, atLeastOnce()).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:slots:key:" + keyId)), any(Object[].class));
}
@Test
void zeroModelConcurrencyUsesOnlyTheIndependentKeyLease() {
StringRedisTemplate redis = mock(StringRedisTemplate.class);
doReturn(1L).when(redis).execute(any(RedisScript.class), anyList(), any(Object[].class));
UUID keyId = UUID.randomUUID();
Map<String, Object> unlimited = Map.of("id", "model-unlimited", "maxConcurrency", 0);
try (GatewayGuard.Admission ignored = new GatewayGuard(redis, 60, 300, 4, 2, 15, 10, "moyi:test:").acquire(unlimited, keyId, 15)) {
// The route is deliberately unlimited at the MOYI model layer.
}
verify(redis, never()).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:slots:model:model-unlimited")), any(Object[].class));
verify(redis, atLeastOnce()).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:slots:key:" + keyId)), any(Object[].class));
}
@Test
void disabledModelGuardDoesNotCreateModelLease() {
StringRedisTemplate redis = mock(StringRedisTemplate.class);
doReturn(1L).when(redis).execute(any(RedisScript.class), anyList(), any(Object[].class));
UUID keyId = UUID.randomUUID();
Map<String, Object> model = Map.of("id", "model-measurement", "maxConcurrency", 1);
try (GatewayGuard.Admission ignored = new GatewayGuard(redis, 60, 300, 4, 2, 15, 10, "moyi:test:", true).acquire(model, keyId, 15)) {
// Measurement mode bypasses only the MOYI model-group lease.
}
verify(redis, never()).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:slots:model:model-measurement")), any(Object[].class));
verify(redis, atLeastOnce()).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:slots:key:" + keyId)), any(Object[].class));
}
@Test
void modelsExposeReadableIdAndKeepRouteIdForDiagnostics() {
CatalogService catalog = mock(CatalogService.class);
GatewayGuard guard = mock(GatewayGuard.class);
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
Map<String, Object> route = Map.of(
"id", "route-1",
"providerId", "provider-apihub",
"modelKey", "deepseek-v4-flash",
"publicName", "DeepSeek V4 Flash",
"vendorName", "DeepSeek",
"sourceLabel", "ApiHub 国内",
"upstreamModel", "deepseek-v4-flash",
"type", "text",
"mode", "sync"
);
when(catalog.authenticateKey("test-key")).thenReturn(key);
when(catalog.listModels(false)).thenReturn(List.of(route));
when(catalog.keyCanInvoke(key, "route-1")).thenReturn(true);
GatewayController controller = controller(catalog, mock(OpenAiCompatibleProvider.class), guard, mock(ChatBillingService.class), new ObjectMapper());
Map<String, Object> response = controller.models("Bearer test-key");
@SuppressWarnings("unchecked")
Map<String, Object> item = ((List<Map<String, Object>>) response.get("data")).getFirst();
assertEquals("deepseek-v4-flash--apihub--" + tag("route-1"), item.get("id"));
assertEquals("route-1", item.get("moyi_route_id"));
assertEquals("DeepSeek V4 Flash", item.get("name"));
assertEquals("deepseek-v4-flash", item.get("model_key"));
assertEquals("DeepSeek V4 Flash", item.get("public_name"));
assertEquals("DeepSeek", item.get("vendor_name"));
assertEquals("ApiHub 国内", item.get("source_label"));
assertEquals("deepseek-v4-flash", item.get("upstream_model"));
}
@Test
void duplicateModelKeysAreDisambiguatedBySourceGroup() {
var first = Map.<String, Object>of(
"id", "route-a", "providerId", "provider-a", "modelKey", "gpt-5.5",
"publicName", "GPT-5.5", "sourceLabel", "ApiHub 国内", "upstreamModel", "gpt-5.5");
var second = Map.<String, Object>of(
"id", "route-b", "providerId", "provider-b", "modelKey", "gpt-5.5",
"publicName", "GPT-5.5", "sourceLabel", "APIPro", "upstreamModel", "gpt-5.5");
var visible = List.of(first, second);
assertEquals("gpt-5.5--apihub--" + tag("route-a"), GatewayModelIds.publicId(first));
assertEquals("gpt-5.5--apipro--" + tag("route-b"), GatewayModelIds.publicId(second));
}
@Test
void readableIdResolvesOnlyToAnAuthorizedRoute() {
CatalogService catalog = mock(CatalogService.class);
GatewayGuard guard = mock(GatewayGuard.class);
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
Map<String, Object> route = Map.of(
"id", "route-readable", "providerId", "provider-a", "modelKey", "glm-5",
"publicName", "GLM-5", "sourceLabel", "ApiHub 国内", "upstreamModel", "glm-5");
when(catalog.activeModel("glm-5--apihub")).thenReturn(null);
when(catalog.listModels(false)).thenReturn(List.of(route));
when(catalog.keyCanInvoke(key, "route-readable")).thenReturn(true);
GatewayController controller = controller(catalog, mock(OpenAiCompatibleProvider.class), guard, mock(ChatBillingService.class), new ObjectMapper());
assertSame(route, controller.resolveModel(key, "glm-5--apihub--" + tag("route-readable")));
verify(catalog).keyCanInvoke(key, "route-readable");
}
@Test
void legacyRouteIdRemainsCompatibleAndUnauthorizedRouteIsRejected() {
CatalogService catalog = mock(CatalogService.class);
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
Map<String, Object> route = Map.of("id", "route-legacy", "modelKey", "gpt-5", "sourceLabel", "APIPro");
when(catalog.listModels(false)).thenReturn(List.of());
when(catalog.activeModel("route-legacy")).thenReturn(route);
when(catalog.keyCanInvoke(key, "route-legacy")).thenReturn(true);
assertSame(route, GatewayModelIds.resolve(catalog, key, "route-legacy"));
Map<String, Object> forbidden = Map.of("id", "route-forbidden", "modelKey", "gpt-5", "sourceLabel", "APIPro");
when(catalog.activeModel("route-forbidden")).thenReturn(forbidden);
when(catalog.keyCanInvoke(key, "route-forbidden")).thenReturn(false);
ApiException error = assertThrows(ApiException.class, () -> GatewayModelIds.resolve(catalog, key, "route-forbidden"));
assertEquals("MODEL_FORBIDDEN", error.getCode());
assertEquals(403, error.getStatus());
}
@Test
void routeTagKeepsPreviouslyImportedIdUsableAfterRename() {
CatalogService catalog = mock(CatalogService.class);
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
Map<String, Object> renamed = Map.of(
"id", "route-renamed", "modelKey", "new-model-key", "publicName", "New Model",
"sourceLabel", "New Group", "upstreamModel", "new-upstream");
String oldPublicId = "old-model--old-group--" + tag("route-renamed");
when(catalog.listModels(false)).thenReturn(List.of(renamed));
when(catalog.keyCanInvoke(key, "route-renamed")).thenReturn(true);
assertSame(renamed, GatewayModelIds.resolve(catalog, key, oldPublicId));
verify(catalog, never()).activeModel(oldPublicId);
}
@Test
void legacyAliasWithMultipleAuthorizedRoutesIsRejectedAsAmbiguous() {
CatalogService catalog = mock(CatalogService.class);
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
Map<String, Object> first = Map.of("id", "route-a", "modelKey", "same-model", "upstreamModel", "same-model", "sourceLabel", "A");
Map<String, Object> second = Map.of("id", "route-b", "modelKey", "same-model", "upstreamModel", "same-model", "sourceLabel", "B");
when(catalog.listModels(false)).thenReturn(List.of(first, second));
when(catalog.activeModel("same-model")).thenReturn(null);
when(catalog.keyCanInvoke(key, "route-a")).thenReturn(true);
when(catalog.keyCanInvoke(key, "route-b")).thenReturn(true);
ApiException error = assertThrows(ApiException.class, () -> GatewayModelIds.resolve(catalog, key, "same-model"));
assertEquals("MODEL_AMBIGUOUS", error.getCode());
assertEquals(400, error.getStatus());
}
private static String tag(String route) {
try {
return java.util.HexFormat.of().formatHex(java.security.MessageDigest.getInstance("SHA-256").digest(route.getBytes(java.nio.charset.StandardCharsets.UTF_8))).substring(0, 16);
} catch (java.security.NoSuchAlgorithmException exception) { throw new AssertionError(exception); }
}
@Test
void invalidRedisPrefixIsRejected() {
assertThrows(IllegalArgumentException.class, () -> new GatewayGuard(mock(StringRedisTemplate.class), 60, 300, 4, ""));
}
@Test
void unconfiguredRealProviderNeverReturnsMockSuccess() {
CatalogService catalog = mock(CatalogService.class);
GatewayGuard guard = mock(GatewayGuard.class);
OpenAiCompatibleProvider provider = mock(OpenAiCompatibleProvider.class);
UsageAuditService usage = mock(UsageAuditService.class);
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", UUID.randomUUID().toString());
when(catalog.authenticateKey("test-key")).thenReturn(key);
when(catalog.activeModel("model-real")).thenReturn(Map.of("id", "model-real", "providerId", "provider-deepseek", "mode", "sync", "type", "text"));
when(catalog.keyCanInvoke(key, "model-real")).thenReturn(true);
GatewayController controller = controller(catalog, provider, guard, mock(ChatBillingService.class),new ObjectMapper());
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "model-real"),null,new MockHttpServletResponse()));
assertEquals("PROVIDER_NOT_CONFIGURED", error.getCode());
assertEquals(503, error.getStatus());
2026-09-29 14:46:02 +08:00
verify(provider, never()).chat(anyString(), anyString(), anyString(), anyMap());
}
@Test
void explicitMockProviderStillWorks() {
CatalogService catalog = mock(CatalogService.class);
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", UUID.randomUUID().toString());
when(catalog.authenticateKey("test-key")).thenReturn(key);
when(catalog.activeModel("mock")).thenReturn(Map.of("id", "mock", "providerId", "provider-mock", "mode", "sync", "type", "text"));
when(catalog.keyCanInvoke(key, "mock")).thenReturn(true);
var billing=mock(ChatBillingService.class);
when(billing.reserve(anyMap(),anyMap(),any(),any(),any())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(),UUID.fromString(key.get("userId").toString()),ChatMeter.rates(null,true)));
when(billing.settle(any(),anyMap(),anyInt(),any())).thenReturn(java.math.BigDecimal.ZERO);
GatewayController controller = controller(catalog, mock(OpenAiCompatibleProvider.class), mock(GatewayGuard.class), billing,new ObjectMapper());
assertEquals(200, controller.chatCompletions("Bearer test-key", Map.of("model", "mock","messages",List.of(Map.of("role","user","content","test"))),null,new MockHttpServletResponse()).getStatusCode().value());
}
@Test
void syncResponsePreservesReasoningAndFinishReasonWhenContentIsEmpty() {
CatalogService catalog = mock(CatalogService.class);
GatewayGuard guard = mock(GatewayGuard.class);
OpenAiCompatibleProvider provider = mock(OpenAiCompatibleProvider.class);
ChatBillingService billing = mock(ChatBillingService.class);
String userId = UUID.randomUUID().toString();
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", userId);
Map<String, Object> model = Map.of("id", "reasoning-route", "providerId", "provider-relay", "upstreamModel", "reasoning-model", "mode", "sync", "type", "text");
when(catalog.authenticateKey("test-key")).thenReturn(key);
when(catalog.activeModel("reasoning-route")).thenReturn(model);
when(catalog.keyCanInvoke(key, "reasoning-route")).thenReturn(true);
2026-09-29 14:46:02 +08:00
when(provider.isConfigured("provider-relay", "reasoning-route", "reasoning-model")).thenReturn(true);
Map<String, Object> upstreamResponse = new java.util.LinkedHashMap<>(Map.of(
"id", "upstream-response",
"choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", "", "reasoning_content", "private reasoning"), "finish_reason", "length")),
"usage", Map.of("prompt_tokens", 2, "completion_tokens", 16, "total_tokens", 18)
));
2026-09-29 14:46:02 +08:00
when(provider.chat(eq("provider-relay"), eq("reasoning-model"), eq("reasoning-route"), anyMap())).thenReturn(upstreamResponse);
when(billing.reserve(anyMap(), anyMap(), any(), isNull(), any())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
when(billing.settle(any(), anyMap(), anyInt(), isNull())).thenReturn(java.math.BigDecimal.ZERO);
GatewayController controller = controller(catalog, provider, guard, billing, new ObjectMapper());
var response = controller.chatCompletions("Bearer test-key", Map.of("model", "reasoning-route", "messages", List.of(Map.of("role", "user", "content", "hello"))), null, new MockHttpServletResponse());
assertEquals(200, response.getStatusCode().value());
Map<?, ?> choice = (Map<?, ?>)((List<?>)response.getBody().get("choices")).getFirst();
Map<?, ?> message = (Map<?, ?>)choice.get("message");
assertEquals("", message.get("content"));
assertEquals("private reasoning", message.get("reasoning_content"));
assertEquals("length", choice.get("finish_reason"));
}
@Test
void trulyEmptySyncResponseIsRejectedBeforeSettlement() {
CatalogService catalog = mock(CatalogService.class);
GatewayGuard guard = mock(GatewayGuard.class);
OpenAiCompatibleProvider provider = mock(OpenAiCompatibleProvider.class);
ChatBillingService billing = mock(ChatBillingService.class);
String userId = UUID.randomUUID().toString();
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", userId);
Map<String, Object> model = Map.of("id", "empty-route", "providerId", "provider-relay", "upstreamModel", "empty-model", "mode", "sync", "type", "text");
when(catalog.authenticateKey("test-key")).thenReturn(key);
when(catalog.activeModel("empty-route")).thenReturn(model);
when(catalog.keyCanInvoke(key, "empty-route")).thenReturn(true);
2026-09-29 14:46:02 +08:00
when(provider.isConfigured("provider-relay", "empty-route", "empty-model")).thenReturn(true);
Map<String, Object> upstreamResponse = new java.util.LinkedHashMap<>(Map.of(
"id", "upstream-empty",
"choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", ""), "finish_reason", "stop"))
));
2026-09-29 14:46:02 +08:00
when(provider.chat(eq("provider-relay"), eq("empty-model"), eq("empty-route"), anyMap())).thenReturn(upstreamResponse);
when(billing.reserve(anyMap(), anyMap(), any(), isNull(), any())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
GatewayController controller = controller(catalog, provider, guard, billing, new ObjectMapper());
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "empty-route", "messages", List.of(Map.of("role", "user", "content", "hello"))), null, new MockHttpServletResponse()));
assertEquals("UPSTREAM_RESPONSE_INVALID", error.getCode());
verify(billing, never()).settle(any(), anyMap(), anyInt(), any());
verify(billing).fail(any(), eq("UPSTREAM_RESPONSE_INVALID"), anyString(), anyInt());
}
@Test
void unimplementedMediaProviderDoesNotCreateFakeTask() {
JdbcTemplate jdbc = mock(JdbcTemplate.class);
CatalogService catalog = mock(CatalogService.class);
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
when(catalog.authenticateKey("test-key")).thenReturn(key);
when(catalog.activeModel("tripo")).thenReturn(Map.of("id", "tripo", "providerId", "provider-vast", "type", "3d", "mode", "async"));
when(catalog.keyCanInvoke(key, "tripo")).thenReturn(true);
AsyncTaskService tasks = new AsyncTaskService(jdbc, new ObjectMapper(), catalog, 100, mock(GatewayGuard.class), mock(UsageAuditService.class));
ApiException error = assertThrows(ApiException.class, () -> tasks.create("Bearer test-key", "3d", Map.of("model", "tripo", "prompt", "test")));
assertEquals(501, error.getStatus());
verifyNoInteractions(jdbc);
}
}