AnimalRepositoryTest.java 3.25 KB
package fr.plil.sio.examen;

import fr.plil.sio.examen.api.Animal;
import fr.plil.sio.examen.api.Comment;
import fr.plil.sio.examen.api.Owner;
import fr.plil.sio.examen.repositories.AnimalRepository;
import fr.plil.sio.examen.repositories.CommentRepository;
import fr.plil.sio.examen.repositories.OwnerRepository;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import static org.junit.Assert.*;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@Transactional
public class AnimalRepositoryTest {
    
    @Autowired
    private AnimalRepository animalRepository;
    
    @Autowired
    private OwnerRepository ownerRepository;
    
    @Autowired
    private CommentRepository commentRepository;
    
    @Test
    public void testSimpleOperations() {
        Owner owner = new Owner();
        owner.setName("mickey");
        ownerRepository.save(owner);
        Animal animal = new Animal();
        animal.setName("pluto");
        animal.setOwner(owner);
        owner.getAnimals().add(animal);
        animalRepository.save(animal);        
    }
    
    @Test
    public void testIfAnimalOwnerNotNullWhenDeleteOwner(){
        Owner owner = new Owner();owner.setName("mickey");
        ownerRepository.save(owner);
        Animal animal = new Animal();
        animal.setName("pluto");
        animal.setOwner(owner);
        owner.getAnimals().add(animal);
        animalRepository.save(animal);
        ownerRepository.delete(owner);
        assertNotNull(animal.getOwner());
    }
    
    @Test 
    public void testDeleteAnimal(){
        Owner owner = new Owner();
        owner.setName("mickey");
        ownerRepository.save(owner);
        Animal animal = new Animal();
        animal.setName("pluto");
        animal.setOwner(owner);
        assertEquals(0,owner.getAnimals().size());
        owner.getAnimals().add(animal);
        animalRepository.save(animal);
        assertEquals(1,owner.getAnimals().size());
        animalRepository.delete(animal);
        assertEquals(animalRepository.findByName(animal.getName()).size(),0);
        assertEquals(animalRepository.findByOwner(owner).size(),0);
    }
    
    @Test 
    public void testDeleteReporterDoesDeleteComment(){
        Owner owner = new Owner();
        owner.setName("mickey");
        ownerRepository.save(owner);
        Animal animal = new Animal();
        animal.setName("pluto");
        animal.setOwner(owner);
        assertEquals(0,owner.getAnimals().size());
        owner.getAnimals().add(animal);
        animalRepository.save(animal);
        Comment comment = new Comment();
        comment.setAnimal(animal);
        comment.setReporter(owner);
        comment.setMessage("Commentaire");
        commentRepository.save(comment);
        assertEquals(1,commentRepository.findByReporter(owner).size());
        ownerRepository.delete(owner);
//        assertEquals(0,commentRepository.findByReporter(owner).size());
    }
}