From 2f1f72b1fc999a8a71d02ca3ffdf199fa5ca6ab1 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Sat, 5 Mar 2022 16:51:12 +0100 Subject: [PATCH 01/17] =?UTF-8?q?Syst=C3=A8me=20de=20synchronisation=20de?= =?UTF-8?q?=20l'information=20entre=20FileFrontEnd<>StorBackEnd=20->=20imp?= =?UTF-8?q?l=C3=A9mentation=20de=20AppData=20+=20ajout=20de=20la=20bibliot?= =?UTF-8?q?h=C3=A8que=20GSON=20=C3=A0=20build.gradle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 2 + .../java/lightcontainer/storage/AppData.java | 132 ++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 app/src/main/java/lightcontainer/storage/AppData.java diff --git a/app/build.gradle b/app/build.gradle index 2a11f36..5345a80 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -22,6 +22,8 @@ dependencies { // This dependency is used by the application. implementation 'com.google.guava:guava:30.1-jre' + // Use gson to serialize/deserialize json files + implementation 'com.google.code.gson:gson:2.9.0' } application { diff --git a/app/src/main/java/lightcontainer/storage/AppData.java b/app/src/main/java/lightcontainer/storage/AppData.java new file mode 100644 index 0000000..ba10605 --- /dev/null +++ b/app/src/main/java/lightcontainer/storage/AppData.java @@ -0,0 +1,132 @@ +package lightcontainer.storage; + +import java.util.HashMap; +import java.util.Map; + +/** + * AppData represents the database of the FileFrontEnd program. + * It contains an AppConfig instance and a collection of Users. + * + * @author Maximilien LEDOUX + * @version 1.0 + * @see AppConfig + * @see User + * @since 1.0 + */ +public class AppData { + + private static AppData instance = null; + private AppConfig appConfig; + private final Map users; + + + /** + * Constructs a new instance of AppData. + * Sets appConfig to null and creates a new Hashmap of users. + */ + private AppData() { + this.appConfig = null; + this.users = new HashMap<>(); + } + + /** + * @return An instance of this class. Always returns the same instance. + */ + public static AppData getInstance() { + if (instance == null) { + instance = new AppData(); + } + return instance; + } + + /** + * @return The AppConfig + */ + public AppConfig getAppConfig() { + return appConfig; + } + + /** + * Sets the AppConfig. This method sets the AppConfig for once and for all. + * It is locked after first call. + * + * @param appConfig The network configuration of the program. + */ + public void setAppConfig(AppConfig appConfig) { + if (this.appConfig == null) { + this.appConfig = appConfig; + } + } + + /** + * @param userName The name of the user. + * @return The user corresponding to userName, null otherwise. + */ + public User getUser(String userName) { + return this.users.get(userName); + } + + /** + * Use this method when a user signs up. + * + * @param user The user to add. + * @return True if the user was added. False if a user with the same name already exists. + */ + public boolean addUser(User user) { + if (this.users.containsKey(user.getName())) { + return false; + } else { + this.users.put(user.getName(), user); + return true; + } + } + + /** + * @param fileName The name of the file + * @param user The user + * @return The file corresponding to the given name and belonging to the user. Null if the user cannot be found or the file cannot be found + * @deprecated Maybe not useful. DO NOT USE FOR THE TIME BEING + */ + public File getFileOf(String fileName, User user) { + return this.users.get(user.getName()).getFile(fileName); + } + + /** + * Call this method after receiving SAVEFILE_OK from the StorBackEnd. + * Do NOT call when receiving SAVEFILE_ERROR, or it will break the system's synchronization. + *

