package com.ruoyi.projectdev; import com.fasterxml.jackson.databind.ObjectMapper; import com.ruoyi.web.service.assistant.AssistantPermissionGuard; import com.ruoyi.web.service.assistant.BusinessActionRegistry; import jakarta.validation.Validator; import java.lang.reflect.Method; import java.util.Map; import java.util.List; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfSystemProperty; import org.springframework.context.ApplicationContext; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.util.ReflectionTestUtils; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.mock; /** Optional SELECT-only verification against the guarded development schema; no fixture inserts or migration. */ @EnabledIfSystemProperty(named = "assistantReadOnlyDbTests", matches = "true") class AssistantReferenceSchemaDatabaseTest { @Test void everyFixedReferenceProjectionCompilesAgainstExistingSchema() throws Exception { var registry = new BusinessActionRegistry(mock(ApplicationContext.class), new ObjectMapper().findAndRegisterModules(), mock(JdbcTemplate.class), mock(Validator.class), mock(AssistantPermissionGuard.class)); Map references = (Map) ReflectionTestUtils.getField(registry, "references"); assertNotNull(references); try (var connection = AuthorizedDatabaseTestSupport.connection()) { connection.setReadOnly(true); var checks=new java.util.ArrayList(); for (var entry : references.entrySet()) checks.add(()->{ Method accessor = entry.getValue().getClass().getDeclaredMethod("sql"); accessor.setAccessible(true); String sql = (String) accessor.invoke(entry.getValue()); if(sql.isBlank())return; // System references use scoped native services, not SQL projections. assertTrue(sql.stripLeading().toLowerCase().startsWith("select ")); try (var statement = connection.prepareStatement("select * from (" + sql + ") assistant_reference_schema where 1=0")) { try (var rows = statement.executeQuery()) { assertFalse(rows.next()); assertTrue(rows.getMetaData().getColumnCount() >= 2); } } catch (Exception failure) { fail("Assistant reference projection is invalid: " + entry.getKey(), failure); } }); try {assertAll("All assistant reference SQL projections",checks);} finally {connection.rollback();} } } @Test void allBusinessCandidateSearchesCompileIncludingExactRankingAndLiteralWildcards() throws Exception { try(var connection=AuthorizedDatabaseTestSupport.connection()) { connection.setReadOnly(true); var jdbc=new JdbcTemplate(new org.springframework.jdbc.datasource.SingleConnectionDataSource(connection,true)); var registry=new BusinessActionRegistry(mock(ApplicationContext.class),new ObjectMapper().findAndRegisterModules(),jdbc,mock(Validator.class),mock(AssistantPermissionGuard.class)); Map references=(Map)ReflectionTestUtils.getField(registry,"references");assertNotNull(references); var checks=new java.util.ArrayList(); for(Object key:references.keySet())if(!key.toString().startsWith("sys"))checks.add(()->{ List rows=ReflectionTestUtils.invokeMethod(registry,"candidates",key.toString(),"AI-LOOKUP-NOT-EXIST_%'",null); assertNotNull(rows,key.toString());assertTrue(rows.size()<=50,key.toString()); }); try{assertAll("Every business candidate search uses valid bounded SELECTs",checks);}finally{connection.rollback();} } } @Test void exactSupplierAndItemNamesOlderThanFiftyFuzzyNamesRemainResolvable() throws Exception { try(var connection=AuthorizedDatabaseTestSupport.connection()) { var jdbc=new JdbcTemplate(new org.springframework.jdbc.datasource.SingleConnectionDataSource(connection,true)); var registry=new BusinessActionRegistry(mock(ApplicationContext.class),new ObjectMapper().findAndRegisterModules(),jdbc,mock(Validator.class),mock(AssistantPermissionGuard.class)); String name="AI-EXACT-"+java.util.UUID.randomUUID(); try { jdbc.update("insert into supplier_info(name,active) values(?,1)",name); jdbc.update("insert into product_info(product_code,product_name,unit) values(?,?,'个')",name,name); for(int i=0;i<65;i++) { jdbc.update("insert into supplier_info(name,active) values(?,1)",name+"-相近"+i); jdbc.update("insert into product_info(product_code,product_name,unit) values(?,?,'个')",name+"-"+i,name+"-相近"+i); } for(String ref:List.of("supplier","item")) { List> found=ReflectionTestUtils.invokeMethod(registry,"resolveReference",ref,name,Map.of()); assertNotNull(found);assertEquals(1,found.size(),ref);assertEquals(name,found.get(0).get("name")); } } finally {connection.rollback();} } } @Test void editPrefillAndBomLinesAreSelectOnlyAndMatchExistingSchema() throws Exception { var registry = new BusinessActionRegistry(mock(ApplicationContext.class), new ObjectMapper().findAndRegisterModules(), mock(JdbcTemplate.class), mock(Validator.class), mock(AssistantPermissionGuard.class)); Method query = BusinessActionRegistry.class.getDeclaredMethod("existingRecordQuery", String.class); query.setAccessible(true); Map actions=(Map)ReflectionTestUtils.getField(registry,"actions");assertNotNull(actions); var statements=new java.util.LinkedHashMap(); for(Object action:actions.keySet()) { String sql=(String)query.invoke(registry,action.toString()); if(sql!=null)statements.put(action.toString(),sql); } statements.put("bom.lines",(String)ReflectionTestUtils.getField(BusinessActionRegistry.class,"BOM_LINES_QUERY")); assertTrue(statements.keySet().containsAll(List.of("project.update","purchase.update","supplier.save","bom.save","production.save"))); try(var connection=AuthorizedDatabaseTestSupport.connection()) { connection.setReadOnly(true); var checks=new java.util.ArrayList(); for(var entry:statements.entrySet()) checks.add(()->{ assertTrue(entry.getValue().stripLeading().toLowerCase().startsWith("select ")); try(var statement=connection.prepareStatement("select * from ("+entry.getValue()+") assistant_edit_schema where 1=0")) { statement.setLong(1,-1L); try(var rows=statement.executeQuery()){assertFalse(rows.next());assertTrue(rows.getMetaData().getColumnCount()>0);} } catch(Exception failure){fail("Assistant edit projection is invalid: "+entry.getKey(),failure);} }); try {assertAll("All assistant edit SQL projections",checks);} finally {connection.rollback();} } } }