1
1
/*
2
- * Copyright 2002-2016 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
package org .springframework .security .acls .jdbc ;
17
17
18
- import static org .mockito .ArgumentMatchers .anyList ;
19
- import static org .mockito .Mockito .when ;
20
-
21
- import java .util .Arrays ;
22
- import java .util .HashMap ;
23
- import java .util .List ;
24
- import java .util .Map ;
25
-
26
- import javax .sql .DataSource ;
27
-
18
+ import org .junit .After ;
28
19
import org .junit .Before ;
29
20
import org .junit .Test ;
30
21
import org .junit .runner .RunWith ;
31
22
import org .mockito .Mock ;
32
23
import org .mockito .junit .MockitoJUnitRunner ;
24
+ import org .springframework .jdbc .core .JdbcOperations ;
25
+ import org .springframework .jdbc .core .RowMapper ;
26
+ import org .springframework .jdbc .datasource .embedded .EmbeddedDatabase ;
27
+ import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseBuilder ;
33
28
import org .springframework .security .acls .domain .ObjectIdentityImpl ;
34
29
import org .springframework .security .acls .domain .PrincipalSid ;
35
30
import org .springframework .security .acls .model .Acl ;
36
31
import org .springframework .security .acls .model .NotFoundException ;
37
32
import org .springframework .security .acls .model .ObjectIdentity ;
38
33
import org .springframework .security .acls .model .Sid ;
39
34
35
+ import javax .sql .DataSource ;
36
+ import java .util .*;
37
+
38
+ import static org .assertj .core .api .Assertions .assertThat ;
39
+ import static org .mockito .AdditionalMatchers .aryEq ;
40
+ import static org .mockito .ArgumentMatchers .*;
41
+ import static org .mockito .Mockito .when ;
42
+
43
+ /**
44
+ * Unit and Integration tests the ACL JdbcAclService using an
45
+ * in-memory database.
46
+ *
47
+ * @author Nena Raab
48
+ */
40
49
@ RunWith (MockitoJUnitRunner .class )
41
50
public class JdbcAclServiceTests {
51
+
52
+ private EmbeddedDatabase embeddedDatabase ;
53
+
42
54
@ Mock
43
55
private DataSource dataSource ;
44
56
45
57
@ Mock
46
58
private LookupStrategy lookupStrategy ;
47
59
60
+ @ Mock
61
+ JdbcOperations jdbcOperations ;
62
+
63
+ private JdbcAclService aclServiceIntegration ;
48
64
private JdbcAclService aclService ;
49
65
50
66
@ Before
51
67
public void setUp () {
52
- aclService = new JdbcAclService (dataSource , lookupStrategy );
68
+ aclService = new JdbcAclService (jdbcOperations , lookupStrategy );
69
+ aclServiceIntegration = new JdbcAclService (embeddedDatabase , lookupStrategy );
70
+ }
71
+
72
+ @ Before
73
+ public void setUpEmbeddedDatabase () {
74
+ embeddedDatabase = new EmbeddedDatabaseBuilder ()//
75
+ .addScript ("createAclSchemaWithAclClassIdType.sql" )
76
+ .addScript ("db/sql/test_data_hierarchy.sql" )
77
+ .build ();
78
+ }
79
+
80
+ @ After
81
+ public void tearDownEmbeddedDatabase () {
82
+ embeddedDatabase .shutdown ();
53
83
}
54
84
55
85
// SEC-1898
@@ -60,8 +90,110 @@ public void readAclByIdMissingAcl() {
60
90
lookupStrategy .readAclsById (anyList (),
61
91
anyList ())).thenReturn (result );
62
92
ObjectIdentity objectIdentity = new ObjectIdentityImpl (Object .class , 1 );
63
- List <Sid > sids = Arrays .<Sid > asList (new PrincipalSid ("user" ));
93
+ List <Sid > sids = Arrays .<Sid >asList (new PrincipalSid ("user" ));
64
94
65
95
aclService .readAclById (objectIdentity , sids );
66
96
}
97
+
98
+ @ Test
99
+ public void findOneChildren () {
100
+ List <ObjectIdentity > result = new ArrayList <>();
101
+ result .add (new ObjectIdentityImpl (Object .class , "5577" ));
102
+ Object [] args = {"1" , "org.springframework.security.acls.jdbc.JdbcAclServiceTests$MockLongIdDomainObject" };
103
+ when (
104
+ jdbcOperations .query (anyString (),
105
+ aryEq (args ), any (RowMapper .class ))).thenReturn (result );
106
+ ObjectIdentity objectIdentity = new ObjectIdentityImpl (MockLongIdDomainObject .class , 1L );
107
+
108
+ List <ObjectIdentity > objectIdentities = aclService .findChildren (objectIdentity );
109
+ assertThat (objectIdentities .size ()).isEqualTo (1 );
110
+ assertThat (objectIdentities .get (0 ).getIdentifier ()).isEqualTo ("5577" );
111
+ }
112
+
113
+ @ Test
114
+ public void findNoChildren () {
115
+ ObjectIdentity objectIdentity = new ObjectIdentityImpl (MockLongIdDomainObject .class , 1L );
116
+
117
+ List <ObjectIdentity > objectIdentities = aclService .findChildren (objectIdentity );
118
+ assertThat (objectIdentities ).isNull ();
119
+ }
120
+
121
+ // ~ Some integration tests
122
+ // ========================================================================================================
123
+
124
+ @ Test
125
+ public void findChildrenWithoutIdType () {
126
+ ObjectIdentity objectIdentity = new ObjectIdentityImpl (MockLongIdDomainObject .class , 4711L );
127
+
128
+ List <ObjectIdentity > objectIdentities = aclServiceIntegration .findChildren (objectIdentity );
129
+ assertThat (objectIdentities .size ()).isEqualTo (1 );
130
+ assertThat (objectIdentities .get (0 ).getType ()).isEqualTo (MockUntypedIdDomainObject .class .getName ());
131
+ assertThat (objectIdentities .get (0 ).getIdentifier ()).isEqualTo (5000L );
132
+ }
133
+
134
+ @ Test
135
+ public void findChildrenForUnknownObject () {
136
+ ObjectIdentity objectIdentity = new ObjectIdentityImpl (Object .class , 33 );
137
+
138
+ List <ObjectIdentity > objectIdentities = aclServiceIntegration .findChildren (objectIdentity );
139
+ assertThat (objectIdentities ).isNull ();
140
+ }
141
+
142
+ @ Test
143
+ public void findChildrenOfIdTypeLong () {
144
+ ObjectIdentity objectIdentity = new ObjectIdentityImpl ("location" , "US-PAL" );
145
+
146
+ List <ObjectIdentity > objectIdentities = aclServiceIntegration .findChildren (objectIdentity );
147
+ assertThat (objectIdentities .size ()).isEqualTo (2 );
148
+ assertThat (objectIdentities .get (0 ).getType ()).isEqualTo (MockLongIdDomainObject .class .getName ());
149
+ assertThat (objectIdentities .get (0 ).getIdentifier ()).isEqualTo (4711L );
150
+ assertThat (objectIdentities .get (1 ).getType ()).isEqualTo (MockLongIdDomainObject .class .getName ());
151
+ assertThat (objectIdentities .get (1 ).getIdentifier ()).isEqualTo (4712L );
152
+ }
153
+
154
+ @ Test
155
+ public void findChildrenOfIdTypeString () {
156
+ ObjectIdentity objectIdentity = new ObjectIdentityImpl ("location" , "US" );
157
+
158
+ aclServiceIntegration .setAclClassIdSupported (true );
159
+ List <ObjectIdentity > objectIdentities = aclServiceIntegration .findChildren (objectIdentity );
160
+ assertThat (objectIdentities .size ()).isEqualTo (1 );
161
+ assertThat (objectIdentities .get (0 ).getType ()).isEqualTo ("location" );
162
+ assertThat (objectIdentities .get (0 ).getIdentifier ()).isEqualTo ("US-PAL" );
163
+ }
164
+
165
+ @ Test
166
+ public void findChildrenOfIdTypeUUID () {
167
+ ObjectIdentity objectIdentity = new ObjectIdentityImpl (MockUntypedIdDomainObject .class , 5000L );
168
+
169
+ aclServiceIntegration .setAclClassIdSupported (true );
170
+ List <ObjectIdentity > objectIdentities = aclServiceIntegration .findChildren (objectIdentity );
171
+ assertThat (objectIdentities .size ()).isEqualTo (1 );
172
+ assertThat (objectIdentities .get (0 ).getType ()).isEqualTo ("costcenter" );
173
+ assertThat (objectIdentities .get (0 ).getIdentifier ()).isEqualTo (UUID .fromString ("25d93b3f-c3aa-4814-9d5e-c7c96ced7762" ));
174
+ }
175
+
176
+ private class MockLongIdDomainObject {
177
+ private Object id ;
178
+
179
+ public Object getId () {
180
+ return id ;
181
+ }
182
+
183
+ public void setId (Object id ) {
184
+ this .id = id ;
185
+ }
186
+ }
187
+
188
+ private class MockUntypedIdDomainObject {
189
+ private Object id ;
190
+
191
+ public Object getId () {
192
+ return id ;
193
+ }
194
+
195
+ public void setId (Object id ) {
196
+ this .id = id ;
197
+ }
198
+ }
67
199
}
0 commit comments