From 558e4427fdb5a6c78a5953eb8aff6bf79fa28041 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Tue, 8 Mar 2022 11:00:36 +0100 Subject: [PATCH] =?UTF-8?q?Json=20adapter=20:=20AppData=20vers=20Json=20->?= =?UTF-8?q?=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")); + } +}