use of io.gravitee.repository.management.model.Subscription in project gravitee-management-rest-api by gravitee-io.
the class SubscriptionServiceTest method shouldFindByApplication.
@Test
public void shouldFindByApplication() throws TechnicalException {
Subscription sub1 = new Subscription();
sub1.setId("subscription-1");
sub1.setStatus(Subscription.Status.ACCEPTED);
sub1.setApplication(APPLICATION_ID);
Subscription sub2 = new Subscription();
sub2.setId("subscription-2");
sub2.setStatus(Subscription.Status.REJECTED);
sub2.setApplication(APPLICATION_ID);
when(subscriptionRepository.search(new SubscriptionCriteria.Builder().applications(Collections.singleton(APPLICATION_ID)).build())).thenReturn(Arrays.asList(sub1, sub2));
Collection<SubscriptionEntity> subscriptions = subscriptionService.findByApplicationAndPlan(APPLICATION_ID, null);
assertEquals(2, subscriptions.size());
}
use of io.gravitee.repository.management.model.Subscription in project gravitee-management-rest-api by gravitee-io.
the class SubscriptionServiceTest method shouldNotUpdateSubscriptionBecauseBadStatus.
@Test(expected = SubscriptionNotUpdatableException.class)
public void shouldNotUpdateSubscriptionBecauseBadStatus() throws Exception {
UpdateSubscriptionEntity updatedSubscription = new UpdateSubscriptionEntity();
updatedSubscription.setId(SUBSCRIPTION_ID);
// subscription object is not a mock since its state is updated by the call to subscriptionService.create()
Subscription subscription = new Subscription();
subscription.setId(SUBSCRIPTION_ID);
subscription.setApplication(APPLICATION_ID);
subscription.setPlan(PLAN_ID);
subscription.setStatus(Subscription.Status.PENDING);
// Stub
when(subscriptionRepository.findById(SUBSCRIPTION_ID)).thenReturn(Optional.of(subscription));
when(subscriptionRepository.update(any())).thenAnswer(returnsFirstArg());
// Run
subscriptionService.update(updatedSubscription);
}
use of io.gravitee.repository.management.model.Subscription in project gravitee-management-rest-api by gravitee-io.
the class SubscriptionServiceTest method shouldNotProcessBecauseClosedPlan.
@Test(expected = PlanAlreadyClosedException.class)
public void shouldNotProcessBecauseClosedPlan() throws Exception {
// Prepare data
ProcessSubscriptionEntity processSubscription = new ProcessSubscriptionEntity();
processSubscription.setId(SUBSCRIPTION_ID);
processSubscription.setAccepted(false);
Subscription subscription = new Subscription();
subscription.setApplication(APPLICATION_ID);
subscription.setPlan(PLAN_ID);
subscription.setStatus(Subscription.Status.PENDING);
when(plan.getStatus()).thenReturn(PlanStatus.CLOSED);
// Stub
when(subscriptionRepository.findById(SUBSCRIPTION_ID)).thenReturn(Optional.of(subscription));
when(planService.findById(PLAN_ID)).thenReturn(plan);
// Run
subscriptionService.process(processSubscription, SUBSCRIPTION_VALIDATOR);
}
use of io.gravitee.repository.management.model.Subscription in project gravitee-management-rest-api by gravitee-io.
the class SubscriptionServiceTest method shouldUpdateSubscriptionWithEndingDateButRevokedApiKey.
@Test
public void shouldUpdateSubscriptionWithEndingDateButRevokedApiKey() throws Exception {
UpdateSubscriptionEntity updatedSubscription = new UpdateSubscriptionEntity();
updatedSubscription.setId(SUBSCRIPTION_ID);
updatedSubscription.setEndingAt(new Date());
// subscription object is not a mock since its state is updated by the call to subscriptionService.create()
Subscription subscription = new Subscription();
subscription.setId(SUBSCRIPTION_ID);
subscription.setApplication(APPLICATION_ID);
subscription.setPlan(PLAN_ID);
subscription.setStatus(Subscription.Status.ACCEPTED);
subscription.setEndingAt(updatedSubscription.getEndingAt());
// Stub
when(subscriptionRepository.findById(SUBSCRIPTION_ID)).thenReturn(Optional.of(subscription));
when(subscriptionRepository.update(subscription)).thenAnswer(returnsFirstArg());
when(apiKeyService.findBySubscription(SUBSCRIPTION_ID)).thenReturn(Collections.singleton(apiKeyEntity));
when(apiKeyEntity.isRevoked()).thenReturn(true);
when(apiKeyEntity.getExpireAt()).thenReturn(null);
when(planService.findById(PLAN_ID)).thenReturn(plan);
when(plan.getApis()).thenReturn(Collections.singleton(API_ID));
when(plan.getSecurity()).thenReturn(PlanSecurityType.API_KEY);
// Run
subscriptionService.update(updatedSubscription);
// Verify
verify(subscriptionRepository, times(1)).update(subscription);
verify(apiKeyService, times(1)).findBySubscription(SUBSCRIPTION_ID);
verify(apiKeyService, never()).update(apiKeyEntity);
}
use of io.gravitee.repository.management.model.Subscription in project gravitee-management-rest-api by gravitee-io.
the class SubscriptionServiceImpl method close.
@Override
public SubscriptionEntity close(String subscriptionId) {
try {
logger.debug("Close subscription {}", subscriptionId);
Optional<Subscription> optSubscription = subscriptionRepository.findById(subscriptionId);
if (!optSubscription.isPresent()) {
throw new SubscriptionNotFoundException(subscriptionId);
}
Subscription subscription = optSubscription.get();
if (subscription.getStatus() == Subscription.Status.ACCEPTED) {
Subscription previousSubscription = new Subscription(subscription);
final Date now = new Date();
subscription.setUpdatedAt(now);
subscription.setStatus(Subscription.Status.CLOSED);
subscription.setClosedAt(new Date());
subscription = subscriptionRepository.update(subscription);
// Send an email to subscriber
final ApplicationEntity application = applicationService.findById(subscription.getApplication());
final PlanEntity plan = planService.findById(subscription.getPlan());
String apiId = plan.getApis().iterator().next();
final ApiModelEntity api = apiService.findByIdForTemplates(apiId);
final PrimaryOwnerEntity owner = application.getPrimaryOwner();
final Map<String, Object> params = new NotificationParamsBuilder().owner(owner).api(api).plan(plan).application(application).build();
notifierService.trigger(ApiHook.SUBSCRIPTION_CLOSED, apiId, params);
notifierService.trigger(ApplicationHook.SUBSCRIPTION_CLOSED, application.getId(), params);
createAudit(apiId, subscription.getApplication(), SUBSCRIPTION_CLOSED, subscription.getUpdatedAt(), previousSubscription, subscription);
// API Keys are automatically revoked
Set<ApiKeyEntity> apiKeys = apiKeyService.findBySubscription(subscription.getId());
for (ApiKeyEntity apiKey : apiKeys) {
Date expireAt = apiKey.getExpireAt();
if (!apiKey.isRevoked() && (expireAt == null || expireAt.equals(now) || expireAt.before(now))) {
apiKey.setExpireAt(now);
apiKey.setRevokedAt(now);
apiKey.setRevoked(true);
apiKeyService.revoke(apiKey.getKey(), false);
}
}
return convert(subscription);
}
throw new SubscriptionNotClosableException(subscription);
} catch (TechnicalException ex) {
logger.error("An error occurs while trying to close subscription {}", subscriptionId, ex);
throw new TechnicalManagementException(String.format("An error occurs while trying to close subscription %s", subscriptionId), ex);
}
}
Aggregations