Skip to content

Commit

Permalink
Fix: Updated previous tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianBalunan committed Apr 12, 2026
1 parent 0b6f9d2 commit 2bc2a18
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 21 deletions.
7 changes: 7 additions & 0 deletions helpmehelpapplication/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@
<version>2.4.240</version>
<scope>test</scope>
</dependency>
<!-- Source: https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.23.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ntnu.systemutvikling.team6.database.DatabaseConnection;
import ntnu.systemutvikling.team6.database.DatabaseSetup;
import ntnu.systemutvikling.team6.models.Charity;
import ntnu.systemutvikling.team6.service.APIToDatabaseService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -23,7 +24,8 @@ void setUp() {

charity = new Charity("123456", "https://test.org", "Test Charity", true, "approved");

manager.addAPIDataToTable(java.util.List.of(charity));
APIToDatabaseService service = new APIToDatabaseService(conn);
service.addAPIDataToTable(java.util.List.of(charity));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import ntnu.systemutvikling.team6.database.Readers.CharitySelect;
import ntnu.systemutvikling.team6.database.Readers.DonationSelect;
import ntnu.systemutvikling.team6.models.Charity;
import ntnu.systemutvikling.team6.models.CharityRegistry;
import ntnu.systemutvikling.team6.service.APIToDatabaseService;
import org.junit.jupiter.api.*;

class DatabaseManagerTest {

private DatabaseSetup dbManager;
private APIToDatabaseService service;
private CharitySelect charitySelect;
private DonationSelect donationSelect;

@BeforeEach
public void setUp() throws SQLException {
DatabaseConnection conn = new DatabaseConnection();
this.dbManager = new DatabaseSetup(conn);
this.service = new APIToDatabaseService(conn);
this.charitySelect = new CharitySelect(conn);
this.donationSelect = new DonationSelect(conn);
}

// Make sure you're connected to the NTNU network for this to work
Expand Down Expand Up @@ -48,14 +56,14 @@ void updateCharitiesShouldInsertCorrectData() throws SQLException {
boolean is_pre_approved = false;

Charity charity = new Charity(org_number, url, name, is_pre_approved, status);
dbManager.addAPIDataToTable(List.of(charity));
service.addAPIDataToTable(List.of(charity));

Charity charity2 = new Charity(org_number, url, name + " Updated", is_pre_approved, status);
dbManager.addAPIDataToTable(List.of(charity2));
service.addAPIDataToTable(List.of(charity2));

dbManager.createTables();

CharityRegistry registry = dbManager.getCharitiesFromDB();
CharityRegistry registry = charitySelect.getCharitiesFromDB();
Charity insertedCharity = registry.findCharityByOrgnumber("12345").get();
assertEquals("Test Charity Updated", insertedCharity.getName());
}
Expand Down Expand Up @@ -91,13 +99,13 @@ void updateCharitiesShouldRemoveDataNotInList() throws SQLException {
charityListBefore.add(charity2);
charityListBefore.add(charity3);

dbManager.addAPIDataToTable(charityListBefore);
service.addAPIDataToTable(charityListBefore);

List<Charity> charityListNew = new ArrayList<>();
charityListNew.add(charity1);
charityListNew.add(charity3);

dbManager.addAPIDataToTable(charityListNew);
service.addAPIDataToTable(charityListNew);

try (Connection conn = new DatabaseConnection().getMySqlConnection()) {
PreparedStatement stmt =
Expand All @@ -117,7 +125,7 @@ void updateCharitiesShouldRemoveDataNotInList() throws SQLException {
void tempTableShouldNotExistAfterUpdating() throws SQLException {
Charity charity = new Charity("99999", "https://temp.no", "Temp Charity", false, "approved");

dbManager.addAPIDataToTable(List.of(charity));
service.addAPIDataToTable(List.of(charity));

try (Connection conn = new DatabaseConnection().getMySqlConnection()) {
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM temp_api_charities");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.*;

import java.time.LocalDateTime;
import java.time.chrono.ChronoLocalDate;
import ntnu.systemutvikling.team6.models.user.Inbox;
import ntnu.systemutvikling.team6.models.user.Role;
import ntnu.systemutvikling.team6.models.user.Settings;
Expand Down Expand Up @@ -36,7 +37,9 @@ void testFeedbackInitialization() {
assertEquals(user, feedback.getUser());

// Date should be between before and after
assertTrue(!feedback.getDate().isBefore(before) && !feedback.getDate().isAfter(after));
assertTrue(
!feedback.getDate().isBefore(ChronoLocalDate.from(before))
&& !feedback.getDate().isAfter(ChronoLocalDate.from(after)));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class InboxTest {
@BeforeEach
public void setup() {
inbox = new Inbox();
newMessage = new Message("Title", "Someone", "Somewhere");
newMessage2 = new Message("Title2", "Someone2", "Somewhere2");
newMessage = new Message("Title", UUID.randomUUID(), "Somewhere");
newMessage2 = new Message("Title2", UUID.randomUUID(), "Somewhere2");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,34 @@ public class MessegeTest {
void shouldThrowExceptionIfNameIsNullOrEmpty() {
assertThrows(
IllegalArgumentException.class,
() -> new Message(null, "Someone", "Something Somewhere Somehow"));
() -> new Message(null, UUID.randomUUID(), "Something Somewhere Somehow"));
assertThrows(
IllegalArgumentException.class,
() -> new Message("", "Someone", "Something Somewhere Somehow"));
() -> new Message("", UUID.randomUUID(), "Something Somewhere Somehow"));
}

@Test
void shouldThrowExceptionIfFromIsNullOrEmpty() {
assertThrows(
IllegalArgumentException.class,
() -> new Message("Title", null, "Something Somewhere Somehow"));
assertThrows(
IllegalArgumentException.class,
() -> new Message("Title", "", "Something Somewhere Somehow"));
}

@Test
void shouldThrowExceptionIfContentIsNullOrEmpty() {
assertThrows(IllegalArgumentException.class, () -> new Message("Title", "Someone", null));
assertThrows(IllegalArgumentException.class, () -> new Message("Title", "Someone", ""));
assertThrows(
IllegalArgumentException.class, () -> new Message("Title", UUID.randomUUID(), null));
assertThrows(IllegalArgumentException.class, () -> new Message("Title", UUID.randomUUID(), ""));
}

@Test
void GettersWork() {
Message newMessage = new Message("Title", "Someone", "Somewhere");
UUID uuid = UUID.randomUUID();
Message newMessage = new Message("Title", uuid, "Somewhere");
assertInstanceOf(UUID.class, newMessage.getId());
assertEquals("Title", newMessage.getTitle());
assertEquals("Someone", newMessage.getFrom());
assertEquals(uuid, newMessage.getFrom());
assertEquals("Somewhere", newMessage.getContent());
assertEquals(LocalDate.now(), newMessage.getTimeAndDate().toLocalDate());
assertEquals(LocalDate.now(), newMessage.getTimeAndDate());
}
}

0 comments on commit 2bc2a18

Please sign in to comment.