HorseServiceTest.java 4.07 KB
Newer Older
1 2 3 4 5
package at.ac.tuwien.sepm.assignment.individual.unit.service;

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 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
import at.ac.tuwien.sepm.assignment.individual.service.HorseService;
import at.ac.tuwien.sepm.assignment.individual.util.ValidationException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.sql.Date;

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

@ExtendWith(SpringExtension.class)
@SpringBootTest
@ActiveProfiles("test")
public class HorseServiceTest {
    @Autowired
    HorseService horseService;

    @Test
    @DisplayName("Adding a new horse with the correct parameters will return the new horse")
    public void addingNewHorse_correctParameters_shouldReturnHorse() {
31
        Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 4, Date.valueOf("2020-01-01"), ERace.APPALOOSA, "files/test.png", null);
32 33 34 35 36 37 38
        Horse savedHorse = horseService.addHorse(newHorse);
        assertEquals(newHorse, savedHorse);
    }

    @Test
    @DisplayName("Adding a new horse with the incorrect parameters will throw a ValidationException")
    public void addingNewHorse_incorrectParameters_shouldThrowValidation() {
39
        Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 80, Date.valueOf("2020-01-01"), null, null, null);
40 41
        assertThrows(ValidationException.class, () -> horseService.addHorse(newHorse));
    }
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 82 83 84 85 86 87 88 89

    @Test
    @DisplayName("Updating a horse with the correct parameters will return the new horse")
    public void updatingHorse_correctParameters_shouldReturnHorse() {
        // Create horse
        Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 4, Date.valueOf("2020-01-01"), ERace.APPALOOSA, "files/test.png", null);
        Horse savedHorse = horseService.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 = horseService.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 will return the new horse")
    public void updatingHorse_incorrectParameters_shouldThrowValidation() {
        // Create horse
        Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 4, Date.valueOf("2020-01-01"), ERace.APPALOOSA, "files/test.png", null);
        Horse savedHorse = horseService.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);
        assertThrows(ValidationException.class, () -> horseService.updateHorse(newHorse));
    }
90
}