18
18
*/
19
19
package com .redhat .lightblue .crud .ldap ;
20
20
21
+ import static com .redhat .lightblue .util .JsonUtils .json ;
21
22
import static com .redhat .lightblue .util .test .AbstractJsonNodeTest .loadJsonNode ;
23
+ import static com .redhat .lightblue .util .test .AbstractJsonNodeTest .loadResource ;
22
24
import static org .junit .Assert .assertEquals ;
23
25
import static org .junit .Assert .assertNotNull ;
26
+ import static org .junit .Assert .assertNull ;
24
27
25
28
import java .io .IOException ;
29
+ import java .util .Arrays ;
26
30
31
+ import org .apache .commons .lang .StringUtils ;
27
32
import org .junit .AfterClass ;
28
33
import org .junit .BeforeClass ;
29
34
import org .junit .ClassRule ;
48
53
import com .redhat .lightblue .metadata .Metadata ;
49
54
import com .redhat .lightblue .mongo .test .MongoServerExternalResource ;
50
55
import com .redhat .lightblue .mongo .test .MongoServerExternalResource .InMemoryMongoServer ;
56
+ import com .redhat .lightblue .test .FakeClientIdentification ;
51
57
import com .redhat .lightblue .util .Error ;
58
+ import com .unboundid .ldap .sdk .Attribute ;
52
59
53
60
/**
54
- * <b>NOTE:</b> This test suite is intended to be run in a certain order. Selectivly running unit tests
61
+ * <b>NOTE:</b> This test suite is intended to be run in a certain order. Selectively running unit tests
55
62
* may produce unwanted results.
56
63
*
57
- * @author dcrissma
64
+ * @author dcrissman
58
65
*/
59
66
@ InMemoryLdapServer
60
67
@ InMemoryMongoServer
61
68
@ FixMethodOrder (MethodSorters .NAME_ASCENDING )
62
69
public class ITCaseLdapCRUDControllerTest {
63
70
71
+ private static final String BASEDB_USERS = "ou=Users,dc=example,dc=com" ;
72
+ private static final String BASEDB_DEPARTMENTS = "ou=Departments,dc=example,dc=com" ;
73
+
64
74
@ ClassRule
65
75
public static LdapServerExternalResource ldapServer = LdapServerExternalResource .createDefaultInstance ();
66
76
@@ -74,10 +84,20 @@ public class ITCaseLdapCRUDControllerTest{
74
84
75
85
@ BeforeClass
76
86
public static void beforeClass () throws Exception {
87
+ ldapServer .add ("ou=Users,dc=example,dc=com" , new Attribute []{
88
+ new Attribute ("objectClass" , "top" ),
89
+ new Attribute ("objectClass" , "organizationalUnit" ),
90
+ new Attribute ("ou" , "Users" )});
91
+ ldapServer .add ("ou=Departments,dc=example,dc=com" , new Attribute []{
92
+ new Attribute ("objectClass" , "top" ),
93
+ new Attribute ("objectClass" , "organizationalUnit" ),
94
+ new Attribute ("ou" , "Departments" )});
95
+
77
96
System .setProperty ("ldap.host" , "localhost" );
78
97
System .setProperty ("ldap.port" , String .valueOf (LdapServerExternalResource .DEFAULT_PORT ));
79
98
System .setProperty ("ldap.database" , "test" );
80
- System .setProperty ("ldap.person.basedn" , "dc=example,dc=com" );
99
+ System .setProperty ("ldap.person.basedn" , BASEDB_USERS );
100
+ System .setProperty ("ldap.department.basedn" , BASEDB_DEPARTMENTS );
81
101
82
102
System .setProperty ("mongo.host" , "localhost" );
83
103
System .setProperty ("mongo.port" , String .valueOf (MongoServerExternalResource .DEFAULT_PORT ));
@@ -90,6 +110,7 @@ public static void beforeClass() throws Exception {
90
110
91
111
Metadata metadata = lightblueFactory .getMetadata ();
92
112
metadata .createNewMetadata (tx .parse (EntityMetadata .class , loadJsonNode ("./metadata/person-metadata.json" )));
113
+ metadata .createNewMetadata (tx .parse (EntityMetadata .class , loadJsonNode ("./metadata/department-metadata.json" )));
93
114
}
94
115
95
116
@ AfterClass
@@ -103,84 +124,186 @@ private void assertNoErrors(Response response){
103
124
e .printStackTrace ();
104
125
collector .addError (e );
105
126
}
127
+ }
106
128
129
+ private void assertNoDataErrors (Response response ){
107
130
for (DataError error : response .getDataErrors ()){
108
- Exception e = new Exception ("DataError: " + error .toJson ().asText ());
131
+ Exception e = new Exception ("DataError: " + error .toJson ().toString ());
109
132
e .printStackTrace ();
110
133
collector .addError (e );
111
134
}
112
135
}
113
136
114
- private <T > T createRequest (Class <T > type , String jsonFile ) throws IOException {
137
+ private <T > T createRequest_FromResource (Class <T > type , String jsonFile ) throws IOException {
138
+ return createRequest (type , loadJsonNode (jsonFile ));
139
+ }
140
+
141
+ private <T > T createRequest_FromJsonString (Class <T > type , String jsonString ) throws IOException {
142
+ return createRequest (type , json (jsonString ));
143
+ }
144
+
145
+ private <T > T createRequest (Class <T > type , JsonNode node ) throws IOException {
115
146
JsonTranslator tx = lightblueFactory .getJsonTranslator ();
116
- return tx .parse (type , loadJsonNode ( jsonFile ) );
147
+ return tx .parse (type , node );
117
148
}
118
149
119
150
@ Test
120
- public void step1Insert () throws Exception {
151
+ public void series1_phase1_Person_Insert () throws Exception {
121
152
Response response = lightblueFactory .getMediator ().insert (
122
- createRequest (InsertionRequest .class , "./crud/insert/person-insert-many.json" ));
153
+ createRequest_FromResource (InsertionRequest .class , "./crud/insert/person-insert-many.json" ));
123
154
124
155
assertNotNull (response );
125
156
assertNoErrors (response );
157
+ assertNoDataErrors (response );
126
158
assertEquals (4 , response .getModifiedCount ());
127
159
128
160
JsonNode entityData = response .getEntityData ();
129
161
assertNotNull (entityData );
130
162
JSONAssert .assertEquals (
131
- "[{\" dn\" :\" uid=junior.doe,dc=example,dc=com \" },{\" dn\" :\" uid=john.doe,dc=example,dc=com \" },{\" dn\" :\" uid=jane.doe,dc=example,dc=com \" },{\" dn\" :\" uid=jack.buck,dc=example,dc=com \" }]" ,
163
+ "[{\" dn\" :\" uid=junior.doe," + BASEDB_USERS + " \" },{\" dn\" :\" uid=john.doe," + BASEDB_USERS + " \" },{\" dn\" :\" uid=jane.doe," + BASEDB_USERS + " \" },{\" dn\" :\" uid=jack.buck," + BASEDB_USERS + " \" }]" ,
132
164
entityData .toString (), false );
133
165
}
134
166
135
167
@ Test
136
- public void step2FindSingle () throws Exception {
168
+ public void series1_phase2_Person_FindSingle () throws Exception {
137
169
Response response = lightblueFactory .getMediator ().find (
138
- createRequest (FindRequest .class , "./crud/find/person-find-single.json" ));
170
+ createRequest_FromResource (FindRequest .class , "./crud/find/person-find-single.json" ));
139
171
140
172
assertNotNull (response );
141
173
assertNoErrors (response );
174
+ assertNoDataErrors (response );
142
175
assertEquals (1 , response .getMatchCount ());
143
176
144
177
JsonNode entityData = response .getEntityData ();
145
178
assertNotNull (entityData );
146
179
JSONAssert .assertEquals (
147
- "[{\" dn\" :\" uid=john.doe,dc=example,dc=com \" ,\" uid\" :\" john.doe\" ,\" objectType\" :\" person\" ,\" objectClass#\" :4}]" ,
148
- entityData .toString (), false );
180
+ "[{\" dn\" :\" uid=john.doe," + BASEDB_USERS + " \" ,\" uid\" :\" john.doe\" ,\" objectType\" :\" person\" ,\" objectClass#\" :4}]" ,
181
+ entityData .toString (), true );
149
182
}
150
183
151
184
@ Test
152
- public void step2FindMany () throws Exception {
185
+ public void series1_phase2_Person_FindMany () throws Exception {
153
186
Response response = lightblueFactory .getMediator ().find (
154
- createRequest (FindRequest .class , "./crud/find/person-find-many.json" ));
187
+ createRequest_FromResource (FindRequest .class , "./crud/find/person-find-many.json" ));
155
188
156
189
assertNotNull (response );
157
190
assertNoErrors (response );
191
+ assertNoDataErrors (response );
158
192
assertEquals (3 , response .getMatchCount ());
159
193
160
194
JsonNode entityData = response .getEntityData ();
161
195
assertNotNull (entityData );
162
196
163
197
//Search requests results in desc order, strict mode is enforced to assure this.
164
198
JSONAssert .assertEquals (
165
- "[{\" dn\" :\" uid=junior.doe,dc=example,dc=com \" },{\" dn\" :\" uid=john.doe,dc=example,dc=com \" },{\" dn\" :\" uid=jane.doe,dc=example,dc=com \" }]" ,
199
+ "[{\" dn\" :\" uid=junior.doe," + BASEDB_USERS + " \" },{\" dn\" :\" uid=john.doe," + BASEDB_USERS + " \" },{\" dn\" :\" uid=jane.doe," + BASEDB_USERS + " \" }]" ,
166
200
entityData .toString (), true );
167
201
}
168
202
169
203
@ Test
170
- public void step2FindMany_WithPagination () throws Exception {
204
+ public void series1_phase2_Person_FindMany_WithPagination () throws Exception {
171
205
Response response = lightblueFactory .getMediator ().find (
172
- createRequest (FindRequest .class , "./crud/find/person-find-many-paginated.json" ));
206
+ createRequest_FromResource (FindRequest .class , "./crud/find/person-find-many-paginated.json" ));
173
207
174
208
assertNotNull (response );
175
209
assertNoErrors (response );
210
+ assertNoDataErrors (response );
176
211
assertEquals (1 , response .getMatchCount ());
177
212
178
213
JsonNode entityData = response .getEntityData ();
179
214
assertNotNull (entityData );
180
215
181
216
JSONAssert .assertEquals (
182
- "[{\" dn\" :\" uid=john.doe,dc=example,dc=com\" }]" ,
183
- entityData .toString (), false );
217
+ "[{\" dn\" :\" uid=john.doe," + BASEDB_USERS + "\" }]" ,
218
+ entityData .toString (), true );
219
+ }
220
+
221
+ @ Test
222
+ public void series2_phase1_Department_InsertWithRoles () throws Exception {
223
+ String insert = loadResource ("./crud/insert/department-insert-template.json" )
224
+ .replaceFirst ("#cn" , "Marketing" )
225
+ .replaceFirst ("#description" , "Department devoted to Marketing" )
226
+ .replaceFirst ("#members" , "\" " + StringUtils .join (Arrays .asList ("cn=John Doe," + BASEDB_USERS , "cn=Jane Doe," + BASEDB_USERS ), "\" ,\" " ) + "\" " );
227
+
228
+ InsertionRequest insertRequest = createRequest_FromJsonString (InsertionRequest .class , insert );
229
+ insertRequest .setClientId (new FakeClientIdentification ("fakeUser" , "admin" ));
230
+
231
+ Response response = lightblueFactory .getMediator ().insert (insertRequest );
232
+
233
+ assertNotNull (response );
234
+ assertNoErrors (response );
235
+ assertNoDataErrors (response );
236
+ assertEquals (1 , response .getModifiedCount ());
237
+
238
+ JsonNode entityData = response .getEntityData ();
239
+ assertNotNull (entityData );
240
+ JSONAssert .assertEquals (
241
+ "[{\" dn\" :\" cn=Marketing," + BASEDB_DEPARTMENTS + "\" }]" ,
242
+ entityData .toString (), true );
243
+ }
244
+
245
+ @ Test
246
+ public void series2_phase1_Department_InsertWithInvalidRoles () throws Exception {
247
+ String insert = loadResource ("./crud/insert/department-insert-template.json" )
248
+ .replaceFirst ("#cn" , "HR" )
249
+ .replaceFirst ("#description" , "Department devoted to HR" )
250
+ .replaceFirst ("#members" , "\" cn=John Doe," + BASEDB_USERS + "\" " );
251
+
252
+ InsertionRequest insertRequest = createRequest_FromJsonString (InsertionRequest .class , insert );
253
+ insertRequest .setClientId (new FakeClientIdentification ("fakeUser" ));
254
+
255
+ Response response = lightblueFactory .getMediator ().insert (insertRequest );
256
+
257
+ assertNotNull (response );
258
+ assertEquals (0 , response .getModifiedCount ());
259
+
260
+ assertNull (response .getEntityData ());
261
+
262
+ assertNoErrors (response );
263
+ assertEquals (1 , response .getDataErrors ().size ());
264
+ JSONAssert .assertEquals ("{\" errors\" :[{\" errorCode\" :\" crud:insert:NoFieldAccess\" ,\" msg\" :\" member\" }]}" ,
265
+ response .getDataErrors ().get (0 ).toJson ().toString (), false );
266
+ }
267
+
268
+ @ Test
269
+ public void series2_phase2_Department_FindWithRoles () throws Exception {
270
+ FindRequest findRequest = createRequest_FromResource (FindRequest .class , "./crud/find/department-find-single.json" );
271
+ findRequest .setClientId (new FakeClientIdentification ("fakeUser" , "admin" ));
272
+
273
+ Response response = lightblueFactory .getMediator ().find (findRequest );
274
+
275
+ assertNotNull (response );
276
+ assertNoErrors (response );
277
+ assertNoDataErrors (response );
278
+ assertEquals (1 , response .getMatchCount ());
279
+
280
+ JsonNode entityData = response .getEntityData ();
281
+ assertNotNull (entityData );
282
+ JSONAssert .assertEquals (
283
+ "[{\" member#\" :2,\" member\" :[\" cn=John Doe," + BASEDB_USERS + "\" ,\" cn=Jane Doe," + BASEDB_USERS + "\" ],\" cn\" :\" Marketing\" ,\" description\" :\" Department devoted to Marketing\" }]" ,
284
+ entityData .toString (), true );
285
+ }
286
+
287
+ @ Test
288
+ public void series2_phase2_Department_FindWithInsufficientRoles () throws Exception {
289
+ FindRequest findRequest = createRequest_FromResource (FindRequest .class , "./crud/find/department-find-single.json" );
290
+ findRequest .setClientId (new FakeClientIdentification ("fakeUser" ));
291
+
292
+ Response response = lightblueFactory .getMediator ().find (findRequest );
293
+
294
+ assertNotNull (response );
295
+ assertEquals (1 , response .getMatchCount ());
296
+
297
+ assertNoErrors (response );
298
+ assertNoDataErrors (response );
299
+
300
+ assertNotNull (response .getEntityData ());
301
+ JsonNode entityData = response .getEntityData ();
302
+ assertNotNull (entityData );
303
+
304
+ JSONAssert .assertEquals (
305
+ "[{\" cn\" :\" Marketing\" ,\" description\" :\" Department devoted to Marketing\" }]" ,
306
+ entityData .toString (), true );
184
307
}
185
308
186
309
}
0 commit comments