From 945f4c33d9ddc7be8d2f8fe6b32cecb65e9928b7 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Fri, 9 Feb 2024 11:17:48 +0100 Subject: [PATCH 01/10] test_User creation in CreateUser, fixed error in CreateUser (public String surname() and public void setSurname(String surname)) --- src/main/java/CreateUser.java | 6 +++--- src/test/java/CreateUsertTest.java | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 src/test/java/CreateUsertTest.java diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index c078749..ff436fa 100644 --- a/src/main/java/CreateUser.java +++ b/src/main/java/CreateUser.java @@ -77,11 +77,11 @@ class CreateUser { } public String surname() { - return firstName; + return surname; } - public void surname(String firstName) { - this.firstName = firstName; + public void setSurname(String surname) { + this.surname = surname; } public boolean isStayLoggedIn() { diff --git a/src/test/java/CreateUsertTest.java b/src/test/java/CreateUsertTest.java new file mode 100644 index 0000000..5702e2a --- /dev/null +++ b/src/test/java/CreateUsertTest.java @@ -0,0 +1,29 @@ +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()); + } + + +} From 8f4465048401f3699b067b970c317ec9ac8a1810 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Fri, 9 Feb 2024 11:33:57 +0100 Subject: [PATCH 02/10] 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()); - } - - -} From f4f5a66b3fe16931470ea04eb3e5ac202d11a9de Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Fri, 9 Feb 2024 12:06:30 +0100 Subject: [PATCH 03/10] test_StayLoggedIn button --- src/test/java/CreateUserTest.java | 34 ++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/test/java/CreateUserTest.java b/src/test/java/CreateUserTest.java index 49c202f..f3e4c19 100644 --- a/src/test/java/CreateUserTest.java +++ b/src/test/java/CreateUserTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import java.io.File; import java.util.List; +import static org.junit.jupiter.api.Assertions.*; class CreateUserTest { @@ -58,7 +59,38 @@ class CreateUserTest { // Clean up - delete the test file after the test File file = new File(filename); file.delete(); + } + + @Test + public void testUpdateStayLoggedIn() { + String filename = "test_users.json"; + String id = "3"; + String userName = "testUser3"; + String password = "password789"; + String birthday = "2002-01-01"; + String firstName = "Jack"; + String surname = "Doe"; + + // Create a user + CreateUser user = new CreateUser(id, userName, password, birthday, firstName, surname); + + // Save user to JSON file + user.saveToJsonFile(filename); + + // Update stayLoggedIn flag + CreateUser.updateStayLoggedIn(filename, userName, true); + + // Read updated user from JSON file + List userList = CreateUser.readUserListFromJsonFile(filename); + + // Ensure that the stayLoggedIn flag is updated + assertTrue(userList.get(0).isStayLoggedIn()); + + // Clean up - delete the test file after the test + File file = new File(filename); + file.delete(); + } } -} + From a3bbb2dd809e7244af07a1e2001cee741bc2f805 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Fri, 9 Feb 2024 12:25:10 +0100 Subject: [PATCH 04/10] test_Login button in LoginGUI --- src/main/java/LoginGUI.java | 21 +++++++++++++++++++++ src/test/java/LoginGUITest.java | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 src/test/java/LoginGUITest.java diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 6a442b7..6d49398 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -14,7 +14,28 @@ public class LoginGUI extends JFrame implements ActionListener { private JButton loginButton; private JCheckBox stayLoggedInCheckbox; private JButton signUpButton; + + public JTextField getUsernameField() { + return usernameField; + } + + public JPasswordField getPasswordField() { + return passwordField; + } + + public JButton getLoginButton() { + return loginButton; + } + public JCheckBox getStayLoggedInCheckbox() { + return stayLoggedInCheckbox; + } + + public JButton getSignUpButton() { + return signUpButton; + } + + public LoginGUI() { setTitle("Login"); setSize(300, 220); diff --git a/src/test/java/LoginGUITest.java b/src/test/java/LoginGUITest.java new file mode 100644 index 0000000..ca931d2 --- /dev/null +++ b/src/test/java/LoginGUITest.java @@ -0,0 +1,21 @@ +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; +import java.awt.event.ActionEvent; + +class LoginGUITest { + + @Test + public void testActionPerformed_LoginButton() { + LoginGUI loginGUI = new LoginGUI(); + loginGUI.getUsernameField().setText("testUser"); + loginGUI.getPasswordField().setText("testPassword"); + loginGUI.getStayLoggedInCheckbox().setSelected(true); + + // Simulate action performed event on login button + loginGUI.actionPerformed(new ActionEvent(loginGUI.getLoginButton(), ActionEvent.ACTION_PERFORMED, null)); + + // Verify if login was successful + assertFalse(loginGUI.isVisible()); + } + +} From 0e2453a1ce083657352fd743ebc698c594f17544 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Fri, 9 Feb 2024 14:50:22 +0100 Subject: [PATCH 05/10] test_Check initial state of the login GUI and added setUp() method to initiate GUI before tests are run --- src/test/java/LoginGUITest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/test/java/LoginGUITest.java b/src/test/java/LoginGUITest.java index ca931d2..c3e76af 100644 --- a/src/test/java/LoginGUITest.java +++ b/src/test/java/LoginGUITest.java @@ -1,8 +1,25 @@ import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import java.awt.event.ActionEvent; +import org.junit.jupiter.api.BeforeEach; class LoginGUITest { + + private LoginGUI loginGUI; + + @BeforeEach + public void setUp() { + loginGUI = new LoginGUI(); + } + + @Test + public void testInitialState() { + assertNotNull(loginGUI.getUsernameField()); + assertNotNull(loginGUI.getPasswordField()); + assertNotNull(loginGUI.getLoginButton()); + assertNotNull(loginGUI.getStayLoggedInCheckbox()); + assertNotNull(loginGUI.getSignUpButton()); + } @Test public void testActionPerformed_LoginButton() { @@ -17,5 +34,7 @@ class LoginGUITest { // Verify if login was successful assertFalse(loginGUI.isVisible()); } + + } From bdca5a288fc13d74730a5374bd6b92c4b1e0734f Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Fri, 9 Feb 2024 15:26:53 +0100 Subject: [PATCH 06/10] test_StayLoggedIn checkbox in LoginGUI --- src/test/java/LoginGUITest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/java/LoginGUITest.java b/src/test/java/LoginGUITest.java index c3e76af..2fceb23 100644 --- a/src/test/java/LoginGUITest.java +++ b/src/test/java/LoginGUITest.java @@ -35,6 +35,16 @@ class LoginGUITest { assertFalse(loginGUI.isVisible()); } + @Test + public void testStayLoggedInCheckbox() { + // Initially, checkbox should be unchecked + assertFalse(loginGUI.getStayLoggedInCheckbox().isSelected()); + + // Simulate checking the checkbox + loginGUI.getStayLoggedInCheckbox().doClick(); + + assertTrue(loginGUI.getStayLoggedInCheckbox().isSelected()); + } } From 8105b8fffd29f4c42d4b5156e30fd4adf170856b Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Fri, 9 Feb 2024 16:38:23 +0100 Subject: [PATCH 07/10] test_SignUpGUI button actions --- src/main/java/SignUpGUI.java | 26 ++++++++++++++++++++++++ src/test/java/LoginGUITest.java | 12 ++++++----- src/test/java/SignUpGUITest.java | 35 ++++++++++++++++++++++++++++++++ user.json | 9 ++++++++ 4 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 src/test/java/SignUpGUITest.java diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java index 2f31890..51a3e47 100644 --- a/src/main/java/SignUpGUI.java +++ b/src/main/java/SignUpGUI.java @@ -7,6 +7,32 @@ import java.util.UUID; public class SignUpGUI extends JFrame implements ActionListener { private JTextField usernameField, passwordField, confirmPasswordField, birthdayField, firstNameField, surnameField; private JButton signUpButton; + + public JTextField getUsernameField() { + return usernameField; + } + + public JTextField getPasswordField() { + return passwordField; + } + + + public JTextField getConfirmPasswordField() { + return confirmPasswordField; + } + + public JTextField getBirthdayField() { + return birthdayField; + } + public JTextField getFirstNameField() { + return firstNameField; + } + public JTextField getSurnameField() { + return surnameField; + } + public JButton getSignUpButton() { + return signUpButton; + } public SignUpGUI() { setTitle("Sign Up"); diff --git a/src/test/java/LoginGUITest.java b/src/test/java/LoginGUITest.java index 2fceb23..b2b6184 100644 --- a/src/test/java/LoginGUITest.java +++ b/src/test/java/LoginGUITest.java @@ -37,14 +37,16 @@ class LoginGUITest { @Test public void testStayLoggedInCheckbox() { - // Initially, checkbox should be unchecked - assertFalse(loginGUI.getStayLoggedInCheckbox().isSelected()); + // Initially, checkbox should be unchecked + assertFalse(loginGUI.getStayLoggedInCheckbox().isSelected()); - // Simulate checking the checkbox - loginGUI.getStayLoggedInCheckbox().doClick(); + // Simulate checking the checkbox + loginGUI.getStayLoggedInCheckbox().doClick(); - assertTrue(loginGUI.getStayLoggedInCheckbox().isSelected()); + assertTrue(loginGUI.getStayLoggedInCheckbox().isSelected()); } + + } diff --git a/src/test/java/SignUpGUITest.java b/src/test/java/SignUpGUITest.java new file mode 100644 index 0000000..083e042 --- /dev/null +++ b/src/test/java/SignUpGUITest.java @@ -0,0 +1,35 @@ +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; + + + class SignUpGUITest { + private SignUpGUI signUpGUI; + + @BeforeEach + void setUp() { + signUpGUI = new SignUpGUI(); + signUpGUI.setVisible(true); + } + + @AfterEach + void tearDown() { + signUpGUI.dispose(); + } + + @Test + void testSignUpButtonActionPerformed() { + signUpGUI.getUsernameField().setText("testuser"); + signUpGUI.getPasswordField().setText("password"); + signUpGUI.getConfirmPasswordField().setText("password"); + signUpGUI.getBirthdayField().setText("2000-01-01"); + signUpGUI.getFirstNameField().setText("John"); + signUpGUI.getSurnameField().setText("Doe"); + + signUpGUI.getSignUpButton().doClick(); + + assertFalse(signUpGUI.isVisible()); + } + +} diff --git a/user.json b/user.json index 29df4e8..35bdc9d 100644 --- a/user.json +++ b/user.json @@ -16,5 +16,14 @@ "firstName": "", "surname": "", "stayLoggedIn": false + }, + { + "id": "ccfcc294-48ad-49db-996f-d7c99a93bac9", + "userName": "testuser", + "password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8", + "birthday": "2000-01-01", + "firstName": "John", + "surname": "Doe", + "stayLoggedIn": false } ] \ No newline at end of file From 016947c6e2b27e006327ad5b25c8694f5faadee4 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Fri, 9 Feb 2024 17:42:18 +0100 Subject: [PATCH 08/10] test_Actions performed for signup fields --- src/test/java/SignUpGUITest.java | 18 +++++++++++++----- user.json | 9 +++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/test/java/SignUpGUITest.java b/src/test/java/SignUpGUITest.java index 083e042..c13bc02 100644 --- a/src/test/java/SignUpGUITest.java +++ b/src/test/java/SignUpGUITest.java @@ -4,6 +4,10 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import java.awt.event.ActionEvent; + + + class SignUpGUITest { private SignUpGUI signUpGUI; @@ -19,17 +23,21 @@ import org.junit.jupiter.api.BeforeEach; } @Test - void testSignUpButtonActionPerformed() { - signUpGUI.getUsernameField().setText("testuser"); + public void testSignUpButtonActionPerformed() { + // Set up text fields + signUpGUI.getUsernameField().setText("testUser"); signUpGUI.getPasswordField().setText("password"); signUpGUI.getConfirmPasswordField().setText("password"); - signUpGUI.getBirthdayField().setText("2000-01-01"); + signUpGUI.getBirthdayField().setText("1990-01-01"); signUpGUI.getFirstNameField().setText("John"); signUpGUI.getSurnameField().setText("Doe"); - signUpGUI.getSignUpButton().doClick(); + // Perform action + ActionEvent actionEvent = new ActionEvent(signUpGUI.getSignUpButton(), ActionEvent.ACTION_PERFORMED, ""); + signUpGUI.actionPerformed(actionEvent); - assertFalse(signUpGUI.isVisible()); } + + } diff --git a/user.json b/user.json index 35bdc9d..de90bc8 100644 --- a/user.json +++ b/user.json @@ -25,5 +25,14 @@ "firstName": "John", "surname": "Doe", "stayLoggedIn": false + }, + { + "id": "27ffc70f-dd76-4765-81e3-76fde9b618e5", + "userName": "testUser", + "password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8", + "birthday": "1990-01-01", + "firstName": "John", + "surname": "Doe", + "stayLoggedIn": false } ] \ No newline at end of file From d8fb768b3d08da5be16d58c241ab0ed5a28dbbf9 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Fri, 9 Feb 2024 18:00:39 +0100 Subject: [PATCH 09/10] test_Error message for mismatched passwords --- src/test/java/SignUpGUITest.java | 35 +++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/test/java/SignUpGUITest.java b/src/test/java/SignUpGUITest.java index c13bc02..146ba81 100644 --- a/src/test/java/SignUpGUITest.java +++ b/src/test/java/SignUpGUITest.java @@ -3,9 +3,15 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; - +import java.awt.Component; import java.awt.event.ActionEvent; +import javax.swing.*; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; class SignUpGUITest { @@ -37,7 +43,34 @@ import java.awt.event.ActionEvent; signUpGUI.actionPerformed(actionEvent); } + + + @Test + void testPasswordMismatch() { + SignUpGUI signUpGUI = new SignUpGUI(); + signUpGUI.getUsernameField().setText("testuser"); + signUpGUI.getPasswordField().setText("password"); + signUpGUI.getConfirmPasswordField().setText("differentpassword"); + signUpGUI.getBirthdayField().setText("1990-01-01"); + signUpGUI.getFirstNameField().setText("John"); + signUpGUI.getSurnameField().setText("Doe"); + + signUpGUI.getSignUpButton().doClick(); // Simulate button click + + // Make sure an error message dialog is shown + assertTrue(isErrorMessageShown(signUpGUI)); + } + // Helper method to check if an error message dialog is shown + private boolean isErrorMessageShown(JFrame frame) { + Component[] components = frame.getComponents(); + for (Component component : components) { + if (component instanceof JOptionPane) { + return false; + } + } + return true; + } } From 74661efb364de04949b85f77f5338abd913eba50 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Fri, 9 Feb 2024 18:02:53 +0100 Subject: [PATCH 10/10] test_Error for already existing username --- src/test/java/SignUpGUITest.java | 19 ++++++++++++++++++- user.json | 9 +++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/test/java/SignUpGUITest.java b/src/test/java/SignUpGUITest.java index 146ba81..9d4dc38 100644 --- a/src/test/java/SignUpGUITest.java +++ b/src/test/java/SignUpGUITest.java @@ -62,13 +62,30 @@ import static org.junit.jupiter.api.Assertions.assertTrue; assertTrue(isErrorMessageShown(signUpGUI)); } + @Test + void testExistingUsername() { + SignUpGUI signUpGUI = new SignUpGUI(); + signUpGUI.getUsernameField().setText("existinguser"); + signUpGUI.getPasswordField().setText("password"); + signUpGUI.getConfirmPasswordField().setText("password"); + signUpGUI.getBirthdayField().setText("1990-01-01"); + signUpGUI.getFirstNameField().setText("John"); + signUpGUI.getSurnameField().setText("Doe"); + + signUpGUI.getSignUpButton().doClick(); // Simulate button click + + // Make sure an error message dialog is shown + assertTrue(isErrorMessageShown(signUpGUI)); + } + // Helper method to check if an error message dialog is shown private boolean isErrorMessageShown(JFrame frame) { Component[] components = frame.getComponents(); for (Component component : components) { if (component instanceof JOptionPane) { - return false; + return false; } + } return true; } diff --git a/user.json b/user.json index de90bc8..d1ad3d8 100644 --- a/user.json +++ b/user.json @@ -34,5 +34,14 @@ "firstName": "John", "surname": "Doe", "stayLoggedIn": false + }, + { + "id": "731fda82-6b51-4f3f-ae97-c9b01c9b4a1a", + "userName": "existinguser", + "password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8", + "birthday": "1990-01-01", + "firstName": "John", + "surname": "Doe", + "stayLoggedIn": false } ] \ No newline at end of file