Skip to content

Commit

Permalink
Merge pull request #82 from cathrkri/81-hotfixfix-unit-tests
Browse files Browse the repository at this point in the history
81 hotfixfix unit tests
  • Loading branch information
roaraf authored Apr 23, 2026
2 parents 960115b + 7edbf89 commit 78b3765
Show file tree
Hide file tree
Showing 10 changed files with 941 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

import static org.junit.jupiter.api.Assertions.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import ntnu.systemutvikling.team6.models.user.Inbox;
import ntnu.systemutvikling.team6.models.user.Role;
import ntnu.systemutvikling.team6.models.user.Settings;
import ntnu.systemutvikling.team6.models.user.User;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -50,4 +57,157 @@ public void testIsVerifiedReturnsCorrectly() {
charity.setVerified();
assertEquals("approved", charity.getStatus());
}

@Test
public void testDatabaseSelectConstructor() {
UUID uuid = UUID.randomUUID();
String orgNumber = "123";
boolean preApproved = true;
String status = "approved";
Charity selectCharity =
new Charity(
uuid.toString(),
orgNumber,
preApproved,
status);

assertEquals(uuid, selectCharity.getUUID());
assertEquals(orgNumber, selectCharity.getOrg_number());
assertEquals(preApproved, selectCharity.getPreApproved());
assertEquals(status, selectCharity.getStatus());
}

@Test
void testDatabaseConstructor() {
UUID uuid = UUID.randomUUID();
String orgNumber = "123";
String name = "testChair";
boolean preApproved = true;
String status = "approved";
String description = "describe";
String logoURL = "www.image.png";
String keyValues = "90,2;10,5;98,0";

byte[] logoBlob = new byte[] {1, 2, 3, 4, 5};

Charity databaseCharity =
new Charity(
uuid.toString(),
orgNumber,
name,
logoURL,
status,
preApproved,
description,
logoURL,
keyValues,
logoBlob
);

assertEquals(uuid, databaseCharity.getUUID());
assertEquals(orgNumber, databaseCharity.getOrg_number());
assertEquals(name, databaseCharity.getName());
assertEquals(logoURL, databaseCharity.getLogoURL());
assertEquals(status, databaseCharity.getStatus());
assertEquals(preApproved, databaseCharity.getPreApproved());
assertEquals(description, databaseCharity.getDescription());
assertEquals(keyValues, databaseCharity.getKeyValues());
assertEquals(logoBlob, databaseCharity.getLogoBlob());
}

@Test
void getAndSetFeedbackShouldBeCorrect() {
String validUsername = "username";
String validEmail = "Email@gmail.com";
String validPassword = "Password";
Role validRole = Role.NORMAL_USER;
Settings validSettings = new Settings();
Inbox validInbox = new Inbox();
User user =
new User(
validUsername,
validEmail,
validPassword,
validRole,
validSettings,
validInbox);
Feedback feedback =
new Feedback(
UUID.randomUUID().toString(),
user,
"Feedback.",
LocalDate.now()
);
ArrayList<Feedback>feedbackList = new ArrayList<>();
feedbackList.add(feedback);

charity.setFeedbacks(feedbackList);

assertSame(charity.getFeedbacks(), feedbackList);
}

@Test
void setNameShouldBeCorrect() {
String name = "Bob's charity";

charity.setName(name);

assertEquals(name, charity.getName());
}

@Test
void setCategoryShouldBeCorrect() {
String category = "Ghana";
List<String> categoryList = new ArrayList<>();
categoryList.add(category);

charity.setCategory(categoryList);

assertEquals(category, charity.getCategory().getFirst());
}

@Test
void setDescriptionShouldBeCorrect() {
String description = "blablabla";

charity.setDescription(description);

assertEquals(description, charity.getDescription());
}

@Test
void setLogoUrlShouldBeCorrect() {
String logoURL = "www.image.png";

charity.setLogoURL(logoURL);

assertEquals(logoURL, charity.getLogoURL());
}

@Test
void setKeyValuesShouldBeCorrect() {
String keyValues = "98,2;5,6;75,4";

charity.setKeyValues(keyValues);

assertEquals(keyValues, charity.getKeyValues());
}

@Test
void setLogoBlobShouldBeCorrect() {
byte[] logoBlob = new byte[] {1, 2, 3, 4, 5};

charity.setLogoBlob(logoBlob);

assertEquals(logoBlob, charity.getLogoBlob());
}

