|
|
@@ -0,0 +1,261 @@
|
|
|
+package com.zsjz.ai.module.eds.xml;
|
|
|
+
|
|
|
+import cn.hutool.extra.spring.SpringUtil;
|
|
|
+import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
|
|
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
|
|
+import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
|
|
|
+import com.baomidou.mybatisplus.core.config.GlobalConfig;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
|
|
|
+import com.thoughtworks.xstream.XStream;
|
|
|
+import com.thoughtworks.xstream.security.WildcardTypePermission;
|
|
|
+import com.zsjz.ai.common.config.DuckdbUnpooledDataSource;
|
|
|
+import com.zsjz.ai.module.dm.mapper.FileInfoMapper;
|
|
|
+import com.zsjz.ai.module.eds.duckdb.EdsImportActivityProbe;
|
|
|
+import com.zsjz.ai.module.eds.mapper.EdsImportMaterialMapper;
|
|
|
+import com.zsjz.ai.module.eds.mapper.EdsMediaFileMapper;
|
|
|
+import com.zsjz.ai.module.eds.mapper.EdsTreeCountMapper;
|
|
|
+import com.zsjz.ai.module.eds.utils.Eds;
|
|
|
+import org.apache.ibatis.mapping.Environment;
|
|
|
+import org.apache.ibatis.session.SqlSession;
|
|
|
+import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
+import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
|
|
|
+import org.junit.jupiter.api.io.TempDir;
|
|
|
+import org.springframework.context.support.GenericApplicationContext;
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
+import org.springframework.data.redis.core.ValueOperations;
|
|
|
+
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.StandardCopyOption;
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.DriverManager;
|
|
|
+import java.sql.PreparedStatement;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.Statement;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.atomic.AtomicLong;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+import static org.mockito.ArgumentMatchers.anyCollection;
|
|
|
+import static org.mockito.ArgumentMatchers.anyLong;
|
|
|
+import static org.mockito.ArgumentMatchers.anyString;
|
|
|
+import static org.mockito.Mockito.atLeastOnce;
|
|
|
+import static org.mockito.Mockito.doAnswer;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 真实 BCP 演示包入库测试(需要显式指定包路径)。
|
|
|
+ *
|
|
|
+ * <p>走真实 {@link EdsExtra#extraZipData}:真实 7-Zip 解压、真实 BCP 解析、
|
|
|
+ * 真实 DuckDB Appender 写主表、真实 MyBatis 写 file_info/eds_tree_count/eds_media_file。
|
|
|
+ * 只有 Redis 和动态数据源路由用 mock 隔离。</p>
|
|
|
+ *
|
|
|
+ * <pre>
|
|
|
+ * mvn -pl ai-server -Dtest=DemoBcpImportTest \
|
|
|
+ * -Deds.demo.zip="C:/Users/cc/Desktop/test/演示bcp包/李如云演示数据.zip" test
|
|
|
+ * </pre>
|
|
|
+ *
|
|
|
+ * @author zsjz
|
|
|
+ */
|
|
|
+@EnabledIfSystemProperty(named = "eds.demo.zip", matches = ".+")
|
|
|
+class DemoBcpImportTest {
|
|
|
+
|
|
|
+ @TempDir
|
|
|
+ Path tempDir;
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ void importsDemoPackageIntoCaseDuckDb() throws Exception {
|
|
|
+ Path zipSource = Path.of(System.getProperty("eds.demo.zip"));
|
|
|
+ assertTrue(Files.isRegularFile(zipSource), "演示包不存在: " + zipSource);
|
|
|
+
|
|
|
+ Path workDir = tempDir.resolve("work");
|
|
|
+ Files.createDirectories(workDir);
|
|
|
+ String zipName = zipSource.getFileName().toString();
|
|
|
+ Path zipCopy = workDir.resolve(zipName);
|
|
|
+ Files.copy(zipSource, zipCopy, StandardCopyOption.REPLACE_EXISTING);
|
|
|
+
|
|
|
+ Path caseDbSource = findRepoFile("QingJian/workspace/9/ylzkBGZJ9");
|
|
|
+ Path caseDb = tempDir.resolve("case.duckdb");
|
|
|
+ Files.copy(caseDbSource, caseDb, StandardCopyOption.REPLACE_EXISTING);
|
|
|
+ String jdbcUrl = "jdbc:duckdb:" + caseDb;
|
|
|
+
|
|
|
+ DuckdbUnpooledDataSource caseDataSource = new DuckdbUnpooledDataSource(jdbcUrl, "org.duckdb.DuckDBDriver");
|
|
|
+ DynamicRoutingDataSource routingDataSource = mock(DynamicRoutingDataSource.class);
|
|
|
+ when(routingDataSource.getDataSource("slave")).thenReturn(caseDataSource);
|
|
|
+
|
|
|
+ StringRedisTemplate redisTemplate = mock(StringRedisTemplate.class);
|
|
|
+ ValueOperations<String, String> valueOperations = mock(ValueOperations.class);
|
|
|
+ when(redisTemplate.opsForValue()).thenReturn(valueOperations);
|
|
|
+ AtomicLong remaining = new AtomicLong();
|
|
|
+ doAnswer(invocation -> {
|
|
|
+ remaining.set(Long.parseLong(invocation.getArgument(1)));
|
|
|
+ return null;
|
|
|
+ }).when(valueOperations).set(anyString(), anyString());
|
|
|
+ when(valueOperations.decrement(anyString())).thenAnswer(invocation -> remaining.decrementAndGet());
|
|
|
+ when(valueOperations.increment(anyString(), anyLong())).thenReturn(1L);
|
|
|
+ when(redisTemplate.delete(anyCollection())).thenReturn(1L);
|
|
|
+
|
|
|
+ MybatisConfiguration configuration = new MybatisConfiguration(
|
|
|
+ new Environment("demo-case", new JdbcTransactionFactory(), caseDataSource));
|
|
|
+ GlobalConfigUtils.setGlobalConfig(configuration, GlobalConfigUtils.defaults());
|
|
|
+ configuration.addMapper(EdsImportMaterialMapper.class);
|
|
|
+ configuration.addMapper(FileInfoMapper.class);
|
|
|
+ configuration.addMapper(EdsTreeCountMapper.class);
|
|
|
+ configuration.addMapper(EdsMediaFileMapper.class);
|
|
|
+ SqlSessionFactory sqlSessionFactory = new MybatisSqlSessionFactoryBuilder().build(configuration);
|
|
|
+
|
|
|
+ XStream xStream = new XStream();
|
|
|
+ xStream.processAnnotations(Message.class);
|
|
|
+ xStream.addPermission(new WildcardTypePermission(new String[]{"com.zsjz.ai.module.eds.**"}));
|
|
|
+
|
|
|
+ ExtraFileDTO dto = new ExtraFileDTO(zipName, null, zipCopy, "test", 999L);
|
|
|
+
|
|
|
+ try (SqlSession sqlSession = sqlSessionFactory.openSession(true)) {
|
|
|
+ EdsImportMaterialMapper importMapper = sqlSession.getMapper(EdsImportMaterialMapper.class);
|
|
|
+ FileInfoMapper fileInfoMapper = sqlSession.getMapper(FileInfoMapper.class);
|
|
|
+ EdsTreeCountMapper treeCountMapper = sqlSession.getMapper(EdsTreeCountMapper.class);
|
|
|
+ EdsMediaFileMapper mediaFileMapper = sqlSession.getMapper(EdsMediaFileMapper.class);
|
|
|
+
|
|
|
+ GenericApplicationContext springContext = new GenericApplicationContext();
|
|
|
+ springContext.getBeanFactory().registerSingleton("edsMediaFileMapper", mediaFileMapper);
|
|
|
+ springContext.refresh();
|
|
|
+ new SpringUtil().setApplicationContext(springContext);
|
|
|
+
|
|
|
+ EdsExtra edsExtra = new EdsExtra(xStream, redisTemplate, importMapper, fileInfoMapper,
|
|
|
+ treeCountMapper, routingDataSource, new EdsImportActivityProbe());
|
|
|
+ try {
|
|
|
+ // 真实入口:真实 7-Zip 解压 -> 索引解析 -> BCP 解析 -> Appender/MyBatis 入库
|
|
|
+ edsExtra.extraZipData(dto);
|
|
|
+ } finally {
|
|
|
+ springContext.close();
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ caseDataSource.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ assertNotNull(dto.getCti(), "采集编号为空,说明基础信息没有解析成功");
|
|
|
+ assertEquals("李如云", dto.getUserName());
|
|
|
+
|
|
|
+ List<String> nonEmptyTables = new ArrayList<>();
|
|
|
+ long totalRows = 0;
|
|
|
+ try (Connection connection = DriverManager.getConnection(jdbcUrl)) {
|
|
|
+ assertEquals(1L, count(connection, "select count(*) from eds_mforensics_010100"));
|
|
|
+ assertEquals(dto.getCti(), queryString(connection,
|
|
|
+ "select collect_target_id from eds_mforensics_010100 limit 1"));
|
|
|
+ assertEquals(1L, count(connection, "select count(*) from eds_mforensics_020100"));
|
|
|
+ assertTrue(count(connection, "select count(*) from eds_mforensics_020500") > 0);
|
|
|
+ assertEquals(1L, countByFileName(connection, zipName));
|
|
|
+ assertEquals(0L, fileInfoPid(connection, zipName));
|
|
|
+ assertTrue(countByCid(connection, "eds_tree_count", dto.getCti()) > 0);
|
|
|
+ assertTrue(countByCid(connection, "eds_media_file", dto.getCti()) > 0);
|
|
|
+
|
|
|
+ // 校验 160100 的字段错位修复:索引里是 " FILE_SIZE"(带前导空格)
|
|
|
+ try (Statement st = connection.createStatement();
|
|
|
+ ResultSet rs = st.executeQuery("""
|
|
|
+ select file_size, brand, model, material_status
|
|
|
+ from eds_mforensics_160100 limit 1
|
|
|
+ """)) {
|
|
|
+ assertTrue(rs.next());
|
|
|
+ assertEquals("64", rs.getString("file_size"));
|
|
|
+ assertEquals("vivo", rs.getString("brand"));
|
|
|
+ assertEquals("Xplay6", rs.getString("model"));
|
|
|
+ assertNull(rs.getObject("material_status"));
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> tables = new ArrayList<>();
|
|
|
+ try (Statement st = connection.createStatement();
|
|
|
+ ResultSet rs = st.executeQuery("""
|
|
|
+ select table_name from information_schema.tables
|
|
|
+ where table_schema = 'main' and table_name like 'eds_mforensics%'
|
|
|
+ order by table_name
|
|
|
+ """)) {
|
|
|
+ while (rs.next()) {
|
|
|
+ tables.add(rs.getString(1));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (String table : tables) {
|
|
|
+ long rows = count(connection, "select count(*) from " + table);
|
|
|
+ if (rows > 0) {
|
|
|
+ nonEmptyTables.add(table + "=" + rows);
|
|
|
+ totalRows += rows;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ System.out.println("采集编号: " + dto.getCti());
|
|
|
+ System.out.println("有数据的表: " + nonEmptyTables);
|
|
|
+ System.out.println("主表总行数: " + totalRows);
|
|
|
+ System.out.println("file_info 行数: " + countByFileName(connection, zipName));
|
|
|
+ System.out.println("eds_tree_count 行数: " + countByCid(connection, "eds_tree_count", dto.getCti()));
|
|
|
+ System.out.println("eds_media_file 行数: " + countByCid(connection, "eds_media_file", dto.getCti()));
|
|
|
+ }
|
|
|
+ assertTrue(totalRows > 0, "没有任何主表数据入库");
|
|
|
+ verify(redisTemplate, atLeastOnce()).delete(anyCollection());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static long count(Connection connection, String sql) throws Exception {
|
|
|
+ try (Statement st = connection.createStatement();
|
|
|
+ ResultSet rs = st.executeQuery(sql)) {
|
|
|
+ rs.next();
|
|
|
+ return rs.getLong(1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static long countByFileName(Connection connection, String fileName) throws Exception {
|
|
|
+ try (PreparedStatement ps = connection.prepareStatement(
|
|
|
+ "select count(*) from file_info where fileName = ?")) {
|
|
|
+ ps.setString(1, fileName);
|
|
|
+ try (ResultSet rs = ps.executeQuery()) {
|
|
|
+ rs.next();
|
|
|
+ return rs.getLong(1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static long fileInfoPid(Connection connection, String fileName) throws Exception {
|
|
|
+ try (PreparedStatement ps = connection.prepareStatement(
|
|
|
+ "select pid from file_info where fileName = ? limit 1")) {
|
|
|
+ ps.setString(1, fileName);
|
|
|
+ try (ResultSet rs = ps.executeQuery()) {
|
|
|
+ return rs.next() ? rs.getLong(1) : -1L;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static long countByCid(Connection connection, String table, String cid) throws Exception {
|
|
|
+ try (PreparedStatement ps = connection.prepareStatement(
|
|
|
+ "select count(*) from " + table + " where collect_target_id = ?")) {
|
|
|
+ ps.setString(1, cid);
|
|
|
+ try (ResultSet rs = ps.executeQuery()) {
|
|
|
+ rs.next();
|
|
|
+ return rs.getLong(1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String queryString(Connection connection, String sql) throws Exception {
|
|
|
+ try (Statement st = connection.createStatement();
|
|
|
+ ResultSet rs = st.executeQuery(sql)) {
|
|
|
+ return rs.next() ? rs.getString(1) : null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Path findRepoFile(String relative) {
|
|
|
+ Path current = Path.of(System.getProperty("user.dir")).toAbsolutePath();
|
|
|
+ while (current != null) {
|
|
|
+ Path candidate = current.resolve(relative);
|
|
|
+ if (Files.isRegularFile(candidate)) {
|
|
|
+ return candidate;
|
|
|
+ }
|
|
|
+ current = current.getParent();
|
|
|
+ }
|
|
|
+ throw new IllegalStateException("找不到文件: " + relative);
|
|
|
+ }
|
|
|
+}
|