Search in sources :

Example 21 with Subscription

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());
}
Also used : SubscriptionCriteria(io.gravitee.repository.management.api.search.SubscriptionCriteria) Subscription(io.gravitee.repository.management.model.Subscription) Test(org.junit.Test)

Example 22 with Subscription

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);
}
Also used : Subscription(io.gravitee.repository.management.model.Subscription) Test(org.junit.Test)

Example 23 with Subscription

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);
}
Also used : Subscription(io.gravitee.repository.management.model.Subscription) Test(org.junit.Test)

Example 24 with Subscription

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);
}
Also used : Subscription(io.gravitee.repository.management.model.Subscription) Test(org.junit.Test)

Example 25 with Subscription

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);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) NotificationParamsBuilder(io.gravitee.management.service.notification.NotificationParamsBuilder) Subscription(io.gravitee.repository.management.model.Subscription)

Aggregations

Subscription (io.gravitee.repository.management.model.Subscription)28 Test (org.junit.Test)19 SubscriptionCriteria (io.gravitee.repository.management.api.search.SubscriptionCriteria)10 SubscriptionRepository (io.gravitee.repository.management.api.SubscriptionRepository)8 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)7 ExecutionContext (io.gravitee.gateway.api.ExecutionContext)5 Request (io.gravitee.gateway.api.Request)5 Response (io.gravitee.gateway.api.Response)5 PolicyChain (io.gravitee.policy.api.PolicyChain)5 UserDetails (io.gravitee.management.idp.api.authentication.UserDetails)3 NotificationParamsBuilder (io.gravitee.management.service.notification.NotificationParamsBuilder)3 Authentication (org.springframework.security.core.Authentication)3 GrantedAuthority (org.springframework.security.core.GrantedAuthority)3 SecurityContext (org.springframework.security.core.context.SecurityContext)3 Element (net.sf.ehcache.Element)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Page (io.gravitee.common.data.domain.Page)1 HttpHeaders (io.gravitee.common.http.HttpHeaders)1 UUID (io.gravitee.common.utils.UUID)1 io.gravitee.management.model (io.gravitee.management.model)1