Search in sources :

Example 6 with ApiKeyCriteria

use of io.gravitee.repository.management.api.search.ApiKeyCriteria in project gravitee-gateway by gravitee-io.

the class ApiKeyRepositoryRefresherTest method shouldRefreshWithRevokedApiKeyAndRemoveFromCache.

@Test
public void shouldRefreshWithRevokedApiKeyAndRemoveFromCache() throws TechnicalException {
    String apiKey = "1234-4567-7890";
    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.getKey()).thenReturn(apiKey);
    Mockito.when(apiKey1.isRevoked()).thenReturn(false);
    ApiKey apiKey2 = Mockito.mock(ApiKey.class);
    Mockito.when(apiKey2.getKey()).thenReturn(apiKey);
    Mockito.when(apiKey2.isRevoked()).thenReturn(true);
    Mockito.when(apiKeyRepository.findByCriteria(Mockito.any(ApiKeyCriteria.class))).thenReturn(Collections.singletonList(apiKey1)).thenReturn(Collections.singletonList(apiKey2));
    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;
        }
    }));
    InOrder inOrderCache = Mockito.inOrder(cache, cache);
    inOrderCache.verify(cache).put(Matchers.any(Element.class));
    inOrderCache.verify(cache).remove(apiKey);
}
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)

Aggregations

ApiKeyCriteria (io.gravitee.repository.management.api.search.ApiKeyCriteria)6 Test (org.junit.Test)6 Plan (io.gravitee.gateway.handlers.api.definition.Plan)5 ApiKey (io.gravitee.repository.management.model.ApiKey)3 Element (net.sf.ehcache.Element)2