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