Compare commits

...

3 commits

Author SHA1 Message Date
Augusto Dwenger J. 905fc4ba35 Refactor reading a player from a file
Reduce code duplication and simplify error handling.
2022-09-23 20:34:13 +02:00
Augusto Dwenger J. 7e0cad6ddb Update jackson-databind version to 2.13.4 2022-09-23 20:20:49 +02:00
Augusto Dwenger J. 01f62303fc Update mockito to version 4.8.0 2022-09-23 20:19:56 +02:00
2 changed files with 21 additions and 25 deletions

View file

@ -13,6 +13,7 @@
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
<junit.jupiter.version>5.9.0</junit.jupiter.version>
<mockito.version>4.8.0</mockito.version>
</properties>
<repositories>
@ -46,20 +47,20 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.7.0</version>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.7.0</version>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<!-- to test my json implementation -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
<version>2.13.4</version>
<scope>test</scope>
</dependency>
</dependencies>

View file

@ -43,13 +43,8 @@ public final class FileSystemDB implements PersistPlayer, FindPlayer {
continue;
}
try {
final String fileContent = Files.readString(file.toPath());
final Player player = serializationFactory.createDeserializer(fileContent);
return Optional.of(player);
} catch (IOException e) {
throw new ReadPlayerException("Player not found with the uuid: " + uuid, e);
}
final Player player = readPlayerFromFile(file);
return Optional.of(player);
}
return Optional.empty();
@ -59,14 +54,9 @@ public final class FileSystemDB implements PersistPlayer, FindPlayer {
public Optional<Player> findByName(final String name) throws ReadPlayerException {
final File[] files = getFiles();
for (final var file : files) {
try {
final String fileContent = Files.readString(file.toPath());
final Player player = serializationFactory.createDeserializer(fileContent);
if (player.name().equals(name)) {
return Optional.of(player);
}
} catch (IOException e) {
throw new ReadPlayerException("Player not found with the name: " + name, e);
final Player player = readPlayerFromFile(file);
if (player.name().equals(name)) {
return Optional.of(player);
}
}
@ -78,13 +68,8 @@ public final class FileSystemDB implements PersistPlayer, FindPlayer {
final File[] files = getFiles();
final var playerList = new ArrayList<Player>();
for (final var file : files) {
try {
final String fileContent = Files.readString(file.toPath());
final Player player = serializationFactory.createDeserializer(fileContent);
playerList.add(player);
} catch (IOException e) {
throw new ReadPlayerException("Player not found with the path: " + file.getAbsolutePath(), e);
}
final Player player = readPlayerFromFile(file);
playerList.add(player);
}
return playerList;
@ -99,4 +84,14 @@ public final class FileSystemDB implements PersistPlayer, FindPlayer {
return files;
}
private Player readPlayerFromFile(final File playerFile) throws ReadPlayerException {
try {
final String fileContent = Files.readString(playerFile.toPath());
return serializationFactory.createDeserializer(fileContent);
} catch (IOException e) {
final String fileName = playerFile.getName();
throw new ReadPlayerException("Could not read file: " + fileName, e);
}
}
}