Skip to content

Commit

Permalink
provided Delete methods for DAO, fixed errors on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
panuozzo77 committed Jan 2, 2024
1 parent 1159ba9 commit c6bb738
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/main/java/model/DAO/DAOConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
38 changes: 34 additions & 4 deletions src/main/java/model/DAO/DAOMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
}
}
}
2 changes: 0 additions & 2 deletions src/main/java/model/DAO/DAOUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 3 additions & 4 deletions src/test/java/model/DAO/DAOMessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
32 changes: 19 additions & 13 deletions src/test/java/model/service/license/LicenseActivationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
78 changes: 78 additions & 0 deletions src/test/java/model/service/user/UserDataTest.java
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]";
String password = "password";
int therapistId = 999;
if(ud.checkIfEmailExists("[email protected]")) {
new DAOUser().deleteUserByIdOrEmail("[email protected]");
}
// 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("[email protected]"), "this has been generated before, it must exists");
}

@Test
void getUser() {
User user = ud.getUser("[email protected]");
assertNotNull(user, " User with email [email protected] must exist");
if (user != null)
assertEquals("[email protected]", 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");
}

}

0 comments on commit c6bb738

Please sign in to comment.