diff --git a/app/src/main/java/lightcontainer/storage/Repository.java b/app/src/main/java/lightcontainer/storage/Repository.java index 1f085a6..27457a5 100644 --- a/app/src/main/java/lightcontainer/storage/Repository.java +++ b/app/src/main/java/lightcontainer/storage/Repository.java @@ -10,11 +10,22 @@ import java.nio.file.StandardOpenOption; public class Repository { + private final String filePath; + private final Adapter adapter; + /** - * @param filePath The path where the file must be saved - * @param adapter The service that converts Objects to Strings + * @param filePath The path to the configuration file + * @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) { String jsonAppData = adapter.toString(); 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 - * @param adapter The service that converts Strings to objects - * @return + * Loads configuration file + * + * @return AppData */ - static AppData load(String filePath, Adapter adapter) { + public AppData load() { String jsonString = readFile(filePath); return adapter.fromString(jsonString); } - private static String readFile(String filePath) { + private String readFile(String filePath) { StringBuilder builder = new StringBuilder(); try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath).toAbsolutePath(), StandardCharsets.UTF_8)) { while (reader.ready()) { diff --git a/app/src/test/java/lightcontainer/storage/RepositoryTests.java b/app/src/test/java/lightcontainer/storage/RepositoryTests.java index abb3f15..77913f8 100644 --- a/app/src/test/java/lightcontainer/storage/RepositoryTests.java +++ b/app/src/test/java/lightcontainer/storage/RepositoryTests.java @@ -1,13 +1,10 @@ package lightcontainer.storage; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.IOException; import java.nio.file.Files; -import java.nio.file.InvalidPathException; -import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.HashSet; @@ -49,7 +46,8 @@ public class RepositoryTests { JsonAdapter jsonAdapter = new JsonAdapter(appData); //WHEN Repository calls save method String filePath = "src/test/resources/test.json"; - Repository.save(filePath, jsonAdapter); + Repository repository = new Repository(filePath, jsonAdapter); + repository.save(); //THEN 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 JsonAdapter jsonAdapter = new JsonAdapter(null); //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 assertNotNull(appData.getAppConfig()); assertEquals("My network interface", appData.getAppConfig().getNetworkInterface());