Blame view

src/test/java/fr/plil/sio/persistence/jdbc/GroupServiceTest.java 4.61 KB
a800fde0   jcartign   First version, in...
1
2
  package fr.plil.sio.persistence.jdbc;
  
292aec6f   jcartign   First version bef...
3
  import fr.plil.sio.persistence.api.*;
7940aa22   jcartign   Finishing API jav...
4
  import org.junit.Before;
a800fde0   jcartign   First version, in...
5
6
  import org.junit.Test;
  import org.junit.runner.RunWith;
7940aa22   jcartign   Finishing API jav...
7
  import org.springframework.beans.factory.annotation.Autowired;
5b26096d   jcartign   Switch to @Spring...
8
  import org.springframework.boot.test.context.SpringBootTest;
a800fde0   jcartign   First version, in...
9
10
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  
7940aa22   jcartign   Finishing API jav...
11
12
  import static org.junit.Assert.*;
  
a800fde0   jcartign   First version, in...
13
  @RunWith(SpringJUnit4ClassRunner.class)
29f04eb8   jcartign   Parameter "classe...
14
  @SpringBootTest
292aec6f   jcartign   First version bef...
15
  public class GroupServiceTest extends AbstractServiceSupport {
a800fde0   jcartign   First version, in...
16
  
7940aa22   jcartign   Finishing API jav...
17
18
19
20
21
22
      @Autowired
      private GroupService groupService;
  
      @Autowired
      private UserService userService;
  
292aec6f   jcartign   First version bef...
23
24
25
26
27
      @Autowired
      private RightService rightService;
  
      private Right parent;
  
7940aa22   jcartign   Finishing API jav...
28
29
      @Before
      public void before() {
292aec6f   jcartign   First version bef...
30
31
          parent = rightService.create("parent");
          rightService.create("sibling", parent);
7940aa22   jcartign   Finishing API jav...
32
33
34
          groupService.create("group");
      }
  
bda41d02   jcartign   All tests method ...
35
36
      @Test
      public void testCreateGroupAndFindByName() {
7940aa22   jcartign   Finishing API jav...
37
38
          Group group = groupService.findByName("group");
          assertEquals("group", group.getName());
bda41d02   jcartign   All tests method ...
39
      }
a800fde0   jcartign   First version, in...
40
  
bda41d02   jcartign   All tests method ...
41
42
      @Test(expected = IllegalArgumentException.class)
      public void testCreateGroupFailsWhenNameNull() {
7940aa22   jcartign   Finishing API jav...
43
          groupService.create(null);
bda41d02   jcartign   All tests method ...
44
45
46
47
      }
  
      @Test(expected = IllegalStateException.class)
      public void testCreateFailsWhenSameGroupUserAlreadyPresent() {
7940aa22   jcartign   Finishing API jav...
48
          groupService.create("group");
bda41d02   jcartign   All tests method ...
49
50
      }
  
77b66887   jcartign   Forgot some @Test...
51
      @Test
bda41d02   jcartign   All tests method ...
52
      public void testDeleteGroup() {
7940aa22   jcartign   Finishing API jav...
53
54
          assertTrue(groupService.delete("group"));
          assertNull(groupService.findByName("group"));
292aec6f   jcartign   First version bef...
55
          assertFalse(groupService.delete("group"));
bda41d02   jcartign   All tests method ...
56
57
      }
  
77b66887   jcartign   Forgot some @Test...
58
      @Test
292aec6f   jcartign   First version bef...
59
60
61
62
63
      public void testDeleteNotExistingGroup() {
          assertFalse(groupService.delete("not-a-group"));
      }
  
  
bda41d02   jcartign   All tests method ...
64
65
      @Test(expected = IllegalArgumentException.class)
      public void testDeleteGroupFailsIfNameNull() {
7940aa22   jcartign   Finishing API jav...
66
          groupService.delete(null);
bda41d02   jcartign   All tests method ...
67
      }
a800fde0   jcartign   First version, in...
68
69
  
      @Test
bda41d02   jcartign   All tests method ...
70
      public void deleteGroupDoesDeleteUsers() {
7940aa22   jcartign   Finishing API jav...
71
          userService.create("user1", "group");
e8b27d59   jcartign   Fix deleteGroupDo...
72
          userService.create("user2", "group");
7940aa22   jcartign   Finishing API jav...
73
74
75
76
77
          assertNotNull(userService.findByName("user1"));
          assertNotNull(userService.findByName("user2"));
          groupService.delete("group");
          assertNull(userService.findByName("user1"));
          assertNull(userService.findByName("user2"));
bda41d02   jcartign   All tests method ...
78
79
      }
  
292aec6f   jcartign   First version bef...
80
      public void testFindByNameIfGroupNotFound() {
7940aa22   jcartign   Finishing API jav...
81
          assertNull(groupService.findByName("unknown"));
bda41d02   jcartign   All tests method ...
82
83
84
85
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testFindByNameFailsIfNameNull() {
7940aa22   jcartign   Finishing API jav...
86
          groupService.findByName(null);
a800fde0   jcartign   First version, in...
87
88
89
      }
  
      @Test
bda41d02   jcartign   All tests method ...
90
      public void testAddRight() {
292aec6f   jcartign   First version bef...
91
92
93
94
95
96
          assertTrue(groupService.addRight("group", parent));
          Group group = groupService.findByName("group");
          assertEquals(1, group.getRights().size());
          assertEquals("parent", group.getRights().get(0).getName());
          assertEquals(1, group.getRights().get(0).getSiblings().size());
          assertEquals("sibling", group.getRights().get(0).getSiblings().iterator().next().getName());
a800fde0   jcartign   First version, in...
97
98
99
      }
  
      @Test
bda41d02   jcartign   All tests method ...
100
      public void testAddRightIfAlreadyPresent() {
292aec6f   jcartign   First version bef...
101
102
          assertTrue(groupService.addRight("group", parent));
          assertFalse(groupService.addRight("group", parent));
a800fde0   jcartign   First version, in...
103
104
      }
  
bda41d02   jcartign   All tests method ...
105
106
      @Test(expected = IllegalArgumentException.class)
      public void testAddRightFailsIfGroupNameNull() {
292aec6f   jcartign   First version bef...
107
          groupService.addRight(null, parent);
bda41d02   jcartign   All tests method ...
108
109
110
111
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testAddRightFailsIfRightNull() {
292aec6f   jcartign   First version bef...
112
          groupService.addRight("group", null);
bda41d02   jcartign   All tests method ...
113
114
115
116
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testAddRightFailsIfGroupNotInDatabase() {
292aec6f   jcartign   First version bef...
117
          groupService.addRight("not-a-group", null);
bda41d02   jcartign   All tests method ...
118
119
      }
  
bda41d02   jcartign   All tests method ...
120
121
      @Test
      public void testRemoveRight() {
292aec6f   jcartign   First version bef...
122
123
124
125
126
127
          assertTrue(groupService.addRight("group", parent));
          Group group = groupService.findByName("group");
          assertEquals(1, group.getRights().size());
          assertTrue(groupService.removeRight("group", parent));
          group = groupService.findByName("group");
          assertEquals(0, group.getRights().size());
bda41d02   jcartign   All tests method ...
128
129
130
131
      }
  
      @Test
      public void testRemoveRightIfNotPresent() {
292aec6f   jcartign   First version bef...
132
133
134
          Right right = new Right();
          right.setName("not-a-right");
          assertFalse(groupService.removeRight("group", right));
bda41d02   jcartign   All tests method ...
135
136
137
138
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testRemoveRightFailsIfGroupNameNull() {
292aec6f   jcartign   First version bef...
139
          groupService.removeRight(null, parent);
bda41d02   jcartign   All tests method ...
140
141
142
143
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testRemoveRightFailsIfRightNull() {
292aec6f   jcartign   First version bef...
144
          groupService.removeRight("group", null);
bda41d02   jcartign   All tests method ...
145
146
147
148
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testRemoveRightFailsIfGroupNotInDatabase() {
292aec6f   jcartign   First version bef...
149
          groupService.removeRight("not-a-group", null);
bda41d02   jcartign   All tests method ...
150
      }
a800fde0   jcartign   First version, in...
151
  }