AnimalRepositoryTest.java
3.25 KB
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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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());
}
}