2020import org .cloudfoundry .client .CloudFoundryClient ;
2121import org .cloudfoundry .client .v2 .users .ListUsersRequest ;
2222import org .cloudfoundry .client .v2 .users .UserResource ;
23+ import org .cloudfoundry .operations .organizations .CreateOrganizationRequest ;
24+ import org .cloudfoundry .operations .spaces .CreateSpaceRequest ;
2325import org .cloudfoundry .operations .useradmin .CreateUserRequest ;
2426import org .cloudfoundry .operations .useradmin .DeleteUserRequest ;
27+ import org .cloudfoundry .operations .useradmin .ListSpaceUsersRequest ;
28+ import org .cloudfoundry .operations .useradmin .SetSpaceRoleRequest ;
29+ import org .cloudfoundry .operations .useradmin .SpaceRole ;
30+ import org .cloudfoundry .operations .useradmin .SpaceUsers ;
31+ import org .cloudfoundry .operations .useradmin .UnsetSpaceRoleRequest ;
2532import org .cloudfoundry .util .PaginationUtils ;
2633import org .cloudfoundry .util .ResourceUtils ;
2734import org .junit .Test ;
3340import java .time .Duration ;
3441
3542import static org .assertj .core .api .Assertions .assertThat ;
43+ import static org .cloudfoundry .operations .useradmin .SpaceRole .AUDITOR ;
44+ import static org .cloudfoundry .operations .useradmin .SpaceRole .MANAGER ;
3645
3746public final class UserAdminTest extends AbstractIntegrationTest {
3847
@@ -51,7 +60,7 @@ public void create() {
5160 .password ("test-password" )
5261 .username (username )
5362 .build ())
54- .thenMany (listUsers (this .cloudFoundryClient ))
63+ .thenMany (requestListUsers (this .cloudFoundryClient ))
5564 .filter (response -> username .equals (ResourceUtils .getEntity (response ).getUsername ()))
5665 .as (StepVerifier ::create )
5766 .expectNextCount (1 )
@@ -79,7 +88,7 @@ public void delete() {
7988 .delete (DeleteUserRequest .builder ()
8089 .username (username )
8190 .build ()))
82- .thenMany (listUsers (this .cloudFoundryClient ))
91+ .thenMany (requestListUsers (this .cloudFoundryClient ))
8392 .filter (response -> username .equals (ResourceUtils .getEntity (response ).getUsername ()))
8493 .as (StepVerifier ::create )
8594 .expectComplete ()
@@ -88,17 +97,107 @@ public void delete() {
8897
8998 @ Test
9099 public void deleteNotFound () {
100+ this .cloudFoundryOperations .userAdmin ()
101+ .delete (DeleteUserRequest .builder ()
102+ .username ("not-found" )
103+ .build ())
104+ .as (StepVerifier ::create )
105+ .consumeErrorWith (t -> assertThat (t ).isInstanceOf (IllegalArgumentException .class ).hasMessage ("User not-found does not exist" ))
106+ .verify (Duration .ofMinutes (5 ));
107+ }
108+
109+ @ Test
110+ public void listSpaceUsers () {
111+ String organizationName = this .nameFactory .getOrganizationName ();
112+ String spaceName = this .nameFactory .getSpaceName ();
91113 String username = this .nameFactory .getUserName ();
92114
93- this .cloudFoundryOperations .userAdmin ()
94- .delete (DeleteUserRequest .builder ()
115+ Mono .when (
116+ createUser (this .cloudFoundryOperations , username ),
117+ createOrganization (this .cloudFoundryOperations , organizationName )
118+ )
119+ .then (createSpace (this .cloudFoundryOperations , organizationName , spaceName ))
120+ .then (setSpaceRole (this .cloudFoundryOperations , organizationName , spaceName , AUDITOR , username ))
121+ .then (this .cloudFoundryOperations .userAdmin ()
122+ .listSpaceUsers (ListSpaceUsersRequest .builder ()
123+ .organizationName (organizationName )
124+ .spaceName (spaceName )
125+ .build ()))
126+ .flatMapIterable (SpaceUsers ::getAuditors )
127+ .as (StepVerifier ::create )
128+ .expectNext (username )
129+ .expectComplete ()
130+ .verify (Duration .ofMinutes (5 ));
131+ }
132+
133+ @ Test
134+ public void setSpaceUser () {
135+ String organizationName = this .nameFactory .getOrganizationName ();
136+ String spaceName = this .nameFactory .getSpaceName ();
137+ String username = this .nameFactory .getUserName ();
138+
139+ Mono .when (
140+ createUser (this .cloudFoundryOperations , username ),
141+ createOrganization (this .cloudFoundryOperations , organizationName )
142+ )
143+ .then (createSpace (this .cloudFoundryOperations , organizationName , spaceName ))
144+ .then (this .cloudFoundryOperations .userAdmin ()
145+ .setSpaceRole (SetSpaceRoleRequest .builder ()
146+ .organizationName (organizationName )
147+ .spaceName (spaceName )
148+ .spaceRole (AUDITOR )
149+ .username (username )
150+ .build ()))
151+ .thenMany (listSpaceUsers (this .cloudFoundryOperations , organizationName , spaceName ))
152+ .flatMapIterable (SpaceUsers ::getAuditors )
153+ .as (StepVerifier ::create )
154+ .expectNextCount (1 )
155+ .expectComplete ()
156+ .verify (Duration .ofMinutes (5 ));
157+ }
158+
159+ @ Test
160+ public void unsetSpaceUser () {
161+ String organizationName = this .nameFactory .getOrganizationName ();
162+ String spaceName = this .nameFactory .getSpaceName ();
163+ String username = this .nameFactory .getUserName ();
164+
165+ Mono .when (
166+ createUser (this .cloudFoundryOperations , username ),
167+ createOrganization (this .cloudFoundryOperations , organizationName )
168+ )
169+ .then (createSpace (this .cloudFoundryOperations , organizationName , spaceName ))
170+ .then (requestSetSpaceRole (this .cloudFoundryOperations , organizationName , spaceName , MANAGER , username ))
171+ .then (this .cloudFoundryOperations .userAdmin ()
172+ .unsetSpaceRole (UnsetSpaceRoleRequest .builder ()
173+ .organizationName (organizationName )
174+ .spaceName (spaceName )
175+ .spaceRole (MANAGER )
95176 .username (username )
96- .build ())
177+ .build ()))
178+ .thenMany (listSpaceUsers (this .cloudFoundryOperations , organizationName , spaceName ))
179+ .flatMapIterable (SpaceUsers ::getManagers )
180+ .filter (username ::equals )
97181 .as (StepVerifier ::create )
98- .consumeErrorWith ( t -> assertThat ( t ). isInstanceOf ( IllegalArgumentException . class ). hasMessage ( "User %s does not exist" , username ) )
182+ .expectComplete ( )
99183 .verify (Duration .ofMinutes (5 ));
100184 }
101185
186+ private static Mono <Void > createOrganization (CloudFoundryOperations cloudFoundryOperations , String organizationName ) {
187+ return cloudFoundryOperations .organizations ()
188+ .create (CreateOrganizationRequest .builder ()
189+ .organizationName (organizationName )
190+ .build ());
191+ }
192+
193+ private static Mono <Void > createSpace (CloudFoundryOperations cloudFoundryOperations , String organizationName , String spaceName ) {
194+ return cloudFoundryOperations .spaces ()
195+ .create (CreateSpaceRequest .builder ()
196+ .name (spaceName )
197+ .organization (organizationName )
198+ .build ());
199+ }
200+
102201 private static Mono <Void > createUser (CloudFoundryOperations cloudFoundryOperations , String username ) {
103202 return cloudFoundryOperations .userAdmin ()
104203 .create (CreateUserRequest .builder ()
@@ -107,11 +206,39 @@ private static Mono<Void> createUser(CloudFoundryOperations cloudFoundryOperatio
107206 .build ());
108207 }
109208
110- private static Flux <UserResource > listUsers (CloudFoundryClient cloudFoundryClient ) {
209+ private static Mono <SpaceUsers > listSpaceUsers (CloudFoundryOperations cloudFoundryOperations , String organizationName , String spaceName ) {
210+ return cloudFoundryOperations .userAdmin ()
211+ .listSpaceUsers (ListSpaceUsersRequest .builder ()
212+ .organizationName (organizationName )
213+ .spaceName (spaceName )
214+ .build ());
215+ }
216+
217+ private static Flux <UserResource > requestListUsers (CloudFoundryClient cloudFoundryClient ) {
111218 return PaginationUtils .requestClientV2Resources (page -> cloudFoundryClient .users ()
112219 .list (ListUsersRequest .builder ()
113220 .page (page )
114221 .build ()));
115222 }
116223
224+ private static Mono <Void > requestSetSpaceRole (CloudFoundryOperations cloudFoundryOperations , String organizationName , String spaceName , SpaceRole spaceRole , String username ) {
225+ return cloudFoundryOperations .userAdmin ()
226+ .setSpaceRole (SetSpaceRoleRequest .builder ()
227+ .organizationName (organizationName )
228+ .spaceName (spaceName )
229+ .spaceRole (spaceRole )
230+ .username (username )
231+ .build ());
232+ }
233+
234+ private static Mono <Void > setSpaceRole (CloudFoundryOperations cloudFoundryOperations , String organizationName , String spaceName , SpaceRole spaceRole , String username ) {
235+ return cloudFoundryOperations .userAdmin ()
236+ .setSpaceRole (SetSpaceRoleRequest .builder ()
237+ .spaceRole (spaceRole )
238+ .organizationName (organizationName )
239+ .spaceName (spaceName )
240+ .username (username )
241+ .build ());
242+ }
243+
117244}
0 commit comments