Skip to content

Commit 8a57145

Browse files
committed
DefaultApplications: .stop() uses v3
1 parent e5b6f60 commit 8a57145

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/DefaultApplications.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
import org.cloudfoundry.client.v3.ToOneRelationship;
9999
import org.cloudfoundry.client.v3.applications.ApplicationFeature;
100100
import org.cloudfoundry.client.v3.applications.ApplicationResource;
101+
import org.cloudfoundry.client.v3.applications.ApplicationState;
101102
import org.cloudfoundry.client.v3.applications.GetApplicationEnvironmentRequest;
102103
import org.cloudfoundry.client.v3.applications.GetApplicationEnvironmentResponse;
103104
import org.cloudfoundry.client.v3.applications.GetApplicationProcessRequest;
@@ -658,8 +659,8 @@ public Mono<Void> start(StartApplicationRequest request) {
658659

659660
@Override
660661
public Mono<Void> stop(StopApplicationRequest request) {
661-
return getApplicationIdWhere(request.getName(), isNotIn(STOPPED_STATE))
662-
.flatMap(applicationId -> stopApplication(applicationId))
662+
return getApplicationV3(request.getName())
663+
.flatMap(this::stopApplicationV3)
663664
.then()
664665
.transform(OperationsLogging.log("Stop Application"))
665666
.checkpoint();
@@ -2344,6 +2345,19 @@ private Mono<AbstractApplicationResource> stopApplication(String applicationId)
23442345
return requestUpdateApplicationState(applicationId, STOPPED_STATE);
23452346
}
23462347

2348+
private Mono<Void> stopApplicationV3(ApplicationResource application) {
2349+
if (application.getState() == ApplicationState.STOPPED) {
2350+
return Mono.empty();
2351+
}
2352+
return this.cloudFoundryClient
2353+
.applicationsV3()
2354+
.stop(
2355+
org.cloudfoundry.client.v3.applications.StopApplicationRequest.builder()
2356+
.applicationId(application.getId())
2357+
.build())
2358+
.then();
2359+
}
2360+
23472361
private Mono<AbstractApplicationResource> stopApplicationIfNotStopped(
23482362
AbstractApplicationResource resource) {
23492363
return isNotIn(resource, STOPPED_STATE)

cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/DefaultApplicationsTest.java

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static org.mockito.Mockito.RETURNS_SMART_NULLS;
2626
import static org.mockito.Mockito.mock;
2727
import static org.mockito.Mockito.verify;
28+
import static org.mockito.Mockito.verifyNoInteractions;
2829
import static org.mockito.Mockito.when;
2930

3031
import java.io.IOException;
@@ -133,6 +134,7 @@
133134
import org.cloudfoundry.client.v3.applications.ListApplicationRoutesResponse;
134135
import org.cloudfoundry.client.v3.applications.ListApplicationsRequest;
135136
import org.cloudfoundry.client.v3.applications.ListApplicationsResponse;
137+
import org.cloudfoundry.client.v3.applications.StopApplicationResponse;
136138
import org.cloudfoundry.client.v3.applications.UpdateApplicationEnvironmentVariablesRequest;
137139
import org.cloudfoundry.client.v3.applications.UpdateApplicationEnvironmentVariablesResponse;
138140
import org.cloudfoundry.client.v3.applications.UpdateApplicationFeatureRequest;
@@ -3782,7 +3784,7 @@ void startStoppedApplication() {
37823784

37833785
@Test
37843786
void stopInvalidApplication() {
3785-
requestApplicationsEmpty(this.cloudFoundryClient, "test-application-name", TEST_SPACE_ID);
3787+
requestApplicationsEmptyV3(this.cloudFoundryClient, "test-application-name", TEST_SPACE_ID);
37863788

37873789
this.applications
37883790
.stop(StopApplicationRequest.builder().name("test-application-name").build())
@@ -3798,27 +3800,36 @@ void stopInvalidApplication() {
37983800

37993801
@Test
38003802
void stopStartedApplication() {
3801-
requestApplicationsSpecificState(
3802-
this.cloudFoundryClient, "test-application-name", TEST_SPACE_ID, "STARTED");
3803-
requestUpdateApplicationState(this.cloudFoundryClient, "test-application-id", "STOPPED");
3803+
requestApplicationsV3(
3804+
this.cloudFoundryClient,
3805+
"test-application-name",
3806+
TEST_SPACE_ID,
3807+
"test-application-id");
3808+
requestStopApplication(this.cloudFoundryClient, "test-application-id");
38043809

38053810
this.applications
38063811
.stop(StopApplicationRequest.builder().name("test-application-name").build())
38073812
.as(StepVerifier::create)
38083813
.expectComplete()
38093814
.verify(Duration.ofSeconds(5));
3815+
verify(this.cloudFoundryClient.applicationsV3()).stop(any());
38103816
}
38113817

38123818
@Test
38133819
void stopStoppedApplication() {
3814-
requestApplicationsSpecificState(
3815-
this.cloudFoundryClient, "test-application-name", TEST_SPACE_ID, "STOPPED");
3820+
requestApplicationsV3(
3821+
this.cloudFoundryClient,
3822+
"test-application-name",
3823+
TEST_SPACE_ID,
3824+
"test-application-id",
3825+
ApplicationState.STOPPED);
38163826

38173827
this.applications
38183828
.stop(StopApplicationRequest.builder().name("test-application-name").build())
38193829
.as(StepVerifier::create)
38203830
.expectComplete()
38213831
.verify(Duration.ofSeconds(5));
3832+
verifyNoInteractions(this.cloudFoundryClient.applicationsV3().stop(any()));
38223833
}
38233834

38243835
@Test
@@ -4738,6 +4749,16 @@ private static void requestApplicationsV3(
47384749
String application,
47394750
String spaceId,
47404751
String applicationId) {
4752+
requestApplicationsV3(
4753+
cloudFoundryClient, application, spaceId, applicationId, ApplicationState.STARTED);
4754+
}
4755+
4756+
private static void requestApplicationsV3(
4757+
CloudFoundryClient cloudFoundryClient,
4758+
String application,
4759+
String spaceId,
4760+
String applicationId,
4761+
ApplicationState state) {
47414762
when(cloudFoundryClient
47424763
.applicationsV3()
47434764
.list(
@@ -4763,7 +4784,7 @@ private static void requestApplicationsV3(
47634784
.build())
47644785
.build())
47654786
.id(applicationId)
4766-
.state(ApplicationState.STARTED)
4787+
.state(state)
47674788
.build())
47684789
.build()));
47694790
}
@@ -5977,6 +5998,18 @@ private static void requestUpdateApplicationState(
59775998
requestUpdateApplicationState(cloudFoundryClient, applicationId, state, 1);
59785999
}
59796000

6001+
private static void requestStopApplication(
6002+
CloudFoundryClient cloudFoundryClient, String applicationId) {
6003+
when(cloudFoundryClient
6004+
.applicationsV3()
6005+
.stop(
6006+
org.cloudfoundry.client.v3.applications.StopApplicationRequest
6007+
.builder()
6008+
.applicationId(applicationId)
6009+
.build()))
6010+
.thenReturn(Mono.just(fill(StopApplicationResponse.builder()).build()));
6011+
}
6012+
59806013
private static void requestUpdateApplicationState(
59816014
CloudFoundryClient cloudFoundryClient,
59826015
String applicationId,

0 commit comments

Comments
 (0)