Commit 9e5908e7995e5f0e32d72b0df4de7cabda5ed219
1 parent
bba62e85
Dernières modifications de fin de TP.
Showing
1 changed file
with
59 additions
and
0 deletions
Show diff stats
src/test/java/fr/plil/sio/examen/AnimalRepositoryTest.java
1 | 1 | package fr.plil.sio.examen; |
2 | 2 | |
3 | 3 | import fr.plil.sio.examen.api.Animal; |
4 | +import fr.plil.sio.examen.api.Comment; | |
4 | 5 | import fr.plil.sio.examen.api.Owner; |
5 | 6 | import fr.plil.sio.examen.repositories.AnimalRepository; |
7 | +import fr.plil.sio.examen.repositories.CommentRepository; | |
6 | 8 | import fr.plil.sio.examen.repositories.OwnerRepository; |
9 | +import org.junit.Assert; | |
7 | 10 | import org.junit.Test; |
8 | 11 | import org.junit.runner.RunWith; |
9 | 12 | import org.springframework.beans.factory.annotation.Autowired; |
... | ... | @@ -11,6 +14,8 @@ import org.springframework.boot.test.context.SpringBootTest; |
11 | 14 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
12 | 15 | import org.springframework.transaction.annotation.Transactional; |
13 | 16 | |
17 | +import static org.junit.Assert.*; | |
18 | + | |
14 | 19 | @RunWith(SpringJUnit4ClassRunner.class) |
15 | 20 | @SpringBootTest |
16 | 21 | @Transactional |
... | ... | @@ -22,6 +27,9 @@ public class AnimalRepositoryTest { |
22 | 27 | @Autowired |
23 | 28 | private OwnerRepository ownerRepository; |
24 | 29 | |
30 | + @Autowired | |
31 | + private CommentRepository commentRepository; | |
32 | + | |
25 | 33 | @Test |
26 | 34 | public void testSimpleOperations() { |
27 | 35 | Owner owner = new Owner(); |
... | ... | @@ -33,4 +41,55 @@ public class AnimalRepositoryTest { |
33 | 41 | owner.getAnimals().add(animal); |
34 | 42 | animalRepository.save(animal); |
35 | 43 | } |
44 | + | |
45 | + @Test | |
46 | + public void testIfAnimalOwnerNotNullWhenDeleteOwner(){ | |
47 | + Owner owner = new Owner();owner.setName("mickey"); | |
48 | + ownerRepository.save(owner); | |
49 | + Animal animal = new Animal(); | |
50 | + animal.setName("pluto"); | |
51 | + animal.setOwner(owner); | |
52 | + owner.getAnimals().add(animal); | |
53 | + animalRepository.save(animal); | |
54 | + ownerRepository.delete(owner); | |
55 | + assertNotNull(animal.getOwner()); | |
56 | + } | |
57 | + | |
58 | + @Test | |
59 | + public void testDeleteAnimal(){ | |
60 | + Owner owner = new Owner(); | |
61 | + owner.setName("mickey"); | |
62 | + ownerRepository.save(owner); | |
63 | + Animal animal = new Animal(); | |
64 | + animal.setName("pluto"); | |
65 | + animal.setOwner(owner); | |
66 | + assertEquals(0,owner.getAnimals().size()); | |
67 | + owner.getAnimals().add(animal); | |
68 | + animalRepository.save(animal); | |
69 | + assertEquals(1,owner.getAnimals().size()); | |
70 | + animalRepository.delete(animal); | |
71 | + assertEquals(animalRepository.findByName(animal.getName()).size(),0); | |
72 | + assertEquals(animalRepository.findByOwner(owner).size(),0); | |
73 | + } | |
74 | + | |
75 | + @Test | |
76 | + public void testDeleteReporterDoesDeleteComment(){ | |
77 | + Owner owner = new Owner(); | |
78 | + owner.setName("mickey"); | |
79 | + ownerRepository.save(owner); | |
80 | + Animal animal = new Animal(); | |
81 | + animal.setName("pluto"); | |
82 | + animal.setOwner(owner); | |
83 | + assertEquals(0,owner.getAnimals().size()); | |
84 | + owner.getAnimals().add(animal); | |
85 | + animalRepository.save(animal); | |
86 | + Comment comment = new Comment(); | |
87 | + comment.setAnimal(animal); | |
88 | + comment.setReporter(owner); | |
89 | + comment.setMessage("Commentaire"); | |
90 | + commentRepository.save(comment); | |
91 | + assertEquals(1,commentRepository.findByReporter(owner).size()); | |
92 | + ownerRepository.delete(owner); | |
93 | +// assertEquals(0,commentRepository.findByReporter(owner).size()); | |
94 | + } | |
36 | 95 | } | ... | ... |