HorseDaoTestBase.java 4.62 KB
Newer Older
1 2 3 4 5
package at.ac.tuwien.sepm.assignment.individual.unit.persistence;

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

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

14
import java.io.IOException;
15 16 17 18 19 20 21 22 23 24
import java.sql.Date;

public abstract class HorseDaoTestBase {

    @Autowired
    HorseDao horseDao;

    @Test
    @DisplayName("Adding a new horse with the correct parameters should return the horse")
    public void addingNewHorse_correctParameters_shouldReturnHorse() {
25
        Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 4, Date.valueOf("2020-01-01"), ERace.APPALOOSA, "files/test.png", null);
26 27 28 29 30 31 32
        Horse savedHorse = horseDao.addHorse(newHorse);
        assertEquals(newHorse, savedHorse);
    }

    @Test
    @DisplayName("Adding a new horse with the incorrect parameters should throw DataAccessException")
    public void addingNewHorse_incorrectParameters_shouldThrowDataAccess() {
33 34 35 36 37 38
        Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 80, Date.valueOf("2020-01-01"), null, null, null);
        assertThrows(DataAccessException.class, () -> horseDao.addHorse(newHorse));
    }

    @Test
    @DisplayName("Updating a horse with the correct parameters should return the horse")
39
    public void updatingHorse_correctParameters_shouldReturnHorse() throws IOException {
40 41 42 43 44 45 46 47 48 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 80 81
        // Create horse
        Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 4, Date.valueOf("2020-01-01"), ERace.APPALOOSA, "files/test.png", null);
        Horse savedHorse = horseDao.addHorse(newHorse);

        // Update horse
        newHorse.setId(savedHorse.getId());
        newHorse.setName("Katrina");
        newHorse.setDescription("Fast horse");
        newHorse.setScore((short) 3);
        newHorse.setBirthday(Date.valueOf("2005-01-01"));
        newHorse.setRace(ERace.MORGAN);
        newHorse.setImagePath("files/katrina.png");
        newHorse.setOwner(null);
        Horse updatedHorse = horseDao.updateHorse(newHorse);

        // Compare everything except updated timestamp
        assertEquals(updatedHorse.getId(), newHorse.getId());
        assertEquals(updatedHorse.getName(), newHorse.getName());
        assertEquals(updatedHorse.getDescription(), newHorse.getDescription());
        assertEquals(updatedHorse.getScore(), newHorse.getScore());
        assertEquals(updatedHorse.getBirthday().toString(), newHorse.getBirthday().toString());
        assertEquals(updatedHorse.getImagePath(), newHorse.getImagePath());
        assertEquals(updatedHorse.getOwner(), newHorse.getOwner());
        assertEquals(updatedHorse.getCreatedAt(), newHorse.getCreatedAt());
    }

    @Test
    @DisplayName("Updating a horse with the incorrect parameters should throw DataAccessException")
    public void updatingHorse_incorrectParameters_shouldThrowDataAccess() {
        // Create horse
        Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 4, Date.valueOf("2020-01-01"), ERace.APPALOOSA, "files/test.png", null);
        Horse savedHorse = horseDao.addHorse(newHorse);

        // Update horse
        newHorse.setId(savedHorse.getId());
        newHorse.setName("Katrina");
        newHorse.setDescription("Fast horse");
        newHorse.setScore((short) 80);
        newHorse.setBirthday(Date.valueOf("2005-01-01"));
        newHorse.setRace(null);
        newHorse.setImagePath(null);
        newHorse.setOwner(null);
82 83 84
        assertThrows(DataAccessException.class, () -> horseDao.addHorse(newHorse));
    }

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    @Test
    @DisplayName("Deleting an existing horse should delete the horse")
    public void deletingHorse_existing_shouldDeleteHorse() throws IOException {
        // Create the horse
        Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 4, Date.valueOf("2020-01-01"), ERace.APPALOOSA, "files/test.png", null);
        Horse savedHorse = horseDao.addHorse(newHorse);

        // Delete the horse
        horseDao.deleteHorse(savedHorse.getId());

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

    @Test
    @DisplayName("Deleting an nonexistent horse should throw NotFoundException")
    public void deletingHorse_nonexistent_shouldThrowNotFound() throws IOException {
        assertThrows(NotFoundException.class, () -> horseDao.deleteHorse(null));
    }

105
}