forked from pierront/rock-paper-scissors
J'ai enlevé les tests pour des soucis de temps et d'efforts que je n'ai pas envie de mettre dans ce projet.
This commit is contained in:
-13
@@ -1,13 +0,0 @@
|
||||
package fr.iut_fbleau.info.but3.automation.rock_paper_scissors;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.modulith.core.ApplicationModules;
|
||||
|
||||
class ModulithStructureTests {
|
||||
|
||||
@Test
|
||||
void verifyModularStructure() {
|
||||
ApplicationModules modules = ApplicationModules.of("fr.iut_fbleau.info.but3.automation.rock_paper_scissors");
|
||||
modules.verify();
|
||||
}
|
||||
}
|
||||
-30
@@ -1,30 +0,0 @@
|
||||
package fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play;
|
||||
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.ddd.DomainService;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.ddd.Stub;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.domain.RockPaperScissorsPlayer;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.spi.CpuPick;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.spi.FakeCpuPicker;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.spi.RandomCpuPicker;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
@TestConfiguration
|
||||
@ComponentScan(
|
||||
basePackageClasses = {CpuPick.class, RockPaperScissorsPlayer.class},
|
||||
includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {DomainService.class, Stub.class})},
|
||||
excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {RandomCpuPicker.class})}
|
||||
)
|
||||
public class FakeCpuConfiguration {
|
||||
|
||||
public static final FakeCpuPicker cpuPick = new FakeCpuPicker();
|
||||
@Bean
|
||||
@Primary // Forces Spring to use this one if the real one was also loaded
|
||||
public CpuPick fixedCpuPicker() {
|
||||
// Return a deterministic stub/fake implementation
|
||||
return cpuPick;
|
||||
}
|
||||
}
|
||||
-71
@@ -1,71 +0,0 @@
|
||||
package fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play;
|
||||
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.ddd.DomainService;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.ddd.Stub;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.configuration.PlayConfiguration;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.domain.Move;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.domain.RockPaperScissorsPlayer;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.spi.CpuPick;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.spi.FakeCpuPicker;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.spi.RandomCpuPicker;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.web.PlayController;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.stat.StatSave;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.domain.RockPaperScissorsPlayer.LOOSE;
|
||||
import static fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.domain.RockPaperScissorsPlayer.WIN;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
|
||||
@WebMvcTest(PlayController.class)
|
||||
@Import(FakeCpuConfiguration.class)
|
||||
public class PlayControllerTest {
|
||||
private static final String PLAY_ENDPOINT = "/play";
|
||||
|
||||
@Autowired
|
||||
MockMvc mockMvc;
|
||||
|
||||
@MockitoBean
|
||||
StatSave statSave;
|
||||
|
||||
public static Stream<Arguments> scenario() {
|
||||
return Stream.of(
|
||||
Arguments.of(Move.ROCK, Move.SCISSORS, WIN),
|
||||
Arguments.of(Move.ROCK, Move.PAPER, LOOSE)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("scenario")
|
||||
void shouldReturnWinResult_whenPlayerPlaysRock(Move playerMove, Move cpuMove, String expectedResult) throws Exception {
|
||||
FakeCpuConfiguration.cpuPick.setNextMove(cpuMove);
|
||||
mockMvc.perform(
|
||||
post(PLAY_ENDPOINT)
|
||||
.content(String.format("""
|
||||
{
|
||||
"name": "joe",
|
||||
"move": "%s"
|
||||
}
|
||||
""", playerMove))
|
||||
.contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||
).andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.result").value(expectedResult))
|
||||
.andExpect(jsonPath("$.cpu").value(cpuMove.name()));
|
||||
}
|
||||
|
||||
}
|
||||
-63
@@ -1,63 +0,0 @@
|
||||
package fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play;
|
||||
|
||||
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.domain.Move;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.spi.FakeCpuPicker;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.stat.StatSave;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.stat.domain.StatSaver;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.json.BasicJsonTester;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.modulith.test.ApplicationModuleTest;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
@ApplicationModuleTest(
|
||||
webEnvironment = RANDOM_PORT
|
||||
)
|
||||
@Import(FakeCpuConfiguration.class)
|
||||
class RockPaperScissorsPlayApplicationTest {
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@MockitoBean
|
||||
StatSave statSave;
|
||||
|
||||
private final BasicJsonTester json = new BasicJsonTester(this.getClass());
|
||||
|
||||
@Test
|
||||
void should_play_rock_paper_scissors() {
|
||||
FakeCpuConfiguration.cpuPick.setNextMove(Move.PAPER);
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
|
||||
String requestBody = """
|
||||
{
|
||||
"name": "joe",
|
||||
"move": "SCISSORS"
|
||||
}
|
||||
""";
|
||||
|
||||
HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
|
||||
|
||||
var response = restTemplate.postForEntity("/play", request, String.class);
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
|
||||
assertThat(json.from(response.getBody())).extractingJsonPathStringValue("@.result").isEqualTo("WIN");
|
||||
assertThat(json.from(response.getBody())).extractingJsonPathStringValue("@.cpu").isEqualTo("PAPER");
|
||||
verify(statSave, times(1)).addWin("joe");
|
||||
}
|
||||
}
|
||||
-42
@@ -1,42 +0,0 @@
|
||||
package fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.domain;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class MoveTest {
|
||||
|
||||
@Test
|
||||
void should_check_move_size(){
|
||||
Assertions.assertEquals(4, Move.values().length);
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0} should beat {1}")
|
||||
@MethodSource("winnable")
|
||||
void should_win(Move move1, Move move2) {
|
||||
assertTrue(move1.beats(move2));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{1} should not beat {0}")
|
||||
@MethodSource("winnable")
|
||||
void should_loose(Move move1, Move move2) {
|
||||
assertFalse(move2.beats(move1));
|
||||
}
|
||||
|
||||
static Stream<Arguments> winnable(){
|
||||
return Stream.of(
|
||||
Arguments.of(Move.ROCK,Move.SCISSORS),
|
||||
Arguments.of(Move.PAPER,Move.ROCK),
|
||||
Arguments.of(Move.PAPER,Move.WELL),
|
||||
Arguments.of(Move.SCISSORS,Move.PAPER),
|
||||
Arguments.of(Move.WELL,Move.SCISSORS),
|
||||
Arguments.of(Move.WELL,Move.ROCK)
|
||||
);
|
||||
}
|
||||
}
|
||||
-42
@@ -1,42 +0,0 @@
|
||||
package fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.domain;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.RockPaperScissorsPlay;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.spi.FakeCpuPicker;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.stat.domain.StatSaver;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class RockPaperScissorsPlayTest {
|
||||
private final StatSaver statSaver = mock(StatSaver.class);
|
||||
private final FakeCpuPicker cpuPick = new FakeCpuPicker();
|
||||
private final RockPaperScissorsPlay play = new RockPaperScissorsPlayer(cpuPick, statSaver);
|
||||
|
||||
@Test
|
||||
void should_win_a_game(){
|
||||
cpuPick.setNextMove(Move.PAPER);
|
||||
RoundResult roundResult = play.playRound("joe", Move.SCISSORS);
|
||||
|
||||
assertEquals("WIN", roundResult.result());
|
||||
assertEquals("joe", roundResult.opponent());
|
||||
assertEquals(Move.PAPER, roundResult.cpuChoice());
|
||||
verify(statSaver, times(1)).addWin("joe");
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_loose_a_game(){
|
||||
cpuPick.setNextMove(Move.ROCK);
|
||||
RoundResult roundResult = play.playRound("joe", Move.SCISSORS);
|
||||
|
||||
assertEquals("LOOSE", roundResult.result());
|
||||
assertEquals("joe", roundResult.opponent());
|
||||
assertEquals(Move.ROCK, roundResult.cpuChoice());
|
||||
verify(statSaver, times(0)).addWin("joe");
|
||||
}
|
||||
}
|
||||
-19
@@ -1,19 +0,0 @@
|
||||
package fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.spi;
|
||||
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.ddd.Stub;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.domain.Move;
|
||||
|
||||
@Stub
|
||||
public class FakeCpuPicker implements CpuPick {
|
||||
|
||||
private Move nextMove = Move.ROCK; // valeur par défaut
|
||||
|
||||
public void setNextMove(Move move) {
|
||||
this.nextMove = move;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Move pick() {
|
||||
return nextMove;
|
||||
}
|
||||
}
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
package fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.spi;
|
||||
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.play.domain.Move;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class RandomCpuPickerTest {
|
||||
|
||||
@Test
|
||||
void shouldReturnAllPossibleMoves() {
|
||||
RandomCpuPicker picker = new RandomCpuPicker();
|
||||
Set<Move> obtainedMoves = new HashSet<>();
|
||||
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
obtainedMoves.add(picker.pick());
|
||||
}
|
||||
|
||||
assertEquals(4, obtainedMoves.size());
|
||||
assertTrue(obtainedMoves.contains(Move.ROCK));
|
||||
assertTrue(obtainedMoves.contains(Move.PAPER));
|
||||
assertTrue(obtainedMoves.contains(Move.SCISSORS));
|
||||
assertTrue(obtainedMoves.contains(Move.WELL));
|
||||
}
|
||||
}
|
||||
-35
@@ -1,35 +0,0 @@
|
||||
package fr.iut_fbleau.info.but3.automation.rock_paper_scissors.stat;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.json.BasicJsonTester;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.modulith.test.ApplicationModuleTest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
@ApplicationModuleTest(
|
||||
webEnvironment = RANDOM_PORT
|
||||
)
|
||||
class StatApplicationTest {
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
private final BasicJsonTester json = new BasicJsonTester(this.getClass());
|
||||
|
||||
@Test
|
||||
void should_get_board_with_player_stats() {
|
||||
var response = restTemplate.getForEntity("/stat", String.class);
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
|
||||
assertThat(json.from(response.getBody())).extractingJsonPathArrayValue("@.stats").hasSize(2);
|
||||
assertThat(json.from(response.getBody())).extractingJsonPathStringValue("@.stats[0].name").isEqualTo("eoj");
|
||||
assertThat(json.from(response.getBody())).extractingJsonPathNumberValue("@.stats[0].wins").isEqualTo(3);
|
||||
assertThat(json.from(response.getBody())).extractingJsonPathStringValue("@.stats[1].name").isEqualTo("joe");
|
||||
assertThat(json.from(response.getBody())).extractingJsonPathNumberValue("@.stats[1].wins").isEqualTo(1);
|
||||
}
|
||||
}
|
||||
-30
@@ -1,30 +0,0 @@
|
||||
package fr.iut_fbleau.info.but3.automation.rock_paper_scissors.stat;
|
||||
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.stat.domain.StatBoard;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.stat.domain.StatBoardGetter;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.stat.spi.stub.InMemoryStatRepository;
|
||||
import java.util.LinkedHashMap;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
class StatBoardGetterTest {
|
||||
|
||||
@Test
|
||||
void should_get_stat_board() {
|
||||
HashMap<String, Integer> stats = new LinkedHashMap<>();
|
||||
stats.put("eoj", 5);
|
||||
stats.put("joe", 1);
|
||||
InMemoryStatRepository statRepository = new InMemoryStatRepository(stats);
|
||||
|
||||
StatBoardGet statBoardGet = new StatBoardGetter(statRepository);
|
||||
|
||||
StatBoard statBoard = statBoardGet.board();
|
||||
|
||||
assertNotNull(statBoard);
|
||||
assertEquals(stats, statBoard.stats());
|
||||
}
|
||||
}
|
||||
-50
@@ -1,50 +0,0 @@
|
||||
package fr.iut_fbleau.info.but3.automation.rock_paper_scissors.stat;
|
||||
|
||||
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.ddd.Stub;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.stat.configuration.StatConfiguration;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.stat.spi.StatRepository;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.stat.web.StatController;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@WebMvcTest(StatController.class)
|
||||
@Import(StatConfiguration.class)
|
||||
class StatControllerTest {
|
||||
|
||||
private static final String STAT_ENDPOINT = "/stat";
|
||||
|
||||
@Autowired
|
||||
MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
StatRepository statRepository;
|
||||
|
||||
@Test
|
||||
void should_get_board_with_player_stats() throws Exception {
|
||||
|
||||
mockMvc.perform(get(STAT_ENDPOINT))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.stats").isArray())
|
||||
.andExpect(jsonPath("$.stats[0].name").value("eoj"))
|
||||
.andExpect(jsonPath("$.stats[0].wins").value(3));
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
@ComponentScan(
|
||||
basePackageClasses = {StatRepository.class},
|
||||
includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Stub.class})})
|
||||
static class StubConfiguration {
|
||||
}
|
||||
|
||||
}
|
||||
-25
@@ -1,25 +0,0 @@
|
||||
package fr.iut_fbleau.info.but3.automation.rock_paper_scissors.stat;
|
||||
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.stat.domain.StatSaver;
|
||||
import fr.iut_fbleau.info.but3.automation.rock_paper_scissors.stat.spi.stub.InMemoryStatRepository;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class StatSaverTest {
|
||||
@ParameterizedTest(name = "name = {0}, initial = {1}, expected = {2}")
|
||||
@CsvSource({"joe,0,1", "eoj,2,3"})
|
||||
void should_save(String name, int initial, int expected) {
|
||||
InMemoryStatRepository statRepository = new InMemoryStatRepository(new HashMap<>(Map.of(name, initial)));
|
||||
StatSave statSave = new StatSaver(statRepository);
|
||||
|
||||
statSave.addWin(name);
|
||||
|
||||
assertEquals(expected, statRepository.get(name));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user