Blame view

src/test/java/fr/plil/sio/persistence/jpa/RightServiceTest.java 3.94 KB
fd5e74ca   jcartign   First student ver...
1
2
3
4
  package fr.plil.sio.persistence.jpa;
  
  import fr.plil.sio.persistence.api.Right;
  import fr.plil.sio.persistence.api.RightService;
01d94034   rvangrev   Merge branch 'mas...
5
  import javax.transaction.Transactional;
fd5e74ca   jcartign   First student ver...
6
7
8
9
10
11
12
13
14
15
  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 static org.junit.Assert.*;
  
  @RunWith(SpringJUnit4ClassRunner.class)
  @SpringBootTest
01d94034   rvangrev   Merge branch 'mas...
16
  @Transactional
fd5e74ca   jcartign   First student ver...
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
  public class RightServiceTest {
  
      @Autowired
      private RightService rightService;
  
      @Test
      public void testCreateParentRightAndFindOne() {
          rightService.create("right");
          assertEquals(1, rightService.findByName("right").size());
      }
  
      @Test
      public void testCreateTwoParentRightsWithSameNameAndFindByName() {
          rightService.create("right");
          rightService.create("right");
          assertEquals(2, rightService.findByName("right").size());
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testCreateFailsIfNameNull() {
          rightService.create(null);
      }
  
      @Test
      public void testCreateSiblingRightAndFindOne() {
          Right parent = rightService.create("parent");
          rightService.create("sibling", parent);
          assertEquals(1, rightService.findByName("sibling").size());
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testCreateSiblingFailsIfNameNull() {
          Right parent = rightService.create("parent");
          rightService.create(null, parent);
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testCreateSiblingFailsIfParentNull() {
          rightService.create("parent");
          rightService.create("sibling", null);
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testCreateSiblingFailsIfParentNotInDatabase() {
          Right right = new Right();
          right.setName("not-a-right");
          rightService.create("sibling", right);
      }
  
      @Test
      public void testDeleteParentRight() {
          Right right = rightService.create("right");
          assertEquals(1, rightService.findByName("right").size());
          rightService.delete(right);
          assertEquals(0, rightService.findByName("right").size());
      }
  
      @Test
      public void testDeleteSiblingRight() {
          Right parent = rightService.create("parent");
          Right sibling = rightService.create("sibling", parent);
          rightService.delete(sibling);
          assertEquals(0, rightService.findByName("sibling").size());
      }
  
      @Test
      public void testDeleteParentAndSiblingRights() {
          Right parent = rightService.create("parent");
          rightService.create("sibling", parent);
          rightService.delete(parent);
          assertEquals(0, rightService.findByName("sibling").size());
          assertEquals(0, rightService.findByName("parent").size());
      }
  
d41edfbc   jcartign   Migrate modificat...
91
92
      @Test(expected = IllegalArgumentException.class)
      public void testDeleteRightIfNotInDatabase() {
fd5e74ca   jcartign   First student ver...
93
94
          Right right = new Right();
          right.setName("not-a-right");
62ed607d   jcartign   Porting update fr...
95
          rightService.delete(right);
fd5e74ca   jcartign   First student ver...
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testDeleteRightFailsIfRightNull() {
          rightService.delete(null);
      }
  
      @Test
      public void testFindByNameIfNameNotFound() {
          assertEquals(0, rightService.findByName("no").size());
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testFindByNameFailsIfNameNull() {
          rightService.findByName(null);
      }
  
      @Test
      public void testFindOne() {
          Right right = rightService.create("right");
          assertNotNull(rightService.findOne(right.getId()));
      }
  
      @Test
      public void testFindOneIfIdNotFound() {
          assertNull(rightService.findOne(153463167809232L));
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testFindOneFailsIfIdNull() {
          rightService.findOne(null);
      }
  }