Skip to content

Commit

Permalink
Feat: Added and implemented FeedbackDAOTest
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianBalunan committed Apr 24, 2026
1 parent 9c8edc1 commit d5758f3
Showing 1 changed file with 245 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,247 @@
package ntnu.systemutvikling.team6.database.DAO;

public class FeedbackDAOTest
{
}
import ntnu.systemutvikling.team6.database.DatabaseConnection;
import ntnu.systemutvikling.team6.models.Charity;
import ntnu.systemutvikling.team6.models.Feedback;
import ntnu.systemutvikling.team6.models.user.*;
import org.junit.jupiter.api.*;
import java.sql.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

public class FeedbackDAOTest {

private DatabaseConnection mockDbConnection;
private Connection mockConn;
private PreparedStatement mockStmt;
private ResultSet mockRs;

private FeedbackDAO feedbackDAO;

@BeforeEach
void setUp() throws SQLException {
mockDbConnection = mock(DatabaseConnection.class);
mockConn = mock(Connection.class);
mockStmt = mock(PreparedStatement.class);
mockRs = mock(ResultSet.class);

when(mockDbConnection.getMySqlConnection()).thenReturn(mockConn);
when(mockConn.prepareStatement(anyString())).thenReturn(mockStmt);

feedbackDAO = new FeedbackDAO(mockDbConnection);
}

// ----------------------------------------------------------------
// Helpers
// ----------------------------------------------------------------

private User buildTestUser(String userId) {
Settings settings = new Settings(false, Language.ENGLISH, true);
User user = new User(
userId,
"TestUser",
"test@example.com",
"hashedpassword",
Role.NORMAL_USER.toString()
);
user.setSettings(settings);
user.setInbox(new Inbox());
return user;
}

private Charity buildTestCharity(String charityId) {
return new Charity(
charityId,
"123456789",
"HelpOrg",
"helporg.com",
"active",
true,
"We help people",
null,
null,
null
);
}

private Feedback buildTestFeedback(User user) {
return new Feedback(
UUID.randomUUID().toString(),
user,
"Great charity!",
LocalDate.now()
);
}

// ----------------------------------------------------------------
// addFeedbackToCharity()
// ----------------------------------------------------------------

@Test
void addFeedbackToCharity_returnsTrueOnSuccess() throws SQLException {
when(mockStmt.executeUpdate()).thenReturn(1);

String userId = UUID.randomUUID().toString();
String charityId = UUID.randomUUID().toString();

boolean result = feedbackDAO.addFeedbackToCharity(
buildTestFeedback(buildTestUser(userId)),
buildTestCharity(charityId)
);

assertTrue(result);
}

@Test
void addFeedbackToCharity_returnsFalseWhenNoRowsAffected() throws SQLException {
when(mockStmt.executeUpdate()).thenReturn(0);

boolean result = feedbackDAO.addFeedbackToCharity(
buildTestFeedback(buildTestUser(UUID.randomUUID().toString())),
buildTestCharity(UUID.randomUUID().toString())
);

assertFalse(result);
}

@Test
void addFeedbackToCharity_returnsFalseOnSQLException() throws SQLException {
when(mockStmt.executeUpdate()).thenThrow(new SQLException("Insert failed"));

boolean result = feedbackDAO.addFeedbackToCharity(
buildTestFeedback(buildTestUser(UUID.randomUUID().toString())),
buildTestCharity(UUID.randomUUID().toString())
);

assertFalse(result);
}

@Test
void addFeedbackToCharity_setsCorrectParameterOrder() throws SQLException {
when(mockStmt.executeUpdate()).thenReturn(1);

String userId = UUID.randomUUID().toString();
String charityId = UUID.randomUUID().toString();
User user = buildTestUser(userId);
Feedback feedback = buildTestFeedback(user);
Charity charity = buildTestCharity(charityId);

feedbackDAO.addFeedbackToCharity(feedback, charity);

verify(mockStmt).setString(1, feedback.getFeedbackId().toString()); // UUID_feedback
verify(mockStmt).setString(2, feedback.getComment()); // feedback_comment
verify(mockStmt).setDate(3, Date.valueOf(feedback.getDate())); // feedback_date
verify(mockStmt).setBoolean(4, user.getSettings().isAnonymous()); // isAnonymous
verify(mockStmt).setString(5, charityId); // charity_id
verify(mockStmt).setString(6, userId); // user_id
}

@Test
void addFeedbackToCharity_setsAnonymousTrueWhenUserIsAnonymous() throws SQLException {
when(mockStmt.executeUpdate()).thenReturn(1);

String userId = UUID.randomUUID().toString();
User anonymousUser = buildTestUser(userId);
anonymousUser.setSettings(new Settings(true, Language.ENGLISH, false));

feedbackDAO.addFeedbackToCharity(
buildTestFeedback(anonymousUser),
buildTestCharity(UUID.randomUUID().toString())
);

verify(mockStmt).setBoolean(4, true);
}

// ----------------------------------------------------------------
// getFeedbackForCharityUUID()
// ----------------------------------------------------------------

@Test
void getFeedbackForCharityUUID_returnsFeedbackList() throws SQLException {
when(mockStmt.executeQuery()).thenReturn(mockRs);
when(mockRs.next()).thenReturn(true, false);

String feedbackId = UUID.randomUUID().toString();
String userId = UUID.randomUUID().toString();

when(mockRs.getString("UUID_feedback")).thenReturn(feedbackId);
when(mockRs.getString("feedback_comment")).thenReturn("Great work!");
when(mockRs.getString("feedback_date")).thenReturn(LocalDate.now().toString());
when(mockRs.getBoolean("isAnonymous")).thenReturn(false);

when(mockRs.getString("UUID_User")).thenReturn(userId);
when(mockRs.getString("user_name")).thenReturn("Alice");
when(mockRs.getString("user_email")).thenReturn("alice@example.com");
when(mockRs.getString("user_password")).thenReturn("hashedpw");
when(mockRs.getString("role")).thenReturn(Role.NORMAL_USER.toString());
when(mockRs.getString("language")).thenReturn(Language.ENGLISH.toString());
when(mockRs.getBoolean("lightmode")).thenReturn(true);

ArrayList<Feedback> result = feedbackDAO.getFeedbackforCharityUUID(UUID.randomUUID().toString());

assertNotNull(result);
assertEquals(1, result.size());
assertEquals(feedbackId, result.getFirst().getFeedbackId().toString());
assertEquals("Great work!", result.getFirst().getComment());
assertEquals(userId, result.getFirst().getUser().getId().toString());
}

@Test
void getFeedbackForCharityUUID_returnsEmptyListWhenNoFeedback() throws SQLException {
when(mockStmt.executeQuery()).thenReturn(mockRs);
when(mockRs.next()).thenReturn(false);

ArrayList<Feedback> result = feedbackDAO.getFeedbackforCharityUUID(UUID.randomUUID().toString());

assertNotNull(result);
assertTrue(result.isEmpty());
}

@Test
void getFeedbackForCharityUUID_returnsMultipleFeedbackEntries() throws SQLException {
when(mockStmt.executeQuery()).thenReturn(mockRs);
when(mockRs.next()).thenReturn(true, true, true, false);

when(mockRs.getString("UUID_feedback")).thenReturn(
UUID.randomUUID().toString(),
UUID.randomUUID().toString(),
UUID.randomUUID().toString()
);
when(mockRs.getString("feedback_comment")).thenReturn("Good", "Excellent", "Amazing");
when(mockRs.getString("feedback_date")).thenReturn(LocalDate.now().toString());
when(mockRs.getBoolean("isAnonymous")).thenReturn(false);
when(mockRs.getString("UUID_User")).thenReturn(UUID.randomUUID().toString());
when(mockRs.getString("user_name")).thenReturn("User");
when(mockRs.getString("user_email")).thenReturn("u@example.com");
when(mockRs.getString("user_password")).thenReturn("pw");
when(mockRs.getString("role")).thenReturn(Role.NORMAL_USER.toString());
when(mockRs.getString("language")).thenReturn(Language.ENGLISH.toString());
when(mockRs.getBoolean("lightmode")).thenReturn(false);

ArrayList<Feedback> result = feedbackDAO.getFeedbackforCharityUUID(UUID.randomUUID().toString());

assertEquals(3, result.size());
}

@Test
void getFeedbackForCharityUUID_throwsRuntimeExceptionOnSQLException() throws SQLException {
when(mockStmt.executeQuery()).thenThrow(new SQLException("Query failed"));

assertThrows(RuntimeException.class,
() -> feedbackDAO.getFeedbackforCharityUUID(UUID.randomUUID().toString()));
}

@Test
void getFeedbackForCharityUUID_passesCorrectCharityIdToQuery() throws SQLException {
when(mockStmt.executeQuery()).thenReturn(mockRs);
when(mockRs.next()).thenReturn(false);

String charityId = UUID.randomUUID().toString();
feedbackDAO.getFeedbackforCharityUUID(charityId);

verify(mockStmt).setString(1, charityId);
}
}

0 comments on commit d5758f3

Please sign in to comment.