OwnerServiceTest.java 2.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
package at.ac.tuwien.sepm.assignment.individual.unit.service;

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

import at.ac.tuwien.sepm.assignment.individual.entity.Owner;
import at.ac.tuwien.sepm.assignment.individual.service.OwnerService;
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 static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith(SpringExtension.class)
@SpringBootTest
@ActiveProfiles("test")
public class OwnerServiceTest {
    @Autowired
    OwnerService ownerService;

    @Test
    @DisplayName("Adding a new owner with the correct parameters will return the new owner")
    public void addingNewOwner_correctParameters_shouldReturnOwner() {
        Owner newOwner = new Owner("Chad");
        Owner savedOwner = ownerService.addOwner(newOwner);
        assertEquals(newOwner, savedOwner);
    }

    @Test
    @DisplayName("Adding a new owner with the incorrect parameters will throw a ValidationException")
    public void addingNewOwner_incorrectParameters_shouldThrowValidation() {
        Owner newOwner = new Owner("");
        assertThrows(ValidationException.class, () -> ownerService.addOwner(newOwner));
    }
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

    @Test
    @DisplayName("Updating a owner with the correct parameters will return the new owner")
    public void updatingOwner_correctParameters_shouldReturnOwner() {
        // Create owner
        Owner newOwner = new Owner("Chad");
        Owner savedOwner = ownerService.addOwner(newOwner);

        // Update owner
        newOwner.setId(savedOwner.getId());
        newOwner.setName("Chad");
        Owner updatedOwner = ownerService.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 will return the new owner")
    public void updatingOwner_incorrectParameters_shouldThrowValidation() {
        // Create owner
        Owner newOwner = new Owner("Chad");
        Owner savedOwner = ownerService.addOwner(newOwner);

        // Update owner
        newOwner.setId(savedOwner.getId());
        newOwner.setName("");
        assertThrows(ValidationException.class, () -> ownerService.updateOwner(newOwner));
    }
70
}