+ * Adds the file of for a specific user. + * True indicates the success of the operation. + * False indicates the failure of the operation. + * + * @param file The file to add + * @param user The user who wants to add the file + * @return True if the user is found and a file with the same name doesn't already exist for this user. False otherwise. + */ + public boolean addFileFor(File file, User user) { + if (!this.users.containsKey(user.getName())) { + return false; + } else { + return this.users.get(user.getName()).addFile(file); + } + } + + /** + * Call this method after receiving REMOVEFILE_OK from the StorBackEnd. + * Do NOT call when receiving REMOVEFILE_ERROR, or it will break the system's synchronization. + * Deletes the file of for a specific user. + * True indicates the success of the operation. + * False indicates the failure of the operation. + * + * @param fileName The name of the file to delete + * @param user The user who wants to delete the file + * @return True if the user is found and the file was deleted. False otherwise. + */ + public boolean deleteFileOf(String fileName, User user) { + if (!this.users.containsKey(user.getName())) { + return false; + } else { + return this.users.get(user.getName()).deleteFile(fileName); + } + } +} From 5e95cee500a33beaf8a70c8cfc44130c012af1bb Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Sat, 5 Mar 2022 16:59:40 +0100 Subject: [PATCH 02/17] =?UTF-8?q?Syst=C3=A8me=20de=20synchronisation=20de?= =?UTF-8?q?=20l'information=20entre=20FileFrontEnd<>StorBackEnd=20->=20imp?= =?UTF-8?q?l=C3=A9mentation=20de=20AppConfig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lightcontainer/storage/AppConfig.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 app/src/main/java/lightcontainer/storage/AppConfig.java diff --git a/app/src/main/java/lightcontainer/storage/AppConfig.java b/app/src/main/java/lightcontainer/storage/AppConfig.java new file mode 100644 index 0000000..7f767b8 --- /dev/null +++ b/app/src/main/java/lightcontainer/storage/AppConfig.java @@ -0,0 +1,88 @@ +package lightcontainer.storage; + +/** + * AppConfig represents all network related information needed for the program to work. + * + * @author Maximilien LEDOUX + * @version 1.0 + * @since 1.0 + */ +public class AppConfig { + + private static AppConfig instance = null; + private int unicastPort; + private String multicastIp; + private int multicastPort; + private String networkInterface; + private boolean isTls; + + /** + * Constructs a new instance of AppConfig. + * Sets all data to default values. + */ + private AppConfig() { + this.unicastPort = -1; + this.multicastIp = "NONE"; + this.multicastPort = -1; + this.networkInterface = "NONE"; + this.isTls = false; + } + + /** + * @return An instance of this class. Always returns the same instance. + */ + public static AppConfig getInstance() { + if (instance == null) { + instance = new AppConfig(); + } + return instance; + } + + public int getUnicastPort() { + return unicastPort; + } + + public void setUnicastPort(int unicastPort) { + if (this.unicastPort == -1) { + this.unicastPort = unicastPort; + } + } + + public String getMulticastIp() { + return multicastIp; + } + + public void setMulticastIp(String multicastIp) { + if (this.multicastIp.equals("NONE")) { + this.multicastIp = multicastIp; + } + } + + public int getMulticastPort() { + return multicastPort; + } + + public void setMulticastPort(int multicastPort) { + if (this.multicastPort == -1) { + this.multicastPort = multicastPort; + } + } + + public String getNetworkInterface() { + return networkInterface; + } + + public void setNetworkInterface(String networkInterface) { + if (this.networkInterface.equals("NONE")) { + this.networkInterface = networkInterface; + } + } + + public boolean isTls() { + return isTls; + } + + public void setTls(boolean tls) { + this.isTls = tls; + } +} From 28fded57636eeaf9ca6d1cfdf2c8cced88c8e43e Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Sat, 5 Mar 2022 17:00:05 +0100 Subject: [PATCH 03/17] =?UTF-8?q?Syst=C3=A8me=20de=20synchronisation=20de?= =?UTF-8?q?=20l'information=20entre=20FileFrontEnd<>StorBackEnd=20->=20cr?= =?UTF-8?q?=C3=A9ation=20de=20Repository=20et=20JsonRepository?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lightcontainer/storage/JsonRepository.java | 15 +++++++++++++++ .../java/lightcontainer/storage/Repository.java | 8 ++++++++ 2 files changed, 23 insertions(+) create mode 100644 app/src/main/java/lightcontainer/storage/JsonRepository.java create mode 100644 app/src/main/java/lightcontainer/storage/Repository.java diff --git a/app/src/main/java/lightcontainer/storage/JsonRepository.java b/app/src/main/java/lightcontainer/storage/JsonRepository.java new file mode 100644 index 0000000..a002404 --- /dev/null +++ b/app/src/main/java/lightcontainer/storage/JsonRepository.java @@ -0,0 +1,15 @@ +package lightcontainer.storage; + +public class JsonRepository implements Repository { + + + @Override + public void save(String fileName, AppData data) { + + } + + @Override + public AppData load(String fileName) { + return null; + } +} diff --git a/app/src/main/java/lightcontainer/storage/Repository.java b/app/src/main/java/lightcontainer/storage/Repository.java new file mode 100644 index 0000000..79f19fd --- /dev/null +++ b/app/src/main/java/lightcontainer/storage/Repository.java @@ -0,0 +1,8 @@ +package lightcontainer.storage; + +public interface Repository { + + void save(String fileName, AppData data); + + AppData load(String fileName); +} From 49fe7fb7e7fd70ae465d5e1443fb050e6a84023d Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Sat, 5 Mar 2022 17:08:36 +0100 Subject: [PATCH 04/17] =?UTF-8?q?Syst=C3=A8me=20de=20synchronisation=20de?= =?UTF-8?q?=20l'information=20entre=20FileFrontEnd<>StorBackEnd=20->=20imp?= =?UTF-8?q?l=C3=A9mentation=20de=20User?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lightcontainer/storage/User.java | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 app/src/main/java/lightcontainer/storage/User.java diff --git a/app/src/main/java/lightcontainer/storage/User.java b/app/src/main/java/lightcontainer/storage/User.java new file mode 100644 index 0000000..1623703 --- /dev/null +++ b/app/src/main/java/lightcontainer/storage/User.java @@ -0,0 +1,72 @@ +package lightcontainer.storage; + +import java.util.Map; +import java.util.Set; + +/** + * User represents a user of the system. + * + * @author Maximilien LEDOUX + * @version 1.0 + * @since 1.0 + */ +public class User { + + private final String Name; + private final String password; + private final String aesKey; + private final Map files; + + public User(String Name, String password, String aesKey, Map files) { + this.Name = Name; + this.password = password; + this.aesKey = aesKey; + this.files = files; + } + + public String getName() { + return Name; + } + + public String getPassword() { + return password; + } + + public String getAesKey() { + return aesKey; + } + + public Set> getFilesIterator() { + return files.entrySet(); + } + + public File getFile(String fileName) { + return this.files.get(fileName); + } + + /** + * @param file The file to add. + * @return False if a file with the same name already exists. Otherwise, adds the file and returns true. + */ + public boolean addFile(File file) { + if (this.files.containsKey(file.getName())) { + return false; + } else { + this.files.put(file.getName(), file); + return true; + } + } + + /** + * @param fileName The name of the file to delete. + * @return True if the file was deleted. False otherwise. + */ + public boolean deleteFile(String fileName) { + if (this.files.containsKey(fileName)) { + this.files.remove(fileName); + return true; + } else { + return false; + } + } +} From b2d34dbaf4eee549477fecf8d2b5be8efb9e8c7a Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Sat, 5 Mar 2022 17:25:55 +0100 Subject: [PATCH 05/17] =?UTF-8?q?Syst=C3=A8me=20de=20synchronisation=20de?= =?UTF-8?q?=20l'information=20entre=20FileFrontEnd<>StorBackEnd=20->=20imp?= =?UTF-8?q?l=C3=A9mentation=20de=20File?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lightcontainer/storage/File.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 app/src/main/java/lightcontainer/storage/File.java diff --git a/app/src/main/java/lightcontainer/storage/File.java b/app/src/main/java/lightcontainer/storage/File.java new file mode 100644 index 0000000..dcee5cc --- /dev/null +++ b/app/src/main/java/lightcontainer/storage/File.java @@ -0,0 +1,44 @@ +package lightcontainer.storage; + +import java.util.Iterator; +import java.util.Set; + +public class File { + + private final String name; + private final int size; + private final String iv; + private final Set storage; + + public File(String name, int size, String iv, Set storage) { + this.name = name; + this.size = size; + this.iv = iv; + this.storage = storage; + } + + public String getName() { + return name; + } + + public int getSize() { + return size; + } + + public String getIv() { + return iv; + } + + public Iterator getStorageIterator() { + return storage.iterator(); + } + + public boolean addStorage(String storage) { + if (this.storage.contains(storage)) { + return false; + } else { + this.storage.add(storage); + return true; + } + } +} From 0161077cdd0506a41008ac9ea1291f7772d17355 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Sat, 5 Mar 2022 17:35:09 +0100 Subject: [PATCH 06/17] =?UTF-8?q?Syst=C3=A8me=20de=20synchronisation=20de?= =?UTF-8?q?=20l'information=20entre=20FileFrontEnd<>StorBackEnd=20->=20add?= =?UTF-8?q?Storage=20:=20ajouter=20des=20StorBackEnd=20=C3=A0=20un=20fichi?= =?UTF-8?q?er?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/lightcontainer/storage/AppData.java | 14 ++++++++++++++ app/src/main/java/lightcontainer/storage/User.java | 13 +++++++++++++ 2 files changed, 27 insertions(+) diff --git a/app/src/main/java/lightcontainer/storage/AppData.java b/app/src/main/java/lightcontainer/storage/AppData.java index ba10605..88b245e 100644 --- a/app/src/main/java/lightcontainer/storage/AppData.java +++ b/app/src/main/java/lightcontainer/storage/AppData.java @@ -129,4 +129,18 @@ public class AppData { return this.users.get(user.getName()).deleteFile(fileName); } } + + /** + * @param user The user who wants to add a storage for their file + * @param file The file that needs a new storage + * @param storage The storage to add + * @return True if the storage was added. False otherwise. + */ + public boolean addStorage(User user, File file, String storage) { + if (!this.users.containsKey(user.getName())) { + return false; + } else { + return this.users.get(user.getName()).addStorage(file, storage); + } + } } diff --git a/app/src/main/java/lightcontainer/storage/User.java b/app/src/main/java/lightcontainer/storage/User.java index 1623703..683fb07 100644 --- a/app/src/main/java/lightcontainer/storage/User.java +++ b/app/src/main/java/lightcontainer/storage/User.java @@ -69,4 +69,17 @@ public class User { return false; } } + + /** + * @param file The file that needs a storage + * @param storage The storage name + * @return True if the storage was added to the file. False otherwise. + */ + public boolean addStorage(File file, String storage) { + if (this.files.containsKey(file.getName())) { + return file.addStorage(storage); + } else { + return false; + } + } } From a185a3ecb77fe04360ab7f2635080743c9f0a764 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Sat, 5 Mar 2022 17:48:56 +0100 Subject: [PATCH 07/17] =?UTF-8?q?Sauvegarde=20et=20lecture=20du=20fichier?= =?UTF-8?q?=20de=20synchronisation=20->=20changement=20de=20structure=20po?= =?UTF-8?q?ur=20une=20meilleur=20d=C3=A9coupe=20des=20responsabilit=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/lightcontainer/storage/FileReader.java | 6 ++++++ app/src/main/java/lightcontainer/storage/FileWriter.java | 6 ++++++ app/src/main/java/lightcontainer/storage/JsonReader.java | 9 +++++++++ .../main/java/lightcontainer/storage/JsonRepository.java | 6 ++++-- app/src/main/java/lightcontainer/storage/JsonWriter.java | 9 +++++++++ app/src/main/java/lightcontainer/storage/Repository.java | 4 ++-- 6 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/lightcontainer/storage/FileReader.java create mode 100644 app/src/main/java/lightcontainer/storage/FileWriter.java create mode 100644 app/src/main/java/lightcontainer/storage/JsonReader.java create mode 100644 app/src/main/java/lightcontainer/storage/JsonWriter.java diff --git a/app/src/main/java/lightcontainer/storage/FileReader.java b/app/src/main/java/lightcontainer/storage/FileReader.java new file mode 100644 index 0000000..4e79a0b --- /dev/null +++ b/app/src/main/java/lightcontainer/storage/FileReader.java @@ -0,0 +1,6 @@ +package lightcontainer.storage; + +public interface FileReader { + + AppData readFile(String fileName); +} diff --git a/app/src/main/java/lightcontainer/storage/FileWriter.java b/app/src/main/java/lightcontainer/storage/FileWriter.java new file mode 100644 index 0000000..3cef3aa --- /dev/null +++ b/app/src/main/java/lightcontainer/storage/FileWriter.java @@ -0,0 +1,6 @@ +package lightcontainer.storage; + +public interface FileWriter { + + void writeToFile(String fileName, AppData appData); +} diff --git a/app/src/main/java/lightcontainer/storage/JsonReader.java b/app/src/main/java/lightcontainer/storage/JsonReader.java new file mode 100644 index 0000000..0b67603 --- /dev/null +++ b/app/src/main/java/lightcontainer/storage/JsonReader.java @@ -0,0 +1,9 @@ +package lightcontainer.storage; + +public class JsonReader implements FileReader { + + @Override + public AppData readFile(String fileName) { + return null; + } +} diff --git a/app/src/main/java/lightcontainer/storage/JsonRepository.java b/app/src/main/java/lightcontainer/storage/JsonRepository.java index a002404..c2c693d 100644 --- a/app/src/main/java/lightcontainer/storage/JsonRepository.java +++ b/app/src/main/java/lightcontainer/storage/JsonRepository.java @@ -4,12 +4,14 @@ public class JsonRepository implements Repository { @Override - public void save(String fileName, AppData data) { + public void save(FileWriter fileWriter) { } @Override - public AppData load(String fileName) { + public AppData load(FileReader fileReader) { return null; } + + } diff --git a/app/src/main/java/lightcontainer/storage/JsonWriter.java b/app/src/main/java/lightcontainer/storage/JsonWriter.java new file mode 100644 index 0000000..56fceb9 --- /dev/null +++ b/app/src/main/java/lightcontainer/storage/JsonWriter.java @@ -0,0 +1,9 @@ +package lightcontainer.storage; + +public class JsonWriter implements FileWriter { + + @Override + public void writeToFile(String fileName, AppData appData) { + + } +} diff --git a/app/src/main/java/lightcontainer/storage/Repository.java b/app/src/main/java/lightcontainer/storage/Repository.java index 79f19fd..b688fc7 100644 --- a/app/src/main/java/lightcontainer/storage/Repository.java +++ b/app/src/main/java/lightcontainer/storage/Repository.java @@ -2,7 +2,7 @@ package lightcontainer.storage; public interface Repository { - void save(String fileName, AppData data); + void save(FileWriter fileWriter); - AppData load(String fileName); + AppData load(FileReader fileReader); } From 57984456f100b203407c5b1cc24f2063450071e6 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Mon, 7 Mar 2022 08:46:48 +0100 Subject: [PATCH 08/17] =?UTF-8?q?Sauvegarde=20et=20lecture=20du=20fichier?= =?UTF-8?q?=20de=20synchronisation=20->=20AppData=20vers=20Json=20termin?= =?UTF-8?q?=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lightcontainer/storage/Adapter.java | 16 +++++ .../java/lightcontainer/storage/AppData.java | 8 ++- .../lightcontainer/storage/JsonAdapter.java | 70 +++++++++++++++++++ .../lightcontainer/storage/JsonReader.java | 9 --- .../storage/JsonRepository.java | 17 ----- .../lightcontainer/storage/JsonWriter.java | 9 --- .../lightcontainer/storage/Repository.java | 12 +++- .../java/lightcontainer/storage/User.java | 15 ++-- 8 files changed, 108 insertions(+), 48 deletions(-) create mode 100644 app/src/main/java/lightcontainer/storage/Adapter.java create mode 100644 app/src/main/java/lightcontainer/storage/JsonAdapter.java delete mode 100644 app/src/main/java/lightcontainer/storage/JsonReader.java delete mode 100644 app/src/main/java/lightcontainer/storage/JsonRepository.java delete mode 100644 app/src/main/java/lightcontainer/storage/JsonWriter.java diff --git a/app/src/main/java/lightcontainer/storage/Adapter.java b/app/src/main/java/lightcontainer/storage/Adapter.java new file mode 100644 index 0000000..7ffb90d --- /dev/null +++ b/app/src/main/java/lightcontainer/storage/Adapter.java @@ -0,0 +1,16 @@ +package lightcontainer.storage; + +public interface Adapter { + + /** + * @param appData The appData to stringify + * @return The string representation of AppData + */ + String toString(AppData appData); + + /** + * @param appDataString The appData string to parse + * @return AppData + */ + AppData fromString(String appDataString); +} diff --git a/app/src/main/java/lightcontainer/storage/AppData.java b/app/src/main/java/lightcontainer/storage/AppData.java index 88b245e..2a693b2 100644 --- a/app/src/main/java/lightcontainer/storage/AppData.java +++ b/app/src/main/java/lightcontainer/storage/AppData.java @@ -1,6 +1,7 @@ package lightcontainer.storage; import java.util.HashMap; +import java.util.Iterator; import java.util.Map; /** @@ -81,6 +82,10 @@ public class AppData { } } + public Iterator usersIterator() { + return users.values().iterator(); + } + /** * @param fileName The name of the file * @param user The user @@ -107,7 +112,8 @@ public class AppData { if (!this.users.containsKey(user.getName())) { return false; } else { - return this.users.get(user.getName()).addFile(file); + this.users.get(user.getName()).addFile(file); + return true; } } diff --git a/app/src/main/java/lightcontainer/storage/JsonAdapter.java b/app/src/main/java/lightcontainer/storage/JsonAdapter.java new file mode 100644 index 0000000..d055c12 --- /dev/null +++ b/app/src/main/java/lightcontainer/storage/JsonAdapter.java @@ -0,0 +1,70 @@ +package lightcontainer.storage; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; + +import java.util.Iterator; + +public class JsonAdapter implements Adapter { + + @Override + public String toString(AppData appData) { + return addData(appData); + } + + private String addData(AppData appData) { + AppConfig appConfig = appData.getAppConfig(); + JsonObject config = new JsonObject(); + config.addProperty("unicast_port", appConfig.getUnicastPort()); + config.addProperty("multicast_ip", appConfig.getMulticastIp()); + config.addProperty("multicast_port", appConfig.getMulticastPort()); + config.addProperty("network_interface", appConfig.getNetworkInterface()); + config.addProperty("tls", appConfig.isTls()); + JsonArray users = new JsonArray(); + Iterator userIterator = appData.usersIterator(); + addUsers(users, userIterator); + config.addProperty("users", users.toString()); + return config.toString(); + } + + private void addUsers(JsonArray users, Iterator userIterator) { + while (userIterator.hasNext()) { + User current = userIterator.next(); + JsonObject user = new JsonObject(); + user.addProperty("name", current.getName()); + user.addProperty("password", current.getPassword()); + user.addProperty("aes_key", current.getAesKey()); + JsonArray files = new JsonArray(); + Iterator fileIterator = current.fileIterator(); + addFiles(fileIterator); + user.addProperty("files", files.toString()); + users.add(user); + } + } + + private void addFiles(Iterator fileIterator) { + while (fileIterator.hasNext()) { + File currentFile = fileIterator.next(); + JsonObject file = new JsonObject(); + file.addProperty("name", currentFile.getName()); + file.addProperty("size", currentFile.getSize()); + file.addProperty("iv", currentFile.getIv()); + JsonArray storage = new JsonArray(); + Iterator storageIterator = currentFile.getStorageIterator(); + addStorage(storage, storageIterator); + file.addProperty("storage", storage.toString()); + } + } + + private void addStorage(JsonArray storage, Iterator storageIterator) { + while (storageIterator.hasNext()) { + String storageString = storageIterator.next(); + storage.add(storageString); + } + } + + @Override + public AppData fromString(String appDataString) { + return null; + } +} diff --git a/app/src/main/java/lightcontainer/storage/JsonReader.java b/app/src/main/java/lightcontainer/storage/JsonReader.java deleted file mode 100644 index 0b67603..0000000 --- a/app/src/main/java/lightcontainer/storage/JsonReader.java +++ /dev/null @@ -1,9 +0,0 @@ -package lightcontainer.storage; - -public class JsonReader implements FileReader { - - @Override - public AppData readFile(String fileName) { - return null; - } -} diff --git a/app/src/main/java/lightcontainer/storage/JsonRepository.java b/app/src/main/java/lightcontainer/storage/JsonRepository.java deleted file mode 100644 index c2c693d..0000000 --- a/app/src/main/java/lightcontainer/storage/JsonRepository.java +++ /dev/null @@ -1,17 +0,0 @@ -package lightcontainer.storage; - -public class JsonRepository implements Repository { - - - @Override - public void save(FileWriter fileWriter) { - - } - - @Override - public AppData load(FileReader fileReader) { - return null; - } - - -} diff --git a/app/src/main/java/lightcontainer/storage/JsonWriter.java b/app/src/main/java/lightcontainer/storage/JsonWriter.java deleted file mode 100644 index 56fceb9..0000000 --- a/app/src/main/java/lightcontainer/storage/JsonWriter.java +++ /dev/null @@ -1,9 +0,0 @@ -package lightcontainer.storage; - -public class JsonWriter implements FileWriter { - - @Override - public void writeToFile(String fileName, AppData appData) { - - } -} diff --git a/app/src/main/java/lightcontainer/storage/Repository.java b/app/src/main/java/lightcontainer/storage/Repository.java index b688fc7..6757ff5 100644 --- a/app/src/main/java/lightcontainer/storage/Repository.java +++ b/app/src/main/java/lightcontainer/storage/Repository.java @@ -1,8 +1,14 @@ package lightcontainer.storage; -public interface Repository { +public class Repository { - void save(FileWriter fileWriter); + //static final String CONFIG_FILE_PATH = "../../resources/config.json"; - AppData load(FileReader fileReader); + void save(String filePath, Adapter adapter) { + + } + + AppData load(String filePath, Adapter adapter) { + return null; + } } diff --git a/app/src/main/java/lightcontainer/storage/User.java b/app/src/main/java/lightcontainer/storage/User.java index 683fb07..9ba83f2 100644 --- a/app/src/main/java/lightcontainer/storage/User.java +++ b/app/src/main/java/lightcontainer/storage/User.java @@ -1,5 +1,7 @@ package lightcontainer.storage; +import java.util.HashMap; +import java.util.Iterator; import java.util.Map; import java.util.Set; @@ -36,8 +38,8 @@ public class User { return aesKey; } - public Set> getFilesIterator() { - return files.entrySet(); + public Iterator fileIterator() { + return files.values().iterator(); } public File getFile(String fileName) { @@ -48,13 +50,8 @@ public class User { * @param file The file to add. * @return False if a file with the same name already exists. Otherwise, adds the file and returns true. */ - public boolean addFile(File file) { - if (this.files.containsKey(file.getName())) { - return false; - } else { - this.files.put(file.getName(), file); - return true; - } + public void addFile(File file) { + this.files.put(file.getName(), file); } /** From d4cff6de094543f24ebe9d521f345d6c96d06966 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Tue, 8 Mar 2022 09:05:03 +0100 Subject: [PATCH 09/17] =?UTF-8?q?Sauvegarde=20et=20lecture=20du=20fichier?= =?UTF-8?q?=20de=20synchronisation=20->=20Json=20vers=20AppData=20termin?= =?UTF-8?q?=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lightcontainer/storage/JsonAdapter.java | 61 ++++++++++++++++++- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/lightcontainer/storage/JsonAdapter.java b/app/src/main/java/lightcontainer/storage/JsonAdapter.java index d055c12..55a4792 100644 --- a/app/src/main/java/lightcontainer/storage/JsonAdapter.java +++ b/app/src/main/java/lightcontainer/storage/JsonAdapter.java @@ -1,9 +1,8 @@ package lightcontainer.storage; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; +import com.google.gson.*; -import java.util.Iterator; +import java.util.*; public class JsonAdapter implements Adapter { @@ -65,6 +64,62 @@ public class JsonAdapter implements Adapter { @Override public AppData fromString(String appDataString) { + try { + JsonElement jsonString = JsonParser.parseString(appDataString); + JsonObject jsonAppData = jsonString.getAsJsonObject(); + AppConfig appConfig = AppConfig.getInstance(); + appConfig.setUnicastPort(jsonAppData.get("unicast_port").getAsInt()); + appConfig.setMulticastIp(jsonAppData.get("multicast_ip").getAsString()); + appConfig.setMulticastPort(jsonAppData.get("multicast_port").getAsInt()); + appConfig.setNetworkInterface(jsonAppData.get("network_interface").getAsString()); + appConfig.setTls(jsonAppData.get("tls").getAsBoolean()); + JsonArray jsonUsers = jsonAppData.getAsJsonArray("users"); + List users = new ArrayList<>(); + getUsers(jsonUsers, users); + AppData appData = AppData.getInstance(); + appData.setAppConfig(appConfig); + for (User user : users) { + appData.addUser(user); + } + return appData; + } catch (JsonParseException parseException) { + System.out.println("[FFE] : Error while loading configuration file"); //TODO - changer en log + } return null; } + + private void getUsers(JsonArray jsonUsers, List users) { + for (JsonElement element : jsonUsers) { + JsonObject jsonUser = element.getAsJsonObject(); + String name = jsonUser.get("username").getAsString(); + String password = jsonUser.get("password").getAsString(); + String aeskey = jsonUser.get("aes_key").getAsString(); + Map userFiles = new HashMap<>(); + JsonArray jsonFiles = jsonUser.getAsJsonArray("files"); + getFiles(userFiles, jsonFiles); + User user = new User(name, password, aeskey, userFiles); + users.add(user); + } + } + + private void getFiles(Map userFiles, JsonArray jsonFiles) { + for (JsonElement fileElement : jsonFiles) { + JsonObject jsonFile = fileElement.getAsJsonObject(); + String fileName = jsonFile.get("name").getAsString(); + int size = jsonFile.get("size").getAsInt(); + String iv = jsonFile.get("iv").getAsString(); + Set storage = new HashSet<>(); + JsonArray jsonStorage = jsonFile.getAsJsonArray("storage"); + getStorage(storage, jsonStorage); + File file = new File(fileName, size, iv, storage); + userFiles.put(file.getName(), file); + } + } + + private void getStorage(Set storage, JsonArray jsonStorage) { + for (JsonElement storageElement : jsonStorage) { + String storageName = storageElement.getAsString(); + storage.add(storageName); + } + } } From 26be3ca8ff4c23590fd74d87a7d55cd4ebefa7da Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Tue, 8 Mar 2022 09:32:26 +0100 Subject: [PATCH 10/17] =?UTF-8?q?Sauvegarde=20et=20lecture=20du=20fichier?= =?UTF-8?q?=20de=20synchronisation=20->=20M=C3=A9thodes=20save=20et=20load?= =?UTF-8?q?=20termin=C3=A9e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lightcontainer/storage/Adapter.java | 11 ++---- .../lightcontainer/storage/JsonAdapter.java | 11 ++++-- .../lightcontainer/storage/Repository.java | 34 +++++++++++++++++-- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/lightcontainer/storage/Adapter.java b/app/src/main/java/lightcontainer/storage/Adapter.java index 7ffb90d..30843ab 100644 --- a/app/src/main/java/lightcontainer/storage/Adapter.java +++ b/app/src/main/java/lightcontainer/storage/Adapter.java @@ -2,15 +2,8 @@ package lightcontainer.storage; public interface Adapter { - /** - * @param appData The appData to stringify - * @return The string representation of AppData - */ - String toString(AppData appData); - /** - * @param appDataString The appData string to parse - * @return AppData - */ + String toString(); + 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 55a4792..af6be59 100644 --- a/app/src/main/java/lightcontainer/storage/JsonAdapter.java +++ b/app/src/main/java/lightcontainer/storage/JsonAdapter.java @@ -6,8 +6,14 @@ import java.util.*; public class JsonAdapter implements Adapter { + private AppData appData; + + public JsonAdapter(AppData appData) { + this.appData = appData; + } + @Override - public String toString(AppData appData) { + public String toString() { return addData(appData); } @@ -81,7 +87,8 @@ public class JsonAdapter implements Adapter { for (User user : users) { appData.addUser(user); } - return appData; + this.appData = appData; + return this.appData; } catch (JsonParseException parseException) { System.out.println("[FFE] : Error while loading configuration file"); //TODO - changer en log } diff --git a/app/src/main/java/lightcontainer/storage/Repository.java b/app/src/main/java/lightcontainer/storage/Repository.java index 6757ff5..778b979 100644 --- a/app/src/main/java/lightcontainer/storage/Repository.java +++ b/app/src/main/java/lightcontainer/storage/Repository.java @@ -1,14 +1,44 @@ package lightcontainer.storage; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; + public class Repository { //static final String CONFIG_FILE_PATH = "../../resources/config.json"; 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)) { + bufferedWriter.write(jsonAppData); + bufferedWriter.flush(); + } catch (IOException e) { + System.out.println("Error while saving configuration file !"); + } + } } AppData load(String filePath, Adapter adapter) { - return null; + String jsonString = readFile(filePath); + return adapter.fromString(jsonString); + } + + private String readFile(String filePath) { + StringBuilder builder = new StringBuilder(); + try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath), StandardCharsets.UTF_8)) { + while (reader.ready()) { + builder.append(reader.readLine()); + } + } catch (IOException e) { + System.out.println("Error while reading configuration file"); + builder.setLength(0); + } + return builder.toString(); } } From 2b2bdd346740d4ec02ee9ec7d6cfb40631315e95 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Tue, 8 Mar 2022 09:56:17 +0100 Subject: [PATCH 11/17] Regex : modification de fileListResult --- app/src/main/resources/rules.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/resources/rules.txt b/app/src/main/resources/rules.txt index e5d69af..8be9487 100644 --- a/app/src/main/resources/rules.txt +++ b/app/src/main/resources/rules.txt @@ -33,7 +33,7 @@ client_signin = ^SIGNIN ([A-Za-z0-9]{2,20}) ([^ !]{5,50})\r\n$ client_signup = ^SIGNUP ([A-Za-z0-9]{2,20}) ([^ !]{5,50})\r\n$ ffe_signresult = ^(SIGN_OK|SIGN_ERROR)\r\n$ client_filelist = ^FILELIST\r\n$ -ffe_filelistresult = ^FILES(( [^ !]{1,20})!([0-9]{1,10})){0,50}$ +ffe_filelistresult = ^FILES( ([^ !]{1,20})!([0-9]{1,10})){0,50}$ client_savefile = ^SAVE_FILE ([^ !]{1,20}) ([0-9]{1,10})\r\n$ ffe_savefileresult = ^(SAVEFILE_OK|SAVEFILE_ERROR)\r\n$ client_getfile = ^GETFILE ([^ !]{1,20})\r\n$ From 558e4427fdb5a6c78a5953eb8aff6bf79fa28041 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Tue, 8 Mar 2022 11:00:36 +0100 Subject: [PATCH 12/17] =?UTF-8?q?Json=20adapter=20:=20AppData=20vers=20Jso?= =?UTF-8?q?n=20->=20test=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lightcontainer/storage/JsonAdapter.java | 11 ++-- .../protocol/rules/reader/HelloRuleTest.java | 1 - .../storage/JsonAdapterTest.java | 51 +++++++++++++++++++ 3 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 app/src/test/java/lightcontainer/storage/JsonAdapterTest.java diff --git a/app/src/main/java/lightcontainer/storage/JsonAdapter.java b/app/src/main/java/lightcontainer/storage/JsonAdapter.java index af6be59..649bd35 100644 --- a/app/src/main/java/lightcontainer/storage/JsonAdapter.java +++ b/app/src/main/java/lightcontainer/storage/JsonAdapter.java @@ -28,7 +28,7 @@ public class JsonAdapter implements Adapter { JsonArray users = new JsonArray(); Iterator userIterator = appData.usersIterator(); addUsers(users, userIterator); - config.addProperty("users", users.toString()); + config.add("users", users); return config.toString(); } @@ -41,13 +41,13 @@ public class JsonAdapter implements Adapter { user.addProperty("aes_key", current.getAesKey()); JsonArray files = new JsonArray(); Iterator fileIterator = current.fileIterator(); - addFiles(fileIterator); - user.addProperty("files", files.toString()); + addFiles(fileIterator, files); + user.add("files", files); users.add(user); } } - private void addFiles(Iterator fileIterator) { + private void addFiles(Iterator fileIterator, JsonArray files) { while (fileIterator.hasNext()) { File currentFile = fileIterator.next(); JsonObject file = new JsonObject(); @@ -57,7 +57,8 @@ public class JsonAdapter implements Adapter { JsonArray storage = new JsonArray(); Iterator storageIterator = currentFile.getStorageIterator(); addStorage(storage, storageIterator); - file.addProperty("storage", storage.toString()); + file.add("storage", storage); + files.add(file); } } diff --git a/app/src/test/java/lightcontainer/protocol/rules/reader/HelloRuleTest.java b/app/src/test/java/lightcontainer/protocol/rules/reader/HelloRuleTest.java index 7b82f38..a356fb5 100644 --- a/app/src/test/java/lightcontainer/protocol/rules/reader/HelloRuleTest.java +++ b/app/src/test/java/lightcontainer/protocol/rules/reader/HelloRuleTest.java @@ -1,7 +1,6 @@ package lightcontainer.protocol.rules.reader; import lightcontainer.protocol.ProtocolReader; -import lightcontainer.protocol.rules.reader.HelloRule; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; diff --git a/app/src/test/java/lightcontainer/storage/JsonAdapterTest.java b/app/src/test/java/lightcontainer/storage/JsonAdapterTest.java new file mode 100644 index 0000000..34348c0 --- /dev/null +++ b/app/src/test/java/lightcontainer/storage/JsonAdapterTest.java @@ -0,0 +1,51 @@ +package lightcontainer.storage; + +import org.junit.jupiter.api.Test; + +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 JsonAdapterTest { + + @Test + public void convertAppDataToJson() { + //GIVEN an AppData instance and a Json Adapter + 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 the adapter converts AppData to Json + String jsonAppData = jsonAdapter.toString(); + System.out.println(jsonAppData); + //THEN + assertTrue(jsonAppData.contains("32000")); + assertTrue(jsonAppData.contains("224.25.0.1")); + assertTrue(jsonAppData.contains("15502")); + assertTrue(jsonAppData.contains("My network interface")); + assertTrue(jsonAppData.contains("false")); + assertTrue(jsonAppData.contains("User1")); + assertTrue(jsonAppData.contains("Password")); + assertTrue(jsonAppData.contains("djdjjdj")); + assertTrue(jsonAppData.contains("File1")); + assertTrue(jsonAppData.contains("15")); + assertTrue(jsonAppData.contains("8d8d8d8d")); + assertTrue(jsonAppData.contains("StorBackEnd1")); + } +} From ac04f86e72c02e4a1b5acba36f4d561d8893dffd Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Tue, 8 Mar 2022 11:17:26 +0100 Subject: [PATCH 13/17] =?UTF-8?q?Json=20adapter=20:=20Json=20vers=20AppDat?= =?UTF-8?q?a=20->=20test=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lightcontainer/storage/JsonAdapter.java | 2 +- .../lightcontainer/storage/JsonAdapterTest.java | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/lightcontainer/storage/JsonAdapter.java b/app/src/main/java/lightcontainer/storage/JsonAdapter.java index 649bd35..076b079 100644 --- a/app/src/main/java/lightcontainer/storage/JsonAdapter.java +++ b/app/src/main/java/lightcontainer/storage/JsonAdapter.java @@ -99,7 +99,7 @@ public class JsonAdapter implements Adapter { private void getUsers(JsonArray jsonUsers, List users) { for (JsonElement element : jsonUsers) { JsonObject jsonUser = element.getAsJsonObject(); - String name = jsonUser.get("username").getAsString(); + String name = jsonUser.get("name").getAsString(); String password = jsonUser.get("password").getAsString(); String aeskey = jsonUser.get("aes_key").getAsString(); Map userFiles = new HashMap<>(); diff --git a/app/src/test/java/lightcontainer/storage/JsonAdapterTest.java b/app/src/test/java/lightcontainer/storage/JsonAdapterTest.java index 34348c0..2bde872 100644 --- a/app/src/test/java/lightcontainer/storage/JsonAdapterTest.java +++ b/app/src/test/java/lightcontainer/storage/JsonAdapterTest.java @@ -33,7 +33,6 @@ public class JsonAdapterTest { JsonAdapter jsonAdapter = new JsonAdapter(appData); //WHEN the adapter converts AppData to Json String jsonAppData = jsonAdapter.toString(); - System.out.println(jsonAppData); //THEN assertTrue(jsonAppData.contains("32000")); assertTrue(jsonAppData.contains("224.25.0.1")); @@ -48,4 +47,20 @@ public class JsonAdapterTest { assertTrue(jsonAppData.contains("8d8d8d8d")); assertTrue(jsonAppData.contains("StorBackEnd1")); } + + @Test + public void convertJsonToAppData() { + //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); + AppData appData = jsonAdapter.fromString(json); + //THEN + assertNotNull(appData.getAppConfig()); + assertEquals("My network interface", appData.getAppConfig().getNetworkInterface()); + assertEquals(32000, appData.getAppConfig().getUnicastPort()); + assertEquals("224.25.0.1", appData.getAppConfig().getMulticastIp()); + assertEquals(15502, appData.getAppConfig().getMulticastPort()); + assertFalse(appData.getAppConfig().isTls()); + } } From d979fbd1f3fe4550e771b4e8c4d6a6df8bececef Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Tue, 8 Mar 2022 11:45:18 +0100 Subject: [PATCH 14/17] =?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())); + } +} From 429161877466ba608427c4e5fa2be3ac2ce85239 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Tue, 8 Mar 2022 12:12:04 +0100 Subject: [PATCH 15/17] =?UTF-8?q?Repository=20:=20load=20test=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lightcontainer/storage/JsonAdapter.java | 2 +- .../lightcontainer/storage/Repository.java | 6 ++--- .../storage/RepositoryTests.java | 17 ++++++++++++- app/src/test/resources/loadTest.json | 24 +++++++++++++++++++ 4 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 app/src/test/resources/loadTest.json diff --git a/app/src/main/java/lightcontainer/storage/JsonAdapter.java b/app/src/main/java/lightcontainer/storage/JsonAdapter.java index 076b079..831b0f1 100644 --- a/app/src/main/java/lightcontainer/storage/JsonAdapter.java +++ b/app/src/main/java/lightcontainer/storage/JsonAdapter.java @@ -92,8 +92,8 @@ public class JsonAdapter implements Adapter { return this.appData; } catch (JsonParseException parseException) { System.out.println("[FFE] : Error while loading configuration file"); //TODO - changer en log + return null; } - return null; } private void getUsers(JsonArray jsonUsers, List users) { diff --git a/app/src/main/java/lightcontainer/storage/Repository.java b/app/src/main/java/lightcontainer/storage/Repository.java index d6f12bc..3ea0ac6 100644 --- a/app/src/main/java/lightcontainer/storage/Repository.java +++ b/app/src/main/java/lightcontainer/storage/Repository.java @@ -10,8 +10,6 @@ import java.nio.file.StandardOpenOption; public class Repository { - //static final String CONFIG_FILE_PATH = "../../resources/config.json"; - static void save(String filePath, Adapter adapter) { if (filePath != null) { String jsonAppData = adapter.toString(); @@ -24,12 +22,12 @@ public class Repository { } } - AppData load(String filePath, Adapter adapter) { + static AppData load(String filePath, Adapter adapter) { String jsonString = readFile(filePath); return adapter.fromString(jsonString); } - static private String readFile(String filePath) { + private static 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 0fa557a..ed90a68 100644 --- a/app/src/test/java/lightcontainer/storage/RepositoryTests.java +++ b/app/src/test/java/lightcontainer/storage/RepositoryTests.java @@ -30,7 +30,7 @@ public class RepositoryTests { @Test public void save() { - //GIVEN an AppData instance, a JsonAdapter and a Repository + //GIVEN an AppData instance and a Json Adapter AppData appData = AppData.getInstance(); AppConfig appConfig = AppConfig.getInstance(); appConfig.setUnicastPort(32000); @@ -53,4 +53,19 @@ public class RepositoryTests { //THEN assertTrue(Files.exists(Paths.get("src/test/resources/test.json").toAbsolutePath())); } + + @Test + public void load() { + //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); + //THEN + assertNotNull(appData.getAppConfig()); + assertEquals("My network interface", appData.getAppConfig().getNetworkInterface()); + assertEquals(32000, appData.getAppConfig().getUnicastPort()); + assertEquals("224.25.0.1", appData.getAppConfig().getMulticastIp()); + assertEquals(15502, appData.getAppConfig().getMulticastPort()); + assertFalse(appData.getAppConfig().isTls()); + } } diff --git a/app/src/test/resources/loadTest.json b/app/src/test/resources/loadTest.json new file mode 100644 index 0000000..f06f3af --- /dev/null +++ b/app/src/test/resources/loadTest.json @@ -0,0 +1,24 @@ +{ + "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" + ] + } + ] + } + ] +} \ No newline at end of file From 4fbe6bb88c380662eda58918a40c3c8c887b6353 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Tue, 8 Mar 2022 14:34:09 +0100 Subject: [PATCH 16/17] Storage : suppresion de 2 interfaces inutiles --- app/src/main/java/lightcontainer/storage/FileReader.java | 6 ------ app/src/main/java/lightcontainer/storage/FileWriter.java | 6 ------ 2 files changed, 12 deletions(-) delete mode 100644 app/src/main/java/lightcontainer/storage/FileReader.java delete mode 100644 app/src/main/java/lightcontainer/storage/FileWriter.java diff --git a/app/src/main/java/lightcontainer/storage/FileReader.java b/app/src/main/java/lightcontainer/storage/FileReader.java deleted file mode 100644 index 4e79a0b..0000000 --- a/app/src/main/java/lightcontainer/storage/FileReader.java +++ /dev/null @@ -1,6 +0,0 @@ -package lightcontainer.storage; - -public interface FileReader { - - AppData readFile(String fileName); -} diff --git a/app/src/main/java/lightcontainer/storage/FileWriter.java b/app/src/main/java/lightcontainer/storage/FileWriter.java deleted file mode 100644 index 3cef3aa..0000000 --- a/app/src/main/java/lightcontainer/storage/FileWriter.java +++ /dev/null @@ -1,6 +0,0 @@ -package lightcontainer.storage; - -public interface FileWriter { - - void writeToFile(String fileName, AppData appData); -} From f9c4ee71b202c1e309c3fad75de05228f2a484f3 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Tue, 8 Mar 2022 14:43:40 +0100 Subject: [PATCH 17/17] Storage : optimisation des imports et ajout de commentaires --- .../main/java/lightcontainer/storage/Adapter.java | 3 +-- app/src/main/java/lightcontainer/storage/File.java | 3 +++ .../java/lightcontainer/storage/JsonAdapter.java | 12 ++++++++++++ .../main/java/lightcontainer/storage/Repository.java | 9 +++++++++ app/src/main/java/lightcontainer/storage/User.java | 2 -- 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/lightcontainer/storage/Adapter.java b/app/src/main/java/lightcontainer/storage/Adapter.java index 30843ab..29d5dc3 100644 --- a/app/src/main/java/lightcontainer/storage/Adapter.java +++ b/app/src/main/java/lightcontainer/storage/Adapter.java @@ -1,8 +1,7 @@ package lightcontainer.storage; public interface Adapter { - - + String toString(); AppData fromString(String appDataString); diff --git a/app/src/main/java/lightcontainer/storage/File.java b/app/src/main/java/lightcontainer/storage/File.java index dcee5cc..e7d5d5f 100644 --- a/app/src/main/java/lightcontainer/storage/File.java +++ b/app/src/main/java/lightcontainer/storage/File.java @@ -3,6 +3,9 @@ package lightcontainer.storage; import java.util.Iterator; import java.util.Set; +/** + * File represents all information related to a file + */ public class File { private final String name; diff --git a/app/src/main/java/lightcontainer/storage/JsonAdapter.java b/app/src/main/java/lightcontainer/storage/JsonAdapter.java index 831b0f1..aaecc55 100644 --- a/app/src/main/java/lightcontainer/storage/JsonAdapter.java +++ b/app/src/main/java/lightcontainer/storage/JsonAdapter.java @@ -4,6 +4,9 @@ import com.google.gson.*; import java.util.*; +/** + * Specific implementation of Adapter that converts AppData to Json and vice-versa + */ public class JsonAdapter implements Adapter { private AppData appData; @@ -12,6 +15,10 @@ public class JsonAdapter implements Adapter { this.appData = appData; } + /** + * + * @return A Json String containing AppData properties + */ @Override public String toString() { return addData(appData); @@ -69,6 +76,11 @@ public class JsonAdapter implements Adapter { } } + /** + * + * @param appDataString The Json String to convert + * @return An AppData instance + */ @Override public AppData fromString(String appDataString) { try { diff --git a/app/src/main/java/lightcontainer/storage/Repository.java b/app/src/main/java/lightcontainer/storage/Repository.java index 3ea0ac6..1f085a6 100644 --- a/app/src/main/java/lightcontainer/storage/Repository.java +++ b/app/src/main/java/lightcontainer/storage/Repository.java @@ -10,6 +10,10 @@ import java.nio.file.StandardOpenOption; public class Repository { + /** + * @param filePath The path where the file must be saved + * @param adapter The service that converts Objects to Strings + */ static void save(String filePath, Adapter adapter) { if (filePath != null) { String jsonAppData = adapter.toString(); @@ -22,6 +26,11 @@ public class Repository { } } + /** + * @param filePath The path where the file is stored + * @param adapter The service that converts Strings to objects + * @return + */ static AppData load(String filePath, Adapter adapter) { String jsonString = readFile(filePath); return adapter.fromString(jsonString); diff --git a/app/src/main/java/lightcontainer/storage/User.java b/app/src/main/java/lightcontainer/storage/User.java index 9ba83f2..47a65f9 100644 --- a/app/src/main/java/lightcontainer/storage/User.java +++ b/app/src/main/java/lightcontainer/storage/User.java @@ -1,9 +1,7 @@ package lightcontainer.storage; -import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import java.util.Set; /** * User represents a user of the system.