Blame view

src/test/java/fr/plil/sio/examen/AnimalRepositoryTest.java 3.25 KB
589e1ad3   jcartign   First version
1
2
3
  package fr.plil.sio.examen;

  

  import fr.plil.sio.examen.api.Animal;

9e5908e7   rvangrev   Dernières modific...
4
  import fr.plil.sio.examen.api.Comment;

589e1ad3   jcartign   First version
5
6
  import fr.plil.sio.examen.api.Owner;

  import fr.plil.sio.examen.repositories.AnimalRepository;

9e5908e7   rvangrev   Dernières modific...
7
  import fr.plil.sio.examen.repositories.CommentRepository;

589e1ad3   jcartign   First version
8
  import fr.plil.sio.examen.repositories.OwnerRepository;

9e5908e7   rvangrev   Dernières modific...
9
  import org.junit.Assert;

589e1ad3   jcartign   First version
10
11
12
13
14
15
16
  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;

  

9e5908e7   rvangrev   Dernières modific...
17
18
  import static org.junit.Assert.*;

  

589e1ad3   jcartign   First version
19
20
21
22
23
24
25
26
27
28
29
  @RunWith(SpringJUnit4ClassRunner.class)

  @SpringBootTest

  @Transactional

  public class AnimalRepositoryTest {

      

      @Autowired

      private AnimalRepository animalRepository;

      

      @Autowired

      private OwnerRepository ownerRepository;

      

9e5908e7   rvangrev   Dernières modific...
30
31
32
      @Autowired

      private CommentRepository commentRepository;

      

589e1ad3   jcartign   First version
33
34
35
36
37
38
39
40
41
42
43
      @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);        

      }

9e5908e7   rvangrev   Dernières modific...
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
      

      @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());

      }

589e1ad3   jcartign   First version
95
  }