-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DE-585] Subscription status endpoints tests (#40)
[DE-585] Subscription status endpoints tests
- Loading branch information
1 parent
0991bb4
commit adb7602
Showing
9 changed files
with
547 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...ng/controllers/subscriptionstatus/SubscriptionStatusControllerCancelSubscriptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package com.maxio.advancedbilling.controllers.subscriptionstatus; | ||
|
||
import com.maxio.advancedbilling.exceptions.ApiException; | ||
import com.maxio.advancedbilling.models.CancellationMethod; | ||
import com.maxio.advancedbilling.models.CancellationOptions; | ||
import com.maxio.advancedbilling.models.CancellationRequest; | ||
import com.maxio.advancedbilling.models.Subscription; | ||
import com.maxio.advancedbilling.models.SubscriptionState; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.time.ZonedDateTime; | ||
import java.time.temporal.ChronoUnit; | ||
|
||
import static com.maxio.advancedbilling.utils.CommonAssertions.assertNotFound; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
|
||
public class SubscriptionStatusControllerCancelSubscriptionTest extends SubscriptionStatusControllerTestBase { | ||
|
||
@Test | ||
void shouldCancelActiveSubscription() throws IOException, ApiException { | ||
// given | ||
Subscription subscription = createSubscription(); | ||
ZonedDateTime timestamp = ZonedDateTime.now().minus(5, ChronoUnit.SECONDS); | ||
|
||
// when | ||
Subscription cancelledSubscription = subscriptionStatusController | ||
.cancelSubscription(subscription.getId(), null).getSubscription(); | ||
|
||
// then | ||
assertThat(cancelledSubscription).usingRecursiveComparison() | ||
.ignoringFields("state", "updatedAt", "canceledAt", "cancelAtEndOfPeriod", | ||
"cancellationMethod", "reasonCode") | ||
.isEqualTo(subscription); | ||
assertThat(cancelledSubscription.getState()).isEqualTo(SubscriptionState.CANCELED); | ||
assertThat(cancelledSubscription.getUpdatedAt()).isAfter(timestamp); | ||
assertThat(cancelledSubscription.getCanceledAt()).isAfter(timestamp); | ||
assertThat(cancelledSubscription.getCancellationMethod()).isEqualTo(CancellationMethod.MERCHANT_API); | ||
assertThat(cancelledSubscription.getReasonCode()) | ||
.isEqualTo("CH:Unknown"); | ||
} | ||
|
||
@Test | ||
void shouldCancelActiveSubscriptionProvidingReasonCodeAndMessage() throws IOException, ApiException { | ||
// given | ||
Subscription subscription = createSubscription(); | ||
ZonedDateTime timestamp = ZonedDateTime.now().minus(5, ChronoUnit.SECONDS); | ||
String reasonCode = "CHURN"; | ||
String cancellationMessage = "Customer left."; | ||
CancellationRequest cancellationRequest = new CancellationRequest( | ||
new CancellationOptions.Builder() | ||
.cancellationMessage(cancellationMessage) | ||
.reasonCode(reasonCode) | ||
.build() | ||
); | ||
|
||
// when | ||
Subscription cancelledSubscription = subscriptionStatusController | ||
.cancelSubscription(subscription.getId(), cancellationRequest).getSubscription(); | ||
|
||
// then | ||
assertThat(cancelledSubscription).usingRecursiveComparison() | ||
.ignoringFields("state", "updatedAt", "canceledAt", "cancelAtEndOfPeriod", | ||
"cancellationMethod", "reasonCode", "cancellationMessage") | ||
.isEqualTo(subscription); | ||
assertThat(cancelledSubscription.getState()).isEqualTo(SubscriptionState.CANCELED); | ||
assertThat(cancelledSubscription.getUpdatedAt()).isAfter(timestamp); | ||
assertThat(cancelledSubscription.getCanceledAt()).isAfter(timestamp); | ||
assertThat(cancelledSubscription.getCancellationMethod()).isEqualTo(CancellationMethod.MERCHANT_API); | ||
assertThat(cancelledSubscription.getReasonCode()).isEqualTo(reasonCode); | ||
assertThat(cancelledSubscription.getCancellationMessage()).isEqualTo(cancellationMessage); | ||
} | ||
|
||
@Test | ||
void shouldNotRetryNonExistentSubscription() { | ||
assertNotFound(() -> subscriptionStatusController.cancelSubscription(99999999, null), | ||
"Not Found"); | ||
} | ||
|
||
@Test | ||
void shouldNotCancelAlreadyCanceledSubscriptionExceedingMessagesLimits() throws IOException, ApiException { | ||
// given | ||
Subscription subscription = createSubscription(); | ||
|
||
String reasonCode = "a".repeat(256); | ||
String cancellationMessage = "a".repeat(65536); | ||
CancellationRequest cancellationRequest = new CancellationRequest( | ||
new CancellationOptions.Builder() | ||
.cancellationMessage(cancellationMessage) | ||
.reasonCode(reasonCode) | ||
.build() | ||
); | ||
|
||
// when-then | ||
assertThatExceptionOfType(ApiException.class) | ||
.isThrownBy(() -> subscriptionStatusController.cancelSubscription(subscription.getId(), cancellationRequest)) | ||
.withMessage("Unprocessable Entity (WebDAV)") | ||
.satisfies(e -> { | ||
assertThat(e.getResponseCode()).isEqualTo(422); | ||
assertThat(e.getHttpContext().getResponse().getBody()).isEqualTo(""" | ||
{"errors":["reason_code size cannot be greater than 255","cancellation_message size cannot be greater than 65535"]}""" | ||
); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldNotCancelAlreadyCanceledSubscription() throws IOException, ApiException { | ||
// given | ||
Subscription subscription = createSubscription(); | ||
|
||
// when | ||
subscriptionStatusController | ||
.cancelSubscription(subscription.getId(), null); | ||
|
||
// then | ||
assertThatExceptionOfType(ApiException.class) | ||
.isThrownBy(() -> subscriptionStatusController.cancelSubscription(subscription.getId(), null) | ||
) | ||
.withMessage("Unprocessable Entity (WebDAV)") | ||
.satisfies(e -> { | ||
assertThat(e.getResponseCode()).isEqualTo(422); | ||
assertThat(e.getHttpContext().getResponse().getBody()).isEqualTo(""" | ||
{"error":"The subscription is already canceled"}""" | ||
); | ||
}); | ||
} | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
...ing/controllers/subscriptionstatus/SubscriptionStatusControllerPauseSubscriptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.maxio.advancedbilling.controllers.subscriptionstatus; | ||
|
||
import com.maxio.advancedbilling.exceptions.ApiException; | ||
import com.maxio.advancedbilling.exceptions.ErrorListResponseException; | ||
import com.maxio.advancedbilling.models.AutoResume; | ||
import com.maxio.advancedbilling.models.PauseRequest; | ||
import com.maxio.advancedbilling.models.Subscription; | ||
import com.maxio.advancedbilling.models.SubscriptionState; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.time.ZonedDateTime; | ||
import java.time.temporal.ChronoUnit; | ||
|
||
import static com.maxio.advancedbilling.utils.CommonAssertions.assertNotFound; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
|
||
public class SubscriptionStatusControllerPauseSubscriptionTest extends SubscriptionStatusControllerTestBase { | ||
|
||
@Test | ||
void shouldPauseActiveSubscription() throws IOException, ApiException { | ||
// given | ||
Subscription subscription = createSubscription(); | ||
ZonedDateTime timestamp = ZonedDateTime.now().minus(5, ChronoUnit.SECONDS); | ||
|
||
// when | ||
Subscription pausedSubscription = subscriptionStatusController | ||
.pauseSubscription(subscription.getId(), null).getSubscription(); | ||
|
||
// then | ||
assertThat(pausedSubscription).usingRecursiveComparison() | ||
.ignoringFields("state", "updatedAt", "onHoldAt", "prepaidDunning", | ||
"productPricePointType") | ||
.isEqualTo(subscription); | ||
assertThat(pausedSubscription.getState()).isEqualTo(SubscriptionState.ON_HOLD); | ||
assertThat(pausedSubscription.getUpdatedAt()).isAfter(timestamp); | ||
assertThat(pausedSubscription.getOnHoldAt()).isAfter(timestamp); | ||
} | ||
|
||
@Test | ||
void shouldPauseActiveSubscriptionSettingAutomaticallyResumeAt() throws IOException, ApiException { | ||
// given | ||
Subscription subscription = createSubscription(); | ||
ZonedDateTime timestamp = ZonedDateTime.now().minus(5, ChronoUnit.SECONDS); | ||
ZonedDateTime resumeAt = ZonedDateTime.now().plusDays(3); | ||
PauseRequest pauseRequest = new PauseRequest( | ||
new AutoResume(resumeAt) | ||
); | ||
|
||
// when | ||
Subscription pausedSubscription = subscriptionStatusController | ||
.pauseSubscription(subscription.getId(), pauseRequest).getSubscription(); | ||
|
||
// then | ||
assertThat(pausedSubscription).usingRecursiveComparison() | ||
.ignoringFields("state", "updatedAt", "onHoldAt", "prepaidDunning", | ||
"productPricePointType", "automaticallyResumeAt") | ||
.isEqualTo(subscription); | ||
assertThat(pausedSubscription.getState()).isEqualTo(SubscriptionState.ON_HOLD); | ||
assertThat(pausedSubscription.getUpdatedAt()).isAfter(timestamp); | ||
assertThat(pausedSubscription.getAutomaticallyResumeAt().toEpochSecond()) | ||
.isEqualTo(resumeAt.toEpochSecond()); | ||
assertThat(pausedSubscription.getOnHoldAt()).isAfter(timestamp); | ||
} | ||
|
||
@Test | ||
void shouldNotPauseAlreadyPausedSubscription() throws IOException, ApiException { | ||
// given | ||
Subscription subscription = createSubscription(); | ||
|
||
// when | ||
subscriptionStatusController | ||
.pauseSubscription(subscription.getId(), null); | ||
|
||
// when-then | ||
assertThatExceptionOfType(ErrorListResponseException.class) | ||
.isThrownBy(() -> subscriptionStatusController.pauseSubscription(subscription.getId(), null) | ||
) | ||
.withMessage("Unprocessable Entity (WebDAV)") | ||
.satisfies(e -> { | ||
assertThat(e.getResponseCode()).isEqualTo(422); | ||
assertThat(e.getErrors()).containsExactlyInAnyOrder( | ||
"This subscription is not eligible to be put on hold."); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldNotPauseNonExistentSubscription() { | ||
assertNotFound(() -> subscriptionStatusController.pauseSubscription(99999999, null)); | ||
} | ||
|
||
} |
Oops, something went wrong.