package com.ruoyi.other.service.impl;
|
|
import com.baomidou.mybatisplus.annotation.FieldStrategy;
|
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
import com.ruoyi.common.core.exception.ServiceException;
|
import com.ruoyi.other.domain.TPlatformConfig;
|
import com.ruoyi.other.mapper.TPlatformConfigMapper;
|
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.Test;
|
import org.mockito.ArgumentCaptor;
|
import org.mockito.Mockito;
|
import org.springframework.test.util.ReflectionTestUtils;
|
|
import java.time.LocalDateTime;
|
import java.util.Arrays;
|
import java.util.Collections;
|
import java.util.List;
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
import static org.junit.jupiter.api.Assertions.assertSame;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.mockito.ArgumentMatchers.anyCollection;
|
import static org.mockito.ArgumentMatchers.anyInt;
|
|
/** 配置保存边界及表映射测试,不启动外部服务。 */
|
class TPlatformConfigServiceImplTest {
|
|
@BeforeAll
|
static void initTableInfo() {
|
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
|
TPlatformConfig.class);
|
}
|
|
@Test
|
void mapsLogicalDeletionAndProtectsCreationTime() {
|
TableInfo table = TableInfoHelper.getTableInfo(TPlatformConfig.class);
|
assertEquals("t_platform_config", table.getTableName());
|
assertEquals(Long.class, table.getKeyType());
|
assertEquals("0", table.getLogicDeleteFieldInfo().getLogicNotDeleteValue());
|
assertEquals("2", table.getLogicDeleteFieldInfo().getLogicDeleteValue());
|
assertEquals(FieldStrategy.NEVER, table.getFieldList().stream()
|
.filter(field -> "create_time".equals(field.getColumn()))
|
.findFirst().get().getUpdateStrategy());
|
}
|
|
@Test
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
void listsAllWithoutPaginationInIdOrder() {
|
TPlatformConfigMapper mapper = Mockito.mock(TPlatformConfigMapper.class);
|
List<TPlatformConfig> rows = Arrays.asList(config(1L), config(2L));
|
Mockito.when(mapper.selectList(Mockito.any())).thenReturn(rows);
|
TPlatformConfigServiceImpl service = service(mapper);
|
|
assertSame(rows, service.listAll());
|
ArgumentCaptor<Wrapper> query = ArgumentCaptor.forClass(Wrapper.class);
|
Mockito.verify(mapper).selectList(query.capture());
|
assertEquals("ORDER BY id ASC", query.getValue().getSqlSegment().trim());
|
}
|
|
@Test
|
void rejectsEmptyOrNullItemsBeforeWriting() {
|
TPlatformConfigServiceImpl service = Mockito.spy(new TPlatformConfigServiceImpl());
|
assertThrows(ServiceException.class, () -> service.saveBatchConfigs(null));
|
assertThrows(ServiceException.class, () -> service.saveBatchConfigs(Collections.emptyList()));
|
assertThrows(ServiceException.class,
|
() -> service.saveBatchConfigs(Arrays.asList(config(null), null)));
|
Mockito.verify(service, Mockito.never()).saveOrUpdateBatch(anyCollection(), anyInt());
|
}
|
|
@Test
|
void rejectsInvalidTypeAndDuplicateIdsBeforeWriting() {
|
TPlatformConfigServiceImpl service = Mockito.spy(new TPlatformConfigServiceImpl());
|
TPlatformConfig invalid = config(null);
|
invalid.setType(3);
|
assertThrows(ServiceException.class,
|
() -> service.saveBatchConfigs(Collections.singletonList(invalid)));
|
assertThrows(ServiceException.class,
|
() -> service.saveBatchConfigs(Arrays.asList(config(1L), config(1L))));
|
assertThrows(ServiceException.class,
|
() -> service.saveBatchConfigs(Collections.singletonList(config(0L))));
|
Mockito.verify(service, Mockito.never()).saveOrUpdateBatch(anyCollection(), anyInt());
|
}
|
|
@Test
|
void rejectsMissingOrDeletedIdBeforeWritingAnyNewRows() {
|
TPlatformConfigServiceImpl service = Mockito.spy(new TPlatformConfigServiceImpl());
|
Mockito.doReturn(Collections.emptyList()).when(service).listByIds(anyCollection());
|
assertThrows(ServiceException.class,
|
() -> service.saveBatchConfigs(Arrays.asList(config(null), config(5L))));
|
Mockito.verify(service, Mockito.never()).saveOrUpdateBatch(anyCollection(), anyInt());
|
}
|
|
@Test
|
void savesMixedBatchAndIgnoresClientManagedMetadata() {
|
TPlatformConfigServiceImpl service = Mockito.spy(new TPlatformConfigServiceImpl());
|
Mockito.doReturn(Collections.singletonList(config(5L))).when(service).listByIds(anyCollection());
|
Mockito.doReturn(true).when(service).saveOrUpdateBatch(anyCollection(), anyInt());
|
TPlatformConfig created = config(null);
|
TPlatformConfig updated = config(5L);
|
updated.setCreateTime(LocalDateTime.of(2000, 1, 1, 0, 0));
|
updated.setUpdateTime(updated.getCreateTime());
|
updated.setDelFlag("2");
|
List<TPlatformConfig> rows = Arrays.asList(created, updated);
|
|
service.saveBatchConfigs(rows);
|
|
Mockito.verify(service).saveOrUpdateBatch(Mockito.same(rows), anyInt());
|
assertNull(created.getId());
|
assertEquals(Long.valueOf(5), updated.getId());
|
assertEquals("0", created.getDelFlag());
|
assertEquals("0", updated.getDelFlag());
|
assertNull(updated.getCreateTime());
|
assertNull(updated.getUpdateTime());
|
}
|
|
@Test
|
void acceptsNullableTypeAsDefinedBySchema() {
|
TPlatformConfigServiceImpl service = Mockito.spy(new TPlatformConfigServiceImpl());
|
Mockito.doReturn(true).when(service).saveOrUpdateBatch(anyCollection(), anyInt());
|
TPlatformConfig row = config(null);
|
row.setType(null);
|
service.saveBatchConfigs(Collections.singletonList(row));
|
Mockito.verify(service).saveOrUpdateBatch(anyCollection(), anyInt());
|
}
|
|
@Test
|
void propagatesBatchFailuresSoTransactionCanRollBack() {
|
TPlatformConfigServiceImpl service = Mockito.spy(new TPlatformConfigServiceImpl());
|
Mockito.doReturn(false).when(service).saveOrUpdateBatch(anyCollection(), anyInt());
|
assertThrows(ServiceException.class,
|
() -> service.saveBatchConfigs(Collections.singletonList(config(null))));
|
IllegalStateException failure = new IllegalStateException("database failure");
|
Mockito.doThrow(failure).when(service).saveOrUpdateBatch(anyCollection(), anyInt());
|
assertSame(failure, assertThrows(IllegalStateException.class,
|
() -> service.saveBatchConfigs(Collections.singletonList(config(null)))));
|
}
|
|
@Test
|
void doesNotIncludeSecretsInToString() {
|
TPlatformConfig row = config(null);
|
row.setPassword("private-password");
|
row.setAppKey("private-key");
|
row.setAppSecret("private-secret");
|
assertTrue(!row.toString().contains("private-"));
|
}
|
|
private TPlatformConfigServiceImpl service(TPlatformConfigMapper mapper) {
|
TPlatformConfigServiceImpl service = new TPlatformConfigServiceImpl();
|
ReflectionTestUtils.setField(service, "baseMapper", mapper);
|
return service;
|
}
|
|
private TPlatformConfig config(Long id) {
|
TPlatformConfig config = new TPlatformConfig();
|
config.setId(id);
|
config.setType(1);
|
config.setPlatformName("利普芯");
|
return config;
|
}
|
}
|