2020import org .cloudfoundry .CloudFoundryVersion ;
2121import org .cloudfoundry .IfCloudFoundryVersion ;
2222import org .cloudfoundry .client .CloudFoundryClient ;
23- import org .cloudfoundry .client .v3 .applications .ApplicationResource ;
2423import org .cloudfoundry .client .v3 .applications .GetApplicationCurrentDropletRequest ;
2524import org .cloudfoundry .client .v3 .applications .GetApplicationCurrentDropletResponse ;
26- import org .cloudfoundry .client .v3 .applications . ListApplicationsRequest ;
25+ import org .cloudfoundry .client .v3 .deployments . CancelDeploymentRequest ;
2726import org .cloudfoundry .client .v3 .deployments .CreateDeploymentRequest ;
2827import org .cloudfoundry .client .v3 .deployments .CreateDeploymentResponse ;
2928import org .cloudfoundry .client .v3 .deployments .DeploymentRelationships ;
29+ import org .cloudfoundry .client .v3 .deployments .DeploymentResource ;
30+ import org .cloudfoundry .client .v3 .deployments .DeploymentState ;
31+ import org .cloudfoundry .client .v3 .deployments .GetDeploymentRequest ;
32+ import org .cloudfoundry .client .v3 .deployments .ListDeploymentsRequest ;
3033import org .cloudfoundry .operations .CloudFoundryOperations ;
34+ import org .cloudfoundry .operations .applications .ApplicationDetail ;
3135import org .cloudfoundry .operations .applications .ApplicationHealthCheck ;
36+ import org .cloudfoundry .operations .applications .GetApplicationRequest ;
3237import org .cloudfoundry .operations .applications .PushApplicationRequest ;
3338import org .cloudfoundry .util .PaginationUtils ;
3439import org .junit .Test ;
3540import org .springframework .beans .factory .annotation .Autowired ;
3641import org .springframework .core .io .ClassPathResource ;
42+ import reactor .core .publisher .Flux ;
3743import reactor .core .publisher .Mono ;
3844import reactor .test .StepVerifier ;
3945
40- import java .io . IOException ;
46+ import java .nio . file . Path ;
4147import java .time .Duration ;
48+ import java .util .function .Consumer ;
4249
50+ import static org .assertj .core .api .Assertions .assertThat ;
4351import static org .cloudfoundry .util .tuple .TupleUtils .function ;
4452
4553@ IfCloudFoundryVersion (greaterThanOrEqualTo = CloudFoundryVersion .PCF_2_4 )
@@ -51,28 +59,184 @@ public final class DeploymentsTest extends AbstractIntegrationTest {
5159 @ Autowired
5260 private CloudFoundryOperations cloudFoundryOperations ;
5361
62+ @ Test
63+ public void cancel () throws Exception {
64+ String name = this .nameFactory .getApplicationName ();
65+ Path path = new ClassPathResource ("test-application.zip" ).getFile ().toPath ();
66+
67+ createApplicationId (this .cloudFoundryOperations , name , path )
68+ .then (applicationId -> Mono .when (
69+ Mono .just (applicationId ),
70+ getDropletId (this .cloudFoundryClient , applicationId )
71+ ))
72+ .then (function ((applicationId , dropletId ) -> Mono .when (
73+ Mono .just (applicationId ),
74+ createDeploymentId (this .cloudFoundryClient , applicationId , dropletId )
75+ )))
76+ .then (function ((applicationId , deploymentId ) -> this .cloudFoundryClient .deploymentsV3 ()
77+ .cancel (CancelDeploymentRequest .builder ()
78+ .deploymentId (deploymentId )
79+ .build ())
80+ .then (Mono .just (applicationId ))))
81+ .flatMapMany (applicationId -> requestListDeployments (this .cloudFoundryClient , applicationId ))
82+ .map (DeploymentResource ::getState )
83+ .as (StepVerifier ::create )
84+ .consumeNextWith (isCancel ())
85+ .expectComplete ()
86+ .verify (Duration .ofMinutes (5 ));
87+ }
88+
5489 @ Test
5590 public void create () throws Exception {
56- String applicationName = this .nameFactory .getApplicationName ();
91+ String name = this .nameFactory .getApplicationName ();
92+ Path path = new ClassPathResource ("test-application.zip" ).getFile ().toPath ();
93+
94+ createApplicationId (this .cloudFoundryOperations , name , path )
95+ .then (applicationId -> Mono .when (
96+ Mono .just (applicationId ),
97+ getDropletId (this .cloudFoundryClient , applicationId )
98+ ))
99+ .then (function ((applicationId , dropletId ) -> this .cloudFoundryClient .deploymentsV3 ()
100+ .create (CreateDeploymentRequest .builder ()
101+ .droplet (Relationship .builder ()
102+ .id (dropletId )
103+ .build ())
104+ .relationships (DeploymentRelationships .builder ()
105+ .app (ToOneRelationship .builder ()
106+ .data (Relationship .builder ()
107+ .id (applicationId )
108+ .build ())
109+ .build ())
110+ .build ())
111+ .build ())
112+ .then (Mono .just (applicationId ))))
113+ .flatMapMany (applicationId -> requestListDeployments (this .cloudFoundryClient , applicationId ))
114+ .as (StepVerifier ::create )
115+ .expectNextCount (1 )
116+ .expectComplete ()
117+ .verify (Duration .ofMinutes (5 ));
118+ }
119+
120+ @ Test
121+ public void get () throws Exception {
122+ String name = this .nameFactory .getApplicationName ();
123+ Path path = new ClassPathResource ("test-application.zip" ).getFile ().toPath ();
124+
125+ createApplicationId (this .cloudFoundryOperations , name , path )
126+ .then (applicationId -> Mono .when (
127+ Mono .just (applicationId ),
128+ getDropletId (this .cloudFoundryClient , applicationId )
129+ ))
130+ .then (function ((applicationId , dropletId ) -> createDeploymentId (this .cloudFoundryClient , applicationId , dropletId )))
131+ .then (deploymentId -> this .cloudFoundryClient .deploymentsV3 ()
132+ .get (GetDeploymentRequest .builder ()
133+ .deploymentId (deploymentId )
134+ .build ()))
135+ .as (StepVerifier ::create )
136+ .expectNextCount (1 )
137+ .expectComplete ()
138+ .verify (Duration .ofMinutes (5 ));
139+ }
140+
141+ @ Test
142+ public void list () throws Exception {
143+ String name = this .nameFactory .getApplicationName ();
144+ Path path = new ClassPathResource ("test-application.zip" ).getFile ().toPath ();
145+
146+ createApplicationId (this .cloudFoundryOperations , name , path )
147+ .then (applicationId -> Mono .when (
148+ Mono .just (applicationId ),
149+ getDropletId (this .cloudFoundryClient , applicationId )
150+ ))
151+ .then (function ((applicationId , dropletId ) -> createDeploymentId (this .cloudFoundryClient , applicationId , dropletId )))
152+ .flatMapMany (deploymentId -> PaginationUtils .requestClientV3Resources (page -> this .cloudFoundryClient .deploymentsV3 ()
153+ .list (ListDeploymentsRequest .builder ()
154+ .page (page )
155+ .build ()))
156+ .filter (resource -> deploymentId .equals (resource .getId ())))
157+ .as (StepVerifier ::create )
158+ .expectNextCount (1 )
159+ .expectComplete ()
160+ .verify (Duration .ofMinutes (5 ));
161+ }
162+
163+ @ Test
164+ public void listFilterByApplication () throws Exception {
165+ String name = this .nameFactory .getApplicationName ();
166+ Path path = new ClassPathResource ("test-application.zip" ).getFile ().toPath ();
57167
58- createApplication (this .cloudFoundryOperations , applicationName )
59- .then (getApplicationId (this .cloudFoundryClient , applicationName ))
168+ createApplicationId (this .cloudFoundryOperations , name , path )
60169 .then (applicationId -> Mono .when (
61170 Mono .just (applicationId ),
62171 getDropletId (this .cloudFoundryClient , applicationId )
63172 ))
64- .then (function (this ::createDeployment ))
65- .map (CreateDeploymentResponse ::getId )
173+ .then (function ((applicationId , dropletId ) -> requestCreateDeployment (this .cloudFoundryClient , applicationId , dropletId )
174+ .then (Mono .just (applicationId ))))
175+ .flatMapMany (applicationId -> PaginationUtils .requestClientV3Resources (page -> this .cloudFoundryClient .deploymentsV3 ()
176+ .list (ListDeploymentsRequest .builder ()
177+ .applicationId (applicationId )
178+ .page (page )
179+ .build ())))
66180 .as (StepVerifier ::create )
67181 .expectNextCount (1 )
68182 .expectComplete ()
69183 .verify (Duration .ofMinutes (5 ));
70184 }
71185
72- private static Mono <Void > createApplication (CloudFoundryOperations cloudFoundryOperations , String name ) throws IOException {
186+ @ Test
187+ public void listFilterByState () throws Exception {
188+ String name = this .nameFactory .getApplicationName ();
189+ Path path = new ClassPathResource ("test-application.zip" ).getFile ().toPath ();
190+
191+ createApplicationId (this .cloudFoundryOperations , name , path )
192+ .then (applicationId -> Mono .when (
193+ Mono .just (applicationId ),
194+ getDropletId (this .cloudFoundryClient , applicationId )
195+ ))
196+ .then (function ((applicationId , dropletId ) -> createDeploymentId (this .cloudFoundryClient , applicationId , dropletId )))
197+ .flatMapMany (deploymentId -> PaginationUtils .requestClientV3Resources (page -> this .cloudFoundryClient .deploymentsV3 ()
198+ .list (ListDeploymentsRequest .builder ()
199+ .states (DeploymentState .DEPLOYED , DeploymentState .DEPLOYING )
200+ .page (page )
201+ .build ()))
202+ .filter (resource -> deploymentId .equals (resource .getId ())))
203+ .as (StepVerifier ::create )
204+ .expectNextCount (1 )
205+ .expectComplete ()
206+ .verify (Duration .ofMinutes (5 ));
207+ }
208+
209+ private static Mono <String > createApplicationId (CloudFoundryOperations cloudFoundryOperations , String name , Path path ) {
210+ return requestCreateApplication (cloudFoundryOperations , name , path )
211+ .then (getApplicationId (cloudFoundryOperations , name ));
212+ }
213+
214+ private static Mono <String > createDeploymentId (CloudFoundryClient cloudFoundryClient , String applicationId , String dropletId ) {
215+ return requestCreateDeployment (cloudFoundryClient , applicationId , dropletId )
216+ .map (CreateDeploymentResponse ::getId );
217+ }
218+
219+ private static Mono <String > getApplicationId (CloudFoundryOperations cloudFoundryOperations , String name ) {
220+ return cloudFoundryOperations .applications ()
221+ .get (GetApplicationRequest .builder ()
222+ .name (name )
223+ .build ())
224+ .map (ApplicationDetail ::getId );
225+ }
226+
227+ private static Mono <String > getDropletId (CloudFoundryClient cloudFoundryClient , String applicationId ) {
228+ return requestCurrentDroplet (cloudFoundryClient , applicationId )
229+ .map (GetApplicationCurrentDropletResponse ::getId );
230+ }
231+
232+ private static Consumer <DeploymentState > isCancel () {
233+ return state -> assertThat (state ).isIn (DeploymentState .CANCELING , DeploymentState .CANCELED );
234+ }
235+
236+ private static Mono <Void > requestCreateApplication (CloudFoundryOperations cloudFoundryOperations , String name , Path path ) {
73237 return cloudFoundryOperations .applications ()
74238 .push (PushApplicationRequest .builder ()
75- .path (new ClassPathResource ( "test-application.zip" ). getFile (). toPath () )
239+ .path (path )
76240 .buildpack ("staticfile_buildpack" )
77241 .diskQuota (256 )
78242 .healthCheckType (ApplicationHealthCheck .PORT )
@@ -82,27 +246,12 @@ private static Mono<Void> createApplication(CloudFoundryOperations cloudFoundryO
82246 .build ());
83247 }
84248
85- private static Mono <String > getApplicationId (CloudFoundryClient cloudFoundryClient , String applicationName ) {
86- return PaginationUtils .requestClientV3Resources (page -> cloudFoundryClient .applicationsV3 ()
87- .list (ListApplicationsRequest .builder ()
88- .name (applicationName )
89- .build ()))
90- .single ()
91- .map (ApplicationResource ::getId );
92- }
93-
94- private static Mono <String > getDropletId (CloudFoundryClient cloudFoundryClient , String applicationId ) {
95- return cloudFoundryClient .applicationsV3 ()
96- .getCurrentDroplet (GetApplicationCurrentDropletRequest .builder ()
97- .applicationId (applicationId )
98- .build ())
99- .map (GetApplicationCurrentDropletResponse ::getId );
100- }
101-
102- private Mono <? extends CreateDeploymentResponse > createDeployment (String applicationId , String dropletId ) {
103- return this .cloudFoundryClient .deploymentsV3 ()
249+ private static Mono <CreateDeploymentResponse > requestCreateDeployment (CloudFoundryClient cloudFoundryClient , String applicationId , String dropletId ) {
250+ return cloudFoundryClient .deploymentsV3 ()
104251 .create (CreateDeploymentRequest .builder ()
105- .droplet (Relationship .builder ().id (dropletId ).build ())
252+ .droplet (Relationship .builder ()
253+ .id (dropletId )
254+ .build ())
106255 .relationships (DeploymentRelationships .builder ()
107256 .app (ToOneRelationship .builder ()
108257 .data (Relationship .builder ()
@@ -113,4 +262,19 @@ private Mono<? extends CreateDeploymentResponse> createDeployment(String applica
113262 .build ());
114263 }
115264
265+ private static Mono <GetApplicationCurrentDropletResponse > requestCurrentDroplet (CloudFoundryClient cloudFoundryClient , String applicationId ) {
266+ return cloudFoundryClient .applicationsV3 ()
267+ .getCurrentDroplet (GetApplicationCurrentDropletRequest .builder ()
268+ .applicationId (applicationId )
269+ .build ());
270+ }
271+
272+ private static Flux <DeploymentResource > requestListDeployments (CloudFoundryClient cloudFoundryClient , String applicationId ) {
273+ return PaginationUtils .requestClientV3Resources (page -> cloudFoundryClient .deploymentsV3 ()
274+ .list (ListDeploymentsRequest .builder ()
275+ .applicationId (applicationId )
276+ .page (page )
277+ .build ()));
278+ }
279+
116280}
0 commit comments