@Test
void setUUIDFromStringShouldBeCorrect() {
UUID uuid = UUID.randomUUID();

charity.setUUIDFromString(uuid.toString());

assertEquals(uuid, charity.getUUID());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.UUID;

import ntnu.systemutvikling.team6.models.user.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class DonationTest {
private Settings settings;
private User user;
private Charity charity;

Expand All @@ -20,7 +21,6 @@ public void setup() {

user =
new User(
"Name",
"username",
"Valid@gmail.com",
"123",
Expand All @@ -43,6 +43,19 @@ void testDonationInitialization() {
assertEquals(user, donation.getDonor());
}

@Test
void testDonationFromDatabaseInitialization() {
LocalDateTime now = LocalDateTime.now();

Donation donation = new Donation(UUID.randomUUID().toString(), 500.0, LocalDate.from(now), charity, user, false);

assertNotNull(donation.getCharityId());
assertEquals(500.0, donation.getAmount());
assertEquals(LocalDate.from(now), donation.getDate());
assertEquals(charity, donation.getCharity());
assertEquals(user, donation.getDonor());
}

@Test
void testAnonymousFlagWhenUserIsAnonymous() {
user.setSettings(new Settings(false, Language.ENGLISH, false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import static org.junit.jupiter.api.Assertions.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.chrono.ChronoLocalDate;
import java.util.UUID;

import ntnu.systemutvikling.team6.models.user.Inbox;
import ntnu.systemutvikling.team6.models.user.Role;
import ntnu.systemutvikling.team6.models.user.Settings;
Expand All @@ -14,15 +17,12 @@
class FeedbackTest {

private User user;
private Settings settings;

// -- Setup --
@BeforeEach
public void setup() {
settings = new Settings(); // default anonymous = true
user =
new User(
"Name",
"username",
"Valid@gmail.com",
"123",
Expand All @@ -48,6 +48,20 @@ void testFeedbackInitialization() {
!feedback.getDate().isBefore(ChronoLocalDate.from(before))
&& !feedback.getDate().isAfter(ChronoLocalDate.from(after)));
}
@Test
void testFeedBackFromDatabaseInitialization() {
LocalDate date = LocalDate.now();
Feedback feedback = new Feedback(
UUID.randomUUID().toString(),
user,
"Nice work!",
date);

assertNotNull(feedback.getFeedbackId());
assertEquals("Nice work!", feedback.getComment());
assertEquals(user, feedback.getUser());
assertEquals(date, feedback.getDate());
}

@Test
void testAnonymousFlagWhenUserIsAnonymous() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package ntnu.systemutvikling.team6.models;
package ntnu.systemutvikling.team6.models.registry;

import static org.junit.jupiter.api.Assertions.*;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import ntnu.systemutvikling.team6.models.registry.CharityRegistry;

import ntnu.systemutvikling.team6.models.Charity;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -66,6 +67,15 @@ void testFindCharityByOrgNumber() {
assertEquals(charity, result.get());
}

@Test
void testFindCharityByOrgNumberNullThrowsException() {
registry.addCharity(charity);

assertThrows(
IllegalArgumentException.class,
() -> registry.findCharityByOrgnumber(null));
}

@Test
void testFindCharityByIdNotFound() {
Optional<Charity> result = registry.findCharityByUUID(UUID.randomUUID());
Expand All @@ -87,6 +97,16 @@ void testRemoveCharitySuccessfully() {
assertTrue(registry.getAllCharities().isEmpty());
}

@Test
void testRemoveCharitySuccessfullyOrgNumber() {
registry.addCharity(charity);

boolean removed = registry.removeCharity(charity.getOrg_number());

assertTrue(removed);
assertTrue(registry.getAllCharities().isEmpty());
}

@Test
void testRemoveCharityNotFound() {
boolean removed = registry.removeCharityUUID(UUID.randomUUID());
Expand All @@ -97,4 +117,11 @@ void testRemoveCharityNotFound() {
void testRemoveCharityNullThrowsException() {
assertThrows(IllegalArgumentException.class, () -> registry.removeCharity(null));
}

@Test
void testRemoveCharityUUIDNullThrowsException() {
UUID uuid = null;
assertThrows(IllegalArgumentException.class, () -> registry.removeCharityUUID(uuid));

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ntnu.systemutvikling.team6.models;
package ntnu.systemutvikling.team6.models.registry;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
Expand All @@ -7,7 +7,9 @@
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import ntnu.systemutvikling.team6.models.registry.DonationRegistry;

import ntnu.systemutvikling.team6.models.Charity;
import ntnu.systemutvikling.team6.models.Donation;
import ntnu.systemutvikling.team6.models.user.Settings;
import ntnu.systemutvikling.team6.models.user.User;
import org.junit.jupiter.api.BeforeEach;
Expand Down
Loading

0 comments on commit 78b3765

Please sign in to comment.