Search in sources :

Example 16 with Subscription

use of io.gravitee.repository.management.model.Subscription in project gravitee-management-rest-api by gravitee-io.

the class SubscriptionServiceTest method shouldFindByPlan.

@Test
public void shouldFindByPlan() throws TechnicalException {
    Subscription sub1 = new Subscription();
    sub1.setId("subscription-1");
    sub1.setStatus(Subscription.Status.ACCEPTED);
    Subscription sub2 = new Subscription();
    sub2.setId("subscription-2");
    sub2.setStatus(Subscription.Status.REJECTED);
    when(subscriptionRepository.search(new SubscriptionCriteria.Builder().plans(Collections.singleton(PLAN_ID)).build())).thenReturn(Arrays.asList(sub1, sub2));
    Collection<SubscriptionEntity> subscriptions = subscriptionService.findByPlan(PLAN_ID);
    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 17 with Subscription

use of io.gravitee.repository.management.model.Subscription in project gravitee-management-rest-api by gravitee-io.

the class SubscriptionServiceTest method shouldCloseSubscription.

@Test
public void shouldCloseSubscription() throws Exception {
    final Date now = new Date();
    final Subscription subscription = new Subscription();
    subscription.setId(SUBSCRIPTION_ID);
    subscription.setStatus(Subscription.Status.ACCEPTED);
    subscription.setEndingAt(now);
    subscription.setPlan(PLAN_ID);
    subscription.setApplication(APPLICATION_ID);
    final ApiKeyEntity apiKey = new ApiKeyEntity();
    apiKey.setKey("api-key");
    apiKey.setRevoked(false);
    when(plan.getApis()).thenReturn(Collections.singleton(API_ID));
    when(subscriptionRepository.findById(SUBSCRIPTION_ID)).thenReturn(Optional.of(subscription));
    when(subscriptionRepository.update(subscription)).thenReturn(subscription);
    when(apiKeyService.findBySubscription(SUBSCRIPTION_ID)).thenReturn(Collections.singleton(apiKey));
    when(apiService.findByIdForTemplates(API_ID)).thenReturn(apiModelEntity);
    when(planService.findById(PLAN_ID)).thenReturn(plan);
    when(applicationService.findById(APPLICATION_ID)).thenReturn(application);
    when(application.getPrimaryOwner()).thenReturn(mock(PrimaryOwnerEntity.class));
    subscriptionService.close(SUBSCRIPTION_ID);
    verify(apiKeyService).revoke("api-key", false);
    verify(notifierService).trigger(eq(ApiHook.SUBSCRIPTION_CLOSED), anyString(), anyMap());
    verify(notifierService).trigger(eq(ApplicationHook.SUBSCRIPTION_CLOSED), anyString(), anyMap());
}
Also used : Subscription(io.gravitee.repository.management.model.Subscription) Test(org.junit.Test)

Example 18 with Subscription

use of io.gravitee.repository.management.model.Subscription in project gravitee-management-rest-api by gravitee-io.

the class SubscriptionServiceTest method shouldNotSubscribe_applicationWithoutClientId.

@Test(expected = PlanNotSubscribableException.class)
public void shouldNotSubscribe_applicationWithoutClientId() throws Exception {
    // Prepare data
    when(plan.getApis()).thenReturn(Collections.singleton(API_ID));
    when(plan.getValidation()).thenReturn(PlanValidationType.AUTO);
    when(plan.getSecurity()).thenReturn(PlanSecurityType.OAUTH2);
    // 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);
    SecurityContextHolder.setContext(new SecurityContext() {

        @Override
        public Authentication getAuthentication() {
            return new Authentication() {

                @Override
                public Collection<? extends GrantedAuthority> getAuthorities() {
                    return null;
                }

                @Override
                public Object getCredentials() {
                    return null;
                }

                @Override
                public Object getDetails() {
                    return null;
                }

                @Override
                public Object getPrincipal() {
                    return new UserDetails("tester", "password", Collections.emptyList());
                }

                @Override
                public boolean isAuthenticated() {
                    return false;
                }

                @Override
                public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
                }

                @Override
                public String getName() {
                    return null;
                }
            };
        }

        @Override
        public void setAuthentication(Authentication authentication) {
        }
    });
    // Stub
    when(planService.findById(PLAN_ID)).thenReturn(plan);
    when(applicationService.findById(APPLICATION_ID)).thenReturn(application);
    when(apiService.findByIdForTemplates(API_ID)).thenReturn(apiModelEntity);
    // Run
    subscriptionService.create(new NewSubscriptionEntity(PLAN_ID, APPLICATION_ID));
}
Also used : UserDetails(io.gravitee.management.idp.api.authentication.UserDetails) Authentication(org.springframework.security.core.Authentication) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SecurityContext(org.springframework.security.core.context.SecurityContext) Subscription(io.gravitee.repository.management.model.Subscription) Test(org.junit.Test)

Example 19 with Subscription

use of io.gravitee.repository.management.model.Subscription in project gravitee-management-rest-api by gravitee-io.

the class SubscriptionServiceTest method shouldUpdateSubscriptionWithEndingDate.

@Test
public void shouldUpdateSubscriptionWithEndingDate() 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(any())).thenAnswer(returnsFirstArg());
    when(apiKeyService.findBySubscription(SUBSCRIPTION_ID)).thenReturn(Collections.singleton(apiKeyEntity));
    when(apiKeyEntity.isRevoked()).thenReturn(false);
    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, times(1)).update(apiKeyEntity);
}
Also used : Subscription(io.gravitee.repository.management.model.Subscription) Test(org.junit.Test)

Example 20 with Subscription

use of io.gravitee.repository.management.model.Subscription in project gravitee-management-rest-api by gravitee-io.

the class SubscriptionServiceTest method shouldNotCreateBecauseExistingSubscription_oauth2.

@Test(expected = PlanNotSubscribableException.class)
public void shouldNotCreateBecauseExistingSubscription_oauth2() throws Exception {
    Subscription sub1 = mock(Subscription.class);
    when(sub1.getStatus()).thenReturn(Subscription.Status.ACCEPTED);
    when(sub1.getPlan()).thenReturn("my-plan-2");
    PlanEntity plan2 = mock(PlanEntity.class);
    when(plan2.getId()).thenReturn("my-plan-2");
    when(plan2.getSecurity()).thenReturn(PlanSecurityType.OAUTH2);
    when(plan.getSecurity()).thenReturn(PlanSecurityType.OAUTH2);
    when(plan.getStatus()).thenReturn(PlanStatus.PUBLISHED);
    /*
        when(subscriptionRepository.findByApplication(APPLICATION_ID)).thenReturn(
                new HashSet<>(Collections.singleton(sub1)));
                */
    when(applicationService.findById(APPLICATION_ID)).thenReturn(application);
    when(planService.findById(PLAN_ID)).thenReturn(plan);
    when(planService.findById("my-plan-2")).thenReturn(plan2);
    // Run
    subscriptionService.create(new NewSubscriptionEntity(PLAN_ID, APPLICATION_ID));
}
Also used : Subscription(io.gravitee.repository.management.model.Subscription) Test(org.junit.Test)

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