yanderong
2026-10-02 f50643366a89ea4548cabddf3cbc783605a6f167
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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;
    }
}