Search in sources :

Example 6 with ApiKey

use of io.gravitee.repository.management.model.ApiKey in project gravitee-gateway by gravitee-io.

the class ApiKeyRepositoryRefresherTest method shouldRefreshWithRevokedApiKeyAndPutIntoCache.

@Test
public void shouldRefreshWithRevokedApiKeyAndPutIntoCache() throws TechnicalException {
    Mockito.when(plan.getSecurity()).thenReturn(io.gravitee.repository.management.model.Plan.PlanSecurityType.API_KEY.name());
    List<Plan> plans = Collections.singletonList(plan);
    Mockito.when(api.getPlans()).thenReturn(plans);
    ApiKey apiKey1 = Mockito.mock(ApiKey.class);
    Mockito.when(apiKey1.isRevoked()).thenReturn(false);
    Mockito.when(apiKeyRepository.findByCriteria(Mockito.any(ApiKeyCriteria.class))).thenReturn(Collections.singletonList(apiKey1));
    refresher.initialize();
    refresher.run();
    refresher.run();
    InOrder inOrder = Mockito.inOrder(apiKeyRepository, apiKeyRepository);
    inOrder.verify(apiKeyRepository).findByCriteria(Matchers.argThat(new ArgumentMatcher<ApiKeyCriteria>() {

        @Override
        public boolean matches(Object arg) {
            ApiKeyCriteria criteria = (ApiKeyCriteria) arg;
            return !criteria.isIncludeRevoked() && criteria.getFrom() == 0 && criteria.getTo() == 0 && criteria.getPlans().size() == 1;
        }
    }));
    inOrder.verify(apiKeyRepository).findByCriteria(Matchers.argThat(new ArgumentMatcher<ApiKeyCriteria>() {

        @Override
        public boolean matches(Object arg) {
            ApiKeyCriteria criteria = (ApiKeyCriteria) arg;
            return criteria.isIncludeRevoked() && criteria.getFrom() != 0 && criteria.getTo() != 0 && criteria.getPlans().size() == 1;
        }
    }));
    Mockito.verify(cache, Mockito.times(2)).put(Matchers.any(Element.class));
}
Also used : ApiKeyCriteria(io.gravitee.repository.management.api.search.ApiKeyCriteria) ApiKey(io.gravitee.repository.management.model.ApiKey) Element(net.sf.ehcache.Element) Plan(io.gravitee.gateway.handlers.api.definition.Plan) Test(org.junit.Test)

Example 7 with ApiKey

use of io.gravitee.repository.management.model.ApiKey in project gravitee-gateway by gravitee-io.

the class ApiKeyRepositoryWrapperTest method shouldFindByCriteria.

@Test
public void shouldFindByCriteria() throws TechnicalException {
    ApiKeyCriteria apiKeyCriteria = Mockito.mock(ApiKeyCriteria.class);
    List<ApiKey> mockApiKeys = Arrays.asList(Mockito.mock(ApiKey.class), Mockito.mock(ApiKey.class));
    Mockito.when(wrappedRepository.findByCriteria(apiKeyCriteria)).thenReturn(mockApiKeys);
    List<ApiKey> apiKeys = repository.findByCriteria(apiKeyCriteria);
    Assert.assertEquals(mockApiKeys, apiKeys);
}
Also used : ApiKeyCriteria(io.gravitee.repository.management.api.search.ApiKeyCriteria) ApiKey(io.gravitee.repository.management.model.ApiKey) Test(org.junit.Test)

Example 8 with ApiKey

use of io.gravitee.repository.management.model.ApiKey in project gravitee-gateway by gravitee-io.

the class ApiKeyRepositoryWrapperTest method shouldFindById_fromCache.

@Test
public void shouldFindById_fromCache() throws TechnicalException {
    String apiKey = "1234-4567-7890";
    ApiKey mockApiKey = Mockito.mock(ApiKey.class);
    Mockito.when(cache.get(apiKey)).thenReturn(new Element(apiKey, mockApiKey));
    Optional<ApiKey> optApiKey = repository.findById(apiKey);
    Assert.assertNotNull(optApiKey);
    Assert.assertTrue(optApiKey.isPresent());
    Assert.assertEquals(mockApiKey, optApiKey.get());
}
Also used : ApiKey(io.gravitee.repository.management.model.ApiKey) Element(net.sf.ehcache.Element) Test(org.junit.Test)

Example 9 with ApiKey

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

the class ApiKeyServiceImpl method findByKey.

@Override
public ApiKeyEntity findByKey(String apiKey) {
    try {
        LOGGER.debug("Find an API Key by key: {}", apiKey);
        Optional<ApiKey> optApiKey = apiKeyRepository.findById(apiKey);
        if (optApiKey.isPresent()) {
            return convert(optApiKey.get());
        }
        throw new ApiKeyNotFoundException();
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to find an API Key by key {}", apiKey, ex);
        throw new TechnicalManagementException(String.format("An error occurs while trying to find an API Key by key: %s", apiKey), ex);
    }
}
Also used : ApiKey(io.gravitee.repository.management.model.ApiKey) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) ApiKeyNotFoundException(io.gravitee.management.service.exceptions.ApiKeyNotFoundException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 10 with ApiKey

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

the class ApiKeyServiceImpl method update.

@Override
public ApiKeyEntity update(ApiKeyEntity apiKeyEntity) {
    try {
        LOGGER.debug("Update API Key {}", apiKeyEntity.getKey());
        Optional<ApiKey> optKey = apiKeyRepository.findById(apiKeyEntity.getKey());
        if (!optKey.isPresent()) {
            throw new ApiKeyNotFoundException();
        }
        ApiKey key = optKey.get();
        setExpiration(apiKeyEntity.getExpireAt(), key);
        return convert(key);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while updating an API Key {}", apiKeyEntity.getKey(), ex);
        throw new TechnicalManagementException(String.format("An error occurs while updating an API Key %s", apiKeyEntity.getKey()), ex);
    }
}
Also used : ApiKey(io.gravitee.repository.management.model.ApiKey) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) ApiKeyNotFoundException(io.gravitee.management.service.exceptions.ApiKeyNotFoundException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Aggregations

ApiKey (io.gravitee.repository.management.model.ApiKey)14 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)6 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)6 Test (org.junit.Test)6 ApiKeyNotFoundException (io.gravitee.management.service.exceptions.ApiKeyNotFoundException)3 ApiKeyCriteria (io.gravitee.repository.management.api.search.ApiKeyCriteria)3 Element (net.sf.ehcache.Element)3 Plan (io.gravitee.gateway.handlers.api.definition.Plan)2 NotificationParamsBuilder (io.gravitee.management.service.notification.NotificationParamsBuilder)2 Date (java.util.Date)2 SubscriptionClosedException (io.gravitee.management.service.exceptions.SubscriptionClosedException)1 Instant (java.time.Instant)1