-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChallengeServiceImplTest.java
67 lines (49 loc) · 2.24 KB
/
ChallengeServiceImplTest.java
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
package microservices.book.multiplication.challenge;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import microservices.book.multiplication.event.ChallengeSolvedEvent;
import microservices.book.multiplication.event.EventDispatcher;
import microservices.book.multiplication.user.User;
import microservices.book.multiplication.user.UserRepository;
import static org.assertj.core.api.BDDAssertions.then;
import static org.mockito.BDDMockito.*;
@ExtendWith(MockitoExtension.class)
public class ChallengeServiceImplTest {
private ChallengeServiceImpl challengeServiceImpl;
@Mock
private ChallengeAttemptRepository attemptRepository;
@Mock
private UserRepository userRepository;
@Mock
private EventDispatcher eventDispatcher;
@BeforeEach
public void setUp() {
challengeServiceImpl = new ChallengeServiceImpl(attemptRepository, userRepository, eventDispatcher);
}
@Test
public void checkCorrectAttemptTest() {
// given
long userId = 9 L, attemptId = 1 L;
User user = new User("john_doe");
User savedUser = new User(userId, "john_doe");
ChallengeAttemptDTO attemptDTO = new ChallengeAttemptDTO(50, 60, "john_doe", 3000);
ChallengeAttempt attempt = new ChallengeAttempt(null, savedUser, 50, 60, 3000, true);
ChallengeAttempt storedAttempt = new ChallengeAttempt(attemptId, savedUser, 50, 60, 3000, true);
ChallengeSolvedEvent event = new ChallengeSolvedEvent(attemptId, true, attempt.getFactorA(), attempt.getFactorB(), userId, attempt.getUser().getAlias());
// user does not exist, should be created
given(userRepository.findByAlias("john_doe")).willReturn(Optional.empty());
given(userRepository.save(user)).willReturn(savedUser);
given(attemptRepository.save(attempt)).willReturn(storedAttempt);
// when
ChallengeAttempt resultAttempt = challengeServiceImpl.checkAttempt(attemptDTO);
// then
then(resultAttempt.isCorrect()).isTrue();
verify(userRepository).save(user);
verify(attemptRepository).save(attempt);
verify(eventDispatcher).send(event);
}
}