|
@@ -2,8 +2,11 @@ package com.etotem.cfc.service;
|
|
|
|
|
|
|
|
import com.etotem.cfc.common.Result;
|
|
import com.etotem.cfc.common.Result;
|
|
|
import com.etotem.cfc.entity.FamilyChallenge;
|
|
import com.etotem.cfc.entity.FamilyChallenge;
|
|
|
|
|
+import com.etotem.cfc.entity.FamilyChallengeParticipant;
|
|
|
import com.etotem.cfc.entity.FamilyChallengeProgress;
|
|
import com.etotem.cfc.entity.FamilyChallengeProgress;
|
|
|
|
|
+import com.etotem.cfc.entity.FamilyMember;
|
|
|
import com.etotem.cfc.mapper.FamilyChallengeMapper;
|
|
import com.etotem.cfc.mapper.FamilyChallengeMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.FamilyChallengeParticipantMapper;
|
|
|
import com.etotem.cfc.mapper.FamilyChallengeProgressMapper;
|
|
import com.etotem.cfc.mapper.FamilyChallengeProgressMapper;
|
|
|
import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
@@ -16,6 +19,7 @@ import java.util.Date;
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.anyList;
|
|
|
import static org.mockito.ArgumentMatchers.anyString;
|
|
import static org.mockito.ArgumentMatchers.anyString;
|
|
|
import static org.mockito.ArgumentMatchers.argThat;
|
|
import static org.mockito.ArgumentMatchers.argThat;
|
|
|
import static org.mockito.ArgumentMatchers.eq;
|
|
import static org.mockito.ArgumentMatchers.eq;
|
|
@@ -27,14 +31,17 @@ public class FamilyChallengeServiceTest {
|
|
|
|
|
|
|
|
@Mock private FamilyChallengeMapper familyChallengeMapper;
|
|
@Mock private FamilyChallengeMapper familyChallengeMapper;
|
|
|
@Mock private FamilyChallengeProgressMapper familyChallengeProgressMapper;
|
|
@Mock private FamilyChallengeProgressMapper familyChallengeProgressMapper;
|
|
|
|
|
+ @Mock private FamilyChallengeParticipantMapper familyChallengeParticipantMapper;
|
|
|
@Mock private FamilyMemberMapper familyMemberMapper;
|
|
@Mock private FamilyMemberMapper familyMemberMapper;
|
|
|
@Mock private HealthScoreService healthScoreService;
|
|
@Mock private HealthScoreService healthScoreService;
|
|
|
@Mock private PointsService pointsService;
|
|
@Mock private PointsService pointsService;
|
|
|
|
|
+ @Mock private ChallengeScoreService challengeScoreService;
|
|
|
@InjectMocks private FamilyChallengeService service;
|
|
@InjectMocks private FamilyChallengeService service;
|
|
|
|
|
|
|
|
@BeforeEach
|
|
@BeforeEach
|
|
|
void setUp() {
|
|
void setUp() {
|
|
|
MockitoAnnotations.openMocks(this);
|
|
MockitoAnnotations.openMocks(this);
|
|
|
|
|
+ when(familyChallengeParticipantMapper.selectList(any())).thenReturn(java.util.Collections.emptyList());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
@Test
|
|
@@ -53,9 +60,15 @@ public class FamilyChallengeServiceTest {
|
|
|
existing.setProgressValue(5);
|
|
existing.setProgressValue(5);
|
|
|
existing.setUpdatedAt(new Date());
|
|
existing.setUpdatedAt(new Date());
|
|
|
|
|
|
|
|
|
|
+ FamilyChallengeParticipant participant = new FamilyChallengeParticipant();
|
|
|
|
|
+ participant.setChallengeId(1L);
|
|
|
|
|
+ participant.setMemberId(100L);
|
|
|
|
|
+ participant.setStatus("agreed");
|
|
|
|
|
+
|
|
|
when(familyChallengeMapper.selectById(1L)).thenReturn(challenge);
|
|
when(familyChallengeMapper.selectById(1L)).thenReturn(challenge);
|
|
|
when(familyChallengeProgressMapper.selectOne(any())).thenReturn(existing);
|
|
when(familyChallengeProgressMapper.selectOne(any())).thenReturn(existing);
|
|
|
when(familyMemberMapper.selectCount(any())).thenReturn(1L);
|
|
when(familyMemberMapper.selectCount(any())).thenReturn(1L);
|
|
|
|
|
+ when(familyChallengeParticipantMapper.selectOne(any())).thenReturn(participant);
|
|
|
|
|
|
|
|
Result<Void> result = service.updateProgress(1L, 100L, 1);
|
|
Result<Void> result = service.updateProgress(1L, 100L, 1);
|
|
|
|
|
|
|
@@ -126,8 +139,106 @@ public class FamilyChallengeServiceTest {
|
|
|
when(familyMemberMapper.selectList(any())).thenReturn(java.util.Collections.emptyList());
|
|
when(familyMemberMapper.selectList(any())).thenReturn(java.util.Collections.emptyList());
|
|
|
when(familyChallengeMapper.update(any(), any())).thenReturn(1);
|
|
when(familyChallengeMapper.update(any(), any())).thenReturn(1);
|
|
|
|
|
|
|
|
- service.getActiveChallenges(10L);
|
|
|
|
|
|
|
+ service.getActiveChallenges(10L, null);
|
|
|
|
|
|
|
|
verify(familyChallengeMapper).update(any(), any());
|
|
verify(familyChallengeMapper).update(any(), any());
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void respondChallenge_agree_shouldTransitionToActiveWhenAllAgreed() {
|
|
|
|
|
+ FamilyChallenge challenge = new FamilyChallenge();
|
|
|
|
|
+ challenge.setId(1L);
|
|
|
|
|
+ challenge.setFamilyId(10L);
|
|
|
|
|
+ challenge.setStatus("pending");
|
|
|
|
|
+ challenge.setTargetMode("aggregate");
|
|
|
|
|
+ challenge.setTargetValue(300);
|
|
|
|
|
+ challenge.setCreatorId(9L);
|
|
|
|
|
+
|
|
|
|
|
+ FamilyChallengeParticipant p = new FamilyChallengeParticipant();
|
|
|
|
|
+ p.setId(1L);
|
|
|
|
|
+ p.setChallengeId(1L);
|
|
|
|
|
+ p.setMemberId(100L);
|
|
|
|
|
+ p.setStatus("pending");
|
|
|
|
|
+
|
|
|
|
|
+ FamilyChallengeParticipant initiator = new FamilyChallengeParticipant();
|
|
|
|
|
+ initiator.setId(2L);
|
|
|
|
|
+ initiator.setChallengeId(1L);
|
|
|
|
|
+ initiator.setMemberId(200L);
|
|
|
|
|
+ initiator.setStatus("agreed");
|
|
|
|
|
+
|
|
|
|
|
+ when(familyChallengeMapper.selectById(1L)).thenReturn(challenge);
|
|
|
|
|
+ when(familyChallengeParticipantMapper.selectOne(any())).thenReturn(p);
|
|
|
|
|
+ when(familyChallengeParticipantMapper.selectList(any()))
|
|
|
|
|
+ .thenReturn(java.util.Arrays.asList(initiator, p));
|
|
|
|
|
+
|
|
|
|
|
+ Result<Void> result = service.respondChallenge(1L, 100L, "agree");
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(Integer.valueOf(200), result.getCode());
|
|
|
|
|
+ verify(challengeScoreService).applyAgree(eq(10L), eq(100L), anyList(), eq(1L));
|
|
|
|
|
+ verify(familyChallengeParticipantMapper).updateById(argThat(pp -> "agreed".equals(pp.getStatus())));
|
|
|
|
|
+ verify(familyChallengeMapper).updateById(argThat(c -> "active".equals(c.getStatus())));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void respondChallenge_reject_shouldCancelWhenOnlyInitiatorLeft() {
|
|
|
|
|
+ FamilyChallenge challenge = new FamilyChallenge();
|
|
|
|
|
+ challenge.setId(1L);
|
|
|
|
|
+ challenge.setFamilyId(10L);
|
|
|
|
|
+ challenge.setStatus("pending");
|
|
|
|
|
+ challenge.setTargetMode("aggregate");
|
|
|
|
|
+ challenge.setTargetValue(300);
|
|
|
|
|
+ challenge.setCreatorId(9L);
|
|
|
|
|
+
|
|
|
|
|
+ FamilyChallengeParticipant p = new FamilyChallengeParticipant();
|
|
|
|
|
+ p.setId(1L);
|
|
|
|
|
+ p.setChallengeId(1L);
|
|
|
|
|
+ p.setMemberId(100L);
|
|
|
|
|
+ p.setStatus("pending");
|
|
|
|
|
+
|
|
|
|
|
+ FamilyChallengeParticipant initiator = new FamilyChallengeParticipant();
|
|
|
|
|
+ initiator.setId(2L);
|
|
|
|
|
+ initiator.setChallengeId(1L);
|
|
|
|
|
+ initiator.setMemberId(200L);
|
|
|
|
|
+ initiator.setStatus("agreed");
|
|
|
|
|
+
|
|
|
|
|
+ FamilyMember member = new FamilyMember();
|
|
|
|
|
+ member.setId(200L);
|
|
|
|
|
+ member.setUserId(9L);
|
|
|
|
|
+
|
|
|
|
|
+ when(familyChallengeMapper.selectById(1L)).thenReturn(challenge);
|
|
|
|
|
+ when(familyChallengeParticipantMapper.selectOne(any())).thenReturn(p);
|
|
|
|
|
+ when(familyChallengeParticipantMapper.selectList(any()))
|
|
|
|
|
+ .thenReturn(java.util.Arrays.asList(initiator, p));
|
|
|
|
|
+ when(familyMemberMapper.selectList(any())).thenReturn(java.util.Arrays.asList(member));
|
|
|
|
|
+
|
|
|
|
|
+ Result<Void> result = service.respondChallenge(1L, 100L, "reject");
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(Integer.valueOf(200), result.getCode());
|
|
|
|
|
+ verify(challengeScoreService).applyReject(eq(10L), eq(100L), eq(200L), anyList(), eq(1L));
|
|
|
|
|
+ verify(familyChallengeMapper).updateById(argThat(c -> "cancelled".equals(c.getStatus())));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void respondChallenge_shouldRejectWhenAlreadyProcessed() {
|
|
|
|
|
+ FamilyChallenge challenge = new FamilyChallenge();
|
|
|
|
|
+ challenge.setId(1L);
|
|
|
|
|
+ challenge.setFamilyId(10L);
|
|
|
|
|
+ challenge.setStatus("pending");
|
|
|
|
|
+ challenge.setTargetMode("aggregate");
|
|
|
|
|
+ challenge.setTargetValue(300);
|
|
|
|
|
+
|
|
|
|
|
+ FamilyChallengeParticipant p = new FamilyChallengeParticipant();
|
|
|
|
|
+ p.setId(1L);
|
|
|
|
|
+ p.setChallengeId(1L);
|
|
|
|
|
+ p.setMemberId(100L);
|
|
|
|
|
+ p.setStatus("agreed"); // 已处理过
|
|
|
|
|
+
|
|
|
|
|
+ when(familyChallengeMapper.selectById(1L)).thenReturn(challenge);
|
|
|
|
|
+ when(familyChallengeParticipantMapper.selectOne(any())).thenReturn(p);
|
|
|
|
|
+
|
|
|
|
|
+ Result<Void> result = service.respondChallenge(1L, 100L, "agree");
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(Integer.valueOf(500), result.getCode());
|
|
|
|
|
+ verify(challengeScoreService, never()).applyAgree(any(), any(), any(), any());
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|