Blame view

src/test/java/fr/plil/sio/persistence/jdbc/GroupServiceTest.java 4.62 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
      }
  
8882a9c3   rvangrev   Premières modific...
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
      }
  
8882a9c3   rvangrev   Premières modific...
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
72
73
74
75
76
77
          userService.create("user1", "group");
          userService.create("user1", "group");
          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
      }
  
8882a9c3   rvangrev   Premières modific...
80
      @Test
292aec6f   jcartign   First version bef...
81
      public void testFindByNameIfGroupNotFound() {
7940aa22   jcartign   Finishing API jav...
82
          assertNull(groupService.findByName("unknown"));
bda41d02   jcartign   All tests method ...
83
84
85
86
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testFindByNameFailsIfNameNull() {
7940aa22   jcartign   Finishing API jav...
87
          groupService.findByName(null);
a800fde0   jcartign   First version, in...
88
89
90
      }
  
      @Test
bda41d02   jcartign   All tests method ...
91
      public void testAddRight() {
292aec6f   jcartign   First version bef...
92
93
94
95
96
97
          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...
98
99
100
      }
  
      @Test
bda41d02   jcartign   All tests method ...
101
      public void testAddRightIfAlreadyPresent() {
292aec6f   jcartign   First version bef...
102
103
          assertTrue(groupService.addRight("group", parent));
          assertFalse(groupService.addRight("group", parent));
a800fde0   jcartign   First version, in...
104
105
      }
  
bda41d02   jcartign   All tests method ...
106
107
      @Test(expected = IllegalArgumentException.class)
      public void testAddRightFailsIfGroupNameNull() {
292aec6f   jcartign   First version bef...
108
          groupService.addRight(null, parent);
bda41d02   jcartign   All tests method ...
109
110
111
112
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testAddRightFailsIfRightNull() {
292aec6f   jcartign   First version bef...
113
          groupService.addRight("group", null);
bda41d02   jcartign   All tests method ...
114
115
116
117
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testAddRightFailsIfGroupNotInDatabase() {
292aec6f   jcartign   First version bef...
118
          groupService.addRight("not-a-group", null);
bda41d02   jcartign   All tests method ...
119
120
      }
  
bda41d02   jcartign   All tests method ...
121
122
      @Test
      public void testRemoveRight() {
292aec6f   jcartign   First version bef...
123
124
125
126
127
128
          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 ...
129
130
131
132
      }
  
      @Test
      public void testRemoveRightIfNotPresent() {
292aec6f   jcartign   First version bef...
133
134
135
          Right right = new Right();
          right.setName("not-a-right");
          assertFalse(groupService.removeRight("group", right));
bda41d02   jcartign   All tests method ...
136
137
138
139
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testRemoveRightFailsIfGroupNameNull() {
292aec6f   jcartign   First version bef...
140
          groupService.removeRight(null, parent);
bda41d02   jcartign   All tests method ...
141
142
143
144
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testRemoveRightFailsIfRightNull() {
292aec6f   jcartign   First version bef...
145
          groupService.removeRight("group", null);
bda41d02   jcartign   All tests method ...
146
147
148
149
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testRemoveRightFailsIfGroupNotInDatabase() {
292aec6f   jcartign   First version bef...
150
          groupService.removeRight("not-a-group", null);
bda41d02   jcartign   All tests method ...
151
      }
a800fde0   jcartign   First version, in...
152
  }