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 | package fr.plil.sio.examen; | 1 | package fr.plil.sio.examen; |
2 | 2 | ||
3 | import fr.plil.sio.examen.api.Animal; | 3 | import fr.plil.sio.examen.api.Animal; |
4 | +import fr.plil.sio.examen.api.Comment; | ||
4 | import fr.plil.sio.examen.api.Owner; | 5 | import fr.plil.sio.examen.api.Owner; |
5 | import fr.plil.sio.examen.repositories.AnimalRepository; | 6 | import fr.plil.sio.examen.repositories.AnimalRepository; |
7 | +import fr.plil.sio.examen.repositories.CommentRepository; | ||
6 | import fr.plil.sio.examen.repositories.OwnerRepository; | 8 | import fr.plil.sio.examen.repositories.OwnerRepository; |
9 | +import org.junit.Assert; | ||
7 | import org.junit.Test; | 10 | import org.junit.Test; |
8 | import org.junit.runner.RunWith; | 11 | import org.junit.runner.RunWith; |
9 | import org.springframework.beans.factory.annotation.Autowired; | 12 | import org.springframework.beans.factory.annotation.Autowired; |
@@ -11,6 +14,8 @@ import org.springframework.boot.test.context.SpringBootTest; | @@ -11,6 +14,8 @@ import org.springframework.boot.test.context.SpringBootTest; | ||
11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | 14 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
12 | import org.springframework.transaction.annotation.Transactional; | 15 | import org.springframework.transaction.annotation.Transactional; |
13 | 16 | ||
17 | +import static org.junit.Assert.*; | ||
18 | + | ||
14 | @RunWith(SpringJUnit4ClassRunner.class) | 19 | @RunWith(SpringJUnit4ClassRunner.class) |
15 | @SpringBootTest | 20 | @SpringBootTest |
16 | @Transactional | 21 | @Transactional |
@@ -22,6 +27,9 @@ public class AnimalRepositoryTest { | @@ -22,6 +27,9 @@ public class AnimalRepositoryTest { | ||
22 | @Autowired | 27 | @Autowired |
23 | private OwnerRepository ownerRepository; | 28 | private OwnerRepository ownerRepository; |
24 | 29 | ||
30 | + @Autowired | ||
31 | + private CommentRepository commentRepository; | ||
32 | + | ||
25 | @Test | 33 | @Test |
26 | public void testSimpleOperations() { | 34 | public void testSimpleOperations() { |
27 | Owner owner = new Owner(); | 35 | Owner owner = new Owner(); |
@@ -33,4 +41,55 @@ public class AnimalRepositoryTest { | @@ -33,4 +41,55 @@ public class AnimalRepositoryTest { | ||
33 | owner.getAnimals().add(animal); | 41 | owner.getAnimals().add(animal); |
34 | animalRepository.save(animal); | 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 | } |