153 lines
4.8 KiB
Java
153 lines
4.8 KiB
Java
package de.deversmann.facade;
|
|
|
|
import de.deversmann.domain.*;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.mockito.Mockito;
|
|
import org.powermock.api.mockito.PowerMockito;
|
|
|
|
import java.io.IOException;
|
|
import java.util.List;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
import static org.mockito.Mockito.*;
|
|
|
|
class FacadeTest {
|
|
|
|
private CSVReader csvReaderMock;
|
|
private HighscoreManager highscoreManagerMock;
|
|
private Zeiterfassung zeiterfassungMock;
|
|
private Facade facade;
|
|
|
|
@BeforeEach
|
|
void setup() throws Exception {
|
|
facade = new Facade();
|
|
|
|
csvReaderMock = mock(CSVReader.class);
|
|
highscoreManagerMock = mock(HighscoreManager.class);
|
|
zeiterfassungMock = mock(Zeiterfassung.class);
|
|
|
|
PowerMockito.field(Facade.class, "csvReader").set(facade, csvReaderMock);
|
|
PowerMockito.field(Facade.class, "highscoreManager").set(facade, highscoreManagerMock);
|
|
PowerMockito.field(Facade.class, "zeiterfassung").set(facade, zeiterfassungMock);
|
|
}
|
|
|
|
@Test
|
|
void testLadeSpielfeldMitEchtenDaten() throws IOException {
|
|
PuzzleData puzzleData = new PuzzleData(new int[][]{{1, 2, 3, 4}}, List.of(new int[]{1, 1}));
|
|
when(csvReaderMock.readPuzzleWithSolution(anyString())).thenReturn(puzzleData);
|
|
|
|
facade.ladeSpielfeld("test.csv");
|
|
|
|
assertNotNull(facade.getAktuellesPuzzle());
|
|
assertEquals(1, facade.getLoesungsKoordinaten().size());
|
|
}
|
|
|
|
@Test
|
|
void testStopZeiterfassung() {
|
|
when(zeiterfassungMock.getElapsedTimeInSeconds()).thenReturn(42L);
|
|
|
|
long elapsedTime = facade.stopZeiterfassung();
|
|
|
|
verify(zeiterfassungMock, times(1)).stop();
|
|
assertEquals(42L, elapsedTime);
|
|
}
|
|
|
|
@Test
|
|
void testGetElapsedTimeSoFar() {
|
|
when(zeiterfassungMock.getElapsedTimeInSeconds()).thenReturn(15L);
|
|
|
|
long elapsedTime = facade.getElapsedTimeSoFar();
|
|
|
|
verify(zeiterfassungMock, times(1)).getElapsedTimeInSeconds();
|
|
assertEquals(15L, elapsedTime);
|
|
}
|
|
|
|
@Test
|
|
void testIncrementErrorCount() {
|
|
assertEquals(0, facade.getCurrentErrorCount());
|
|
|
|
facade.incrementErrorCount();
|
|
|
|
assertEquals(1, facade.getCurrentErrorCount());
|
|
}
|
|
|
|
@Test
|
|
void testAddHighscoreForCurrentPuzzle() throws IOException {
|
|
facade.ladeSpielfeld("test.csv");
|
|
|
|
facade.addHighscoreForCurrentPuzzle("Player1", 100L, 3);
|
|
|
|
verify(highscoreManagerMock, times(1))
|
|
.addHighscore(eq("test.csv"), eq("Player1"), eq(100L), eq(3));
|
|
}
|
|
|
|
@Test
|
|
void testGetHighscoresForCurrentPuzzle() throws IOException {
|
|
when(highscoreManagerMock.getHighscores("test.csv"))
|
|
.thenReturn(List.of(new HighscoreEntry("Player1", 100L, 2)));
|
|
|
|
facade.ladeSpielfeld("test.csv");
|
|
List<HighscoreEntry> highscores = facade.getHighscoresForCurrentPuzzle();
|
|
|
|
assertEquals(1, highscores.size());
|
|
assertEquals("Player1", highscores.get(0).getPlayerName());
|
|
}
|
|
|
|
@Test
|
|
void testGetAverageTimeForCurrentPuzzle() throws IOException {
|
|
when(highscoreManagerMock.getAverageTime("test.csv")).thenReturn(75.0);
|
|
|
|
facade.ladeSpielfeld("test.csv");
|
|
double avgTime = facade.getAverageTimeForCurrentPuzzle();
|
|
|
|
assertEquals(75.0, avgTime, 0.01);
|
|
}
|
|
|
|
@Test
|
|
void testSaveCurrentPuzzleHighscoreToFile() throws IOException {
|
|
facade.ladeSpielfeld("test.csv");
|
|
|
|
facade.saveCurrentPuzzleHighscoreToFile();
|
|
|
|
verify(highscoreManagerMock, times(1))
|
|
.saveSinglePuzzle(eq("test.csv"), anyString());
|
|
}
|
|
|
|
@Test
|
|
void testLoadCurrentPuzzleHighscoreFromFile() throws IOException {
|
|
facade.ladeSpielfeld("test.csv");
|
|
|
|
facade.loadCurrentPuzzleHighscoreFromFile();
|
|
|
|
verify(highscoreManagerMock, times(1))
|
|
.loadSinglePuzzle(eq("test.csv"), anyString());
|
|
}
|
|
|
|
@Test
|
|
void testGetAktuellesPuzzle() throws IOException {
|
|
PuzzleData puzzleData = new PuzzleData(new int[][]{{1, 2, 3, 4}}, List.of());
|
|
when(csvReaderMock.readPuzzleWithSolution(anyString())).thenReturn(puzzleData);
|
|
|
|
facade.ladeSpielfeld("test.csv");
|
|
|
|
int[][] puzzle = facade.getAktuellesPuzzle();
|
|
|
|
assertNotNull(puzzle);
|
|
assertEquals(4, puzzle[0].length);
|
|
}
|
|
|
|
@Test
|
|
void testGetLoesungsKoordinaten() throws IOException {
|
|
PuzzleData puzzleData = new PuzzleData(new int[][]{{1, 2}}, List.of(new int[]{1, 1}));
|
|
when(csvReaderMock.readPuzzleWithSolution(anyString())).thenReturn(puzzleData);
|
|
|
|
facade.ladeSpielfeld("test.csv");
|
|
|
|
List<int[]> solutionCoordinates = facade.getLoesungsKoordinaten();
|
|
|
|
assertNotNull(solutionCoordinates);
|
|
assertEquals(1, solutionCoordinates.size());
|
|
assertArrayEquals(new int[]{1, 1}, solutionCoordinates.get(0));
|
|
}
|
|
} |