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());
}
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());
}
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));
}
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);
}
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));
}
Aggregations