HorseServiceTest.java 6.58 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
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;

17
import java.io.IOException;
18
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 28 29 30 31 32 33 34

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() {
35
        Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 4, Date.valueOf("2020-01-01"), ERace.APPALOOSA, "files/test.png", null);
36 37 38 39 40 41 42
        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() {
43
        Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 80, Date.valueOf("2020-01-01"), null, null, null);
44 45
        assertThrows(ValidationException.class, () -> horseService.addHorse(newHorse));
    }
46 47 48

    @Test
    @DisplayName("Updating a horse with the correct parameters will return the new horse")
49
    public void updatingHorse_correctParameters_shouldReturnHorse() throws IOException {
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 90 91 92 93
        // 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));
    }
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 135 136 137 138 139

    @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 = horseService.addHorse(new Horse("Zephyr", "Nice horse", (short) 4, Date.valueOf("2020-01-01"), ERace.APPALOOSA, "files/test.png", Long.valueOf(0)));
        Horse secondHorse = horseService.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 = horseService.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 = horseService.addHorse(new Horse("Zephyr", "Nice horse", (short) 4, Date.valueOf("2020-01-01"), ERace.APPALOOSA, "files/test.png", Long.valueOf(0)));
        Horse secondHorse = horseService.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 = horseService.getFiltered(filters);
        assertTrue(allHorses.contains(firstHorse));
    }

    @Test
    @DisplayName("Searching all horses with incorrect filters given no horses should throw ValidationException")
    public void searchingHorses_withIncorrectFiltersGivenNoHorses_shouldThrowValidation() {
        // Test for exception
        Map<String, String> filters = new HashMap<String, String>();
        filters.put("name", "Tester Horse");
        filters.put("description", "A horse for testing");
        filters.put("score", "10");
        filters.put("birthday", "test");
        filters.put("race", "yes");

        assertThrows(ValidationException.class, () -> horseService.getFiltered(filters));
    }
140
}