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/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/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/CreateUserTest.java b/src/test/java/CreateUserTest.java new file mode 100644 index 0000000..f3e4c19 --- /dev/null +++ b/src/test/java/CreateUserTest.java @@ -0,0 +1,96 @@ +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 { + + @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(); + } + + @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(); + } + } + + + diff --git a/src/test/java/LoginGUITest.java b/src/test/java/LoginGUITest.java new file mode 100644 index 0000000..b2b6184 --- /dev/null +++ b/src/test/java/LoginGUITest.java @@ -0,0 +1,52 @@ +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() { + 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()); + } + + @Test + public void testStayLoggedInCheckbox() { + // Initially, checkbox should be unchecked + assertFalse(loginGUI.getStayLoggedInCheckbox().isSelected()); + + // Simulate checking the checkbox + loginGUI.getStayLoggedInCheckbox().doClick(); + + assertTrue(loginGUI.getStayLoggedInCheckbox().isSelected()); + } + + + + +} diff --git a/src/test/java/SignUpGUITest.java b/src/test/java/SignUpGUITest.java new file mode 100644 index 0000000..9d4dc38 --- /dev/null +++ b/src/test/java/SignUpGUITest.java @@ -0,0 +1,93 @@ +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; + +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 { + private SignUpGUI signUpGUI; + + @BeforeEach + void setUp() { + signUpGUI = new SignUpGUI(); + signUpGUI.setVisible(true); + } + + @AfterEach + void tearDown() { + signUpGUI.dispose(); + } + + @Test + public void testSignUpButtonActionPerformed() { + // Set up text fields + signUpGUI.getUsernameField().setText("testUser"); + signUpGUI.getPasswordField().setText("password"); + signUpGUI.getConfirmPasswordField().setText("password"); + signUpGUI.getBirthdayField().setText("1990-01-01"); + signUpGUI.getFirstNameField().setText("John"); + signUpGUI.getSurnameField().setText("Doe"); + + // Perform action + ActionEvent actionEvent = new ActionEvent(signUpGUI.getSignUpButton(), ActionEvent.ACTION_PERFORMED, ""); + 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)); + } + + @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 true; + } + +} diff --git a/user.json b/user.json index 29df4e8..d1ad3d8 100644 --- a/user.json +++ b/user.json @@ -16,5 +16,32 @@ "firstName": "", "surname": "", "stayLoggedIn": false + }, + { + "id": "ccfcc294-48ad-49db-996f-d7c99a93bac9", + "userName": "testuser", + "password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8", + "birthday": "2000-01-01", + "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 + }, + { + "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