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