HorseDaoTestBase.java 7.46 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
import java.sql.Date;
16 17 18
import java.util.HashMap;
import java.util.List;
import java.util.Map;
19 20 21 22 23 24

public abstract class HorseDaoTestBase {

    @Autowired
    HorseDao horseDao;

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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
    @Test
    @DisplayName("Getting all horses given two horses should return a list with the horses")
    public void gettingAllHorses_givenTwoHorses_shouldReturnHorses() {
        // Create the horses
        Horse firstHorse = horseDao.addHorse(new Horse("Zephyr", "Nice horse", (short) 4, Date.valueOf("2020-01-01"), ERace.APPALOOSA, "files/test.png", Long.valueOf(0)));
        Horse secondHorse = horseDao.addHorse(new Horse("Katrina", "Bad horse", (short) 1, Date.valueOf("2005-01-01"), ERace.APPALOOSA, "files/example.png", Long.valueOf(0)));

        // Test if the horses are present
        List<Horse> allHorses = horseDao.getAll();
        assertTrue(allHorses.contains(firstHorse));
        assertTrue(allHorses.contains(secondHorse));
    }

    @Test
    @DisplayName("Searching all horses with correct filters given two horses should return a list with the horse")
    public void searchingHorses_withCorrectFiltersGivenTwoHorses_shouldReturnHorse() {
        // Create the horses
        Horse firstHorse = horseDao.addHorse(new Horse("Zephyr", "Nice horse", (short) 4, Date.valueOf("2020-01-01"), ERace.APPALOOSA, "files/test.png", Long.valueOf(0)));
        Horse secondHorse = horseDao.addHorse(new Horse("Katrina", "Bad horse", (short) 1, Date.valueOf("2005-01-01"), ERace.APPALOOSA, "files/example.png", Long.valueOf(0)));

        // Test if the horses are present
        Map<String, String> filters = new HashMap<String, String>();
        filters.put("name", "Zephyr");
        filters.put("description", "Nice");
        filters.put("score", "4");
        filters.put("birthday", "2020-03-23");
        filters.put("race", ERace.APPALOOSA.name());

        List<Horse> allHorses = horseDao.getFiltered(filters);
        assertTrue(allHorses.contains(firstHorse));
    }

    @Test
    @DisplayName("Searching all horses with incorrect filters given two horses should throw NotFoundException")
    public void searchingHorses_withIncorrectFiltersGivenTwoHorses_shouldThrowNotFound() {
        // Create the horses
        Horse firstHorse = horseDao.addHorse(new Horse("Zephyr", "Nice horse", (short) 4, Date.valueOf("2020-01-01"), ERace.APPALOOSA, "files/test.png", Long.valueOf(0)));
        Horse secondHorse = horseDao.addHorse(new Horse("Katrina", "Bad horse", (short) 1, Date.valueOf("2005-01-01"), ERace.APPALOOSA, "files/example.png", Long.valueOf(0)));

        // Test if the horses are present
        Map<String, String> filters = new HashMap<String, String>();
        filters.put("name", "Tester Horse");
        filters.put("description", "A horse for testing");
        filters.put("score", "1");
        filters.put("birthday", "2020-02-01");
        filters.put("race", ERace.APPALOOSA.name());

        assertThrows(NotFoundException.class, () -> horseDao.getFiltered(filters));
    }

75 76 77
    @Test
    @DisplayName("Adding a new horse with the correct parameters should return the horse")
    public void addingNewHorse_correctParameters_shouldReturnHorse() {
78
        Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 4, Date.valueOf("2020-01-01"), ERace.APPALOOSA, "files/test.png", null);
79 80 81 82 83 84 85
        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() {
86 87 88 89 90 91
        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")
92
    public void updatingHorse_correctParameters_shouldReturnHorse() throws IOException {
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        // 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);
135
        assertThrows(DataAccessException.class, () -> horseDao.updateHorse(newHorse));
136 137
    }

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    @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));
    }
157
}