|
@@ -0,0 +1,75 @@
|
|
|
|
|
+package com.etotem.cfc.unit;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.etotem.cfc.entity.Article;
|
|
|
|
|
+import com.etotem.cfc.mapper.ArticleMapper;
|
|
|
|
|
+import com.etotem.cfc.service.ArticleService;
|
|
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
|
|
+
|
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 存量空标签文章一次性回填单元测试
|
|
|
|
|
+ */
|
|
|
|
|
+class ArticleBackfillTest {
|
|
|
|
|
+
|
|
|
|
|
+ private ArticleService service;
|
|
|
|
|
+ private ArticleMapper articleMapper;
|
|
|
|
|
+ private ThreadPoolTaskExecutor taskExecutor;
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ void setUp() throws Exception {
|
|
|
|
|
+ service = new ArticleService();
|
|
|
|
|
+ articleMapper = mock(ArticleMapper.class);
|
|
|
|
|
+ taskExecutor = mock(ThreadPoolTaskExecutor.class);
|
|
|
|
|
+ inject("articleMapper", articleMapper);
|
|
|
|
|
+ inject("taskExecutor", taskExecutor);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void inject(String field, Object value) throws Exception {
|
|
|
|
|
+ Field f = ArticleService.class.getDeclaredField(field);
|
|
|
|
|
+ f.setAccessible(true);
|
|
|
|
|
+ f.set(service, value);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Article published(Long id, String tags) {
|
|
|
|
|
+ Article a = new Article();
|
|
|
|
|
+ a.setId(id);
|
|
|
|
|
+ a.setStatus("published");
|
|
|
|
|
+ a.setTags(tags);
|
|
|
|
|
+ a.setTitle("t" + id);
|
|
|
|
|
+ a.setContent("<p>正文</p>");
|
|
|
|
|
+ return a;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void backfillEmptyPublishedTags_onlyEmptyPublishedScheduled() {
|
|
|
|
|
+ Article empty1 = published(1L, "");
|
|
|
|
|
+ Article empty2 = published(2L, null);
|
|
|
|
|
+ when(articleMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(Arrays.asList(empty1, empty2));
|
|
|
|
|
+ when(articleMapper.selectById(1L)).thenReturn(empty1);
|
|
|
|
|
+ when(articleMapper.selectById(2L)).thenReturn(empty2);
|
|
|
|
|
+
|
|
|
|
|
+ int n = service.backfillEmptyPublishedTags();
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(2, n);
|
|
|
|
|
+ verify(taskExecutor, times(2)).execute(any(Runnable.class));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void backfillEmptyPublishedTags_noEmptyArticles_returnsZero() {
|
|
|
|
|
+ when(articleMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(java.util.Collections.emptyList());
|
|
|
|
|
+
|
|
|
|
|
+ int n = service.backfillEmptyPublishedTags();
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(0, n);
|
|
|
|
|
+ verify(taskExecutor, never()).execute(any(Runnable.class));
|
|
|
|
|
+ }
|
|
|
|
|
+}
|