Modification de repository

This commit is contained in:
Maximilien LEDOUX 2022-03-11 16:54:28 +01:00
parent 8f09dd29b6
commit dd9eb7bd2d
5 changed files with 16 additions and 25 deletions

View File

@ -1,8 +1,8 @@
package lightcontainer.storage; package lightcontainer.storage;
public interface Adapter { public interface Adapter {
String toString(); String toString(AppData appData);
AppData fromString(String appDataString); AppData fromString(String appDataString);
} }

View File

@ -9,18 +9,11 @@ import java.util.*;
*/ */
public class JsonAdapter implements Adapter { public class JsonAdapter implements Adapter {
private AppData appData;
public JsonAdapter(AppData appData) {
this.appData = appData;
}
/** /**
*
* @return A Json String containing AppData properties * @return A Json String containing AppData properties
*/ */
@Override @Override
public String toString() { public String toString(AppData appData) {
return addData(appData); return addData(appData);
} }
@ -77,7 +70,6 @@ public class JsonAdapter implements Adapter {
} }
/** /**
*
* @param appDataString The Json String to convert * @param appDataString The Json String to convert
* @return An AppData instance * @return An AppData instance
*/ */
@ -100,8 +92,7 @@ public class JsonAdapter implements Adapter {
for (User user : users) { for (User user : users) {
appData.addUser(user.getName(), user.getPassword(), user.getAesKey()); appData.addUser(user.getName(), user.getPassword(), user.getAesKey());
} }
this.appData = appData; return appData;
return this.appData;
} catch (JsonParseException parseException) { } catch (JsonParseException parseException) {
System.out.println("[FFE] : Error while loading configuration file"); //TODO - changer en log System.out.println("[FFE] : Error while loading configuration file"); //TODO - changer en log
return null; return null;

View File

@ -12,13 +12,15 @@ public class Repository {
private final String filePath; private final String filePath;
private final Adapter adapter; private final Adapter adapter;
private final AppData appData;
/** /**
* @param filePath The path to the configuration file * @param filePath The path to the configuration file
* @param adapter The adapter that converts objects to string and vice-versa * @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.filePath = filePath;
this.appData = appData;
this.adapter = adapter; this.adapter = adapter;
} }

View File

@ -22,17 +22,15 @@ public class JsonAdapterTests {
appConfig.setMulticastPort(15502); appConfig.setMulticastPort(15502);
appConfig.setNetworkInterface("My network interface"); appConfig.setNetworkInterface("My network interface");
appConfig.setTls(false); appConfig.setTls(false);
Map<String, File> files = new HashMap<>();
Set<String> storage = new HashSet<>(); Set<String> storage = new HashSet<>();
storage.add("StorBackEnd1"); storage.add("StorBackEnd1");
File file1 = new File("File1", 15, "8d8d8d8d", storage); 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.setAppConfig(appConfig);
//appData.addUser(user1); appData.addUser("User1", "Password","djdjjdj");
JsonAdapter jsonAdapter = new JsonAdapter(appData); appData.addFileFor(file1, "User1");
JsonAdapter jsonAdapter = new JsonAdapter();
//WHEN the adapter converts AppData to Json //WHEN the adapter converts AppData to Json
String jsonAppData = jsonAdapter.toString(); String jsonAppData = jsonAdapter.toString(appData);
//THEN //THEN
assertTrue(jsonAppData.contains("32000")); assertTrue(jsonAppData.contains("32000"));
assertTrue(jsonAppData.contains("224.25.0.1")); assertTrue(jsonAppData.contains("224.25.0.1"));
@ -53,7 +51,7 @@ public class JsonAdapterTests {
//GIVEN a Json string //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\"]}]}]}"; 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 //WHEN the adapter converts Json to Appdata
JsonAdapter jsonAdapter = new JsonAdapter(null); JsonAdapter jsonAdapter = new JsonAdapter();
AppData appData = jsonAdapter.fromString(json); AppData appData = jsonAdapter.fromString(json);
//THEN //THEN
assertNotNull(appData.getAppConfig()); assertNotNull(appData.getAppConfig());

View File

@ -40,11 +40,11 @@ public class RepositoryTests {
File file1 = new File("File1", 15, "8d8d8d8d", storage); File file1 = new File("File1", 15, "8d8d8d8d", storage);
appData.setAppConfig(appConfig); appData.setAppConfig(appConfig);
appData.addUser("User1", "Password", "djdjjdj"); appData.addUser("User1", "Password", "djdjjdj");
JsonAdapter jsonAdapter = new JsonAdapter(appData); JsonAdapter jsonAdapter = new JsonAdapter();
appData.addFileFor(file1, "User1"); appData.addFileFor(file1, "User1");
//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 repository = new Repository(filePath, jsonAdapter); Repository repository = new Repository(filePath, appData, jsonAdapter);
repository.save(); 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()));
@ -53,9 +53,9 @@ public class RepositoryTests {
@Test @Test
public void load() { public void load() {
//GIVEN a test json file loadTest.json //GIVEN a test json file loadTest.json
JsonAdapter jsonAdapter = new JsonAdapter(null); JsonAdapter jsonAdapter = new JsonAdapter();
//WHEN repository calls load method //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(); AppData appData = repository.load();
//THEN //THEN
assertNotNull(appData.getAppConfig()); assertNotNull(appData.getAppConfig());