OwnerDaoTestBase.java 4.48 KB
Newer Older
1 2 3 4
package at.ac.tuwien.sepm.assignment.individual.unit.persistence;

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

5
import at.ac.tuwien.sepm.assignment.individual.entity.Horse;
6
import at.ac.tuwien.sepm.assignment.individual.entity.Owner;
7
import at.ac.tuwien.sepm.assignment.individual.enums.ERace;
8
import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException;
9
import at.ac.tuwien.sepm.assignment.individual.persistence.HorseDao;
10 11 12 13
import at.ac.tuwien.sepm.assignment.individual.persistence.OwnerDao;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.dao.DataAccessException;
15
import org.springframework.dao.DataIntegrityViolationException;
16

17 18 19
import java.io.IOException;
import java.sql.Date;

20 21 22 23 24
public abstract class OwnerDaoTestBase {

    @Autowired
    OwnerDao ownerDao;

25 26 27
    @Autowired
    HorseDao horseDao;

28 29 30 31
    @Test
    @DisplayName("Finding owner by non-existing ID should throw NotFoundException")
    public void findingOwnerById_nonExisting_shouldThrowNotFoundException() {
        assertThrows(NotFoundException.class,
32
            () -> ownerDao.findOneById(0L));
33 34
    }

35 36 37 38 39 40 41 42 43
    @Test
    @DisplayName("Adding a new owner with the correct parameters should return the owner")
    public void addingNewOwner_correctParameters_shouldReturnOwner() {
        Owner newOwner = new Owner("Chad");
        Owner savedOwner = ownerDao.addOwner(newOwner);
        assertEquals(newOwner, savedOwner);
    }

    @Test
44
    @DisplayName("Adding a new owner with the incorrect parameters should throw DataAccessException")
45 46 47 48
    public void addingNewOwner_incorrectParameters_shouldThrowDataAccess() {
        Owner newOwner = new Owner("");
        assertThrows(DataAccessException.class, () -> ownerDao.addOwner(newOwner));
    }
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

    @Test
    @DisplayName("Updating a owner with the correct parameters should return the owner")
    public void updatingOwner_correctParameters_shouldReturnOwner() throws IOException {
        // Create owner
        Owner newOwner = new Owner("Chad");
        Owner savedOwner = ownerDao.addOwner(newOwner);

        // Update owner
        newOwner.setId(savedOwner.getId());
        newOwner.setName("Gigachad");
        Owner updatedOwner = ownerDao.updateOwner(newOwner);

        // Compare everything except updated timestamp
        assertEquals(updatedOwner.getId(), newOwner.getId());
        assertEquals(updatedOwner.getName(), newOwner.getName());
        assertEquals(updatedOwner.getCreatedAt(), newOwner.getCreatedAt());
    }

    @Test
    @DisplayName("Updating a owner with the incorrect parameters should throw DataAccessException")
    public void updatingOwner_incorrectParameters_shouldThrowDataAccess() {
        // Create owner
        Owner newOwner = new Owner("Chad");
        Owner savedOwner = ownerDao.addOwner(newOwner);

        // Update owner
        newOwner.setId(savedOwner.getId());
        newOwner.setName("");
        assertThrows(DataAccessException.class, () -> ownerDao.updateOwner(newOwner));
    }
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

    @Test
    @DisplayName("Deleting an existing owner without horses should delete the owner")
    public void deletingOwner_existingOwnerNoHorsesOwned_shouldDeleteOwner() throws IOException {
        // Create the owner
        Owner newOwner = new Owner("Chad");
        Owner savedOwner = ownerDao.addOwner(newOwner);

        // Delete the owner
        ownerDao.deleteOwner(savedOwner.getId());

        // Check if deleted
        assertThrows(NotFoundException.class, () -> ownerDao.findOneById(savedOwner.getId()));
    }

    @Test
    @DisplayName("Deleting an nonexistent owner should throw NotFoundException")
    public void deletingOwner_nonexistent_shouldThrowNotFound() throws IOException {
        assertThrows(NotFoundException.class, () -> ownerDao.deleteOwner(null));
    }

    @Test
    @DisplayName("Deleting an existing owner with horses should throw DataIntegrityViolationException")
    public void deletingHorse_existing_shouldDeleteHorse() {
        // Create the owner
        Owner newOwner = new Owner("Chad");
        Owner savedOwner = ownerDao.addOwner(newOwner);

        // Create the horse
        Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 4, Date.valueOf("2020-01-01"), ERace.APPALOOSA, "files/test.png", savedOwner.getId());
        Horse savedHorse = horseDao.addHorse(newHorse);

        // Delete the owner
        assertThrows(DataIntegrityViolationException.class, () -> ownerDao.deleteOwner(savedOwner.getId()));
    }
115
}