|
|
@@ -0,0 +1,169 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.etotem.cfc.entity.Activity;
|
|
|
+import com.etotem.cfc.entity.DimensionWeight;
|
|
|
+import com.etotem.cfc.mapper.ActivityMapper;
|
|
|
+import com.etotem.cfc.service.ActivityAdminService;
|
|
|
+import com.etotem.cfc.service.DimensionWeightService;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.ArgumentMatchers.eq;
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 审核中心活动审核:动作兼容 publish/reject,审核通过时必须补充五维权重。
|
|
|
+ */
|
|
|
+class ActivityAuditTest {
|
|
|
+
|
|
|
+ private ActivityAdminService service;
|
|
|
+ private ActivityMapper activityMapper;
|
|
|
+ private DimensionWeightService dimensionWeightService;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() throws Exception {
|
|
|
+ service = new ActivityAdminService();
|
|
|
+ activityMapper = mock(ActivityMapper.class);
|
|
|
+ dimensionWeightService = mock(DimensionWeightService.class);
|
|
|
+ set("activityMapper", activityMapper);
|
|
|
+ set("dimensionWeightService", dimensionWeightService);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void set(String name, Object value) throws Exception {
|
|
|
+ Field f = ActivityAdminService.class.getDeclaredField(name);
|
|
|
+ f.setAccessible(true);
|
|
|
+ f.set(service, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Activity pending() {
|
|
|
+ Activity a = new Activity();
|
|
|
+ a.setId(9L);
|
|
|
+ a.setTitle("测试活动");
|
|
|
+ a.setStatus("pending");
|
|
|
+ a.setAuditStatus("pending");
|
|
|
+ return a;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<Map<String, Object>> weights(int body, int mind, int wisdom, int action, int wealth) {
|
|
|
+ List<Map<String, Object>> list = new ArrayList<>();
|
|
|
+ String[] dims = {"body", "mind", "wisdom", "action", "wealth"};
|
|
|
+ int[] vals = {body, mind, wisdom, action, wealth};
|
|
|
+ for (int i = 0; i < dims.length; i++) {
|
|
|
+ Map<String, Object> m = new HashMap<>();
|
|
|
+ m.put("dimension", dims[i]);
|
|
|
+ m.put("weight", vals[i]);
|
|
|
+ m.put("enabled", vals[i] > 0);
|
|
|
+ list.add(m);
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void audit_审核中心提交publish动作_按通过处理() {
|
|
|
+ Activity activity = pending();
|
|
|
+ when(activityMapper.selectById(9L)).thenReturn(activity);
|
|
|
+ when(activityMapper.updateById(any(Activity.class))).thenReturn(1);
|
|
|
+
|
|
|
+ service.auditActivity(9L, "publish", null, 1L, "body", weights(20, 20, 20, 20, 20));
|
|
|
+
|
|
|
+ assertEquals("published", activity.getStatus());
|
|
|
+ assertEquals("approved", activity.getAuditStatus());
|
|
|
+ assertEquals("body", activity.getDimensionCode());
|
|
|
+ assertEquals(Long.valueOf(1L), activity.getAuditorId());
|
|
|
+ assertNotNull(activity.getAuditedAt());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void audit_通过时保存五维权重() {
|
|
|
+ Activity activity = pending();
|
|
|
+ when(activityMapper.selectById(9L)).thenReturn(activity);
|
|
|
+ when(activityMapper.updateById(any(Activity.class))).thenReturn(1);
|
|
|
+
|
|
|
+ service.auditActivity(9L, "approved", null, 1L, "middle", weights(40, 15, 15, 15, 15));
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ ArgumentCaptor<List<DimensionWeight>> captor = ArgumentCaptor.forClass(List.class);
|
|
|
+ verify(dimensionWeightService).saveWeights(eq("activity"), eq(9L), captor.capture());
|
|
|
+
|
|
|
+ List<DimensionWeight> saved = captor.getValue();
|
|
|
+ assertEquals(5, saved.size());
|
|
|
+ assertEquals(100, saved.stream().mapToInt(DimensionWeight::getWeight).sum());
|
|
|
+ DimensionWeight body = saved.stream().filter(w -> "body".equals(w.getDimension())).findFirst().orElse(null);
|
|
|
+ assertNotNull(body);
|
|
|
+ assertEquals(40, body.getWeight());
|
|
|
+ assertTrue(body.getEnabled());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void audit_通过时缺少五维权重_报错且不落库() {
|
|
|
+ Activity activity = pending();
|
|
|
+ when(activityMapper.selectById(9L)).thenReturn(activity);
|
|
|
+
|
|
|
+ IllegalStateException e = assertThrows(IllegalStateException.class,
|
|
|
+ () -> service.auditActivity(9L, "approved", null, 1L, "body", null));
|
|
|
+
|
|
|
+ assertTrue(e.getMessage().contains("五维权重"), e.getMessage());
|
|
|
+ verify(activityMapper, never()).updateById(any(Activity.class));
|
|
|
+ verify(dimensionWeightService, never()).saveWeights(any(), any(), any());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void audit_通过时权重合计不为100_报错() {
|
|
|
+ Activity activity = pending();
|
|
|
+ when(activityMapper.selectById(9L)).thenReturn(activity);
|
|
|
+
|
|
|
+ IllegalStateException e = assertThrows(IllegalStateException.class,
|
|
|
+ () -> service.auditActivity(9L, "approved", null, 1L, "body", weights(20, 20, 20, 20, 19)));
|
|
|
+
|
|
|
+ assertTrue(e.getMessage().contains("100"), e.getMessage());
|
|
|
+ verify(activityMapper, never()).updateById(any(Activity.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void audit_通过时维度不完整_报错() {
|
|
|
+ Activity activity = pending();
|
|
|
+ when(activityMapper.selectById(9L)).thenReturn(activity);
|
|
|
+
|
|
|
+ List<Map<String, Object>> onlyFour = weights(25, 25, 25, 25, 0);
|
|
|
+ onlyFour.removeIf(m -> "wealth".equals(m.get("dimension")));
|
|
|
+
|
|
|
+ IllegalStateException e = assertThrows(IllegalStateException.class,
|
|
|
+ () -> service.auditActivity(9L, "approved", null, 1L, "body", onlyFour));
|
|
|
+
|
|
|
+ assertTrue(e.getMessage().contains("五个维度"), e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void audit_驳回不要求五维权重() {
|
|
|
+ Activity activity = pending();
|
|
|
+ when(activityMapper.selectById(9L)).thenReturn(activity);
|
|
|
+ when(activityMapper.updateById(any(Activity.class))).thenReturn(1);
|
|
|
+
|
|
|
+ service.auditActivity(9L, "reject", "材料不全", 1L, null, null);
|
|
|
+
|
|
|
+ assertEquals("rejected", activity.getStatus());
|
|
|
+ assertEquals("rejected", activity.getAuditStatus());
|
|
|
+ assertEquals("材料不全", activity.getAuditReason());
|
|
|
+ verify(dimensionWeightService, never()).saveWeights(any(), any(), any());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void audit_无效动作_报审核操作无效() {
|
|
|
+ Activity activity = pending();
|
|
|
+ when(activityMapper.selectById(9L)).thenReturn(activity);
|
|
|
+
|
|
|
+ IllegalStateException e = assertThrows(IllegalStateException.class,
|
|
|
+ () -> service.auditActivity(9L, "unknown", null, 1L, "body", weights(20, 20, 20, 20, 20)));
|
|
|
+
|
|
|
+ assertTrue(e.getMessage().contains("审核操作无效"), e.getMessage());
|
|
|
+ }
|
|
|
+}
|