OwnerDaoTestBase.java 2.75 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.Owner;
6 7 8 9 10
import at.ac.tuwien.sepm.assignment.individual.exception.NotFoundException;
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;
11
import org.springframework.dao.DataAccessException;
12

13 14 15
import java.io.IOException;
import java.sql.Date;

16 17 18 19 20 21 22 23 24
public abstract class OwnerDaoTestBase {

    @Autowired
    OwnerDao ownerDao;

    @Test
    @DisplayName("Finding owner by non-existing ID should throw NotFoundException")
    public void findingOwnerById_nonExisting_shouldThrowNotFoundException() {
        assertThrows(NotFoundException.class,
25
            () -> ownerDao.findOneById(0L));
26 27
    }

28 29 30 31 32 33 34 35 36
    @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
37
    @DisplayName("Adding a new owner with the incorrect parameters should throw DataAccessException")
38 39 40 41
    public void addingNewOwner_incorrectParameters_shouldThrowDataAccess() {
        Owner newOwner = new Owner("");
        assertThrows(DataAccessException.class, () -> ownerDao.addOwner(newOwner));
    }
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("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));
    }
73
}