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

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

View File

@ -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;

View File

@ -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;
}

View File

@ -22,17 +22,15 @@ public class JsonAdapterTests {
appConfig.setMulticastPort(15502);
appConfig.setNetworkInterface("My network interface");
appConfig.setTls(false);
Map<String, File> files = new HashMap<>();
Set<String> 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());

View File

@ -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());