Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 175 additions & 0 deletions src/test/java/org/example/vet1177/entities/PetEntityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package org.example.vet1177.entities;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDate;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

public class PetEntityTest {
private Pet pet;
private User owner;

@BeforeEach
void setUp(){
owner = new User("Kalle Karlsson", "kalle.k@example.se", "hasg123", Role.OWNER);
pet = new Pet();
}
//Getters & setters (name, species, breed, dateOfBirth, weightKg, owner)
@Test
void setName_shouldStoreAndReturnCorrectValue(){
pet.setName("Harry");

assertThat(pet.getName()).isEqualTo("Harry");
}

@Test
void setSpecies_shouldStoreAndReturnCorrectValue(){
pet.setSpecies("hund");

assertThat(pet.getSpecies()).isEqualTo("hund");
}


@Test
void setBreed_shouldStoreAndReturnCorrectValue(){
pet.setBreed("Labrador");

assertThat(pet.getBreed()).isEqualTo("Labrador");
}

@Test
void setDateOfBirth_shouldStoreAndReturnCorrectValue(){
LocalDate dob = LocalDate.of(2025, 12, 21);
pet.setDateOfBirth(dob);

assertThat(pet.getDateOfBirth()).isEqualTo(dob);
}

@Test
void setWeightKg_shouldStoreAndReturnCorrectValue(){
BigDecimal weight = new BigDecimal("13.50");
pet.setWeightKg(weight);

assertThat(pet.getWeightKg()).isEqualByComparingTo(weight);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

@Test
void setOwner_ShouldStoreAndReturnCorrectUser(){
pet.setOwner(owner);
assertThat(pet.getOwner()).isSameAs(owner);
}

//Konstruktorn med alla fält
@Test
void fullConstructor_shouldSetAllFields(){
LocalDate dob = LocalDate.of(2025,12,23);
BigDecimal weight = new BigDecimal("12.20");

Pet constructed = new Pet(owner, "Harry", "hund", "pudel", dob, weight);

assertThat(constructed.getOwner()).isSameAs(owner);
assertThat(constructed.getName()).isEqualTo("Harry");
assertThat(constructed.getSpecies()).isEqualTo("hund");
assertThat(constructed.getBreed()).isEqualTo("pudel");
assertThat(constructed.getDateOfBirth()).isEqualTo(dob);
assertThat(constructed.getWeightKg()).isEqualByComparingTo(weight);
}

@Test
void fullConstructor_shouldAllowNullBreed(){
Pet constructed = new Pet(owner, "Max", "Katt", null, LocalDate.now(), new BigDecimal("5.00"));
assertThat(constructed.getBreed()).isNull();
}

//onCreate() sätter createdAt och updatedAt
@Test
void onCreate_shouldSetBothTimestampsToNonNull() {
pet.onCreate();

assertThat(pet.getCreatedAt()).isNotNull();
assertThat(pet.getUpdatedAt()).isNotNull();
}

@Test
void onCreate_shouldSetTimestampsCloseToNow() {
Instant before = Instant.now();
pet.onCreate();
Instant after = Instant.now();

assertThat(pet.getCreatedAt()).isBetween(before, after);
assertThat(pet.getUpdatedAt()).isBetween(before, after);
}

//onUpdate() uppdaterar updatedAt men rör inte createdAt

@Test
void onUpdate_shouldRefreshUpdatedAt() {
pet.onCreate();

pet.onUpdate();

assertThat(pet.getUpdatedAt()).isNotNull();
}

@Test
void onUpdate_shouldNotModifyCreatedAt() {
pet.onCreate();
Instant originalCreatedAt = pet.getCreatedAt();

pet.onUpdate();

assertThat(pet.getCreatedAt()).isEqualTo(originalCreatedAt);
}


//Standardvärden är null innan persist
@Test
void getId_shouldBeNullBeforePersist() {
assertThat(pet.getId()).isNull();
}

@Test
void getName_shouldBeNullWhenNotSet() {
assertThat(pet.getName()).isNull();
}

@Test
void getSpecies_shouldBeNullWhenNotSet() {
assertThat(pet.getSpecies()).isNull();
}

@Test
void getBreed_shouldBeNullWhenNotSet() {
assertThat(pet.getBreed()).isNull();
}

@Test
void getDateOfBirth_shouldBeNullWhenNotSet() {
assertThat(pet.getDateOfBirth()).isNull();
}

@Test
void getWeightKg_shouldBeNullWhenNotSet() {
assertThat(pet.getWeightKg()).isNull();
}

@Test
void getOwner_shouldBeNullWhenNotSet() {
assertThat(pet.getOwner()).isNull();
}

@Test
void getCreatedAt_shouldBeNullBeforeOnCreate() {
assertThat(pet.getCreatedAt()).isNull();
}

@Test
void getUpdatedAt_shouldBeNullBeforeOnCreate() {
assertThat(pet.getUpdatedAt()).isNull();
}

}