1- /*******************************************************************************
1+ /*******************************************************************************
22 * Copyright 2010-2014 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
33 *
44 * This file is part of SITools2.
2424import static org .junit .Assert .assertTrue ;
2525import static org .junit .Assert .fail ;
2626
27+ import java .io .IOException ;
2728import java .util .ArrayList ;
2829import java .util .Iterator ;
2930import java .util .LinkedHashMap ;
3031import java .util .List ;
3132import java .util .Map ;
3233
34+ import fr .cnes .sitools .security .authorization .client .ResourceAuthorization ;
35+ import fr .cnes .sitools .security .authorization .client .RoleAndMethodsAuthorization ;
36+ import fr .cnes .sitools .server .Consts ;
37+ import fr .cnes .sitools .utils .GetRepresentationUtils ;
38+ import fr .cnes .sitools .utils .GetResponseUtils ;
3339import org .junit .Test ;
3440import org .restlet .data .ChallengeResponse ;
3541import org .restlet .data .ChallengeScheme ;
3642import org .restlet .data .MediaType ;
43+ import org .restlet .data .Method ;
3744import org .restlet .engine .Engine ;
3845import org .restlet .representation .Representation ;
3946import org .restlet .resource .ClientResource ;
@@ -55,10 +62,12 @@ public class AbstractUserRoleTestCase extends AbstractSitoolsServerTestCase {
5562
5663 private String userpwd = "admin" ;
5764
65+ private String appName = "" ;
66+
5867 private SitoolsSettings settings = SitoolsSettings .getInstance ();
5968
6069 /**
61- *
70+ *
6271 */
6372 @ Test
6473 public void testUserRole () {
@@ -77,7 +86,7 @@ public void testUserRole() {
7786 }
7887
7988 /**
80- *
89+ *
8190 */
8291 @ Test
8392 public void testUserRoleDocAPI () {
@@ -95,9 +104,9 @@ public void testUserRoleDocAPI() {
95104
96105 docAPI .close ();
97106 }
98-
107+
99108 /**
100- *
109+ *
101110 */
102111 @ Test
103112 public void testUserRoleCreateThenDelete () {
@@ -116,7 +125,34 @@ public void testUserRoleCreateThenDelete() {
116125 }
117126
118127 /**
119- *
128+ * Create a role
129+ * Add the role to an authorization on an application
130+ * Delete the role => Should fail
131+ * Remove all authorization for this role
132+ * Delete the role => Should be OK
133+ */
134+ @ Test
135+ public void testUserRoleCreateAndDeleteWhenUsedOnApplication () throws IOException {
136+
137+ Role role = new Role ();
138+ role .setId ("role_tmp" );
139+ role .setName ("role_tmp" );
140+ role .setDescription ("A temporary role for tests" );
141+ persistRole (role );
142+
143+ ResourceAuthorization auth = createAuthorizationObject (role .getName ());
144+ persistAuthorization (auth );
145+
146+ deleteRole (role , false );
147+
148+ deleteAuthorization (auth );
149+
150+ deleteRole (role , true );
151+
152+ }
153+
154+ /**
155+ *
120156 * @param name
121157 * @param pwd
122158 * @param expectedRoles
@@ -167,7 +203,7 @@ private void getUserRoleNoUserError() {
167203
168204 /**
169205 * Assert that the 2 list of roles are the same
170- *
206+ *
171207 * @param expectedRoles
172208 * the expected list of roles
173209 * @param roles
@@ -176,11 +212,11 @@ private void getUserRoleNoUserError() {
176212 private void assertRoles (List <Role > expectedRoles , List <Role > roles ) {
177213 if (expectedRoles != null && roles != null ) {
178214 assertEquals (expectedRoles .size (), roles .size ());
179- for (Iterator <Role > iterator = expectedRoles .iterator (); iterator .hasNext ();) {
215+ for (Iterator <Role > iterator = expectedRoles .iterator (); iterator .hasNext (); ) {
180216 Role expectedRole = (Role ) iterator .next ();
181217 String roleName = expectedRole .getName ();
182218 boolean found = false ;
183- for (Iterator <Role > iterator2 = roles .iterator (); iterator2 .hasNext () && !found ;) {
219+ for (Iterator <Role > iterator2 = roles .iterator (); iterator2 .hasNext () && !found ; ) {
184220 Role role = (Role ) iterator2 .next ();
185221 if (role .getName ().equals (roleName )) {
186222 found = true ;
@@ -198,12 +234,151 @@ private void assertRoles(List<Role> expectedRoles, List<Role> roles) {
198234
199235 }
200236
237+ private String getBaseUrlRoles () {
238+ return getBaseUrl () + settings .getString (Consts .APP_ROLES_URL );
239+ }
240+
241+ /**
242+ * Invokes POST method to create a new Role
243+ *
244+ * @param item
245+ * Role
246+ */
247+ public void persistRole (Role item ) {
248+ Representation rep = GetRepresentationUtils .getRepresentationRole (item , getMediaTest ());
249+ ClientResource cr = new ClientResource (getBaseUrlRoles ());
250+ docAPI .appendRequest (Method .POST , cr , rep );
251+ Representation result = cr .post (rep , getMediaTest ());
252+ if (!docAPI .appendResponse (result )) {
253+ assertNotNull (result );
254+ assertTrue (cr .getStatus ().isSuccess ());
255+
256+ Response response = GetResponseUtils .getResponseRole (getMediaTest (), result , Role .class );
257+ assertTrue (response .getSuccess ());
258+ Role role = (Role ) response .getItem ();
259+ assertEquals (role .getName (), item .getName ());
260+ assertEquals (role .getDescription (), item .getDescription ());
261+ }
262+ RIAPUtils .exhaust (result );
263+ }
264+
265+
266+ /**
267+ * Invokes DELETE method for deleting Role with specified id.
268+ *
269+ * @param item
270+ * Role
271+ */
272+ public void deleteRole (Role item , boolean expectSuccess ) {
273+ String url = getBaseUrlRoles () + "/" + item .getId ();
274+ ClientResource cr = new ClientResource (url );
275+ docAPI .appendRequest (Method .DELETE , cr );
276+ Representation result = cr .delete (getMediaTest ());
277+ if (!docAPI .appendResponse (result )) {
278+ assertNotNull (result );
279+ assertTrue (cr .getStatus ().isSuccess ());
280+ if (expectSuccess ) {
281+ Response response = GetResponseUtils .getResponseRole (getMediaTest (), result , Role .class );
282+ assertTrue (response .getMessage (), response .getSuccess ());
283+ }
284+ else {
285+ Response response = GetResponseUtils
286+ .getResponseResourceAuthorization (getMediaTest (), result , ResourceAuthorization .class , true );
287+ assertFalse (response .getMessage (), response .getSuccess ());
288+ }
289+ }
290+ RIAPUtils .exhaust (result );
291+ }
292+
293+ private String getBaseUrlAuthorizations () {
294+ return getBaseUrl () + settings .getString (Consts .APP_AUTHORIZATIONS_URL );
295+ }
296+
297+ /**
298+ * Create an object for tests
299+ *
300+ * @return ResourceAuthorization
301+ */
302+ private ResourceAuthorization createAuthorizationObject (String roleName ) {
303+ ResourceAuthorization item = new ResourceAuthorization ();
304+ item .setId ("urn:uuid:SolrApplication:type:fr.cnes.sitools.solr.SolrApplication" );
305+ item .setName ("SolrApplication" );
306+ item .setDescription ("Sample Solr integration" );
307+ item .setUrl ("http://localhost:8182/sitools/solr" );
308+ ArrayList <RoleAndMethodsAuthorization > authorizations = new ArrayList <RoleAndMethodsAuthorization >();
309+ RoleAndMethodsAuthorization aut = new RoleAndMethodsAuthorization ();
310+ aut .setRole (roleName );
311+ aut .setAllMethod (true );
312+ aut .setGetMethod (true );
313+ authorizations .add (aut );
314+ item .setAuthorizations (authorizations );
315+ return item ;
316+ }
317+
318+ /**
319+ * Invoke POST
320+ *
321+ * @param item
322+ * RsourceAuthorization
323+ * @throws IOException
324+ * Exception when copying configuration files from TEST to data/TESTS
325+ */
326+ public void persistAuthorization (ResourceAuthorization item ) throws IOException {
327+ Representation rep = GetRepresentationUtils .getRepresentationResourceAuthorization (item , getMediaTest ());
328+ String url = getBaseUrlAuthorizations ();
329+ if (docAPI .isActive ()) {
330+ Map <String , String > parameters = new LinkedHashMap <String , String >();
331+ putDocAPI (url , "" , rep , parameters , url );
332+ }
333+ else {
334+ ClientResource cr = new ClientResource (url );
335+ Representation result = cr .post (rep , getMediaTest ());
336+ assertNotNull (result );
337+ assertTrue (cr .getStatus ().isSuccess ());
338+ Response response = GetResponseUtils
339+ .getResponseResourceAuthorization (getMediaTest (), result , ResourceAuthorization .class , false );
340+ assertTrue (response .getSuccess ());
341+ ResourceAuthorization authorization = (ResourceAuthorization ) response .getItem ();
342+ assertEquals (authorization .getName (), item .getName ());
343+ assertEquals (authorization .getDescription (), item .getDescription ());
344+ RIAPUtils .exhaust (result );
345+ }
346+ }
347+
348+ /**
349+ * Invoke Delete
350+ *
351+ * @param item
352+ * ResourceAuthorization
353+ * @throws IOException
354+ * Exception when copying configuration files from TEST to data/TESTS
355+ */
356+ public void deleteAuthorization (ResourceAuthorization item ) throws IOException {
357+ ClientResource cr = new ClientResource (getBaseUrlAuthorizations () + "/" + item .getId ());
358+ if (docAPI .isActive ()) {
359+ Map <String , String > parameters = new LinkedHashMap <String , String >();
360+ parameters .put ("identifier" , "Identifier application" );
361+ deleteDocAPI (getBaseUrlAuthorizations () + "/" + item .getId (), "" , parameters ,
362+ getBaseUrlAuthorizations () + "/%identifier%" );
363+ }
364+ else {
365+ Representation result = cr .delete (getMediaTest ());
366+ assertNotNull (result );
367+ assertTrue (cr .getStatus ().isSuccess ());
368+ Response response = getResponse (getMediaTest (), result , ResourceAuthorization .class , false );
369+ assertTrue (response .getSuccess ());
370+ assertEquals (null , response .getTotal ());
371+ RIAPUtils .exhaust (result );
372+ }
373+ }
374+
375+
201376 // ------------------------------------------------------------
202377 // RESPONSE REPRESENTATION WRAPPING
203378
204379 /**
205380 * REST API Response wrapper for single item expected.
206- *
381+ *
207382 * @param media
208383 * MediaType expected
209384 * @param representation
@@ -218,7 +393,7 @@ public static Response getResponse(MediaType media, Representation representatio
218393
219394 /**
220395 * REST API Response Representation wrapper for single or multiple items expexted
221- *
396+ *
222397 * @param media
223398 * MediaType expected
224399 * @param representation
@@ -229,7 +404,8 @@ public static Response getResponse(MediaType media, Representation representatio
229404 * if true wrap the data property else wrap the item property
230405 * @return Response
231406 */
232- public static Response getResponse (MediaType media , Representation representation , Class <?> dataClass , boolean isArray ) {
407+ public static Response getResponse (MediaType media , Representation representation , Class <?> dataClass ,
408+ boolean isArray ) {
233409 try {
234410 if (!media .isCompatible (getMediaTest ()) && !media .isCompatible (MediaType .APPLICATION_XML )) {
235411 Engine .getLogger (AbstractSitoolsTestCase .class .getName ()).warning ("Only JSON or XML supported in tests" );
0 commit comments