Blame view

src/test/java/fr/plil/sio/persistence/jdbc/GroupServiceTest.java 4.65 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;
a800fde0   jcartign   First version, in...
8
9
10
  import org.springframework.boot.test.SpringApplicationConfiguration;
  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
14
  @RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = JdbcApplication.class)
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
51
      }
  
      public void testDeleteGroup() {
7940aa22   jcartign   Finishing API jav...
52
53
          assertTrue(groupService.delete("group"));
          assertNull(groupService.findByName("group"));
292aec6f   jcartign   First version bef...
54
          assertFalse(groupService.delete("group"));
bda41d02   jcartign   All tests method ...
55
56
      }
  
292aec6f   jcartign   First version bef...
57
58
59
60
61
      public void testDeleteNotExistingGroup() {
          assertFalse(groupService.delete("not-a-group"));
      }
  
  
bda41d02   jcartign   All tests method ...
62
63
      @Test(expected = IllegalArgumentException.class)
      public void testDeleteGroupFailsIfNameNull() {
7940aa22   jcartign   Finishing API jav...
64
          groupService.delete(null);
bda41d02   jcartign   All tests method ...
65
      }
a800fde0   jcartign   First version, in...
66
67
  
      @Test
bda41d02   jcartign   All tests method ...
68
      public void deleteGroupDoesDeleteUsers() {
7940aa22   jcartign   Finishing API jav...
69
70
71
72
73
74
75
          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 ...
76
77
      }
  
292aec6f   jcartign   First version bef...
78
      public void testFindByNameIfGroupNotFound() {
7940aa22   jcartign   Finishing API jav...
79
          assertNull(groupService.findByName("unknown"));
bda41d02   jcartign   All tests method ...
80
81
82
83
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testFindByNameFailsIfNameNull() {
7940aa22   jcartign   Finishing API jav...
84
          groupService.findByName(null);
a800fde0   jcartign   First version, in...
85
86
87
      }
  
      @Test
bda41d02   jcartign   All tests method ...
88
      public void testAddRight() {
292aec6f   jcartign   First version bef...
89
90
91
92
93
94
          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...
95
96
97
      }
  
      @Test
bda41d02   jcartign   All tests method ...
98
      public void testAddRightIfAlreadyPresent() {
292aec6f   jcartign   First version bef...
99
100
          assertTrue(groupService.addRight("group", parent));
          assertFalse(groupService.addRight("group", parent));
a800fde0   jcartign   First version, in...
101
102
      }
  
bda41d02   jcartign   All tests method ...
103
104
      @Test(expected = IllegalArgumentException.class)
      public void testAddRightFailsIfGroupNameNull() {
292aec6f   jcartign   First version bef...
105
          groupService.addRight(null, parent);
bda41d02   jcartign   All tests method ...
106
107
108
109
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testAddRightFailsIfRightNull() {
292aec6f   jcartign   First version bef...
110
          groupService.addRight("group", null);
bda41d02   jcartign   All tests method ...
111
112
113
114
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testAddRightFailsIfGroupNotInDatabase() {
292aec6f   jcartign   First version bef...
115
          groupService.addRight("not-a-group", null);
bda41d02   jcartign   All tests method ...
116
117
      }
  
bda41d02   jcartign   All tests method ...
118
119
      @Test
      public void testRemoveRight() {
292aec6f   jcartign   First version bef...
120
121
122
123
124
125
          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 ...
126
127
128
129
      }
  
      @Test
      public void testRemoveRightIfNotPresent() {
292aec6f   jcartign   First version bef...
130
131
132
          Right right = new Right();
          right.setName("not-a-right");
          assertFalse(groupService.removeRight("group", right));
bda41d02   jcartign   All tests method ...
133
134
135
136
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testRemoveRightFailsIfGroupNameNull() {
292aec6f   jcartign   First version bef...
137
          groupService.removeRight(null, parent);
bda41d02   jcartign   All tests method ...
138
139
140
141
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testRemoveRightFailsIfRightNull() {
292aec6f   jcartign   First version bef...
142
          groupService.removeRight("group", null);
bda41d02   jcartign   All tests method ...
143
144
145
146
      }
  
      @Test(expected = IllegalArgumentException.class)
      public void testRemoveRightFailsIfGroupNotInDatabase() {
292aec6f   jcartign   First version bef...
147
          groupService.removeRight("not-a-group", null);
bda41d02   jcartign   All tests method ...
148
      }
a800fde0   jcartign   First version, in...
149
  }