From 8f4465048401f3699b067b970c317ec9ac8a1810 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Fri, 9 Feb 2024 11:33:57 +0100 Subject: [PATCH] test_Reading and writing to a JSON file --- src/test/java/CreateUserTest.java | 64 ++++++++++++++++++++++++++++++ src/test/java/CreateUsertTest.java | 29 -------------- 2 files changed, 64 insertions(+), 29 deletions(-) create mode 100644 src/test/java/CreateUserTest.java delete mode 100644 src/test/java/CreateUsertTest.java diff --git a/src/test/java/CreateUserTest.java b/src/test/java/CreateUserTest.java new file mode 100644 index 0000000..49c202f --- /dev/null +++ b/src/test/java/CreateUserTest.java @@ -0,0 +1,64 @@ +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; +import java.io.File; +import java.util.List; + +class CreateUserTest { + + @Test + public void testCreateUser() { + String id = "1"; + String userName = "testUser"; + String password = "password123"; + String birthday = "2000-01-01"; + String firstName = "John"; + String surname = "Doe"; + + CreateUser user = new CreateUser(id, userName, password, birthday, firstName, surname); + + assertEquals(id, user.getId()); + assertEquals(userName, user.getUserName()); + assertEquals(birthday, user.getBirthday()); + assertEquals(firstName, user.getFirstName()); + assertEquals(surname, user.surname()); + + // Password should be hashed + assertNotEquals(password, user.getPassword()); + } + + + @Test + public void testSaveAndReadFromJsonFile() { + String filename = "test_users.json"; + String id = "2"; + String userName = "testUser2"; + String password = "password456"; + String birthday = "2001-01-01"; + String firstName = "Jane"; + String surname = "Doe"; + + // Create a user + CreateUser user = new CreateUser(id, userName, password, birthday, firstName, surname); + + // Save user to JSON file + user.saveToJsonFile(filename); + + // Read users from JSON file + List userList = CreateUser.readUserListFromJsonFile(filename); + + // Ensure that the user has been saved and can be read correctly + assertEquals(1, userList.size()); + CreateUser savedUser = userList.get(0); + assertEquals(id, savedUser.getId()); + assertEquals(userName, savedUser.getUserName()); + assertEquals(birthday, savedUser.getBirthday()); + assertEquals(firstName, savedUser.getFirstName()); + assertEquals(surname, savedUser.surname()); + + // Clean up - delete the test file after the test + File file = new File(filename); + file.delete(); + } + + +} diff --git a/src/test/java/CreateUsertTest.java b/src/test/java/CreateUsertTest.java deleted file mode 100644 index 5702e2a..0000000 --- a/src/test/java/CreateUsertTest.java +++ /dev/null @@ -1,29 +0,0 @@ -import static org.junit.jupiter.api.Assertions.*; - -import org.junit.jupiter.api.Test; - -class CreateUsertTest { - - @Test - public void testCreateUser() { - String id = "1"; - String userName = "testUser"; - String password = "password123"; - String birthday = "2000-01-01"; - String firstName = "John"; - String surname = "Doe"; - - CreateUser user = new CreateUser(id, userName, password, birthday, firstName, surname); - - assertEquals(id, user.getId()); - assertEquals(userName, user.getUserName()); - assertEquals(birthday, user.getBirthday()); - assertEquals(firstName, user.getFirstName()); - assertEquals(surname, user.surname()); - - // Password should be hashed - assertNotEquals(password, user.getPassword()); - } - - -}