Skip to content

Commit 834730d

Browse files
committed
Add unit tests
1 parent 422f477 commit 834730d

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package de.unistuttgart.iste.meitrex.gamification_service.service;
2+
import static org.mockito.ArgumentMatchers.any;
3+
import static org.mockito.Mockito.*;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import java.util.Optional;
7+
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
import org.mockito.*;
11+
12+
import de.unistuttgart.iste.meitrex.gamification_service.persistence.entity.PlayerHexadScoreEntity;
13+
import de.unistuttgart.iste.meitrex.gamification_service.persistence.mapper.PlayerHexadScoreMapper;
14+
import de.unistuttgart.iste.meitrex.gamification_service.persistence.repository.PlayerHexadScoreRepository;
15+
import de.unistuttgart.iste.meitrex.generated.dto.*;
16+
import java.util.UUID;
17+
import java.util.Arrays;
18+
import java.util.Collections;
19+
import java.util.List;
20+
21+
public class PlayerHexadScoreServiceTest {
22+
23+
@Mock
24+
private PlayerHexadScoreRepository playerHexadScoreRepository;
25+
26+
27+
@Mock
28+
private PlayerHexadScoreMapper playerHexadScoreMapper;
29+
30+
private PlayerHexadScoreService playerHexadScoreService;
31+
32+
@Mock
33+
private PlayerAnswerInput input;
34+
35+
36+
@BeforeEach
37+
public void setUp() {
38+
MockitoAnnotations.openMocks(this);
39+
playerHexadScoreService = new PlayerHexadScoreService(playerHexadScoreRepository, playerHexadScoreMapper);
40+
}
41+
42+
@Test
43+
void testEvaluateWithEmptyQuestions() {
44+
// Arrange
45+
PlayerHexadScoreService spyService = spy(playerHexadScoreService);
46+
when(input.getQuestions()).thenReturn(Collections.emptyList());
47+
when(playerHexadScoreRepository.findByUserId(any(UUID.class)))
48+
.thenReturn(Optional.empty());
49+
50+
float defaultValue = 100f / PlayerType.values().length;
51+
double expectedValue = defaultValue;
52+
53+
// Act
54+
PlayerHexadScore result = spyService.evaluate(UUID.randomUUID(), input);
55+
56+
verify(spyService).calculateDefault();
57+
verify(spyService, times(0)).calculateFromInput(input);
58+
verify(playerHexadScoreRepository, times(1)).save(any());
59+
60+
61+
// Assert
62+
assertEquals(6, result.getScores().size(), "There should be 6 scores");
63+
List<PlayerTypeScore> scores = result.getScores();
64+
for (PlayerTypeScore score : scores) {
65+
assertEquals(expectedValue, score.getValue(), 0.0001, "Value for " + score.getType() + " should be " + expectedValue);
66+
}
67+
}
68+
69+
@Test
70+
public void testEvaluateWithInput(){
71+
// Arrange
72+
PlayerHexadScoreService spyService = spy(playerHexadScoreService);
73+
74+
75+
AnswerInput answer1 = new AnswerInput(
76+
"Mock answer 1",
77+
Arrays.asList(PlayerType.SOCIALISER, PlayerType.PHILANTHROPIST)
78+
);
79+
80+
AnswerInput answer2 = new AnswerInput(
81+
"Mock answer 2",
82+
Arrays.asList(PlayerType.SOCIALISER, PlayerType.PLAYER)
83+
);
84+
85+
QuestionInput question = new QuestionInput(
86+
"Mock question",
87+
answer1,
88+
Arrays.asList(answer1, answer2)
89+
);
90+
91+
PlayerAnswerInput playerAnswerInput = new PlayerAnswerInput(Collections.singletonList(question));
92+
when(input.getQuestions()).thenReturn(playerAnswerInput.getQuestions());
93+
when(playerHexadScoreRepository.findByUserId(any(UUID.class)))
94+
.thenReturn(Optional.empty());
95+
96+
// Act
97+
PlayerHexadScore result = spyService.evaluate(UUID.randomUUID(), input);
98+
99+
verify(spyService).calculateFromInput(input);
100+
verify(spyService, times(0)).calculateDefault();
101+
verify(playerHexadScoreRepository, times(1)).save(any());
102+
103+
// Assert
104+
assertEquals(6, result.getScores().size(), "There should be 6 scores");
105+
Optional<PlayerTypeScore> philanthropistScore = result.getScores().stream()
106+
.filter(score -> score.getType() == PlayerType.PHILANTHROPIST)
107+
.findFirst();
108+
109+
Optional<PlayerTypeScore> socialiserScore = result.getScores().stream()
110+
.filter(score -> score.getType() == PlayerType.SOCIALISER)
111+
.findFirst();
112+
113+
assertEquals(100, philanthropistScore.get().getValue());
114+
assertEquals(50, socialiserScore.get().getValue());
115+
}
116+
117+
@Test
118+
public void testEvaluateHexadScoreAlreadyEvaluated(){
119+
// Arrange
120+
PlayerHexadScoreService spyService = spy(playerHexadScoreService);
121+
Optional<PlayerHexadScoreEntity> existingScore = Optional.of(new PlayerHexadScoreEntity());
122+
when(playerHexadScoreRepository.findByUserId(any(UUID.class)))
123+
.thenReturn(existingScore);
124+
125+
when(input.getQuestions()).thenReturn(Collections.emptyList());
126+
127+
// Act & Assert
128+
assertThrows(IllegalStateException.class, () -> {
129+
spyService.evaluate(UUID.randomUUID(), input);
130+
});
131+
132+
verify(spyService, times(0)).calculateDefault();
133+
verify(spyService, times(0)).calculateFromInput(input);
134+
}
135+
}

0 commit comments

Comments
 (0)