Storage Repository : système simplifié

This commit is contained in:
Maximilien LEDOUX 2022-03-11 15:52:02 +01:00
parent 56a8121782
commit 1a3b796f61
2 changed files with 24 additions and 13 deletions

View File

@ -10,11 +10,22 @@ import java.nio.file.StandardOpenOption;
public class Repository { public class Repository {
private final String filePath;
private final Adapter adapter;
/** /**
* @param filePath The path where the file must be saved * @param filePath The path to the configuration file
* @param adapter The service that converts Objects to Strings * @param adapter The adapter that converts objects to string and vice-versa
*/ */
static void save(String filePath, Adapter adapter) { public Repository(String filePath, Adapter adapter) {
this.filePath = filePath;
this.adapter = adapter;
}
/**
* Saves configuration file
*/
public void save() {
if (filePath != null) { if (filePath != null) {
String jsonAppData = adapter.toString(); String jsonAppData = adapter.toString();
try (BufferedWriter bufferedWriter = Files.newBufferedWriter(Paths.get(filePath).toAbsolutePath(), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) { try (BufferedWriter bufferedWriter = Files.newBufferedWriter(Paths.get(filePath).toAbsolutePath(), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
@ -26,17 +37,18 @@ public class Repository {
} }
} }
/** /**
* @param filePath The path where the file is stored * Loads configuration file
* @param adapter The service that converts Strings to objects *
* @return * @return AppData
*/ */
static AppData load(String filePath, Adapter adapter) { public AppData load() {
String jsonString = readFile(filePath); String jsonString = readFile(filePath);
return adapter.fromString(jsonString); return adapter.fromString(jsonString);
} }
private static String readFile(String filePath) { private String readFile(String filePath) {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath).toAbsolutePath(), StandardCharsets.UTF_8)) { try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath).toAbsolutePath(), StandardCharsets.UTF_8)) {
while (reader.ready()) { while (reader.ready()) {

View File

@ -1,13 +1,10 @@
package lightcontainer.storage; package lightcontainer.storage;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -49,7 +46,8 @@ public class RepositoryTests {
JsonAdapter jsonAdapter = new JsonAdapter(appData); JsonAdapter jsonAdapter = new JsonAdapter(appData);
//WHEN Repository calls save method //WHEN Repository calls save method
String filePath = "src/test/resources/test.json"; String filePath = "src/test/resources/test.json";
Repository.save(filePath, jsonAdapter); Repository repository = new Repository(filePath, jsonAdapter);
repository.save();
//THEN //THEN
assertTrue(Files.exists(Paths.get("src/test/resources/test.json").toAbsolutePath())); assertTrue(Files.exists(Paths.get("src/test/resources/test.json").toAbsolutePath()));
} }
@ -59,7 +57,8 @@ public class RepositoryTests {
//GIVEN a test json file loadTest.json //GIVEN a test json file loadTest.json
JsonAdapter jsonAdapter = new JsonAdapter(null); JsonAdapter jsonAdapter = new JsonAdapter(null);
//WHEN repository calls load method //WHEN repository calls load method
AppData appData = Repository.load("src/test/resources/loadTest.json", jsonAdapter); Repository repository = new Repository("src/test/resources/loadTest.json", jsonAdapter);
AppData appData = repository.load();
//THEN //THEN
assertNotNull(appData.getAppConfig()); assertNotNull(appData.getAppConfig());
assertEquals("My network interface", appData.getAppConfig().getNetworkInterface()); assertEquals("My network interface", appData.getAppConfig().getNetworkInterface());