diff --git a/src/main/java/model/DAO/DAOConnection.java b/src/main/java/model/DAO/DAOConnection.java index dac0176..6fb8922 100644 --- a/src/main/java/model/DAO/DAOConnection.java +++ b/src/main/java/model/DAO/DAOConnection.java @@ -44,6 +44,7 @@ public Connection getConnection() throws SQLException { ); } + @Override public Connection getConnection(String username, String password) throws SQLException { return DriverManager.getConnection(props.getProperty("db.url"), username, password); diff --git a/src/main/java/model/DAO/DAOMessage.java b/src/main/java/model/DAO/DAOMessage.java index 731d814..8c29c7b 100644 --- a/src/main/java/model/DAO/DAOMessage.java +++ b/src/main/java/model/DAO/DAOMessage.java @@ -2,10 +2,7 @@ import model.entity.Message; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.sql.*; import java.util.*; public class DAOMessage { @@ -203,4 +200,37 @@ public int countReceivedMessages(int recipientId) { return count; } + + public void deleteLastInsertedMessage() { + Connection conn = null; + Statement stmt = null; + ResultSet rs = null; + + try { + conn = DAOConnection.getConnection(); + stmt = conn.createStatement(); + + String sql = "SELECT MAX(ID_message) FROM message"; + + rs = stmt.executeQuery(sql); + + if (rs.next()) { + int lastId = rs.getInt(1); + sql = "DELETE FROM message WHERE ID_message = " + lastId; + stmt.executeUpdate(sql); + } + + } catch (SQLException e) { + // Handle exceptions (e.g., print stack trace, log error, etc.) + e.printStackTrace(); + } finally { + try { + if (rs != null) rs.close(); + if (stmt != null) stmt.close(); + DAOConnection.releaseConnection(conn); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } } diff --git a/src/main/java/model/DAO/DAOUser.java b/src/main/java/model/DAO/DAOUser.java index ed611e0..295e72f 100644 --- a/src/main/java/model/DAO/DAOUser.java +++ b/src/main/java/model/DAO/DAOUser.java @@ -359,8 +359,6 @@ public boolean deleteUserByIdOrEmail(Object idOrEmail) { return rowsDeleted > 0; } catch (SQLException e) { - // Handle the exception (e.g., log or throw) - e.printStackTrace(); } finally { try { if (preparedStatement != null) preparedStatement.close(); diff --git a/src/test/java/model/DAO/DAOMessageTest.java b/src/test/java/model/DAO/DAOMessageTest.java index 85be589..3745f62 100644 --- a/src/test/java/model/DAO/DAOMessageTest.java +++ b/src/test/java/model/DAO/DAOMessageTest.java @@ -58,7 +58,7 @@ void testRetrieveMessages() { // Check the returned list assertNotNull(messages, "Returned list should not be null"); assertFalse(messages.isEmpty(), "Returned list should not be empty"); - assertNotEquals(0, messages.size()); // Assuming there are 2 messages between these users + assertNotEquals(0, messages.size()); // Assuming there are messages between these users } @Test @@ -83,9 +83,8 @@ void testSendMessage() { // Call the method to test daoMessage.sendMessage(senderId, recipientId, text); - - // Since the method doesn't return anything, we can't really verify its correctness - // However, we can assume it worked correctly if no exceptions were thrown + //clean it + daoMessage.deleteLastInsertedMessage(); } @Test diff --git a/src/test/java/model/service/license/LicenseActivationTest.java b/src/test/java/model/service/license/LicenseActivationTest.java index 1c8984c..fa27dba 100644 --- a/src/test/java/model/service/license/LicenseActivationTest.java +++ b/src/test/java/model/service/license/LicenseActivationTest.java @@ -71,24 +71,30 @@ void testIsForTherapist() { assertEquals(0, licenseActivation.isForTherapist(null)); } + @Test - void testActivate() { - // Mock the DAOLicense class - DAOLicense daoLicenseMock = mock(DAOLicense.class); + void testGeneratingLicenses(){ + DAOLicense daoLicense = new DAOLicense(); - // Set up the LicenseActivation class with the mocked DAO LicenseActivation licenseActivation = new LicenseActivation(); - licenseActivation.setDAOLicense(daoLicenseMock); + String licenseCode = licenseActivation.generateLicense(); + String pinCode = licenseActivation.generatePin(9); - // Create a sample license - License sampleLicense = new License(); - sampleLicense.setSequence("1234"); - sampleLicense.setIdUser(1); + assertEquals(8, licenseCode.length()); + assertEquals(4, pinCode.length()); + + licenseActivation.activate(daoLicense.getLicenseByCode(licenseCode), 0); + licenseActivation.activate(daoLicense.getLicenseByCode(pinCode), 999); + + License license1 = daoLicense.getLicenseByCode(licenseCode); + assertTrue(license1.isActive()); + assertEquals(0, license1.getIdUser()); - // Test the activate method - licenseActivation.activate(sampleLicense, 1); + License license2 = daoLicense.getLicenseByCode(pinCode); + assertTrue(license2.isActive()); + assertEquals(999, license2.getIdUser()); - // Verify that the DAO's activate method was called with the correct parameters - verify(daoLicenseMock, times(1)).activate(eq(sampleLicense), eq(1)); + daoLicense.deleteLicense(licenseCode); + daoLicense.deleteLicense(pinCode); } } diff --git a/src/test/java/model/service/user/UserDataTest.java b/src/test/java/model/service/user/UserDataTest.java new file mode 100644 index 0000000..d550c59 --- /dev/null +++ b/src/test/java/model/service/user/UserDataTest.java @@ -0,0 +1,78 @@ +package model.service.user; + + +import model.DAO.DAOUser; +import model.entity.User; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; +public class UserDataTest { + + private UserData ud; + private int id; + @BeforeEach + void setup() { ud = new UserData(); } + + @Test + void createUser() { + // Arrange + String email = "unittest@example.com"; + String password = "password"; + int therapistId = 999; + if(ud.checkIfEmailExists("unittest@example.com")) { + new DAOUser().deleteUserByIdOrEmail("unittest@example.com"); + } + // Act + int result = ud.createUser(email, password, therapistId); + // Assert + assertTrue(result > 0, "The result should be positive"); + new DAOUser().deleteUserByIdOrEmail(id); + id = result; + } + + @Test + void checkIfEmailExists(){ + assertTrue(ud.checkIfEmailExists("doc1@example.com"), "this has been generated before, it must exists"); + } + + @Test + void getUser() { + User user = ud.getUser("doc1@example.com"); + assertNotNull(user, " User with email unittest@example.com must exist"); + if (user != null) + assertEquals("doc1@example.com", user.getEmail(), "Email should match"); + } + + @Test + void isTherapist() { + User user = new User(); + user.setIdTherapist(0); + boolean result = ud.isTherapist(user); + assertTrue(result, "User should be a therapist"); + + User user2 = new User(); + user.setIdTherapist(9); + boolean result2 = ud.isTherapist(user); + assertFalse(result2, "User should not be a therapist"); + } + + @Test + void updateAnalyticsPreference() { + + + String userId = Integer.toString(id); + boolean result = ud.updateAnalyticsPreference(userId, true); + assertTrue(result, "Analytics preference should be updated"); + } + + @Test + void updateEmailTime() { + String userId = Integer.toString(id); + boolean result = ud.updateEmailTime(userId, "11:00|12:00"); + assertTrue(result, "Email time should be updated"); + } + +}