OwnerDaoTestBase.java 7.3 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
import java.io.IOException;
import java.sql.Date;
19 20 21
import java.util.HashMap;
import java.util.List;
import java.util.Map;
22

23 24 25 26 27
public abstract class OwnerDaoTestBase {

    @Autowired
    OwnerDao ownerDao;

28 29 30
    @Autowired
    HorseDao horseDao;

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
    @Test
    @DisplayName("Getting all owners given two owners should return a list with the owners")
    public void gettingAllOwners_givenTwoOwners_shouldReturnOwners() {
        // Create the owners
        Owner firstOwner = ownerDao.addOwner(new Owner("Chad"));
        Owner secondOwner = ownerDao.addOwner(new Owner("Lad"));

        // Test if the owners are present
        List<Owner> allOwners = ownerDao.getAll();
        assertTrue(allOwners.contains(firstOwner));
        assertTrue(allOwners.contains(secondOwner));
    }

    @Test
    @DisplayName("Searching all owners with correct filters given two owners should return a list with the owner")
    public void searchingOwners_withCorrectFiltersGivenTwoOwners_shouldReturnOwner() {
        // Create the owners
        Owner firstOwner = ownerDao.addOwner(new Owner("Brad"));
        Owner secondOwner = ownerDao.addOwner(new Owner("Becky"));

        // Test if the owners are present
        Map<String, String> filters = new HashMap<String, String>();
        filters.put("name", "brad");

        List<Owner> allOwners = ownerDao.getFiltered(filters);
        assertTrue(allOwners.contains(firstOwner));
    }

    @Test
    @DisplayName("Searching all owners with incorrect filters given two owners should throw NotFoundException")
    public void searchingOwners_withIncorrectFiltersGivenTwoOwners_shouldThrowNotFound() {
        // Create the owners
        Owner firstOwner = ownerDao.addOwner(new Owner("Brad"));
        Owner secondOwner = ownerDao.addOwner(new Owner("Stacy"));

        // Test if the owners are present
        Map<String, String> filters = new HashMap<String, String>();
        filters.put("name", "Tester Owner");

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

73 74 75 76
    @Test
    @DisplayName("Finding owner by non-existing ID should throw NotFoundException")
    public void findingOwnerById_nonExisting_shouldThrowNotFoundException() {
        assertThrows(NotFoundException.class,
77
            () -> ownerDao.findOneById(0L));
78 79
    }

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    @Test
    @DisplayName("Getting horses of owner with one horse should return the Horse")
    public void gettingHorsesOfOwner_oneHorseAssigned_shouldReturnHorse() {
        Owner savedOwner = ownerDao.addOwner(new Owner("Brad"));

        // 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);

        // Temporary, until the database mapping is fixed
        savedHorse.setOwner(null);
        assertTrue(ownerDao.getOwnedHorses(savedOwner.getId()).contains(newHorse));
    }

    @Test
    @DisplayName("Getting horses of owner without horses should throw NotFoundException")
    public void gettingHorsesOfOwner_noHorsesAssigned_shouldThrowNotFound() {
        Owner savedOwner = ownerDao.addOwner(new Owner("Brad"));

        assertThrows(NotFoundException.class, () -> ownerDao.getOwnedHorses(savedOwner.getId()));
    }

102 103 104 105 106 107 108 109 110
    @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
111
    @DisplayName("Adding a new owner with the incorrect parameters should throw DataAccessException")
112 113 114 115
    public void addingNewOwner_incorrectParameters_shouldThrowDataAccess() {
        Owner newOwner = new Owner("");
        assertThrows(DataAccessException.class, () -> ownerDao.addOwner(newOwner));
    }
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

    @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));
    }
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

    @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()));
    }
182
}