Json adapter : AppData vers Json -> testé

This commit is contained in:
Maximilien LEDOUX 2022-03-08 11:00:36 +01:00
parent 2b2bdd3467
commit 558e4427fd
3 changed files with 57 additions and 6 deletions

View File

@ -28,7 +28,7 @@ public class JsonAdapter implements Adapter {
JsonArray users = new JsonArray();
Iterator<User> 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<File> fileIterator = current.fileIterator();
addFiles(fileIterator);
user.addProperty("files", files.toString());
addFiles(fileIterator, files);
user.add("files", files);
users.add(user);
}
}
private void addFiles(Iterator<File> fileIterator) {
private void addFiles(Iterator<File> 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<String> storageIterator = currentFile.getStorageIterator();
addStorage(storage, storageIterator);
file.addProperty("storage", storage.toString());
file.add("storage", storage);
files.add(file);
}
}

View File

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

View File

@ -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<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);
//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"));
}
}