From d979fbd1f3fe4550e771b4e8c4d6a6df8bececef Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Tue, 8 Mar 2022 11:45:18 +0100 Subject: [PATCH] =?UTF-8?q?Repository=20:=20save=20test=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lightcontainer/storage/Repository.java | 8 +-- ...AdapterTest.java => JsonAdapterTests.java} | 2 +- .../storage/RepositoryTests.java | 56 +++++++++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) rename app/src/test/java/lightcontainer/storage/{JsonAdapterTest.java => JsonAdapterTests.java} (98%) create mode 100644 app/src/test/java/lightcontainer/storage/RepositoryTests.java diff --git a/app/src/main/java/lightcontainer/storage/Repository.java b/app/src/main/java/lightcontainer/storage/Repository.java index 778b979..d6f12bc 100644 --- a/app/src/main/java/lightcontainer/storage/Repository.java +++ b/app/src/main/java/lightcontainer/storage/Repository.java @@ -12,10 +12,10 @@ public class Repository { //static final String CONFIG_FILE_PATH = "../../resources/config.json"; - void save(String filePath, Adapter adapter) { + static void save(String filePath, Adapter adapter) { if (filePath != null) { String jsonAppData = adapter.toString(); - try (BufferedWriter bufferedWriter = Files.newBufferedWriter(Paths.get(filePath), 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)) { bufferedWriter.write(jsonAppData); bufferedWriter.flush(); } catch (IOException e) { @@ -29,9 +29,9 @@ public class Repository { return adapter.fromString(jsonString); } - private String readFile(String filePath) { + static private String readFile(String filePath) { StringBuilder builder = new StringBuilder(); - try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath), StandardCharsets.UTF_8)) { + try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath).toAbsolutePath(), StandardCharsets.UTF_8)) { while (reader.ready()) { builder.append(reader.readLine()); } diff --git a/app/src/test/java/lightcontainer/storage/JsonAdapterTest.java b/app/src/test/java/lightcontainer/storage/JsonAdapterTests.java similarity index 98% rename from app/src/test/java/lightcontainer/storage/JsonAdapterTest.java rename to app/src/test/java/lightcontainer/storage/JsonAdapterTests.java index 2bde872..bd9006a 100644 --- a/app/src/test/java/lightcontainer/storage/JsonAdapterTest.java +++ b/app/src/test/java/lightcontainer/storage/JsonAdapterTests.java @@ -10,7 +10,7 @@ import java.util.Set; import static org.junit.jupiter.api.Assertions.*; -public class JsonAdapterTest { +public class JsonAdapterTests { @Test public void convertAppDataToJson() { diff --git a/app/src/test/java/lightcontainer/storage/RepositoryTests.java b/app/src/test/java/lightcontainer/storage/RepositoryTests.java new file mode 100644 index 0000000..0fa557a --- /dev/null +++ b/app/src/test/java/lightcontainer/storage/RepositoryTests.java @@ -0,0 +1,56 @@ +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; +import java.util.Map; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.*; + + +public class RepositoryTests { + + @AfterEach + public void destroyTestFile() { + try { + Files.deleteIfExists(Paths.get("src", "test", "resources", "test.json").toAbsolutePath()); + } catch (IOException e) { + System.out.println("Error while destroying file"); + } + } + + @Test + public void save() { + //GIVEN an AppData instance, a JsonAdapter and a Repository + AppData appData = AppData.getInstance(); + AppConfig appConfig = AppConfig.getInstance(); + appConfig.setUnicastPort(32000); + appConfig.setMulticastIp("224.25.0.1"); + appConfig.setMulticastPort(15502); + appConfig.setNetworkInterface("My network interface"); + appConfig.setTls(false); + Map files = new HashMap<>(); + Set storage = new HashSet<>(); + storage.add("StorBackEnd1"); + File file1 = new File("File1", 15, "8d8d8d8d", storage); + files.put(file1.getName(), file1); + User user1 = new User("User1", "Password", "djdjjdj", files); + appData.setAppConfig(appConfig); + appData.addUser(user1); + JsonAdapter jsonAdapter = new JsonAdapter(appData); + //WHEN Repository calls save method + String filePath = "src/test/resources/test.json"; + Repository.save(filePath, jsonAdapter); + //THEN + assertTrue(Files.exists(Paths.get("src/test/resources/test.json").toAbsolutePath())); + } +}