Search in sources :

Example 1 with ApiKeyQuery

use of io.gravitee.rest.api.model.key.ApiKeyQuery in project gravitee-management-rest-api by gravitee-io.

the class ScheduledSubscriptionPreExpirationNotificationServiceTest method shouldFindApiKeyExpirationsToNotify.

@Test
public void shouldFindApiKeyExpirationsToNotify() {
    Instant now = Instant.ofEpochMilli(1469022010000L);
    Integer daysBeforeNotification = 10;
    ApiKeyEntity apiKey = mock(ApiKeyEntity.class);
    when(apiKeyService.search(any(ApiKeyQuery.class))).thenReturn(Collections.singletonList(apiKey));
    Collection<ApiKeyEntity> apiKeysToNotify = service.findApiKeyExpirationsToNotify(now, daysBeforeNotification);
    assertEquals(Collections.singletonList(apiKey), apiKeysToNotify);
    verify(apiKeyService, times(1)).search(argThat(apiKeyQuery -> !apiKeyQuery.isIncludeRevoked() && // 1469886010000 -> now + 10 days
    apiKeyQuery.getExpireAfter() == 1469886010000L && // 1469889610000 -> now + 10 days + 1h (cron period)
    apiKeyQuery.getExpireBefore() == 1469889610000L));
}
Also used : InjectMocks(org.mockito.InjectMocks) io.gravitee.rest.api.service(io.gravitee.rest.api.service) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) Test(org.junit.Test) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Mockito(org.mockito.Mockito) ApiKeyQuery(io.gravitee.rest.api.model.key.ApiKeyQuery) EmailNotificationBuilder(io.gravitee.rest.api.service.builder.EmailNotificationBuilder) io.gravitee.rest.api.model(io.gravitee.rest.api.model) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assert.assertEquals(org.junit.Assert.assertEquals) Instant(java.time.Instant) ApiKeyQuery(io.gravitee.rest.api.model.key.ApiKeyQuery) Test(org.junit.Test)

Example 2 with ApiKeyQuery

use of io.gravitee.rest.api.model.key.ApiKeyQuery in project gravitee-management-rest-api by gravitee-io.

the class ScheduledSubscriptionPreExpirationNotificationService method findApiKeyExpirationsToNotify.

@VisibleForTesting
Collection<ApiKeyEntity> findApiKeyExpirationsToNotify(Instant now, Integer daysToExpiration) {
    long expirationStartingTime = now.plus(Duration.ofDays((long) daysToExpiration)).getEpochSecond() * 1000;
    ApiKeyQuery query = new ApiKeyQuery();
    query.setIncludeRevoked(false);
    query.setExpireAfter(expirationStartingTime);
    query.setExpireBefore(expirationStartingTime + cronPeriodInMs);
    return apiKeyService.search(query);
}
Also used : ApiKeyQuery(io.gravitee.rest.api.model.key.ApiKeyQuery) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

ApiKeyQuery (io.gravitee.rest.api.model.key.ApiKeyQuery)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 io.gravitee.rest.api.model (io.gravitee.rest.api.model)1 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)1 SubscriptionQuery (io.gravitee.rest.api.model.subscription.SubscriptionQuery)1 io.gravitee.rest.api.service (io.gravitee.rest.api.service)1 EmailNotificationBuilder (io.gravitee.rest.api.service.builder.EmailNotificationBuilder)1 GraviteeContext (io.gravitee.rest.api.service.common.GraviteeContext)1 Instant (java.time.Instant)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Test (org.junit.Test)1 RunWith (org.junit.runner.RunWith)1 InjectMocks (org.mockito.InjectMocks)1 Mock (org.mockito.Mock)1 Mockito (org.mockito.Mockito)1 MockitoJUnitRunner (org.mockito.junit.MockitoJUnitRunner)1