feat: AuditLog 模型与索引注册(里程碑 2)
- AuditLog Extension(group audit.example.com 占位)+ AuditLogSpec,字段与产品设计 4.1 一一对应 - 操作时间复用 metadata.creationTimestamp(D3) - start() 注册 6 个业务索引(username/resourceType/method/statusCode/resourceName/path),stop() 注销 - 资源名称/路径索引用于关键字模糊筛选(QueryFactory.contains 需索引字段) - 生命周期测试覆盖注册索引清单与注销
This commit is contained in:
+95
@@ -0,0 +1,95 @@
|
||||
package run.halo.operationaudit;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Consumer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pf4j.RuntimeMode;
|
||||
import run.halo.app.extension.GroupVersionKind;
|
||||
import run.halo.app.extension.Scheme;
|
||||
import run.halo.app.extension.SchemeManager;
|
||||
import run.halo.app.extension.index.IndexSpecs;
|
||||
import run.halo.app.extension.index.ValueIndexSpec;
|
||||
import run.halo.app.plugin.PluginContext;
|
||||
import run.halo.operationaudit.audit.AuditLog;
|
||||
|
||||
/**
|
||||
* 插件生命周期测试:启动注册 {@link AuditLog} 模型与全部索引,停止注销。
|
||||
*/
|
||||
class OperationAuditPluginTest {
|
||||
|
||||
@Test
|
||||
void shouldRegisterAuditLogWithExpectedIndexesOnStart() {
|
||||
var schemeManager = mock(SchemeManager.class);
|
||||
var registeredSpecs = new AtomicReference<Consumer<IndexSpecs<AuditLog>>>();
|
||||
doAnswer(invocation -> {
|
||||
registeredSpecs.set(invocation.getArgument(1));
|
||||
return null;
|
||||
}).when(schemeManager).register(eq(AuditLog.class), any());
|
||||
|
||||
var plugin = new OperationAuditPlugin(pluginContext(), schemeManager);
|
||||
plugin.start();
|
||||
|
||||
var recordingSpecs = new RecordingIndexSpecs();
|
||||
registeredSpecs.get().accept(recordingSpecs);
|
||||
|
||||
assertThat(recordingSpecs.names()).containsExactlyInAnyOrder(
|
||||
"spec.username",
|
||||
"spec.resourceType",
|
||||
"spec.method",
|
||||
"spec.statusCode",
|
||||
"spec.resourceName",
|
||||
"spec.path");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldUnregisterSchemeOnStop() {
|
||||
var schemeManager = mock(SchemeManager.class);
|
||||
var scheme = mock(Scheme.class);
|
||||
var gvk = new GroupVersionKind("audit.example.com", "v1alpha1", "AuditLog");
|
||||
org.mockito.Mockito.when(schemeManager.fetch(gvk))
|
||||
.thenReturn(java.util.Optional.of(scheme));
|
||||
|
||||
var plugin = new OperationAuditPlugin(pluginContext(), schemeManager);
|
||||
plugin.stop();
|
||||
|
||||
verify(schemeManager).unregister(scheme);
|
||||
}
|
||||
|
||||
private static PluginContext pluginContext() {
|
||||
return PluginContext.builder()
|
||||
.name("operation-audit")
|
||||
.version("0.0.1")
|
||||
.configMapName("operation-audit-configmap")
|
||||
.runtimeMode(RuntimeMode.DEVELOPMENT)
|
||||
.build();
|
||||
}
|
||||
|
||||
/** 记录型 IndexSpecs 桩,捕获注册的索引名。 */
|
||||
private static final class RecordingIndexSpecs implements IndexSpecs<AuditLog> {
|
||||
|
||||
private final List<ValueIndexSpec<AuditLog, ?>> specs = new ArrayList<>();
|
||||
|
||||
List<String> names() {
|
||||
return specs.stream().map(ValueIndexSpec::getName).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <K extends Comparable<K>> void add(ValueIndexSpec<AuditLog, K> indexSpec) {
|
||||
specs.add(indexSpec);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ValueIndexSpec<AuditLog, ?>> getIndexSpecs() {
|
||||
return specs;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user