1818import com .gooddata .sdk .service .account .AccountService ;
1919import org .mockito .Mock ;
2020import org .mockito .MockitoAnnotations ;
21+ import org .springframework .http .HttpStatus ;
2122import org .springframework .web .client .RestClientException ;
2223import org .springframework .web .client .RestTemplate ;
2324import org .testng .annotations .BeforeMethod ;
2425import org .testng .annotations .Test ;
2526
27+ import java .net .MalformedURLException ;
2628import java .net .URI ;
29+ import java .net .URISyntaxException ;
30+ import java .net .URL ;
2731import java .util .Collection ;
2832
2933import static com .gooddata .sdk .service .project .ProjectService .LIST_PROJECTS_TEMPLATE ;
3236import static org .hamcrest .CoreMatchers .is ;
3337import static org .hamcrest .MatcherAssert .assertThat ;
3438import static org .hamcrest .Matchers .hasSize ;
39+ import static org .mockito .Mockito .doNothing ;
3540import static org .mockito .Mockito .doReturn ;
3641import static org .mockito .Mockito .doThrow ;
3742import static org .mockito .Mockito .mock ;
@@ -203,4 +208,71 @@ public void testGetRoleByUri() {
203208 assertThat (roleByUri , is (role ));
204209 }
205210
211+ @ Test (expectedExceptions = IllegalArgumentException .class )
212+ public void removeUserFromProjectInvalidProject (){
213+ service .removeUserFromProject (project , null );
214+ }
215+
216+ @ Test (expectedExceptions = IllegalArgumentException .class )
217+ public void removeUserFromProjectInvalidAccount (){
218+ service .removeUserFromProject (null , account );
219+ }
220+
221+ @ Test (expectedExceptions = GoodDataException .class )
222+ public void removeUserProjectFail () throws URISyntaxException {
223+ when (project .getId ()).thenReturn ("1" );
224+ when (account .getId ()).thenReturn ("1" );
225+ doThrow (GoodDataRestException .class ).when (restTemplate ).delete (new URI ("/gdc/projects/1/users/1" ));
226+ service .removeUserFromProject (project , account );
227+ }
228+
229+ @ Test (expectedExceptions = GoodDataException .class ,
230+ expectedExceptionsMessageRegExp = "You cannot leave the project 1 if you are the " +
231+ "only admin in it. You can make another user an admin in this project, and then re-issue the call." )
232+ public void removeUserProjectFailForbidden () throws URISyntaxException {
233+ when (project .getId ()).thenReturn ("1" );
234+ when (account .getId ()).thenReturn ("1" );
235+ final GoodDataRestException goodDataRestException = new GoodDataRestException (
236+ HttpStatus .FORBIDDEN .value (),
237+ "r1" ,
238+ "not allowd" ,
239+ "component" ,
240+ "errorClass" );
241+ doThrow (goodDataRestException ).when (restTemplate ).delete (new URI ("/gdc/projects/1/users/1" ));
242+ service .removeUserFromProject (project , account );
243+ }
244+
245+ @ Test (expectedExceptions = GoodDataException .class ,
246+ expectedExceptionsMessageRegExp = "You either misspelled your user ID or tried to remove another user but " +
247+ "did not have the canSuspendUser permission in this project. Check your ID in the request and your " +
248+ "permissions in the project 1, then re-issue the call." )
249+ public void removeUserProjectFailNotAllowed () throws URISyntaxException {
250+ when (project .getId ()).thenReturn ("1" );
251+ when (account .getId ()).thenReturn ("1" );
252+ final GoodDataRestException goodDataRestException = new GoodDataRestException (
253+ HttpStatus .METHOD_NOT_ALLOWED .value () ,
254+ "r1" ,
255+ "not allowd" ,
256+ "component" ,
257+ "errorClass" );
258+ doThrow (goodDataRestException ).when (restTemplate ).delete (new URI ("/gdc/projects/1/users/1" ));
259+ service .removeUserFromProject (project , account );
260+ }
261+
262+ @ Test (expectedExceptions = GoodDataException .class )
263+ public void removeUserProjectFailRestletClientException () throws URISyntaxException {
264+ when (project .getId ()).thenReturn ("1" );
265+ when (account .getId ()).thenReturn ("1" );
266+ doThrow (RestClientException .class ).when (restTemplate ).delete (new URI ("/gdc/projects/1/users/1" ));
267+ service .removeUserFromProject (project , account );
268+ }
269+
270+ @ Test
271+ public void removeUserProject () throws URISyntaxException {
272+ when (project .getId ()).thenReturn ("1" );
273+ when (account .getId ()).thenReturn ("1" );
274+ doNothing ().when (restTemplate ).delete (new URI ("/gdc/projects/1/users/1" ));
275+ service .removeUserFromProject (project , account );
276+ }
277+
206278}
0 commit comments