package jnpf.dmsMapper; import org.apache.ibatis.builder.xml.XMLMapperBuilder; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.session.Configuration; import org.junit.jupiter.api.Test; import java.io.InputStream; import java.util.Arrays; import java.util.Collections; import static org.junit.jupiter.api.Assertions.*; class DmsPermissionGroupDisplayMapperTest { private MappedStatement statement() throws Exception { Configuration configuration = new Configuration(); String resource = "mapper/DmsPermissionGroupMapper.xml"; try (InputStream stream = getClass().getClassLoader().getResourceAsStream(resource)) { assertNotNull(stream); new XMLMapperBuilder(stream, configuration, resource, configuration.getSqlFragments()).parse(); } return configuration.getMappedStatement("jnpf.dmsMapper.DmsPermissionGroupMapper.selectDisplayByIds"); } @Test void bindsIdsAndMapsNamesWithoutHidingDisabledHistoricalGroups() throws Exception { MappedStatement statement = statement(); BoundSql bound = statement.getBoundSql(Collections.singletonMap("ids", Arrays.asList("group-1", "group-2"))); String sql = bound.getSql().replaceAll("\\s+", " "); assertTrue(sql.contains("FROM base_permission_group")); assertTrue(sql.contains("f_delete_mark IS NULL")); assertTrue(sql.contains("f_id IN")); assertFalse(sql.contains("enabled_mark")); assertFalse(sql.contains("group-1")); assertEquals(2, bound.getParameterMappings().size()); assertEquals("group-1", bound.getAdditionalParameter(bound.getParameterMappings().get(0).getProperty())); assertEquals("group-2", bound.getAdditionalParameter(bound.getParameterMappings().get(1).getProperty())); assertTrue(statement.getResultMaps().get(0).getResultMappings().stream() .anyMatch(mapping -> "id".equals(mapping.getProperty()) && "f_id".equals(mapping.getColumn()))); assertTrue(statement.getResultMaps().get(0).getResultMappings().stream() .anyMatch(mapping -> "fullName".equals(mapping.getProperty()) && "f_full_name".equals(mapping.getColumn()))); } @Test void emptyOrNullIdsCannotQueryAllGroups() throws Exception { MappedStatement statement = statement(); assertTrue(statement.getBoundSql(Collections.singletonMap("ids", Collections.emptyList())).getSql().contains("1 = 0")); assertTrue(statement.getBoundSql(Collections.singletonMap("ids", null)).getSql().contains("1 = 0")); } }