From dd9eb7bd2d5efe4f207b97ae19557e65012c2243 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Fri, 11 Mar 2022 16:54:28 +0100 Subject: [PATCH] Modification de repository --- .../main/java/lightcontainer/storage/Adapter.java | 4 ++-- .../java/lightcontainer/storage/JsonAdapter.java | 13 ++----------- .../java/lightcontainer/storage/Repository.java | 4 +++- .../lightcontainer/storage/JsonAdapterTests.java | 12 +++++------- .../lightcontainer/storage/RepositoryTests.java | 8 ++++---- 5 files changed, 16 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/lightcontainer/storage/Adapter.java b/app/src/main/java/lightcontainer/storage/Adapter.java index 29d5dc3..804f163 100644 --- a/app/src/main/java/lightcontainer/storage/Adapter.java +++ b/app/src/main/java/lightcontainer/storage/Adapter.java @@ -1,8 +1,8 @@ package lightcontainer.storage; public interface Adapter { - - String toString(); + + String toString(AppData appData); AppData fromString(String appDataString); } diff --git a/app/src/main/java/lightcontainer/storage/JsonAdapter.java b/app/src/main/java/lightcontainer/storage/JsonAdapter.java index dcf09c3..4cb8060 100644 --- a/app/src/main/java/lightcontainer/storage/JsonAdapter.java +++ b/app/src/main/java/lightcontainer/storage/JsonAdapter.java @@ -9,18 +9,11 @@ import java.util.*; */ public class JsonAdapter implements Adapter { - private AppData appData; - - public JsonAdapter(AppData appData) { - this.appData = appData; - } - /** - * * @return A Json String containing AppData properties */ @Override - public String toString() { + public String toString(AppData appData) { return addData(appData); } @@ -77,7 +70,6 @@ public class JsonAdapter implements Adapter { } /** - * * @param appDataString The Json String to convert * @return An AppData instance */ @@ -100,8 +92,7 @@ public class JsonAdapter implements Adapter { for (User user : users) { appData.addUser(user.getName(), user.getPassword(), user.getAesKey()); } - this.appData = appData; - return this.appData; + return appData; } catch (JsonParseException parseException) { System.out.println("[FFE] : Error while loading configuration file"); //TODO - changer en log return null; diff --git a/app/src/main/java/lightcontainer/storage/Repository.java b/app/src/main/java/lightcontainer/storage/Repository.java index 27457a5..324b125 100644 --- a/app/src/main/java/lightcontainer/storage/Repository.java +++ b/app/src/main/java/lightcontainer/storage/Repository.java @@ -12,13 +12,15 @@ public class Repository { private final String filePath; private final Adapter adapter; + private final AppData appData; /** * @param filePath The path to the configuration file * @param adapter The adapter that converts objects to string and vice-versa */ - public Repository(String filePath, Adapter adapter) { + public Repository(String filePath, AppData appData, Adapter adapter) { this.filePath = filePath; + this.appData = appData; this.adapter = adapter; } diff --git a/app/src/test/java/lightcontainer/storage/JsonAdapterTests.java b/app/src/test/java/lightcontainer/storage/JsonAdapterTests.java index dc87270..d00a0eb 100644 --- a/app/src/test/java/lightcontainer/storage/JsonAdapterTests.java +++ b/app/src/test/java/lightcontainer/storage/JsonAdapterTests.java @@ -22,17 +22,15 @@ public class JsonAdapterTests { 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); + appData.addUser("User1", "Password","djdjjdj"); + appData.addFileFor(file1, "User1"); + JsonAdapter jsonAdapter = new JsonAdapter(); //WHEN the adapter converts AppData to Json - String jsonAppData = jsonAdapter.toString(); + String jsonAppData = jsonAdapter.toString(appData); //THEN assertTrue(jsonAppData.contains("32000")); assertTrue(jsonAppData.contains("224.25.0.1")); @@ -53,7 +51,7 @@ public class JsonAdapterTests { //GIVEN a Json string String json = "{\"unicast_port\":32000,\"multicast_ip\":\"224.25.0.1\",\"multicast_port\":15502,\"network_interface\":\"My network interface\",\"tls\":false,\"users\":[{\"name\":\"User1\",\"password\":\"Password\",\"aes_key\":\"djdjjdj\",\"files\":[{\"name\":\"File1\",\"size\":15,\"iv\":\"8d8d8d8d\",\"storage\":[\"StorBackEnd1\"]}]}]}"; //WHEN the adapter converts Json to Appdata - JsonAdapter jsonAdapter = new JsonAdapter(null); + JsonAdapter jsonAdapter = new JsonAdapter(); AppData appData = jsonAdapter.fromString(json); //THEN assertNotNull(appData.getAppConfig()); diff --git a/app/src/test/java/lightcontainer/storage/RepositoryTests.java b/app/src/test/java/lightcontainer/storage/RepositoryTests.java index 543ee54..5f96ea5 100644 --- a/app/src/test/java/lightcontainer/storage/RepositoryTests.java +++ b/app/src/test/java/lightcontainer/storage/RepositoryTests.java @@ -40,11 +40,11 @@ public class RepositoryTests { File file1 = new File("File1", 15, "8d8d8d8d", storage); appData.setAppConfig(appConfig); appData.addUser("User1", "Password", "djdjjdj"); - JsonAdapter jsonAdapter = new JsonAdapter(appData); + JsonAdapter jsonAdapter = new JsonAdapter(); appData.addFileFor(file1, "User1"); //WHEN Repository calls save method String filePath = "src/test/resources/test.json"; - Repository repository = new Repository(filePath, jsonAdapter); + Repository repository = new Repository(filePath, appData, jsonAdapter); repository.save(); //THEN assertTrue(Files.exists(Paths.get("src/test/resources/test.json").toAbsolutePath())); @@ -53,9 +53,9 @@ public class RepositoryTests { @Test public void load() { //GIVEN a test json file loadTest.json - JsonAdapter jsonAdapter = new JsonAdapter(null); + JsonAdapter jsonAdapter = new JsonAdapter(); //WHEN repository calls load method - Repository repository = new Repository("src/test/resources/loadTest.json", jsonAdapter); + Repository repository = new Repository("src/test/resources/loadTest.json", AppData.getInstance(), jsonAdapter); AppData appData = repository.load(); //THEN assertNotNull(appData.getAppConfig());